def test_update_book(self): """ Test updating book """ book = BookFactory() url = reverse('books:books-detail', kwargs={'pk': book.pk}) response = self.client.patch(url, {'title': 'new_title'}) book.refresh_from_db() self.assertEqual(book.title, 'new_title') self.assertEqual(response.status_code, status.HTTP_200_OK)
def _load_fixtures(self): """ Create and save the necessary factories. """ self.stdout.write("Attempting to load mysite fixtures...") author1 = AuthorFactory.create() author2 = AuthorFactory.create() BookFactory.create(authors=[author1, author2]) self.stdout.write("Fixtures loaded successfully.")
def handle(self, *args, **options): num = options['num_books'] print num Book.objects.all().delete() BookImage.objects.all().delete() for x in range(num): BookFactory.create() for book in Book.objects.all(): BookImageFactory.create(book=book)
def test_review_list_returns_books_to_review(self): # Setup our data books_without_reviews = BookFactory.create_batch(2, authors=[self.author, ]) self.client.login(username="******", password="******") response = self.client.get(reverse('review-books')) books = list(response.context['books']) self.assertEqual(books, books_without_reviews)
def test_retrieve_book(self): """ Test retrieving a book """ book = BookFactory() url = reverse('books:books-detail', kwargs={'pk': book.pk}) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(response.data)
def test_review_list_returns_books_to_review(self): # set up data books_without_reviews = BookFactory.create_batch(4) self.client.login(username='******', password='******') response = self.client.get(reverse('review-books')) books_to_test = list(response.context['books']) self.assertEqual(books_to_test, books_without_reviews)
def test_list_books_returns_books_with_reviews(self): author = AuthorFactory() books_with_reviews = ReviewFactory.create_batch(2, authors=[author,]) books_without_reviews = BookFactory.create_batch(4, authors=[author,]) response = self.client.get(reverse(list_books)) books = list(response.context['books']) self.assertEquals(books, books_with_reviews) self.assertNotEquals(books, books_without_reviews)
def test_review_list_returns_books_to_review(self): # Setup our data books_without_reviews = BookFactory.create_batch(2, authors=[self.author,]) self.client.login(username="******", password="******") response = self.client.get(reverse('review-books')) books = list(response.context['books']) self.assertEqual(books, books_without_reviews)
def test_that_book_can_be_created(self): # given author = AuthorFactory() book = BookFactory(authors=[author], ) # when actual = Book.objects.get(title=book.title) # then assert_that(actual).is_equal_to(book)
def test_review_list_returns_books_to_review(self): # Setup the data books_without_reviews = BookFactory.create_batch(2, authors=[self.author,]) c = Client() loginStatus = c.login(username='******', password='******') response = c.get(reverse('review-books')) books = list(response.context['books']) self.assertEqual(books_without_reviews, books)
def setUp(self): """ Initial settings (create user, profile, token, genre, book) """ self.profile = ProfileFactory() self.token = Token.objects.first().key self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) self.book = BookFactory() self.test_comment_data = { 'profile': self.profile.pk, 'book': self.book.pk, 'text': 'Test text', }
def test_update_is_not_allowed(self): """ Make sure that put action is not allowed. """ book = BookFactory() new_title = 'new title' url = '{0}{1}'.format(BASE_URL, book.id) request = self.factory.put(url, {'title': new_title}, format='json') response = self.view(request, pk=book.id) # Assert that method is not allowed, no data has been returned and the object was not changed. self.assertEquals(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) self.assertEquals(response.data, {'detail': 'Method "PUT" not allowed.'}) book.refresh_from_db() self.assertNotEquals(book.title, new_title)
def test_list_books_retrurns_books_with_reviews(self): # Setup our data author = AuthorFactory() books_with_reviews = ReviewFactory.create_batch(2, authors=[author,]) books_without_reviews = BookFactory.create_batch(4, authors=[author,]) response = self.client.get(reverse(list_books)) books = list(response.context['books']) self.assertEqual(books_with_reviews, books) self.assertNotEqual(books_without_reviews, books)
def test_retrieve(self): """ Make sure that retrieve is working as expected. """ book = BookFactory.create() self.view = BookViewSet.as_view({'get': 'retrieve'}) url = "{0}{1}".format(BASE_URL, book.id) request = self.factory.get(url) response = self.view(request, pk=book.id) self.assertEqual(response.data['id'], book.id)
def test_list(self): """ Make sure that list method is working as expected. """ number_of_books = 5 author = UserFactory() # Use the same author, rather than creating a new instance for each one. _ = BookFactory.create_batch(number_of_books, author=author) request = self.factory.get(BASE_URL, format='json') self.view = BookViewSet.as_view({'get': 'list'}) response = self.view(request) self.assertEqual(len(response.data), number_of_books)
def test_partial_update(self): """ Make sure that partial update is working. """ book = BookFactory() new_title = 'new title' url = '{0}{1}'.format(BASE_URL, book.id) request = self.factory.patch(url, data={'title': new_title}, format='json') response = self.view(request, pk=book.id) # Assert that method that a new object has been created, status code is 201, object details are returned. self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(response.data['title'], new_title)
def test_delete(self): """ Make sure that delete method is working. """ # Create a book via book factory. book = BookFactory() url = '{0}{1}'.format(BASE_URL, book.id) request = self.factory.delete(url) response = self.view(request, pk=book.id) # Assert that method is not allowed and no data has been returned. and the object was not changed. self.assertEquals(response.status_code, status.HTTP_204_NO_CONTENT) self.assertIsNone(response.data) self.assertEquals(Book.objects.all().count(), 0)
def test_list_books_returns_books_with_reviews(self): # this view only returns books with a review # so we test another thing, we make a set of reviewed books and a set of # not reviewed books and test. author = AuthorFactory() books_with_reviews = ReviewFactory.create_batch(2, authors=[ author, ]) books_without_reviews = BookFactory.create_batch(4, authors=[ author, ]) response = self.client.get(reverse(list_books)) books = list(response.context['books']) self.assertEqual(books_with_reviews, books) self.assertNotEqual(books_without_reviews, books)
def test_serializer_save(self): other_book = BookFactory() serializer_data = { 'delivery_address_id': self.address.id, 'billing_address_id': self.address.id, 'user': self.user.id, 'books': [{ 'book_id': self.book.id, 'quantity': 1 }, { 'book_id': other_book.id, 'quantity': 1 }] } serializer = OrderSerializer(data=serializer_data) serializer.is_valid() serializer.save() self.assertEqual(Order.objects.count(), 2) self.assertEqual(OrderBooks.objects.count(), 3)
def test_create_action(self): """ Make sure that create is working as expected. """ book = BookFactory.build() author = UserFactory() book_json = { 'title': book.title, 'price': book.price, 'short_description': 'this is a short description', 'author': author.id, 'isbn': '1012167402' } request = self.factory.post(BASE_URL, data=book_json, format='json') response = self.view(request) # Assert that method that a new object has been created, status code is 201, object details are returned. self.assertEquals(response.status_code, status.HTTP_201_CREATED, response.data) self.assertEqual(response.data['title'], book_json['title']) self.assertEquals(Book.objects.all().count(), 1)
def test_get_books(self): """ Test retrieving all books """ BookFactory() response = self.client.get(reverse('books:books-list')) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(response.data)
def test_delete_book(self): """ Test deleting book """ book = BookFactory() url = reverse('books:books-detail', kwargs={'pk': book.pk}) response = self.client.delete(url) self.assertFalse(models.Book.objects.filter(pk=book.pk).exists())
def setUp(self): self.author = AuthorFactory self.book = BookFactory(title='MyNewBook',authors=[self.author,])
def setUp(self): self.book1 = BookFactory(title="Title1") self.book2 = BookFactory(title="Title2") self.book3 = BookFactory(title="Other")
def test_get_book(self): book = BookFactory() self.assertEqual(book.title, "Title 0")
def setUp(self): self.user = UserFactory() self.book = BookFactory() self.order = OrderWithBookFactory(user=self.user) self.address = AddressFactory(user=self.user)
def setUp(self): self.author = AuthorFactory() self.book = BookFactory(title="MyNewBook", authors=[ self.author, ])
def test_review_list_returns_books_to_review(self): books_without_revies = BookFactory.create_batch(10, authors=[self.author,]) self.client.login(username='******', password='******') response = self.client.get(reverse('review-books')) books = list(response.context['books']) self.assertEquals(books_without_revies, books)
def setUp(self): """ Function doc """ self.author = AuthorFactory self.book = BookFactory(title="MyNewBook", authors=[ self.author, ])
def setUp(self): self.order = OrderWithBookFactory() self.address = AddressFactory() self.book = BookFactory() self.user = UserFactory() self.serializer = OrderSerializer(instance=self.order)