def test_delete_orphans(self): """ Verify the delete_orphans method deletes orphaned instances. """ instances = (ImageFactory(), VideoFactory(),) AbstractDataLoader.delete_orphans() for instance in instances: self.assertFalse(instance.__class__.objects.filter(pk=instance.pk).exists()) # pylint: disable=no-member
def assert_organization_loaded(self, body, partner_has_marketing_site=True): """ Assert an Organization corresponding to the specified data body was properly loaded into the database. """ organization = Organization.objects.get(key=AbstractDataLoader.clean_string(body['short_name'])) if not partner_has_marketing_site: self.assertEqual(organization.name, AbstractDataLoader.clean_string(body['name'])) self.assertEqual(organization.description, AbstractDataLoader.clean_string(body['description'])) self.assertEqual(organization.logo_image_url, AbstractDataLoader.clean_string(body['logo']))
def assert_program_loaded(self, body): """ Assert a Program corresponding to the specified data body was properly loaded into the database. """ program = Program.objects.get(uuid=AbstractDataLoader.clean_string(body['uuid']), partner=self.partner) self.assertEqual(program.title, body['name']) for attr in ('subtitle', 'status', 'marketing_slug',): self.assertEqual(getattr(program, attr), AbstractDataLoader.clean_string(body[attr])) self.assertEqual(program.type, ProgramType.objects.get(name='XSeries')) keys = [org['key'] for org in body['organizations']] expected_organizations = list(Organization.objects.filter(key__in=keys)) self.assertEqual(keys, [org.key for org in expected_organizations]) self.assertListEqual(list(program.authoring_organizations.all()), expected_organizations) banner_image_url = body.get('banner_image_urls', {}).get('w1440h480') self.assertEqual(program.banner_image_url, banner_image_url) course_run_keys = set() course_codes = body.get('course_codes', []) for course_code in course_codes: course_run_keys.update([course_run['course_key'] for course_run in course_code['run_modes']]) courses = list(Course.objects.filter(course_runs__key__in=course_run_keys).distinct().order_by('key')) self.assertEqual(list(program.courses.order_by('key')), courses) # Verify the additional course runs added in create_mock_courses_and_runs are excluded. self.assertEqual(program.excluded_course_runs.count(), len(course_codes))
def test_parse_date(self): """ Verify the method properly parses dates. """ # Do nothing for empty values self.assertIsNone(AbstractDataLoader.parse_date('')) self.assertIsNone(AbstractDataLoader.parse_date(None)) # Parse datetime strings dt = datetime.datetime.utcnow() self.assertEqual(AbstractDataLoader.parse_date(dt.isoformat()), dt)
def assert_program_loaded(self, body): """ Assert a Program corresponding to the specified data body was properly loaded into the database. """ program = Program.objects.get(uuid=AbstractDataLoader.clean_string( body['uuid']), partner=self.partner) self.assertEqual(program.title, body['name']) for attr in ( 'subtitle', 'status', 'marketing_slug', ): self.assertEqual(getattr(program, attr), AbstractDataLoader.clean_string(body[attr])) self.assertEqual(program.type, ProgramType.objects.get(name='XSeries')) keys = [org['key'] for org in body['organizations']] expected_organizations = list( Organization.objects.filter(key__in=keys)) self.assertEqual(keys, [org.key for org in expected_organizations]) self.assertListEqual(list(program.authoring_organizations.all()), expected_organizations) banner_image_url = body.get('banner_image_urls', {}).get('w1440h480') self.assertEqual(program.banner_image_url, banner_image_url) course_run_keys = set() course_codes = body.get('course_codes', []) for course_code in course_codes: course_run_keys.update([ course_run['course_key'] for course_run in course_code['run_modes'] ]) courses = list( Course.objects.filter(course_runs__key__in=course_run_keys). distinct().order_by('key')) self.assertEqual(list(program.courses.order_by('key')), courses) # Verify the additional course runs added in create_mock_courses_and_runs are excluded. self.assertEqual(program.excluded_course_runs.count(), len(course_codes))
def test_clean_html(self): """ Verify the method removes unnecessary HTML attributes. """ data = (( '', '', ), ('<p>Hello!</p>', 'Hello!'), ('<em>Testing</em>', '<em>Testing</em>'), ('Hello&world !', 'Hello&world!')) for content, expected in data: self.assertEqual(AbstractDataLoader.clean_html(content), expected)
def test_clean_html(self): """ Verify the method removes unnecessary HTML attributes. """ data = ( ('', '',), ('<p>Hello!</p>', 'Hello!'), ('<em>Testing</em>', '<em>Testing</em>'), ('Hello&world !', 'Hello&world!') ) for content, expected in data: self.assertEqual(AbstractDataLoader.clean_html(content), expected)
def assert_program_banner_image_loaded(self, body): """ Assert a program corresponding to the specified data body has banner image loaded into DB """ program = Program.objects.get(uuid=AbstractDataLoader.clean_string(body['uuid']), partner=self.partner) banner_image_url = body.get('banner_image_urls', {}).get('w1440h480') if banner_image_url: for size_key in program.banner_image.field.variations: # Get different sizes specs from the model field # Then get the file path from the available files sized_image = getattr(program.banner_image, size_key, None) self.assertIsNotNone(sized_image) if sized_image: path = getattr(program.banner_image, size_key).url self.assertIsNotNone(path) self.assertIsNotNone(program.banner_image.field.variations[size_key]['width']) self.assertIsNotNone(program.banner_image.field.variations[size_key]['height'])
def test_clean_string(self): """ Verify the method leading and trailing spaces, and returns None for empty strings. """ # Do nothing for non-string input self.assertIsNone(AbstractDataLoader.clean_string(None)) self.assertEqual(AbstractDataLoader.clean_string(3.14), 3.14) # Return None for empty strings self.assertIsNone(AbstractDataLoader.clean_string('')) self.assertIsNone(AbstractDataLoader.clean_string(' ')) self.assertIsNone(AbstractDataLoader.clean_string('\t')) # Return the stripped value for non-empty strings for s in ('\tabc', 'abc', ' abc ', 'abc ', '\tabc\t '): self.assertEqual(AbstractDataLoader.clean_string(s), 'abc')
def test_clean_string(self): """ Verify the method leading and trailing spaces, and returns None for empty strings. """ # Do nothing for non-string input assert AbstractDataLoader.clean_string(None) is None assert AbstractDataLoader.clean_string(3.14) == 3.14 # Return None for empty strings assert AbstractDataLoader.clean_string('') is None assert AbstractDataLoader.clean_string(' ') is None assert AbstractDataLoader.clean_string('\t') is None # Return the stripped value for non-empty strings for s in ('\tabc', 'abc', ' abc ', 'abc ', '\tabc\t '): assert AbstractDataLoader.clean_string(s) == 'abc'