Skip to content

Commit

Permalink
tests: improve ProlongationFactory to create consistent request
Browse files Browse the repository at this point in the history
  • Loading branch information
xavfernandez committed Oct 4, 2024
1 parent 4dcd03b commit 050bfec
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
36 changes: 33 additions & 3 deletions tests/approvals/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,40 @@ class Params:
class ProlongationFactory(BaseProlongationFactory):
class Meta:
model = Prolongation
skip_postgeneration_save = True


class ProlongationWithRequestFactory(ProlongationFactory):
request = factory.SubFactory(ProlongationRequestFactory)
@factory.post_generation
def with_request(self, create, extracted, **kwargs):
if not create:
return
if extracted:
self.request = ProlongationRequestFactory(
status=ProlongationRequestStatus.GRANTED,
processed_at=timezone.now(),
processed_by=self.validated_by,
updated_by=self.validated_by,
**{
field: getattr(self, field)
for field in (
"approval",
"start_at",
"end_at",
"reason",
"reason_explanation",
"declared_by",
"declared_by_siae",
"prescriber_organization",
"created_by",
"report_file",
"require_phone_interview",
"contact_email",
"contact_phone",
)
if getattr(self, field)
}
| kwargs,
)
self.save(update_fields=("request",))


class PoleEmploiApprovalFactory(factory.django.DjangoModelFactory):
Expand Down
8 changes: 4 additions & 4 deletions tests/metabase/management/test_populate_metabase_emplois.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from tests.approvals.factories import (
ApprovalFactory,
PoleEmploiApprovalFactory,
ProlongationFactory,
ProlongationRequestDenyInformationFactory,
ProlongationWithRequestFactory,
)
from tests.companies.factories import CompanyFactory, CompanyMembershipFactory, JobDescriptionFactory
from tests.eligibility.factories import IAEEligibilityDiagnosisFactory
Expand Down Expand Up @@ -589,7 +589,7 @@ def test_populate_approvals():
@pytest.mark.django_db(transaction=True)
@pytest.mark.usefixtures("metabase")
def test_populate_prolongations():
prolongation = ProlongationWithRequestFactory()
prolongation = ProlongationFactory(with_request=True)
prolongation_request = prolongation.request

num_queries = 1 # Count prolongations
Expand Down Expand Up @@ -630,14 +630,14 @@ def test_populate_prolongations():
@pytest.mark.django_db(transaction=True)
@pytest.mark.usefixtures("metabase")
def test_populate_prolongation_requests():
prolongation = ProlongationWithRequestFactory()
prolongation = ProlongationFactory(with_request=True)
prolongation_request = prolongation.request

deny_information = ProlongationRequestDenyInformationFactory.build(request=None)
with transaction.atomic():
prolongation_request.deny(prolongation_request.validated_by, deny_information)

ProlongationWithRequestFactory() # add another one to ensure we don't fail without a deny_information
ProlongationFactory(with_request=True) # add another one to ensure we don't fail without a deny_information

num_queries = 1 # Count prolongation_requests
num_queries += 1 # COMMIT Queryset counts (autocommit mode)
Expand Down

0 comments on commit 050bfec

Please sign in to comment.