Skip to content

Commit

Permalink
batchorder factory for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanreveille committed Mar 11, 2025
1 parent e039bb8 commit d9ad163
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/backend/joanie/core/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -1464,3 +1464,50 @@ class Meta:
)
multiple_use = False
multiple_users = False


class BatchOrderFactory(DebugModelFactory, factory.django.DjangoModelFactory):
"""Factory for the Batch Order model"""

class Meta:
model = models.BatchOrder

relation = factory.SubFactory(
CourseProductRelationFactory,
product__type=enums.PRODUCT_TYPE_CREDENTIAL,
product__contract_definition=factory.SubFactory(ContractDefinitionFactory),
)
owner = factory.SubFactory(UserFactory)
identification_number = factory.Faker("random_number", digits=14, fix_len=True)
company_name = factory.Faker("word")
address = factory.Faker("street_address")
post_code = factory.Faker("postcode")
city = factory.Faker("city")
country = factory.Faker("country_code")
nb_seats = factory.fuzzy.FuzzyInteger(1, 20)
voucher = None
contract = None

@factory.lazy_attribute
def organization(self):
"""Retrieve the organization from the product/course relation."""
course_relations = self.relation.product.course_relations
return course_relations.first().organizations.order_by("?").first()

@factory.lazy_attribute
def total(self):
"""Generate the total of the batch order from the product price and the number of seats"""
total = self.nb_seats * self.relation.product.price
return total

# pylint: disable=unused-argument
@factory.post_generation
def trainees(self, create, extracted, **kwargs):
"""
Prepare a list of dictionary with first name and last name of students.
We ensure that the length of trainees matches the number of seats.
"""
return [
{"first_name": factory.Faker("name"), "last_name": factory.Faker("name")}
for _ in range(self.nb_seats)
]

0 comments on commit d9ad163

Please sign in to comment.