Skip to content

Commit

Permalink
Remplacer des fixtures par des factories
Browse files Browse the repository at this point in the history
  • Loading branch information
Anto59290 committed Mar 3, 2025
1 parent efcf6d7 commit 23f3984
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 51 deletions.
22 changes: 0 additions & 22 deletions sv/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import random

import pytest
from django.conf import settings
from django.contrib.auth import get_user_model
Expand All @@ -8,7 +6,6 @@

from sv.models import (
FicheDetection,
FicheZoneDelimitee,
StatutReglementaire,
Region,
Departement,
Expand Down Expand Up @@ -48,25 +45,6 @@ def add_status_reglementaire_objects():
StatutReglementaire.objects.get_or_create(code=code, libelle=libelle)


@pytest.fixture
def fiche_zone_bakery(db, mocked_authentification_user):
def _fiche_zone_bakery():
return baker.make(
FicheZoneDelimitee,
_fill_optional=True,
createur=mocked_authentification_user.agent.structure,
rayon_zone_tampon=random.uniform(1, 100),
surface_tampon_totale=random.uniform(1, 100),
)

return _fiche_zone_bakery


@pytest.fixture
def fiche_zone(fiche_zone_bakery):
return fiche_zone_bakery()


@pytest.fixture
def fiche_detection_bakery(db, mocked_authentification_user):
def _fiche_detection_bakery():
Expand Down
16 changes: 7 additions & 9 deletions sv/tests/test_fichezonedelimitee_detail.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from model_bakery import baker
from playwright.sync_api import Page, expect

from sv.factories import EvenementFactory
from sv.factories import EvenementFactory, FicheZoneFactory
from sv.models import ZoneInfestee, FicheDetection


def test_fichezonedelimitee_with_zoneinfestee_detail(live_server, fiche_zone, page: Page, mocked_authentification_user):
fiche_zone_delimitee = fiche_zone
def test_fichezonedelimitee_with_zoneinfestee_detail(live_server, page: Page, mocked_authentification_user):
fiche_zone_delimitee = FicheZoneFactory()
evenement = EvenementFactory(fiche_zone_delimitee=fiche_zone_delimitee)
zone_infestee_1 = baker.make(ZoneInfestee, fiche_zone_delimitee=fiche_zone_delimitee, _fill_optional=True)
fiche_detection_fiche_zone_delimitee = baker.make(
Expand All @@ -18,13 +18,11 @@ def test_fichezonedelimitee_with_zoneinfestee_detail(live_server, fiche_zone, pa
page.get_by_role("tab", name="Zone").click()

expect(page.get_by_text(fiche_zone_delimitee.commentaire)).to_be_visible()
rayon_zone_tampon = str(fiche_zone_delimitee.rayon_zone_tampon).rstrip("0").rstrip(".")
expect(page.get_by_text(f"{rayon_zone_tampon} {fiche_zone_delimitee.unite_rayon_zone_tampon}")).to_be_visible()
surface_tampon_totale = str(fiche_zone_delimitee.surface_tampon_totale).rstrip("0").rstrip(".")
expect(
page.get_by_text(f"{fiche_zone_delimitee.rayon_zone_tampon} {fiche_zone_delimitee.unite_rayon_zone_tampon}")
).to_be_visible()
expect(
page.get_by_text(
f"{fiche_zone_delimitee.surface_tampon_totale} {fiche_zone_delimitee.unite_surface_tampon_totale}"
)
page.get_by_text(f"{surface_tampon_totale} {fiche_zone_delimitee.unite_surface_tampon_totale}")
).to_be_visible()
expect(page.get_by_role("link", name=f"{str(fiche_detection_fiche_zone_delimitee.numero)}")).to_be_visible()
expect(page.get_by_text(f"{zone_infestee_1.nom}")).to_be_visible()
Expand Down
36 changes: 16 additions & 20 deletions sv/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,60 +175,56 @@ def test_prelevement_officiel_cant_be_from_exploitant():


@pytest.mark.django_db
def test_cant_add_rayon_zone_tampon_negative_in_fiche_zone_delimitee(fiche_zone):
fiche_zone.rayon_zone_tampon = -1.0
def test_cant_add_rayon_zone_tampon_negative_in_fiche_zone_delimitee():
fiche_zone = FicheZoneFactory(rayon_zone_tampon=-1.0)
with pytest.raises(ValidationError) as exc_info:
fiche_zone.full_clean()
assert "rayon_zone_tampon" in exc_info.value.error_dict


@pytest.mark.django_db
def test_can_add_rayon_zone_tampon_zero_in_fiche_zone_delimitee(fiche_zone):
fiche_zone.surface_tampon_totale = None
fiche_zone.rayon_zone_tampon = 0
fiche_zone.full_clean()
def test_can_add_rayon_zone_tampon_zero_in_fiche_zone_delimitee():
FicheZoneFactory(surface_tampon_totale=None, rayon_zone_tampon=0)


@pytest.mark.django_db
def test_cant_add_surface_tampon_totale_negative_in_fiche_zone_delimitee(fiche_zone):
fiche_zone.surface_tampon_totale = -1.0
def test_cant_add_surface_tampon_totale_negative_in_fiche_zone_delimitee():
fiche_zone = FicheZoneFactory(surface_tampon_totale=-1.0)
with pytest.raises(ValidationError) as exc_info:
fiche_zone.full_clean()
assert "surface_tampon_totale" in exc_info.value.error_dict


@pytest.mark.django_db
def test_can_add_surface_tampon_totale_zero_in_fiche_zone_delimitee(fiche_zone):
fiche_zone.rayon_zone_tampon = None
fiche_zone.surface_tampon_totale = 0
fiche_zone.full_clean()
def test_can_add_surface_tampon_totale_zero_in_fiche_zone_delimitee():
FicheZoneFactory(rayon_zone_tampon=None, surface_tampon_totale=0)


@pytest.mark.django_db
def test_cant_add_rayon_negative_in_zone_infestee(fiche_zone):
zone = ZoneInfestee(rayon=-1.0, fiche_zone_delimitee=fiche_zone)
def test_cant_add_rayon_negative_in_zone_infestee():
zone = ZoneInfestee(rayon=-1.0, fiche_zone_delimitee=FicheZoneFactory())
with pytest.raises(ValidationError) as exc_info:
zone.full_clean()
assert "rayon" in exc_info.value.error_dict


@pytest.mark.django_db
def test_can_add_rayon_zero_in_zone_infestee(fiche_zone):
zone = ZoneInfestee(rayon=0, fiche_zone_delimitee=fiche_zone)
def test_can_add_rayon_zero_in_zone_infestee():
zone = ZoneInfestee(rayon=0, fiche_zone_delimitee=FicheZoneFactory())
zone.full_clean()


@pytest.mark.django_db
def test_cant_add_surface_infestee_totale_negative_in_zone_infestee(fiche_zone):
zone = ZoneInfestee(surface_infestee_totale=-1.0, fiche_zone_delimitee=fiche_zone)
def test_cant_add_surface_infestee_totale_negative_in_zone_infestee():
zone = ZoneInfestee(surface_infestee_totale=-1.0, fiche_zone_delimitee=FicheZoneFactory())
with pytest.raises(ValidationError) as exc_info:
zone.full_clean()
assert "surface_infestee_totale" in exc_info.value.error_dict


@pytest.mark.django_db
def test_can_add_surface_infestee_totale_zero_in_zone_infestee(fiche_zone):
zone = ZoneInfestee(surface_infestee_totale=0, fiche_zone_delimitee=fiche_zone)
def test_can_add_surface_infestee_totale_zero_in_zone_infestee():
zone = ZoneInfestee(surface_infestee_totale=0, fiche_zone_delimitee=FicheZoneFactory())
zone.full_clean()


Expand Down

0 comments on commit 23f3984

Please sign in to comment.