def test_deleting_existing_director_should_remove_movie(self): director = Director(name='steven', surname='spilberg') director.save() movie = Movie(title='IT Crowd', director=Director.objects.get(pk=1)) movie.save() director.delete() self.assertEqual(0, Director.objects.count()) self.assertEqual(0, Movie.objects.count())
def post(self, request): title = request.POST.get("title") director_id = int(request.POST.get("director")) director = Person.objects.get(id=director_id) screenplay_id = int(request.POST.get("screenplay")) screenplay = Person.objects.get(id=screenplay_id) starring_id = int(request.POST.get("starring")) starring = Person.objects.get(id=starring_id) role = request.POST.get("role") year = int(request.POST.get("year")) rating = float(request.POST.get("rating")) genre = Genre.objects.get(id=int(request.POST.get("genre"))) new_movie = Movie() new_movie.title = title new_movie.director_id = director.id new_movie.screenplay_id = screenplay.id new_movie.year = year new_movie.rating = rating new_movie.save() new_movie.genre.add(genre) MoviePersonRole.objects.create(movie=new_movie, person=starring, role=role) return redirect("/movies")
def insert_movies(): for i in range(50): titles = [movie.title for movie in Movie.objects.all()] while True: title = "The {} {}".format(random.choice(TITLES_DATA[0]), random.choice(TITLES_DATA[1])) if title not in titles: break persons = Person.objects.all() genres = Genre.objects.all() director = Person.objects.get(id=random.choice(persons).id) screenplay = Person.objects.get(id=random.choice(persons).id) starring = Person.objects.get(id=random.choice(persons).id) role = random.choice(ROLES_DATA) year = random.randint(1900, 2020) rating = random.randint(15, 100) / 10 genre = Genre.objects.get(id=random.choice(genres).id) new_movie = Movie() new_movie.title = title new_movie.director_id = director.id new_movie.screenplay_id = screenplay.id new_movie.year = year new_movie.rating = rating new_movie.save() new_movie.genre.add(genre) MoviePersonRole.objects.create(movie=new_movie, person=starring, role=role)
def test_directs_related_property_from_movie(self): Director(name='steven', surname='spilberg').save() Director(name='robert', surname='zucker').save() Actor(name='Leonardo', surname='diCaprio').save() Movie(title='Logan', director=Director.objects.get(pk=1)).save() Movie(title='Top Gun', director=Director.objects.get(pk=2)).save() url = reverse('director-detail', kwargs={'pk': 1}) response = self.client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['id'], 1) self.assertEqual(response.data['name'], 'steven') self.assertEqual(response.data['surname'], 'spilberg') self.assertEqual(len(response.data['directs']), 1)
def test_delete_director_deletes_movies(self): g = Director(name='steven', surname='spilberg') g.save() Movie(title='Logan', director=Director.objects.get(pk=1)).save() url = reverse('director-detail', kwargs={'pk': g.id}) response = self.client.delete(url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(Director.objects.count(), 0) self.assertEqual(Movie.objects.count(), 0)
def test_deleting_actor_shouldnt_remove_movie(self): director = Director(name='steven', surname='spilberg') director.save() movie = Movie(title='IT Crowd', director=Director.objects.get(pk=1)) movie.save() actor = Actor(name='jane', surname='doe') actor.save() movie.actor.add(actor) movie.save() actor.delete() self.assertEqual(0, Actor.objects.count()) self.assertEqual(1, Movie.objects.count())
def test_delete_actor_not_deletes_movies(self): Director(name='steven', surname='spilberg').save() m = Movie(title='Logan', director=Director.objects.get(pk=1)) m.save() a = Actor(name='Bill', surname='Murray') a.save() m.actor.add(a) m.save() url = reverse('actor-detail', kwargs={'pk': a.id}) response = self.client.delete(url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(Actor.objects.count(), 0) self.assertEqual(Movie.objects.count(), 1)
def test_directs_related_property_from_movie(self): Director(name='steven', surname='spilberg').save() a1 = Actor(name='Leonardo', surname='diCaprio') a1.save() a2 = Actor(name='Bill', surname='Murray') a2.save() m1 = Movie(title='Logan', director=Director.objects.get(pk=1)) m1.save() m1.actor.add(a1) m1.save() m2 = Movie(title='Top Gun', director=Director.objects.get(pk=1)) m2.save() m1.actor.add(a2) m1.save() url = reverse('actor-detail', kwargs={'pk': 2}) response = self.client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['id'], 2) self.assertEqual(response.data['name'], 'Bill') self.assertEqual(response.data['surname'], 'Murray') self.assertEqual(len(response.data['plays']), 1)