|
| 1 | +from datetime import timedelta |
| 2 | + |
| 3 | +import factory |
| 4 | + |
| 5 | +from django.test import TestCase |
| 6 | +from django.core.exceptions import ValidationError |
| 7 | +from django.utils import timezone |
| 8 | + |
| 9 | +from styleguide_example.utils.tests import faker |
| 10 | +from styleguide_example.test_examples.selectors import ( |
| 11 | + school_list_school_courses, |
| 12 | + SCHOOL_LIST_SCHOOL_COURSES_PROVIDE_START_DATE_MSG |
| 13 | +) |
| 14 | +from styleguide_example.test_examples.tests.factories import ( |
| 15 | + SchoolFactory, |
| 16 | + SchoolCourseFactory |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +class SchoolListSchoolCoursesTests(TestCase): |
| 21 | + def _order_school_courses(self, school_courses): |
| 22 | + # Helper method - the result from the selector is ordered |
| 23 | + return sorted(school_courses, key=lambda x: x.start_date) |
| 24 | + |
| 25 | + def test_selector_raises_validation_error_if_unexpected_period_is_given(self): |
| 26 | + with self.assertRaisesMessage( |
| 27 | + ValidationError, |
| 28 | + SCHOOL_LIST_SCHOOL_COURSES_PROVIDE_START_DATE_MSG |
| 29 | + ): |
| 30 | + school_list_school_courses( |
| 31 | + school=SchoolFactory.build(), |
| 32 | + start_date=None, |
| 33 | + end_date=faker.future_date() |
| 34 | + ) |
| 35 | + |
| 36 | + def test_selector_returns_all_school_courses_if_no_dates_passed(self): |
| 37 | + school = SchoolFactory() |
| 38 | + school_courses = SchoolCourseFactory.create_batch(3, school=school) |
| 39 | + |
| 40 | + result = school_list_school_courses(school=school) |
| 41 | + |
| 42 | + self.assertEqual( |
| 43 | + self._order_school_courses(school_courses), |
| 44 | + list(result) |
| 45 | + ) |
| 46 | + |
| 47 | + def test_selector_does_not_return_school_courses_for_other_schools(self): |
| 48 | + expected_school_course = SchoolCourseFactory() |
| 49 | + other_school_course = SchoolCourseFactory() |
| 50 | + |
| 51 | + result = school_list_school_courses(school=expected_school_course.school) |
| 52 | + |
| 53 | + self.assertNotIn(other_school_course, result) |
| 54 | + self.assertEqual([expected_school_course], list(result)) |
| 55 | + |
| 56 | + def test_selector_returns_all_school_courses_for_a_given_start_date(self): |
| 57 | + school = SchoolFactory() |
| 58 | + now = timezone.now() |
| 59 | + |
| 60 | + future_school_course = SchoolCourseFactory( |
| 61 | + school=school, |
| 62 | + start_date=faker.future_date() |
| 63 | + ) |
| 64 | + started_school_course = SchoolCourseFactory( |
| 65 | + school=school, |
| 66 | + start_date=faker.past_date(), |
| 67 | + end_date=faker.future_date() |
| 68 | + ) |
| 69 | + |
| 70 | + finished_school_course = SchoolCourseFactory( |
| 71 | + school=school, |
| 72 | + start_date=faker.past_date(), |
| 73 | + # Uses timedelta in order to make sure the generated date is not equal to the period |
| 74 | + end_date=factory.LazyAttribute(lambda _self: faker.date_between_dates( |
| 75 | + date_start=_self.start_date + timedelta(days=1), |
| 76 | + date_end=now - timedelta(days=1) |
| 77 | + )) |
| 78 | + ) |
| 79 | + |
| 80 | + result = school_list_school_courses(school=school, start_date=now) |
| 81 | + |
| 82 | + self.assertNotIn(finished_school_course, result) |
| 83 | + self.assertEqual( |
| 84 | + [started_school_course, future_school_course], |
| 85 | + list(result) |
| 86 | + ) |
| 87 | + |
| 88 | + def test_selector_returns_all_school_courses_for_a_given_period(self): |
| 89 | + school = SchoolFactory() |
| 90 | + |
| 91 | + period_start = timezone.now() |
| 92 | + period_end = period_start + timedelta(days=100) |
| 93 | + |
| 94 | + started_school_course = SchoolCourseFactory( |
| 95 | + school=school, |
| 96 | + start_date=period_start - timedelta(days=10), |
| 97 | + end_date=period_end - timedelta(days=10) |
| 98 | + ) |
| 99 | + school_course_inside_period = SchoolCourseFactory( |
| 100 | + school=school, |
| 101 | + start_date=period_start + timedelta(days=10), |
| 102 | + end_date=period_end - timedelta(days=10) |
| 103 | + ) |
| 104 | + school_course_wrapping_period = SchoolCourseFactory( |
| 105 | + school=school, |
| 106 | + start_date=period_start - timedelta(days=5), |
| 107 | + end_date=period_end + timedelta(days=5) |
| 108 | + ) |
| 109 | + starting_school_course = SchoolCourseFactory( |
| 110 | + school=school, |
| 111 | + start_date=period_end - timedelta(days=10), |
| 112 | + end_date=period_end + timedelta(days=10) |
| 113 | + ) |
| 114 | + |
| 115 | + past_school_course = SchoolCourseFactory( |
| 116 | + school=school, |
| 117 | + start_date=period_start - timedelta(days=20), |
| 118 | + end_date=period_start - timedelta(days=10) |
| 119 | + ) |
| 120 | + future_school_course = SchoolCourseFactory( |
| 121 | + school=school, |
| 122 | + start_date=period_end + timedelta(days=10), |
| 123 | + end_date=period_end + timedelta(days=20) |
| 124 | + ) |
| 125 | + |
| 126 | + result = school_list_school_courses( |
| 127 | + school=school, |
| 128 | + start_date=period_start, |
| 129 | + end_date=period_end |
| 130 | + ) |
| 131 | + |
| 132 | + self.assertNotIn(past_school_course, result) |
| 133 | + self.assertNotIn(future_school_course, result) |
| 134 | + self.assertEqual( |
| 135 | + self._order_school_courses([ |
| 136 | + started_school_course, |
| 137 | + school_course_inside_period, |
| 138 | + school_course_wrapping_period, |
| 139 | + starting_school_course |
| 140 | + ]), |
| 141 | + list(result) |
| 142 | + ) |
0 commit comments