def test_get_search(self): url = reverse('book-list') response = self.client.get(url, data={'search': 'Author 1'}) serializer_data = BooksSerializer([self.book_1, self.book_3], many=True).data self.assertEqual(status.HTTP_200_OK, response.status_code) self.assertEqual(serializer_data, response.data)
def test_get(self): url = reverse('book-list') # test `select_related` and `prefetch_related` with CaptureQueriesContext(connection) as queries: response = self.client.get(url) # without `prefetch_related` or 'select_related` 2 != 4 # without `prefetch_related` and 'select_related` 2 != 6 # with `prefetch_related` and 'select_related`: self.assertEqual(2, len(queries)) books = Book.objects.all().annotate( annotated_likes=Count( Case(When(userbookrelation__like=True, then=1))), # rating=Avg('userbookrelation__rate'), discount_price=F('price') - F('discount'), ).order_by('id') serializer_data = BooksSerializer(books, many=True).data self.assertEqual(status.HTTP_200_OK, response.status_code) self.assertEqual(serializer_data, response.data) book1, = [ book for book in serializer_data if book.get('name') == 'test1' ] self.assertEqual(book1['rating'], '5.00') self.assertEqual(book1['annotated_likes'], 1)
def test_get_filter(self): url = reverse('book-list') response = self.client.get(url, data={'price': 55}) serializer_data = BooksSerializer([self.book_2, self.book_3], many=True).data self.assertEqual(status.HTTP_200_OK, response.status_code) self.assertEqual(serializer_data, response.data)
def test_ok(self): user1 = User.objects.create(username='******') user2 = User.objects.create(username='******') user3 = User.objects.create(username='******') book1 = Book.objects.create(name='test book 1', price=25, author_name='author 1') book2 = Book.objects.create(name='test book 2', price=55, author_name='author 2') UserBookRelation.objects.create(user=user1, book=book1, like=True, rate=5) UserBookRelation.objects.create(user=user2, book=book1, like=True, rate=5) UserBookRelation.objects.create(user=user3, book=book1, like=True, rate=4) UserBookRelation.objects.create(user=user1, book=book2, like=True, rate=3) UserBookRelation.objects.create(user=user2, book=book2, like=True, rate=4) UserBookRelation.objects.create(user=user3, book=book2, like=False) books = Book.objects.all().annotate( annotated_likes=Count( Case(When(userbookrelation__like=True, then=1))), rating=Avg('userbookrelation__rate')).order_by('id') data = BooksSerializer(books, many=True).data expected_data = [{ 'id': book1.id, 'name': 'test book 1', 'price': '25.00', 'author_name': 'author 1', 'likes_count': 3, 'annotated_likes': 3, 'rating': '4.67' }, { 'id': book2.id, 'name': 'test book 2', 'price': '55.00', 'author_name': 'author 2', 'likes_count': 2, 'annotated_likes': 2, 'rating': '3.50' }] self.assertEqual(expected_data, data)
def test_get(self): book_1 = Book.objects.create(name='Test book 1', price=25) book_2 = Book.objects.create(name='Test book 2', price=55) url = reverse('book-list') response = self.client.get(url) serializer_data = BooksSerializer([book_1, book_2], many=True).data self.assertEqual(status.HTTP_200_OK, response.status_code) self.assertEqual(serializer_data, response.data)
def test_get_search(self): url = reverse('book-list') books = Book.objects.filter(id__in=[self.book_1.id, self.book_3.id]).annotate( annotated_likes=Count(Case(When(userbookrelation__like=True, then=1))) ).order_by('id') response = self.client.get(url, data={'search': 'Author 1'}) serializer_data = BooksSerializer([books], many=True).data self.assertEqual(status.HTTP_200_OK, response.status_code) self.assertEqual(serializer_data, response.data)
def test_ok(self): user1=User.objects.create(username='******',first_name='Ivan',last_name='Petrov') user2 = User.objects.create(username='******',first_name='Ivan',last_name='Sidorov') user3 = User.objects.create(username='******',first_name='1',last_name='2') book_1=Book.objects.create(name='Test book 1',price=25,author_name='Author 1',owner=user1) book_2 = Book.objects.create(name='Test book 2', price=55,author_name='Author 2') UserBookRelation.objects.create(user=user1,book=book_1,like=True, rate=5) UserBookRelation.objects.create(user=user2, book=book_1, like=True, rate=5) UserBookRelation.objects.create(user=user3, book=book_1, like=True, rate=4) UserBookRelation.objects.create(user=user1, book=book_2, like=True, rate=3) UserBookRelation.objects.create(user=user2, book=book_2, like=True, rate=4) UserBookRelation.objects.create(user=user3, book=book_2, like=False) books=Book.objects.all().annotate(annotated_likes=Count(Case(When(userbookrelation__like=True,then=1))) ).order_by('id') data=BooksSerializer(books,many=True).data expected_data=[ {'id':book_1.id, 'name':'Test book 1', 'price':'25.00', 'author_name':'Author 1', # 'likes_count':3, 'annotated_likes':3, 'rating':'4.67', 'owner_name':'user1', 'readers':[ {'first_name':'Ivan','last_name':'Petrov'}, {'first_name': 'Ivan', 'last_name': 'Sidorov'}, {'first_name': '1', 'last_name': '2'}, ] }, {'id':book_2.id, 'name':'Test book 2', 'price':'55.00', 'author_name':'Author 2', # 'likes_count':2, 'annotated_likes':2, 'rating':'3.50', 'owner_name':'', 'readers': [ {'first_name': 'Ivan', 'last_name': 'Petrov'}, {'first_name': 'Ivan', 'last_name': 'Sidorov'}, {'first_name': '1', 'last_name': '2'}, ] }, ] self.assertEqual(expected_data,data)
def test_get_ordering(self): url = reverse('book-list') books = Book.objects.all().annotate( annotated_likes=Count(Case(When(userbookrelation__like=True, then=1))), rating=Avg('userbookrelation__rate') ).order_by('author_name') response = self.client.get(url, data={'ordering': 'author_name'}) serializer_data = BooksSerializer(books, many=True).data self.assertEqual(status.HTTP_200_OK, response.status_code) self.assertEqual(serializer_data, response.data)
def test_get(self): url = reverse('book-list') response = self.client.get(url) books = Book.objects.all().annotate(annotated_likes=Count(Case(When(userbookrelation__like=True, then=1)))).order_by('id') serializer_data = BooksSerializer(books, many=True).data self.assertEqual(status.HTTP_200_OK, response.status_code) self.assertEqual(serializer_data, response.data) self.assertEqual(serializer_data[0]['rating'], '5.00') # self.assertEqual(serializer_data[0]['likes_count'], 1) self.assertEqual(serializer_data[0]['annotated_likes'], 1)
def test_filter_by_discount(self): url = reverse('book-list') response = self.client.get(url, data={'discount': '5.00'}) books = Book.objects.filter(id__in=[self.book2.id]).annotate( annotated_likes=Count( Case(When(userbookrelation__like=True, then=1))), # rating=Avg('userbookrelation__rate'), discount_price=F('price') - F('discount')) serializer_data = BooksSerializer(books, many=True).data self.assertEqual(status.HTTP_200_OK, response.status_code) self.assertEqual(serializer_data, response.data)
def test_get_filter(self): url = reverse('book-list') books = Book.objects.filter(id__in=[self.book2.id, self.book3.id]).annotate( annotated_likes=Count(Case(When(userbookrelation__like=True, then=1))), rating=Avg('userbookrelation__rate') ).order_by('id') response = self.client.get(url, data={'price': 23.53}) serializer_data = BooksSerializer(books, many=True).data self.assertEqual(status.HTTP_200_OK, response.status_code) self.assertEqual(serializer_data, response.data)
def test_get(self): url = reverse('book-list') with CaptureQueriesContext(connection) as queries: response = self.client.get(url) self.assertEqual(2, len(queries)) books = Book.objects.all().annotate(annotated_likes=Count( Case(When(userbookrelation__like=True, then=1)))).order_by('id') serializer_data = BooksSerializer(books, many=True).data self.assertEqual(status.HTTP_200_OK, response.status_code) self.assertEqual(serializer_data, response.data) self.assertEqual(serializer_data[0]['raiting'], '5.00') self.assertEqual(serializer_data[0]['annotated_likes'], 1)
def test_ok(self): book_1 = Book.objects.create(name='Test Book 1', price=25) book_2 = Book.objects.create(name='Test Book 2', price=35) data = BooksSerializer([book_1, book_2], many=True).data expected_data = [{ 'id': book_1.id, 'name': 'Test Book 1', 'price': '25.00' }, { 'id': book_2.id, 'name': 'Test Book 2', 'price': '35.00' }] self.assertEqual(expected_data, data)
def test_get_ordering(self): url = reverse('book-list') response = self.client.get(url, data={'ordering': '-price'}) books = Book.objects.filter( id__in=[self.book2.id, self.book1.id, self.book3.id]).annotate( annotated_likes=Count( Case(When(userbookrelation__like=True, then=1))), # rating=Avg('userbookrelation__rate'), discount_price=F('price') - F('discount')) serializer_data = BooksSerializer(books, many=True).data with_reverse_price = [ d for d in sorted( serializer_data, key=itemgetter('price'), reverse=True) ] self.assertEqual(status.HTTP_200_OK, response.status_code) self.assertEqual(with_reverse_price, response.data)
def test_ok(self): user1 = User.objects.create(username='******', first_name='Foo', last_name='Bar') user2 = User.objects.create(username='******', first_name='1', last_name='2') user3 = User.objects.create(username='******') book1 = Book.objects.create(name='test1', price=25, author='author 1', discount=3) book2 = Book.objects.create(name='test2', price=55, author='author 2', owner=user1) UserBookRelation.objects.create(user=user1, book=book1, like=True, rate=5) UserBookRelation.objects.create(user=user2, book=book1, like=True, rate=5) UserBookRelation.objects.create(user=user3, book=book1, like=True, rate=4) UserBookRelation.objects.create(user=user1, book=book2, like=True, rate=4) user_book2 = UserBookRelation.objects.create(user=user2, book=book2, like=True) user_book2.rate = 3 user_book2.save() UserBookRelation.objects.create(user=user3, book=book2, like=False) books = Book.objects.all().annotate( annotated_likes=Count( Case(When(userbookrelation__like=True, then=1))), # rating=Avg('userbookrelation__rate'), discount_price=F('price') - F('discount')).order_by('id') data = BooksSerializer(books, many=True).data expected_data = [{ 'id': book1.id, 'name': 'test1', 'price': '25.00', 'author': 'author 1', 'annotated_likes': 3, 'rating': '4.67', 'discount': '3.00', 'discount_price': 22.0, 'owner_name': '', 'readers': [{ 'first_name': 'Foo', 'last_name': 'Bar' }, { 'first_name': '1', 'last_name': '2' }, { 'first_name': '', 'last_name': '' }] }, { 'id': book2.id, 'name': 'test2', 'price': '55.00', 'author': 'author 2', 'annotated_likes': 2, 'rating': '3.50', 'discount': '0.00', 'discount_price': 55.0, 'owner_name': user1.__str__(), 'readers': [{ 'first_name': 'Foo', 'last_name': 'Bar' }, { 'first_name': '1', 'last_name': '2' }, { 'first_name': '', 'last_name': '' }] }] self.assertEqual(expected_data, data)
def test_ok(self): user1 = User.objects.create(username='******', first_name='Erich', last_name='Remark') user2 = User.objects.create(username='******', first_name='Emil', last_name='Zolia') user3 = User.objects.create(username='******', first_name='Franc', last_name='Kafka') book_1 = Book.objects.create(name='Test book 1', price=25, author_name='Author 1') book_2 = Book.objects.create(name='Test book 2', price=50, author_name='Author 2') UserBookRelation.objects.create(user=user1, book=book_1, like=True, rate=5) UserBookRelation.objects.create(user=user2, book=book_1, like=True, rate=5) UserBookRelation.objects.create(user=user3, book=book_1, like=True, rate=4) UserBookRelation.objects.create(user=user1, book=book_2, like=True, rate=3) UserBookRelation.objects.create(user=user2, book=book_2, like=True, rate=4) UserBookRelation.objects.create(user=user3, book=book_2, like=False) books = Book.objects.all().annotate( annotated_likes=Count( Case(When(userbookrelation__like=True, then=1))), rating=Avg('userbookrelation__rate')).order_by('id') data = BooksSerializer(books, many=True).data expected_data = [ { 'id': book_1.id, 'name': 'Test book 1', 'price': '25.00', 'author_name': 'Author 1', #'likes_count': 3, 'annotated_likes': 3, 'rating': '4.67', 'readers': [{ 'first_name': 'Erich', 'last_name': 'Remark' }, { 'first_name': 'Emil', 'last_name': 'Zolia' }, { 'first_name': 'Franc', 'last_name': 'Kafka' }] }, { 'id': book_2.id, 'name': 'Test book 2', 'price': '50.00', 'author_name': 'Author 2', #'likes_count': 2, 'annotated_likes': 2, 'rating': '3.50', 'readers': [{ 'first_name': 'Erich', 'last_name': 'Remark' }, { 'first_name': 'Emil', 'last_name': 'Zolia' }, { 'first_name': 'Franc', 'last_name': 'Kafka' }] } ] self.assertEqual(expected_data, data)