def test_movie_with_cast(self): m = MovieFactory() for i in range(100): cast = CastFactory(movie=m) CastFactory(role=cast.role) query = ''' query Movie($id: ID!, $role: ID!) { movie(id: $id) { id cast(role: $role) { edges { node { name person { firstNameEn, lastNameEn } role { nameEn } } } } } } ''' with self.assertNumQueries(3): result = self.execute( query, dict(id=global_id(m), role=global_id(cast.role))) self.assertEqual(len(result['movie']['cast']['edges']), m.cast.filter(role=cast.role).count())
def test_movie_with_cast(self): movie = MovieFactory() movie.cast.add(CastFactory()) movie.cast.add(CastFactory()) self.assertEqual(Person.objects.count(), 2) self.assertEqual(Cast.objects.count(), 2) self.assertEqual(movie.cast.count(), 2)
def test_person_with_career(self): person = PersonFactory() person.career.add(CastFactory()) person.career.add(CastFactory()) self.assertEqual(Movie.objects.count(), 2) self.assertEqual(Cast.objects.count(), 2) self.assertEqual(person.career.count(), 2)
def test_search_and_sync_right_movie_by_person(self, search_movie): movie1 = MovieFactory(year=2014, title_en='The Prince') movie2 = MovieFactory(year=2014, title_en='The Prince') ImdbPersonFactory(id=2357819, person=CastFactory(movie=movie1).person) ImdbPersonFactory(id=3167230, person=CastFactory(movie=movie2).person) sync_movie(movie1.id) sync_movie(movie2.id) self.assertEqual(movie1.imdb.id, 1085492) self.assertEqual(movie2.imdb.id, 3505782) self.assertFalse(search_movie.called)
def test_person_roles(self): person = PersonFactory(roles=[]) CastFactory(person=person, role=Role.objects.get_scenarist()) for i in range(9): CastFactory(person=person, role=Role.objects.get_actor()) person.set_roles() self.assertQuerysetEqual(person.roles.all(), [Role.objects.get_actor().name]) # exceed threshold CastFactory(person=person, role=Role.objects.get_scenarist()) person.set_roles() self.assertQuerysetEqual(person.roles.all(), [Role.objects.get_actor().name, Role.objects.get_scenarist().name])
def test_add_imdb_id_to_movies_of_person(self): imdb_person = self.imdb_dennis_hopper() # TODO: fix if cast for easy rider with role actor, director will not be created cast1 = CastFactory(person=imdb_person.person, movie__title_en='Easy Rider', movie__year=1969, role=self.director) cast2 = CastFactory(person=imdb_person.person, movie__title_en='True Romance', movie__year=1994, role=self.actor) imdb_person.sync(roles=True) self.assert_dennis_hopper_career(imdb_person, cast1.movie, cast2.movie)
def test_cast_repr(self): cast = CastFactory(movie=self.easy_rider(), person=self.jack(), role=Role.objects.get_actor(), name_en='George Hanson', name_ru='Джордж Хэнсон') translation.activate('en') self.assertEqual(repr(cast), 'Easy Rider - Jack Nicholson (actor: George Hanson)') translation.activate('ru') self.assertEqual(repr(cast), 'Беспечный Ездок - Джек Николсон (актер: Джордж Хэнсон)')
def handle(self, *args, **options): self.flush_db() for _ in range(1000): CastFactory() self.stdout.write(self.style.SUCCESS('Successfully seeded {} movies, {} persons, {} cast'.format( Movie.objects.count(), Person.objects.count(), Cast.objects.count())))
def test_add_imdb_id_to_persons_of_movie(self): imdb_movie = ImdbMovieFactory(id=133093) cast1 = CastFactory(movie=imdb_movie.movie, person__first_name_en='Lilly', person__last_name_en='Wachowski', role=self.director) cast2 = CastFactory(movie=imdb_movie.movie, person__first_name_en='Keanu', person__last_name_en='Reeves', role=self.actor) cast3 = CastFactory(movie=imdb_movie.movie, person__first_name_en='Jeremy', person__last_name_en='Ball', role=self.actor) imdb_movie.sync(roles=True) self.assert_matrix_cast(imdb_movie, cast1.person, cast2.person, cast3.person)
def test_search_and_sync_right_person_by_movie(self, search_person): person1 = PersonFactory(first_name_en='Allison', last_name_en='Williams') person2 = PersonFactory(first_name_en='Allison', last_name_en='Williams') person3 = PersonFactory(first_name_en='Allison', last_name_en='Williams') ImdbMovieFactory(id=1672719, movie=CastFactory(person=person1).movie) ImdbMovieFactory(id=2702724, movie=CastFactory(person=person2).movie) ImdbMovieFactory(id=1985034, movie=CastFactory(person=person3).movie) sync_person(person1.id) sync_person(person2.id) sync_person(person3.id) self.assertEqual(person1.imdb.id, 930009) self.assertEqual(person2.imdb.id, 8050010) self.assertEqual(person3.imdb.id, 4613572) self.assertFalse(search_person.called)
def test_cast_sources(self): cast = CastFactory() self.assertEqual(cast.sources, '') cast.set_source('site1') self.assertEqual(cast.sources, 'site1') cast.set_source('site2') self.assertEqual(cast.sources, 'site1,site2')
def test_sync_movie_with_wrong_person(self): dan_brown_wrong = ImdbPersonFactory(id=1640149, person__country=None, person__first_name_en='Dan', person__last_name_en='Brown') imdb_movie = ImdbMovieFactory(id=382625, movie=CastFactory( person=dan_brown_wrong.person, role=self.producer).movie) sync_movie(imdb_movie.movie.id, roles='all') self.assertTrue( imdb_movie.movie.cast.get(role=self.producer, person__imdb__id=1467010))
def test_person_with_career(self): p = PersonFactory() for i in range(100): cast = CastFactory(person=p) CastFactory(role=cast.role) query = ''' query Person($id: ID!, $role: ID!) { person(id: $id) { id career(role: $role) { edges { node { name movie { titleEn } role { nameEn } } } } } } ''' with self.assertNumQueries(3): result = self.execute(query, dict(id=global_id(p), role=global_id(cast.role))) self.assertEqual(len(result['person']['career']['edges']), p.career.filter(role=cast.role).count())
def test_sync_object_with_duplicated_cast_objects( self, factory, factory_kwargs, imdb_factory, imdb_kwargs, sync_method, field_name, roles_field, imdb_id): instance1 = factory(**factory_kwargs) instance2 = factory(**factory_kwargs) imdb_object = imdb_factory(**imdb_kwargs) instance = getattr(imdb_object, field_name) roles = getattr(instance, roles_field) instance_type = factory._meta.model.__name__.lower() cast_kwargs = { field_name: instance, 'role': self.actor, instance_type: instance1 } CastFactory(**cast_kwargs) cast_kwargs[instance_type] = instance2 CastFactory(**cast_kwargs) sync_method(instance.id, roles='all') get_kwargs = { 'role': self.actor, instance_type: instance1, f'{instance_type}__imdb__id': imdb_id } self.assertTrue(roles.get(**get_kwargs))
def test_sync_person_movies_with_the_same_title(self): # person has 2 movies "The Conversation" 1974 and 1995 movie1 = MovieFactory(title_en='The Conversation', year=1974) imdb_person = ImdbPersonFactory(id=338) CastFactory(movie=movie1, person=imdb_person.person, role=self.producer) sync_person(imdb_person.person.id, roles='all') self.assertTrue( imdb_person.person.career.get(role=self.producer, movie=movie1, movie__imdb__id=71360)) self.assertTrue( imdb_person.person.career.get(role=self.producer, movie__imdb__id=8041860))
def test_object_page(self, object_type, factory, imdb_factory, kinopoisk_factory, queries): instance = factory() imdb_factory(**{object_type: instance}) kinopoisk_factory(**{object_type: instance}) for i in range(100): CastFactory(**{object_type: instance}) for i in range(10): ImageLinkFactory(object=instance) # TODO: prefetch thumbnails with one extra query with self.assertNumQueries(queries + 10): response = self.client.get( reverse(f'admin:core_{object_type}_change', args=(instance.id, ))) self.assertEqual(response.status_code, 200) self.assertContains(response, f'Imdb {object_type}s') self.assertContains(response, f'Kinopoisk {object_type}s') self.assertContains(response, 'Cast') self.assertContains(response, 'Image links')
def test_search_and_sync_object_with_roles(self, factory, sync_method, kwargs, en_title, ru_title, roles=()): instance = factory(**kwargs) # create instances linked to the main linked_instances = [] for role in roles: role[0].update({instance._meta.model_name: instance}) cast = CastFactory(**role[0], role=self.actor) field = list(role[0])[0].split('__')[0] linked_instances.append(getattr(cast, field)) # create underscore version of wikipedia en page WikipediaPage.objects.safe_create(role[1].replace(' ', '_'), 'en', getattr(cast, field)) sync_method(instance.id) self.assert_wikipedia(instance, en_title, ru_title) # check instances linked to the main for i, role in enumerate(roles): self.assert_wikipedia(linked_instances[i], role[1], role[2], synced=False)
def test_sync_object_update_cast_sources_dates(self, imdb_factory, imdb_kwargs, sync_method, field_name, roles_field, cast_kwargs): now = timezone.now() before = now - datetime.timedelta(days=9) imdb_object = imdb_factory(**imdb_kwargs) instance = getattr(imdb_object, field_name) roles = getattr(instance, roles_field) cast_kwargs.update(**{field_name: instance}) # created old cast with freeze_time(before): cast_old = CastFactory(role=self.actor, **cast_kwargs) self.assertEqual(cast_old.created_at, before) self.assertEqual(cast_old.updated_at, before) self.assertEqual(roles.count(), 1) with freeze_time(now): sync_method(instance.id, roles='all') self.assertGreater(roles.count(), 1) for cast in roles.all(): self.assertEqual(cast.sources, 'imdb') self.assertEqual(cast.updated_at, now) self.assertEqual(cast.created_at, before if cast == cast_old else now)