def test_get_top_movies_returns_movies_and_their_ranks(self): movie_1 = Movie(title='Glass') movie_2 = Movie(title='Forrest Gump') movie_3 = Movie(title='it') movie_1.save() movie_2.save() movie_3.save() Comment(body='first movie', movie=movie_1).save() Comment(body='first movie again', movie=movie_1).save() Comment(body='second movie', movie=movie_2).save() Comment(body='yet another second movie', movie=movie_2).save()\ expected_response = [{ 'title': 'Glass', 'total_comments': 2, 'rank': 1, }, { 'title': 'Forrest Gump', 'total_comments': 2, 'rank': 1, }, { 'title': 'it', 'total_comments': 0, 'rank': 2, }] response = self.client.get('/top/') self.assertEqual(response.json(), expected_response)
def mutate(self, info, body, movie_id): movie_row_id = from_global_id(movie_id)[-1] movie = get_object_or_404(Movie, id=movie_row_id) comment = Comment(body=body, movie=movie) comment.save() return CreateComment(id=comment.id, body=comment.body, movie=movie)
def populate_comments(movie_title, amount): counter = 0 movie = Movie.objects.filter(title=movie_title).first() while counter < amount: Comment(movie=movie, comment='test').save() counter += 1 return
def comment(request): """ /comments POST /comments @param `movie_id` required @param `comment` required save to db @response (return comment) GET /comments @param `movie_id` optional @response fetch all comments, `movie_id` filtered if present """ if request.method == "POST": movie_id = request.POST.get("movie_id", "") comment = request.POST.get("comment", "") #check if any POST argument is empty if (movie_id == "") or (comment == ""): return JsonResponse({"error": "empty argument"}, status=400) #check if movie exists in database entry = Movie.objects.filter(id=movie_id) if not(entry.exists()): return JsonResponse({"error": "movie not found in database"}, status=400) #create new Comment object cmnt = Comment(movie_id=movie_id, comment_body=comment) #commit to database cmnt.save() #serialize what you got serialized_cmnt = CommentSerializer(cmnt) #return it return JsonResponse(serialized_cmnt.data, status=201) if request.method == "GET": movie_id = request.GET.get("movie_id", "") #get all comments all_comments = Comment.objects.all() #if movie_id is not empty filter them if not(movie_id == ""): all_comments = all_comments.filter(movie_id=movie_id) #serialize what you got serialized_all = CommentSerializer(all_comments, many=True) #return it return JsonResponse(serialized_all.data, status=201, safe=False)
def test_get_with_movie_id_returns_only_specific_comments(self): movie_1 = Movie(title='Glass', released=datetime.date(2000, 1, 1), genre="Drama") movie_2 = Movie(title='it', released=datetime.date(2000, 1, 1), genre="Drama") movie_1.save() movie_2.save() Comment(body='specific comment', movie=movie_1).save() Comment(body='invisible', movie=movie_2).save() response = self.client.get(f'/comments/?movie={movie_1.pk}') expected_response = [{ 'body': 'specific comment', 'movie': 'Glass', }] self.assertEqual(response.json(), expected_response)
def test_get_returns_all_comments(self): movie = Movie(title='Glass', released=datetime.date(2000, 1, 1), genre="Drama") movie.save() Comment(body='asd', movie=movie).save() response = self.client.get('/comments/') expected_response = [{ 'body': 'asd', 'movie': 'Glass', }] self.assertEqual(response.json(), expected_response)
def add_comment(request, movie_id): form = CommentForm(request.POST) article = get_object_or_404(Movie, id=movie_id) if form.is_valid(): comment = Comment() comment.path = [] comment.article_id = article comment.author_id = auth.get_user(request) comment.content = form.cleaned_data['comment_area'] comment.save() try: comment.path.extend( Comment.objects.get( id=form.cleaned_data['parent_comment']).path) comment.path.append(comment.id) except ObjectDoesNotExist: comment.path.append(comment.id) comment.save() return HttpResponse("!")
def save_comment(content, movie_id): try: movie = Movie.objects.get(pk=movie_id) new_comment = Comment() new_comment.movie = movie new_comment.content = content new_comment.save() return new_comment except: return None
def test_comment_get_route(self): # Populate db jaws_movie = Movie.objects.filter(title='Jaws').first() comments = [ 'Sharkz are spooky :c', 'So many people eaten, like that!', 'This is stupid' ] for comment in comments: Comment(movie=jaws_movie, comment=comment).save() # Get comments from api, and from db response = self.client.get('/comments/') response_data = response.json() comments_amount_in_db = Comment.objects.count() comment_values_from_response = [ comm['comment'] for comm in response_data ] self.assertEqual(comments_amount_in_db, 3) self.assertTrue(len(response_data), 3) self.assertTrue(response.status_code, 200) for comment in comments: self.assertTrue(comment in comment_values_from_response)
def setUp(self): movie_obj = Movie(title='test') movie_obj.save() comment_obj = Comment(content='test content', movie=movie_obj) comment_obj.save()