From 53ec1c907d9f15013802598d2838394f02b7a874 Mon Sep 17 00:00:00 2001 From: Tobias <49518857+to-sta@users.noreply.github.com> Date: Fri, 20 Dec 2024 01:09:40 +0100 Subject: [PATCH] remove django model related testing (#1065) * 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 --- .vscode/settings.json | 5 - .../management/commands/populate_db.py | 2 + backend/entities/models.py | 4 +- backend/entities/tests/test_group.py | 136 ++---------------- backend/entities/tests/test_group_text.py | 17 --- frontend/i18n/check/run_i18n_checks.py | 5 +- 6 files changed, 18 insertions(+), 151 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index aa5ef6aa2..f0a0fd5f1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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 } diff --git a/backend/backend/management/commands/populate_db.py b/backend/backend/management/commands/populate_db.py index 0aad213b5..34994e155 100644 --- a/backend/backend/management/commands/populate_db.py +++ b/backend/backend/management/commands/populate_db.py @@ -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): diff --git a/backend/entities/models.py b/backend/entities/models.py index dfaacfd3d..5558b4005 100644 --- a/backend/entities/models.py +++ b/backend/entities/models.py @@ -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) diff --git a/backend/entities/tests/test_group.py b/backend/entities/tests/test_group.py index 2ddcab3e2..2573c4c6f 100644 --- a/backend/entities/tests/test_group.py +++ b/backend/entities/tests/test_group.py @@ -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 @@ -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( @@ -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, @@ -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. @@ -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, @@ -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, @@ -175,33 +81,11 @@ 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( @@ -209,7 +93,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, @@ -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, diff --git a/backend/entities/tests/test_group_text.py b/backend/entities/tests/test_group_text.py index df958a5a2..6764b7f44 100644 --- a/backend/entities/tests/test_group_text.py +++ b/backend/entities/tests/test_group_text.py @@ -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 diff --git a/frontend/i18n/check/run_i18n_checks.py b/frontend/i18n/check/run_i18n_checks.py index f1f5941c5..e094cd0fc 100644 --- a/frontend/i18n/check/run_i18n_checks.py +++ b/frontend/i18n/check/run_i18n_checks.py @@ -6,6 +6,7 @@ """ import subprocess +from pathlib import Path def run_check(script_name): @@ -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: