Skip to content

Commit

Permalink
Alternative fix for digital acteur (#1077)
Browse files Browse the repository at this point in the history
* Make test fail

* Alternative fix

* Add comment
  • Loading branch information
fabienheureux authored Nov 27, 2024
1 parent 06e065f commit 561a1ea
Showing 3 changed files with 22 additions and 12 deletions.
10 changes: 7 additions & 3 deletions qfdmo/models/acteur.py
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@ class ActeurReprise(models.TextChoices):


class ActeurType(CodeAsNaturalKeyModel):
_digital_acteur_type_id: int = 0
_digital_acteur_type_id: int | None = None

class Meta:
verbose_name = "Type d'acteur"
@@ -103,8 +103,12 @@ class Meta:

@classmethod
def get_digital_acteur_type_id(cls) -> int:
if not cls._digital_acteur_type_id:
(digital_acteur_type, _) = cls.objects.get_or_create(code="acteur_digital")
"""Returns the ID of the `ActeurType` with the code "acteur_digital",
creating it if necessary. Caches the ID to minimize database queries.
Subsequent calls use the cached ID after the first query.
"""
if cls._digital_acteur_type_id is None:
digital_acteur_type, _ = cls.objects.get_or_create(code="acteur_digital")
cls._digital_acteur_type_id = digital_acteur_type.id
return cls._digital_acteur_type_id

4 changes: 3 additions & 1 deletion unit_tests/core/test_jinja2_handler.py
Original file line number Diff line number Diff line change
@@ -147,7 +147,9 @@ def test_distance_to_acteur_not_digital(self, adresse, request_params, expected)
assert distance_to_acteur(request, adresse) == expected

def test_distance_to_acteur_digital(self, adresse):
ActeurType._digital_acteur_type_id = 0
# Reset ActeurType cache for digital acteur type id to prevent pollution
# from previous tests
ActeurType._digital_acteur_type_id = None
adresse.acteur_type = ActeurTypeFactory(code="acteur_digital")
request = type("", (), {})()
request.GET = {"longitude": str(1000 / 111320), "latitude": str(1000 / 111320)}
20 changes: 12 additions & 8 deletions unit_tests/qfdmo/test_acteur.py
Original file line number Diff line number Diff line change
@@ -75,15 +75,17 @@ def test_isdigital_false(self):
).is_digital

def test_isdigital_true(self):
ActeurType._digital_acteur_type_id = 0
acteur_type = ActeurTypeFactory(code="acteur_digital")
assert ActeurFactory.build(
nom="Test Object 1", acteur_type=acteur_type
).is_digital
# Reset ActeurType cache for digital acteur type id to prevent pollution
# from previous tests
ActeurType._digital_acteur_type_id = None
acteur_type = ActeurTypeFactory(code="acteur_digital", id=5)
assert ActeurFactory(nom="Test Object 1", acteur_type=acteur_type).is_digital

def test_isdigital_hides_address(self):
ActeurType._digital_acteur_type_id = 0
acteur_type = ActeurTypeFactory(code="acteur_digital")
# Reset ActeurType cache for digital acteur type id to prevent pollution
# from previous tests
ActeurType._digital_acteur_type_id = None
acteur_type = ActeurTypeFactory(code="acteur_digital", id=5)
acteur = DisplayedActeurFactory(acteur_type=acteur_type)
assert acteur.is_digital
assert not acteur.should_display_adresse
@@ -165,7 +167,9 @@ def test_location_validation_raise(self):
acteur.save()

def test_location_validation_dont_raise(self):
ActeurType._digital_acteur_type_id = 0
# Reset ActeurType cache for digital acteur type id to prevent pollution
# from previous tests
ActeurType._digital_acteur_type_id = None
acteur_type = ActeurTypeFactory(code="acteur_digital")
acteur = Acteur(
nom="Test Object 1", identifiant_unique="123", acteur_type=acteur_type

0 comments on commit 561a1ea

Please sign in to comment.