Skip to content

Instantly share code, notes, and snippets.

View goujonbe's full-sized avatar
👋

Benoît Goujon goujonbe

👋
View GitHub Profile
@goujonbe
goujonbe / dagger-gh-actions.yml
Created May 26, 2022 06:33
YAML config file for CI with GH Actions and dagger
name: dagger CI
on:
push:
branches:
- main
jobs:
ci:
runs-on: ubuntu-latest
package ci
import (
"dagger.io/dagger"
"universe.dagger.io/bash" // import this package to execute bash commands inside a docker container
"universe.dagger.io/docker" // import this package to set up docker
)
dagger.#Plan & {
// configure the client so that dagger takes only the files it needs
@pytest.mark.parametrize(
"test_input, expected",
[
("correct@gmail.com", True),
("another.correct@custom.fr", True),
("and-another@custom.org", True),
("inavlid.com", False),
("veryb@d@.com", False),
("domain@too.long", False)
]
def is_valid_email_address(email: str) -> bool:
regex = "^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$"
return re.search(regex, email) is not None
@pytest.mark.freeze_time("2020-01-15 13:00:00")
def test_get_time_since_subscription():
test_subscribtion_date = datetime.datetime(2020, 1, 10, 12)
assert get_time_since_subscription(test_subscribtion_date) == 5
def get_time_since_subscription(subscription_date: datetime.datetime) -> int:
current_date = datetime.datetime.utcnow()
return (current_date - subscription_date).days
def test_email_validation():
with pytest.raises(InvalidEmailAddressError):
assert validate_email_address(None, None, "invalid.com")
class InvalidEmailAddressError(Exception):
pass
def validate_email_address(ctx, param, value):
regex = "^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$"
if not re.search(regex, value):
raise InvalidEmailAddressError(f"Invalid email address: {value}")
return value
@goujonbe
goujonbe / test_logging_example.py
Last active October 31, 2020 16:23
Gist that is part of a tutorial on Pytest features.
def test_translate_animal_names(caplog):
animals_in_french = ["lapin", "grenouille", "cheval"]
french_english_translation = {
"lapin": "rabbit",
"cheval": "horse"
}
animals_in_english = translate_animal_names(
animals_in_french,
french_english_translation
)
@goujonbe
goujonbe / logging_example.py
Last active October 31, 2020 16:23
Gist that is part of a tutorial on pytest features. About logging testing.
def translate_animal_names(
animals: List[str],
translations: Dict[str, str]
) -> List[str]:
translated_animal_names = []
for animal in animals:
if animal in translations:
translated_animal_names.append(translations[animal])
else:
logging.warning(