def test_ingest_verified_deadline(self, mock_push_to_ecomm): """ Verify the method ingests data from the Courses API. """ api_data = self.mock_api() self.assertEqual(Course.objects.count(), 0) self.assertEqual(CourseRun.objects.count(), 0) # Assume that while we are relying on ORGS_ON_OLD_PUBLISHER it will never be empty with self.settings(ORGS_ON_OLD_PUBLISHER='OTHER'): self.loader.ingest() # Verify the API was called with the correct authorization header self.assert_api_called(4) runs = CourseRun.objects.all() # Run with a verified entitlement, but no change in end date run1 = runs[0] run1.seats.add( SeatFactory(course_run=run1, type=SeatTypeFactory.verified())) run1.save() # Run with a verified entitlement, and the end date has changed run2 = runs[1] run2.seats.add( SeatFactory( course_run=run2, type=SeatTypeFactory.verified(), upgrade_deadline=datetime.datetime.now(pytz.UTC), )) original_run2_deadline = run2.seats.first().upgrade_deadline run2.end = datetime.datetime.now(pytz.UTC) run2.save() # Run with a credit entitlement, and the end date has changed should not run3 = runs[2] run3.seats.add( SeatFactory( course_run=run3, type=SeatTypeFactory.credit(), upgrade_deadline=None, )) run3.end = datetime.datetime.now(pytz.UTC) run3.save() # Verify the CourseRuns were created correctly expected_num_course_runs = len(api_data) self.assertEqual(CourseRun.objects.count(), expected_num_course_runs) # Verify multiple calls to ingest data do NOT result in data integrity errors. self.loader.ingest() calls = [ mock.call(run2), mock.call(run3), ] mock_push_to_ecomm.assert_has_calls(calls) # Make sure the verified seat with a course run end date is changed self.assertNotEqual(original_run2_deadline, run2.seats.first().upgrade_deadline) # Make sure the credit seat with a course run end date is unchanged self.assertIsNone(run3.seats.first().upgrade_deadline)
def test_csv(self): SeatFactory(type=SeatTypeFactory.audit(), course_run=self.course_run) SeatFactory(type=SeatTypeFactory.verified(), course_run=self.course_run) SeatFactory(type=SeatTypeFactory.credit(), course_run=self.course_run, credit_provider='ASU', credit_hours=9) SeatFactory(type=SeatTypeFactory.credit(), course_run=self.course_run, credit_provider='Hogwarts', credit_hours=4) url = reverse('api:v1:catalog-csv', kwargs={'id': self.catalog.id}) with self.assertNumQueries(23): response = self.client.get(url) course_run = self.serialize_catalog_flat_course_run(self.course_run) expected = [ course_run['announcement'], course_run['content_language'], course_run['course_key'], course_run['end'], course_run['enrollment_end'], course_run['enrollment_start'], course_run['expected_learning_items'], course_run['full_description'], '', # image description '', # image height course_run['image']['src'], '', # image width course_run['key'], str(course_run['level_type']), course_run['marketing_url'], str(course_run['max_effort']), str(course_run['min_effort']), course_run['modified'], course_run['owners'], course_run['pacing_type'], course_run['prerequisites'], course_run['seats']['audit']['type'], '{}'.format(course_run['seats']['credit']['credit_hours']), '{}'.format(course_run['seats']['credit']['credit_provider']), '{}'.format(course_run['seats']['credit']['currency']), '{}'.format(str(course_run['seats']['credit']['price'])), '{}'.format(course_run['seats']['credit']['type']), '{}'.format(course_run['seats']['credit']['upgrade_deadline']), course_run['seats']['honor']['type'], course_run['seats']['masters']['type'], course_run['seats']['professional']['currency'], str(course_run['seats']['professional']['price']), course_run['seats']['professional']['type'], course_run['seats']['professional']['upgrade_deadline'], course_run['seats']['verified']['currency'], str(course_run['seats']['verified']['price']), course_run['seats']['verified']['type'], course_run['seats']['verified']['upgrade_deadline'], course_run['short_description'], course_run['sponsors'], course_run['start'], course_run['subjects'], course_run['title'], course_run['video']['description'], course_run['video']['image']['description'], str(course_run['video']['image']['height']), course_run['video']['image']['src'], str(course_run['video']['image']['width']), course_run['video']['src'], ] # collect streamed content received_content = b'' for item in response.streaming_content: received_content += item # convert received content to csv for comparison f = StringIO(received_content.decode('utf-8')) reader = csv.reader(f) content = list(reader) assert response.status_code == 200 assert expected == content[1]