Example #1
0
def test_list_books_can_by_ordered_by_author_full_name():
    author1 = AuthorFactory(first_name='Robert', last_name='Pirsig')
    author2 = AuthorFactory(first_name='Robert', last_name='Rush')
    author3 = AuthorFactory(first_name='Julio', last_name='Cortazar')

    book1 = BookFactory(title='Harry Potter II', author=author2)
    book2 = BookFactory(title='A Book', author=author1)
    book3 = BookFactory(title='Zen and the Art of Motorcycle Maintenance',
                        author=author3)

    client = APIClient()
    response = client.get('/api/book/', {'ordering': 'author__full_name'},
                          format='json')
    assert response.status_code == status.HTTP_200_OK

    assert response.data['count'] == 3
    assert len(response.data['results']) == 3
    assert [result['id'] for result in response.data['results']
            ] == [book3.id, book2.id, book1.id]

    # Can also order desc
    client = APIClient()
    response = client.get('/api/book/', {'ordering': '-author__full_name'},
                          format='json')
    assert response.status_code == status.HTTP_200_OK
    assert [result['id'] for result in response.data['results']
            ] == [book1.id, book2.id, book3.id]
Example #2
0
def test_related_books_excludes_requested_book():
    a_book = BookFactory(title='Harry Potter III')
    another_book = BookFactory(title='Harry Potter I')

    related_books = get_related_books(a_book)

    assert related_books.count() == 1
    assert related_books.first() == another_book
Example #3
0
    def test_books_list_logged(self):
        user1 = UserFactory.create()
        user2 = UserFactory.create()

        book1 = BookFactory.create(owner=user1)
        book2 = BookFactory.create(owner=user1)
        book3 = BookFactory.create(owner=user2)

        with self.login(user1):
            response = self.client.get('/books/my/')

            self.assertEqual(response.status_code, 200)
            self.assertContains(response, book1.title)
            self.assertContains(response, book2.title)
            self.assertNotContains(response, book3.title)
Example #4
0
 def setUp(self):
     self.num_books = 3
     self.books = []
     for i in range(self.num_books):
         self.books.append(
             BookFactory.create(name=f"test book{i}",
                                isbn=f"111-111111111{i}"))
Example #5
0
def test_select_books(customer_client):
    book = BookFactory()
    response = customer_client.get(f"/v1/books/",
                                   content_type="application/json")

    # THEN
    assert response.status_code == status.HTTP_200_OK
    assert book.id_book == json.loads(response.content)[0].get('id_book')
Example #6
0
def test_list_books_can_by_ordered_by_title():
    book1 = BookFactory(title='Harry Potter II')
    book2 = BookFactory(title='A Book')
    book3 = BookFactory(title='Zen and the Art of Motorcycle Maintenance')

    client = APIClient()
    response = client.get('/api/book/', {'ordering': 'title'}, format='json')
    assert response.status_code == status.HTTP_200_OK

    assert response.data['count'] == 3
    assert len(response.data['results']) == 3
    assert [result['id'] for result in response.data['results']
            ] == [book2.id, book1.id, book3.id]
    for result in response.data['results']:
        assert set(result.keys()) == {
            'id', 'title', 'author', 'publication_date', 'tags'
        }
        assert set(result['author'].keys()) == {'full_name', 'date_of_birth'}
Example #7
0
def test_related_books_first_returns_books_ordered_by_matching_tag_count():
    book1 = BookFactory(title='Harry Potter I')
    book2 = BookFactory(title='Harry Potter II')
    book3 = BookFactory(title='Harry Potter III')
    book4 = BookFactory(title='Harry Potter IV')

    tag1 = Tag.objects.create(name='tag1')
    tag2 = Tag.objects.create(name='tag2')

    book1.tags.add(tag1, tag2)
    # book2 has non of these tags
    book3.tags.add(tag1, tag2)
    book4.tags.add(tag2)

    related_books = get_related_books(book1)

    assert related_books.count() == 3
    assert list(related_books) == [book3, book4, book2]
Example #8
0
def test_reserve_book_before_3_days(customer_client):
    date_booking = datetime.today() - timedelta(days=2)
    book = BookFactory(date_booking=date_booking)
    response = customer_client.get(f"/v1/books/{book.id_book}/reserve/",
                                   content_type="application/json")

    # THEN
    assert response.status_code == status.HTTP_400_BAD_REQUEST
    assert "This book it's already reserved less than 3 days" == json.loads(
        response.content)[0]
Example #9
0
def test_related_books_when_same_matching_tag_count_returns_same_author_books_first(
):
    author1 = AuthorFactory()
    author2 = AuthorFactory()

    book1 = BookFactory(title='Harry Potter I', author=author1)
    book2 = BookFactory(title='Harry Potter II', author=author2)
    book3 = BookFactory(title='Harry Potter III', author=author1)

    tag1 = Tag.objects.create(name='tag1')

    book1.tags.add(tag1)
    book2.tags.add(tag1)
    book3.tags.add(tag1)

    related_books = get_related_books(book1)

    assert related_books.count() == 2
    assert list(related_books) == [book3, book2]
Example #10
0
def test_find_by_client_14_days_delay(customer_client):
    book = BookFactory()
    client = ClientFactory()
    date_borrow = datetime.today() - timedelta(days=14)
    borrow = BorrowFactory(client=client, book=book, date_borrow=date_borrow)
    response = customer_client.get(f"/v1/client/{client.id_client}/books/",
                                   content_type="application/json")

    # THEN
    content = json.loads(response.content)[0]
    assert response.status_code == status.HTTP_200_OK
    assert content.get('days_of_delay') == 14
    assert content.get('fine_rate') == 5.33
Example #11
0
 def setUp(self):
     self.book1 = BookFactory.create(name="test book1",
                                     isbn="111-1111111111")
Example #12
0
 def setUp(self):
     self.book1 = BookFactory.create()
Example #13
0
 def setUp(self):
     self.client = Client()
     self.book = BookFactory.create()
     self.user = UserFactory.create()
Example #14
0
 def test_request_by_owner(self):
     book = BookFactory.create()
     with self.assertRaises(BookRequestError):
         book.request(book.owner)
Example #15
0
 def setUp(self):
     self.client = Client()
     self.book1 = BookFactory.create()
     self.book2 = BookFactory.create()
 def setUp(self):
     self.authors_factory = AuthorFactory()
     self.book = BookFactory(authors=[self.authors_factory])
Example #17
0
    def test_delete_book(self):
        book = BookFactory.create()
        book_services.delete_book(book.id)

        with self.assertRaises(Book.DoesNotExist):
            Book.objects.get(id=book.id)
Example #18
0
 def test_requests(self):
     user = UserFactory.create()
     book = BookFactory.create()
     request = BookRequest(book=book, requester=user)
     request.save()
     self.assertEqual(list(book.requests()), [request])