Skip to content

Commit

Permalink
group tests in classes
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Jan 27, 2025
1 parent dc98940 commit 79cdf07
Showing 1 changed file with 50 additions and 61 deletions.
111 changes: 50 additions & 61 deletions lacommunaute/users/tests/tests_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,64 +17,53 @@ def fixture_email_last_seen(db):
return EmailLastSeenFactory(email=email)


# User model tests


def test_create_user_without_username(db):
user = User.objects.create_user(email=email)
assert re.match(r"^[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}$", user.username)
assert user.email == email


# EmailLastSeen model tests


def test_email_uniqueness(db, email_last_seen):
with pytest.raises(IntegrityError):
EmailLastSeenFactory(email=email)


def test_compute_hash_on_save(db, email_last_seen):
assert email_last_seen.email_hash == hashlib.sha256(email.encode("utf-8")).hexdigest()


@pytest.mark.parametrize("updated_email", [None, email])
def test_hash_remains_unchanged_on_update(db, email_last_seen, updated_email):
email_last_seen.email = updated_email
email_last_seen.save()
email_last_seen.refresh_from_db()
assert email_last_seen.email_hash == hashlib.sha256(email.encode("utf-8")).hexdigest()


def test_soft_delete(db, email_last_seen):
email_last_seen.soft_delete()
email_last_seen.refresh_from_db()
assert email_last_seen.deleted_at is not None
assert email_last_seen.email is None
assert email_last_seen.email_hash not in [None, ""]


# EmailLastSeenQuerySet tests


@pytest.mark.parametrize("kind", [kind for kind, _ in EmailLastSeenKind.choices])
def test_seen(db, email_last_seen, kind):
EmailLastSeen.objects.seen(email, kind)

email_last_seen.refresh_from_db()
assert email_last_seen.last_seen_kind == kind
assert email_last_seen.last_seen_at is not None


def test_seen_invalid_kind(db, email_last_seen):
with pytest.raises(ValueError):
EmailLastSeen.objects.seen(email, "invalid_kind")


@pytest.mark.parametrize("kind", [kind for kind, _ in EmailLastSeenKind.choices])
def test_seen_unknown_email(db, kind):
EmailLastSeen.objects.seen(email, kind)

email_last_seen = EmailLastSeen.objects.get(email=email)
assert email_last_seen.last_seen_kind == kind
assert email_last_seen.last_seen_at is not None
class TestUserModel:
def test_create_user_without_username(self, db):
user = User.objects.create_user(email=email)
assert re.match(r"^[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}$", user.username)
assert user.email == email


class TestEmailLastSeenModel:
def test_email_uniqueness(self, db, email_last_seen):
with pytest.raises(IntegrityError):
EmailLastSeenFactory(email=email)

def test_compute_hash_on_save(self, db, email_last_seen):
assert email_last_seen.email_hash == hashlib.sha256(email.encode("utf-8")).hexdigest()

@pytest.mark.parametrize("updated_email", [None, email])
def test_hash_remains_unchanged_on_update(self, db, email_last_seen, updated_email):
email_last_seen.email = updated_email
email_last_seen.save()
email_last_seen.refresh_from_db()
assert email_last_seen.email_hash == hashlib.sha256(email.encode("utf-8")).hexdigest()

def test_soft_delete(self, db, email_last_seen):
email_last_seen.soft_delete()
email_last_seen.refresh_from_db()
assert email_last_seen.deleted_at is not None
assert email_last_seen.email is None
assert email_last_seen.email_hash not in [None, ""]


class TestEmailLastSeenQueryset:
@pytest.mark.parametrize("kind", [kind for kind, _ in EmailLastSeenKind.choices])
def test_seen(self, db, email_last_seen, kind):
EmailLastSeen.objects.seen(email, kind)

email_last_seen.refresh_from_db()
assert email_last_seen.last_seen_kind == kind
assert email_last_seen.last_seen_at is not None

def test_seen_invalid_kind(self, db, email_last_seen):
with pytest.raises(ValueError):
EmailLastSeen.objects.seen(email, "invalid_kind")

@pytest.mark.parametrize("kind", [kind for kind, _ in EmailLastSeenKind.choices])
def test_seen_unknown_email(self, db, kind):
EmailLastSeen.objects.seen(email, kind)

email_last_seen = EmailLastSeen.objects.get(email=email)
assert email_last_seen.last_seen_kind == kind
assert email_last_seen.last_seen_at is not None

0 comments on commit 79cdf07

Please sign in to comment.