def test_list(self): """ Verify the endpoint returns a list of all programs. """ url = reverse('api:v1:program-list') ProgramFactory.create_batch(3) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['results'], ProgramSerializer(Program.objects.all(), many=True).data)
def test_uuids_only(self): """ Verify that the list view returns a simply list of UUIDs when the uuids_only query parameter is passed. """ active = ProgramFactory.create_batch(3, partner=self.partner) retired = [ ProgramFactory(status=ProgramStatus.Retired, partner=self.partner) ] programs = active + retired querystring = {'uuids_only': 1} url = '{base}?{query}'.format( base=self.list_path, query=urllib.parse.urlencode(querystring)) response = self.client.get(url) assert set(response.data) == {program.uuid for program in programs} # Verify that filtering (e.g., by status) is still supported. querystring['status'] = ProgramStatus.Retired url = '{base}?{query}'.format( base=self.list_path, query=urllib.parse.urlencode(querystring)) response = self.client.get(url) assert set(response.data) == {program.uuid for program in retired}
def test_sanity_check_success(self): """ Verify the command does not raise a CommandError error if the new index passes the sanity check. """ CourseRunFactory.create_batch(59) ProgramFactory.create_batch(59) PersonFactory.create_batch(59) record_count = 60 # Ensure that no error is raised and the sanity check passes the second time with mock.patch( 'course_discovery.apps.core.utils.ElasticsearchUtils.set_alias', return_value=True): with mock.patch( 'course_discovery.apps.edx_elasticsearch_dsl_extensions.management.commands.' 'update_index.Command.get_record_count', return_value=record_count): call_command('update_index')
def test_filter_by_marketable(self, status, is_marketable, expected_query_count): """ Verify the endpoint filters programs to those that are marketable. """ url = self.list_path + '?marketable=1' ProgramFactory(marketing_slug='', partner=self.partner) programs = ProgramFactory.create_batch(3, status=status, partner=self.partner) expected = programs if is_marketable else [] assert list(Program.objects.marketable()) == expected self.assert_list_results(url, expected, expected_query_count)
def test_filter_by_marketable(self, status, is_marketable, expected_query_count): """ Verify the endpoint filters programs to those that are marketable. """ url = self.list_path + '?marketable=1' ProgramFactory(marketing_slug='') programs = ProgramFactory.create_batch(3, status=status) programs.reverse() expected = programs if is_marketable else [] self.assertEqual(list(Program.objects.marketable()), expected) self.assert_list_results(url, expected, expected_query_count)
def test_filter_by_uuids(self): """ Verify that the endpoint filters programs to those matching the provided UUIDs. """ expected = ProgramFactory.create_batch(2, partner=self.partner) uuids = [str(p.uuid) for p in expected] url = self.list_path + '?uuids=' + ','.join(uuids) # Create a third program, which should be filtered out. ProgramFactory(partner=self.partner) self.assert_list_results(url, expected, 14)
def test_filter_by_types(self): """ Verify that the endpoint filters programs to those matching the provided ProgramType slugs. """ expected = ProgramFactory.create_batch(2, partner=self.partner) type_slugs = [p.type.slug for p in expected] url = self.list_path + '?types=' + ','.join(type_slugs) # Create a third program, which should be filtered out. ProgramFactory(partner=self.partner) self.assert_list_results(url, expected, 14)
def test_filter_by_uuids(self): """ Verify that the endpoint filters programs to those matching the provided UUIDs. """ expected = ProgramFactory.create_batch(2) expected.reverse() uuids = [str(p.uuid) for p in expected] url = self.list_path + '?uuids=' + ','.join(uuids) # Create a third program, which should be filtered out. ProgramFactory() self.assert_list_results(url, expected, 8)
def test_program_autocomplete(self, admin_client): """ Verify Program autocomplete returns the data. """ programs = ProgramFactory.create_batch(3) path = reverse('admin_metadata:program-autocomplete') response = admin_client.get(path) data = json.loads(response.content.decode('utf-8')) assert response.status_code == 200 assert len(data['results']) == 3 # Search for substrings of program titles program = programs[0] self.assert_valid_query_result(admin_client, path, program.title[5:], program) program = programs[1] self.assert_valid_query_result(admin_client, path, program.title[5:], program) admin_client.logout() response = admin_client.get(path) data = json.loads(response.content.decode('utf-8')) assert response.status_code == 200 assert len(data['results']) == 0