Skip to content

Commit 37fbc26

Browse files
martin056RadoRado
authored andcommitted
Add an example with Params and improve the RosterFactory
1 parent 0fe7f60 commit 37fbc26

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

styleguide_example/test_examples/tests/factories.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,13 @@ class Meta:
5959

6060
student = factory.SubFactory(StudentFactory)
6161
# Make sure the student and the school_course are from the same school
62-
school_course = factory.SubFactory(SchoolCourseFactory, school=factory.SelfAttribute('..student.school'))
62+
school_course = factory.SubFactory(
63+
SchoolCourseFactory,
64+
school=factory.SelfAttribute('..student.school')
65+
)
6366

64-
# The code below might cause bugs because of the school_course period
65-
# start_date = factory.LazyAttribute(lambda _: faker.past_date())
66-
# end_date = factory.LazyAttribute(lambda _: faker.future_date())
67-
start_date = factory.LazyAttribute(lambda self: faker.date_between_dates(
68-
date_start=self.school_course.start_date,
69-
date_end=self.school_course.end_date - timedelta(days=2)
70-
))
71-
end_date = factory.LazyAttribute(lambda self: faker.date_between_dates(
72-
date_start=self.start_date + timedelta(days=1),
73-
date_end=self.school_course.end_date
74-
))
67+
start_date = factory.SelfAttribute('school_course.start_date')
68+
end_date = factory.SelfAttribute('school_course.end_date')
7569

7670
active = True
7771
deactivated_at = factory.Maybe(
@@ -84,6 +78,20 @@ class Meta:
8478
)
8579

8680

81+
def get_future_roster_start_date(roster_obj):
82+
if not roster_obj.start_after:
83+
return faker.future_date()
84+
85+
return roster_obj.start_after + timedelta(days=faker.pyint(2, 100))
86+
87+
88+
class FutureRosterFactory(RosterFactory):
89+
class Params:
90+
start_after = None
91+
92+
start_date = factory.LazyAttribute(get_future_roster_start_date)
93+
94+
8795
class SchoolCourseWithRostersFactory(SchoolCourseFactory):
8896
@factory.post_generation
8997
def rosters(obj, create, extracted, **kwargs):

0 commit comments

Comments
 (0)