|
| 1 | +import datetime |
| 2 | +from urllib import urlencode |
| 3 | + |
| 4 | +import ddt |
| 5 | +from django_dynamic_fixture import G |
| 6 | +import pytz |
| 7 | + |
| 8 | +from django.conf import settings |
| 9 | + |
| 10 | +from analytics_data_api.constants import enrollment_modes |
| 11 | +from analytics_data_api.v0 import models, serializers |
| 12 | +from analytics_data_api.v0.tests.views import CourseSamples, VerifyCourseIdMixin |
| 13 | +from analyticsdataserver.tests import TestCaseWithAuthentication |
| 14 | + |
| 15 | + |
| 16 | +@ddt.ddt |
| 17 | +class CourseSummariesViewTests(VerifyCourseIdMixin, TestCaseWithAuthentication): |
| 18 | + model = models.CourseMetaSummaryEnrollment |
| 19 | + serializer = serializers.CourseMetaSummaryEnrollmentSerializer |
| 20 | + expected_summaries = [] |
| 21 | + |
| 22 | + def setUp(self): |
| 23 | + super(CourseSummariesViewTests, self).setUp() |
| 24 | + self.now = datetime.datetime.utcnow() |
| 25 | + |
| 26 | + def tearDown(self): |
| 27 | + self.model.objects.all().delete() |
| 28 | + |
| 29 | + def path(self, course_ids=None, fields=None): |
| 30 | + query_params = {} |
| 31 | + for query_arg, data in zip(['course_ids', 'fields'], [course_ids, fields]): |
| 32 | + if data: |
| 33 | + query_params[query_arg] = ','.join(data) |
| 34 | + query_string = '?{}'.format(urlencode(query_params)) |
| 35 | + return '/api/v0/course_summaries/{}'.format(query_string) |
| 36 | + |
| 37 | + def generate_data(self, course_ids=None, modes=None): |
| 38 | + """Generate course summary data for """ |
| 39 | + if course_ids is None: |
| 40 | + course_ids = CourseSamples.course_ids |
| 41 | + |
| 42 | + if modes is None: |
| 43 | + modes = enrollment_modes.ALL |
| 44 | + |
| 45 | + for course_id in course_ids: |
| 46 | + for mode in modes: |
| 47 | + G(self.model, course_id=course_id, catalog_course_title='Title', catalog_course='Catalog', |
| 48 | + start_date=datetime.datetime(2016, 10, 11, tzinfo=pytz.utc), |
| 49 | + end_date=datetime.datetime(2016, 12, 18, tzinfo=pytz.utc), |
| 50 | + pacing_type='instructor', availability='current', mode=mode, |
| 51 | + count=5, cumulative_count=10, count_change_7_days=1, create=self.now,) |
| 52 | + |
| 53 | + def expected_summary(self, course_id, modes=None): |
| 54 | + """Expected summary information for a course and modes to populate with data.""" |
| 55 | + if modes is None: |
| 56 | + modes = enrollment_modes.ALL |
| 57 | + |
| 58 | + num_modes = len(modes) |
| 59 | + count_factor = 5 |
| 60 | + cumulative_count_factor = 10 |
| 61 | + count_change_factor = 1 |
| 62 | + summary = { |
| 63 | + 'course_id': course_id, |
| 64 | + 'catalog_course_title': 'Title', |
| 65 | + 'catalog_course': 'Catalog', |
| 66 | + 'start_date': datetime.datetime(2016, 10, 11, tzinfo=pytz.utc).strftime(settings.DATETIME_FORMAT), |
| 67 | + 'end_date': datetime.datetime(2016, 12, 18, tzinfo=pytz.utc).strftime(settings.DATETIME_FORMAT), |
| 68 | + 'pacing_type': 'instructor', |
| 69 | + 'availability': 'current', |
| 70 | + 'modes': {}, |
| 71 | + 'count': count_factor * num_modes, |
| 72 | + 'cumulative_count': cumulative_count_factor * num_modes, |
| 73 | + 'count_change_7_days': count_change_factor * num_modes, |
| 74 | + 'created': self.now.strftime(settings.DATETIME_FORMAT), |
| 75 | + } |
| 76 | + summary['modes'].update({ |
| 77 | + mode: { |
| 78 | + 'count': count_factor, |
| 79 | + 'cumulative_count': cumulative_count_factor, |
| 80 | + 'count_change_7_days': count_change_factor, |
| 81 | + } for mode in modes |
| 82 | + }) |
| 83 | + summary['modes'].update({ |
| 84 | + mode: { |
| 85 | + 'count': 0, |
| 86 | + 'cumulative_count': 0, |
| 87 | + 'count_change_7_days': 0, |
| 88 | + } for mode in set(enrollment_modes.ALL) - set(modes) |
| 89 | + }) |
| 90 | + no_prof = summary['modes'].pop(enrollment_modes.PROFESSIONAL_NO_ID) |
| 91 | + prof = summary['modes'].get(enrollment_modes.PROFESSIONAL) |
| 92 | + prof.update({ |
| 93 | + 'count': prof['count'] + no_prof['count'], |
| 94 | + 'cumulative_count': prof['cumulative_count'] + no_prof['cumulative_count'], |
| 95 | + 'count_change_7_days': prof['count_change_7_days'] + no_prof['count_change_7_days'], |
| 96 | + }) |
| 97 | + return summary |
| 98 | + |
| 99 | + def all_expected_summaries(self, modes=None): |
| 100 | + if modes is None: |
| 101 | + modes = enrollment_modes.ALL |
| 102 | + return [self.expected_summary(course_id, modes) for course_id in CourseSamples.course_ids] |
| 103 | + |
| 104 | + @ddt.data( |
| 105 | + None, |
| 106 | + CourseSamples.course_ids, |
| 107 | + ['not/real/course'].extend(CourseSamples.course_ids), |
| 108 | + ) |
| 109 | + def test_all_courses(self, course_ids): |
| 110 | + self.generate_data() |
| 111 | + response = self.authenticated_get(self.path(course_ids=course_ids)) |
| 112 | + self.assertEquals(response.status_code, 200) |
| 113 | + self.assertItemsEqual(response.data, self.all_expected_summaries()) |
| 114 | + |
| 115 | + @ddt.data(*CourseSamples.course_ids) |
| 116 | + def test_one_course(self, course_id): |
| 117 | + self.generate_data() |
| 118 | + response = self.authenticated_get(self.path(course_ids=[course_id])) |
| 119 | + self.assertEquals(response.status_code, 200) |
| 120 | + self.assertItemsEqual(response.data, [self.expected_summary(course_id)]) |
| 121 | + |
| 122 | + @ddt.data( |
| 123 | + ['availability'], |
| 124 | + ['modes', 'course_id'], |
| 125 | + ) |
| 126 | + def test_fields(self, fields): |
| 127 | + self.generate_data() |
| 128 | + response = self.authenticated_get(self.path(fields=fields)) |
| 129 | + self.assertEquals(response.status_code, 200) |
| 130 | + |
| 131 | + # remove fields not requested from expected results |
| 132 | + expected_summaries = self.all_expected_summaries() |
| 133 | + for expected_summary in expected_summaries: |
| 134 | + for field_to_remove in set(expected_summary.keys()) - set(fields): |
| 135 | + expected_summary.pop(field_to_remove) |
| 136 | + |
| 137 | + self.assertItemsEqual(response.data, expected_summaries) |
| 138 | + |
| 139 | + @ddt.data( |
| 140 | + [enrollment_modes.VERIFIED], |
| 141 | + [enrollment_modes.HONOR, enrollment_modes.PROFESSIONAL], |
| 142 | + ) |
| 143 | + def test_empty_modes(self, modes): |
| 144 | + self.generate_data(modes=modes) |
| 145 | + response = self.authenticated_get(self.path()) |
| 146 | + self.assertEquals(response.status_code, 200) |
| 147 | + self.assertItemsEqual(response.data, self.all_expected_summaries(modes)) |
| 148 | + |
| 149 | + def test_no_summaries(self): |
| 150 | + response = self.authenticated_get(self.path()) |
| 151 | + self.assertEquals(response.status_code, 404) |
| 152 | + |
| 153 | + def test_no_matching_courses(self): |
| 154 | + self.generate_data() |
| 155 | + response = self.authenticated_get(self.path(course_ids=['no/course/found'])) |
| 156 | + self.assertEquals(response.status_code, 404) |
| 157 | + |
| 158 | + @ddt.data( |
| 159 | + ['malformed-course-id'], |
| 160 | + [CourseSamples.course_ids[0], 'malformed-course-id'], |
| 161 | + ) |
| 162 | + def test_bad_course_id(self, course_ids): |
| 163 | + response = self.authenticated_get(self.path(course_ids=course_ids)) |
| 164 | + self.verify_bad_course_id(response) |
0 commit comments