Skip to content

Commit

Permalink
remove django model related testing (#1065)
Browse files Browse the repository at this point in the history
* remove django model related testing

* Fix location calls in group tests + reapply i18n checks

* Convert i18n check to use pathlib for windows users

* Fix remaining faker city calls for group tests

* Try many to one rel to fix backend tests

* Switch group location to foreign key

* Add options into populate DB types

---------

Co-authored-by: Andrew Tavis McAllister <[email protected]>
  • Loading branch information
to-sta and andrewtavis authored Dec 20, 2024
1 parent 76a72d1 commit 53ec1c9
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 151 deletions.
5 changes: 0 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@
"eslint.validate": ["javascript", "typescript", "vue"],
"eslint.useFlatConfig": true,
"typescript.tsdk": "./frontend/node_modules/typescript/lib",
"python.testing.pytestArgs": [
"backend"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
2 changes: 2 additions & 0 deletions backend/backend/management/commands/populate_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Options(TypedDict):
orgs_per_user: int
groups_per_org: int
events_per_org: int
resources_per_entity: int
faq_entries_per_entity: int


class Command(BaseCommand):
Expand Down
4 changes: 2 additions & 2 deletions backend/entities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class Group(models.Model):
group_name = models.CharField(max_length=255)
name = models.CharField(max_length=255)
tagline = models.CharField(max_length=255, blank=True)
location = models.OneToOneField(
"content.Location", on_delete=models.CASCADE, null=False, blank=False
location = models.ForeignKey(
"content.Location", on_delete=models.CASCADE, blank=False, null=False
)
category = models.CharField(max_length=255)
get_involved_url = models.URLField(blank=True)
Expand Down
136 changes: 10 additions & 126 deletions backend/entities/tests/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
from faker import Faker

from authentication.factories import UserFactory
from entities.factories import (
GroupFactory,
OrganizationFactory,
)
from content.factories import EntityLocationFactory
from entities.factories import OrganizationFactory
from entities.models import Group

pytestmark = pytest.mark.django_db
Expand All @@ -24,6 +22,7 @@ def test_group_creation() -> None:
"""Test complete group creation with all fields."""
user = UserFactory()
org = OrganizationFactory(created_by=user)
location = EntityLocationFactory()
fake = Faker()

group = Group.objects.create(
Expand All @@ -32,7 +31,7 @@ def test_group_creation() -> None:
group_name=fake.company(),
name=fake.company(),
tagline=fake.catch_phrase(),
location=fake.city(),
location=location,
category=fake.word(),
get_involved_url=fake.url(),
terms_checked=True,
Expand All @@ -46,104 +45,11 @@ def test_group_creation() -> None:
assert group.terms_checked is True


def test_required_fields() -> None:
"""Test that required fields raise validation error when missing."""
user = UserFactory()
org = OrganizationFactory(created_by=user)

# 1. Test missing group_name.
with pytest.raises(ValidationError):
group = Group(
org_id=org,
created_by=user,
name="Test Name",
location="Test Location",
category="Test Category",
)
group.full_clean()

# 2. Test missing location.
with pytest.raises(ValidationError):
group = Group(
org_id=org,
created_by=user,
group_name="Test Group",
name="Test Name",
category="Test Category",
)
group.full_clean()


def test_optional_fields() -> None:
"""Test that optional fields can be blank or null."""
user = UserFactory()
org = OrganizationFactory(created_by=user)

group = Group.objects.create(
org_id=org,
created_by=user,
group_name="Test Group",
name="Test Name",
location="Test Location",
category="Test Category",
)

# Should not raise ValidationError.
group.full_clean()
assert group.tagline == ""
assert group.get_involved_url == ""
assert group.terms_checked is False


def test_field_max_lengths() -> None:
"""Test that fields have correct max lengths."""
user = UserFactory()
org = OrganizationFactory(created_by=user)
fake = Faker()

group = Group.objects.create(
org_id=org,
created_by=user,
group_name=fake.company(),
name=fake.company(),
tagline=fake.catch_phrase(),
location=fake.city(),
category=fake.word(),
get_involved_url=fake.url(),
terms_checked=True,
)

assert len(group.group_name) <= 100
assert len(group.name) <= 100
assert len(group.tagline) <= 200
assert len(group.location) <= 100
assert len(group.category) <= 100
assert len(group.get_involved_url) <= 200


def test_cascade_delete() -> None:
"""Test that deleting an organization deletes all associated groups."""
user = UserFactory()
org = OrganizationFactory(created_by=user)

GroupFactory(org_id=org, created_by=user)

assert Group.objects.count() == 1
org.delete()
assert Group.objects.count() == 0

org = OrganizationFactory(created_by=user)
GroupFactory(org_id=org, created_by=user)

assert Group.objects.count() == 1
user.delete()
assert Group.objects.count() == 0


def test_url_validations() -> None:
"""Test that get_involved_url field is a valid URL."""
user = UserFactory()
org = OrganizationFactory(created_by=user)
location = EntityLocationFactory()
fake = Faker()

# 1. Test invalid URL.
Expand All @@ -153,7 +59,7 @@ def test_url_validations() -> None:
created_by=user,
group_name=fake.company(),
name=fake.company(),
location=fake.city(),
location=location,
category=fake.word(),
get_involved_url="not a url",
terms_checked=True,
Expand All @@ -166,7 +72,7 @@ def test_url_validations() -> None:
created_by=user,
group_name=fake.company(),
name=fake.company(),
location=fake.city(),
location=location,
category=fake.word(),
get_involved_url=fake.url(),
terms_checked=True,
Expand All @@ -175,41 +81,19 @@ def test_url_validations() -> None:
group.full_clean()


def test_auto_fields() -> None:
"""Test that auto fields are set correctly."""
user = UserFactory()
org = OrganizationFactory(created_by=user)
fake = Faker()

group = Group.objects.create(
org_id=org,
created_by=user,
group_name=fake.company(),
name=fake.company(),
location=fake.city(),
category=fake.word(),
get_involved_url=fake.url(),
terms_checked=True,
)

assert group.creation_date is not None
assert isinstance(group.creation_date, datetime)
assert group.id is not None
assert isinstance(group.id, UUID)


def test_multiple_groups_per_org() -> None:
"""Test that multiple groups can be created per organization."""
user = UserFactory()
org = OrganizationFactory(created_by=user)
location = EntityLocationFactory()
fake = Faker()

group1 = Group.objects.create(
org_id=org,
created_by=user,
group_name=fake.company(),
name=fake.company(),
location=fake.city(),
location=location,
category=fake.word(),
get_involved_url=fake.url(),
terms_checked=True,
Expand All @@ -220,7 +104,7 @@ def test_multiple_groups_per_org() -> None:
created_by=user,
group_name=fake.company(),
name=fake.company(),
location=fake.city(),
location=location,
category=fake.word(),
get_involved_url=fake.url(),
terms_checked=True,
Expand Down
17 changes: 0 additions & 17 deletions backend/entities/tests/test_group_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,3 @@ def test_group_text_languages() -> None:
assert secondary_text.primary is False
assert secondary_text.iso == "spa"
assert secondary_text.description == "Descripción"


def test_group_text_field_lengths() -> None:
"""Test field length constraints."""
group = GroupFactory()

text = GroupTextFactory(
group_id=group,
iso="eng",
description="A" * 500,
get_involved="B" * 500,
donate_prompt="C" * 500,
)
assert len(text.description) <= 500
assert len(text.get_involved) <= 500
assert len(text.donate_prompt) <= 500
assert len(text.iso) <= 3
5 changes: 4 additions & 1 deletion frontend/i18n/check/run_i18n_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

import subprocess
from pathlib import Path


def run_check(script_name):
Expand All @@ -23,7 +24,9 @@ def run_check(script_name):
An error that the given check script has failed.
"""
try:
subprocess.run(["python", f"./frontend/i18n/check/{script_name}"], check=True)
subprocess.run(
["python", Path("frontend") / "i18n" / "check" / script_name], check=True
)
print(f"{script_name} ran successfully.")

except subprocess.CalledProcessError as e:
Expand Down

0 comments on commit 53ec1c9

Please sign in to comment.