Beispiel #1
0
 def setUp(self):
     AuthorFactory.create_batch(3)
     self.data = {
         'name': 'book test',
         'edition': 1,
         'publication_year': 2002,
         'authors': Author.objects.values_list('pk', flat=True),
     }
Beispiel #2
0
    def test_retrieve_authors_pagination(self):
        """
        Test items per page and count total
        """
        AuthorFactory.create_batch(40)

        url = reverse('author-list')
        get_response = self.client.get(url, format='json')
        count_total = get_response.data.get('count')

        self.assertEqual(count_total, 40)
Beispiel #3
0
    def test_retrieve_authors_filter_name(self):
        AuthorFactory.create_batch(10)
        AuthorFactory(name='New Author 1')
        AuthorFactory(name='New Author 2')

        url = reverse('author-list')
        get_response = self.client.get(
            url,
            data={'name': 'New Author'},
            format='json',
        )
        data = get_response.data.get('results')
        self.assertEqual(len(data), 2)
Beispiel #4
0
 def test_delete(self):
     """
     Test delete Authors
     """
     author = AuthorFactory()
     url = '{}{}/'.format(reverse('author-list'), author.pk)
     self.client.delete(url, format='json')
     self.assertFalse(Author.objects.exists())
Beispiel #5
0
 def test_updating_author_name(self):
     """
     Test update of Authors name
     """
     author = AuthorFactory()
     url = '{}{}/'.format(reverse('author-list'), author.pk)
     data = {
         'name': 'New Name',
     }
     post_response = self.client.put(url, data, format='json')
     data = post_response.data
     self.assertEqual(data.get('name'), 'New Name')
Beispiel #6
0
    def test_retrieve_books_pagination(self):
        """
        Test items per page and count total
        """
        authors = AuthorFactory.create_batch(3)
        BookFactory.create_batch(50, authors=authors)

        url = reverse('book-list')
        get_response = self.client.get(url, format='json')
        count_total = get_response.data.get('count')

        self.assertEqual(count_total, 50)
Beispiel #7
0
    def test_retrieve_books_filter_name(self):
        """
        Test Filter books by name ya
        """
        authors = AuthorFactory.create_batch(3)
        BookFactory.create_batch(10, authors=authors)
        first_author = authors[0]

        BookFactory(name='tai ya', authors=[first_author])
        BookFactory(name='yaya', authors=[first_author])
        data = self._get('name', 'ya')

        self.assertEqual(len(data), 2)
Beispiel #8
0
    def test_retrieve_books_filter_edition(self):
        """
        Test Filter books by second edition
        """
        authors = AuthorFactory.create_batch(3)
        first_author = authors[0]
        edition = 2

        BookFactory.create_batch(10, authors=authors)
        BookFactory.create_batch(2, authors=[first_author], edition=edition)

        data = self._get('edition', edition)
        first, second = data

        self.assertEqual(len(data), 2)
        self.assertEqual(first.get('edition'), edition)
        self.assertEqual(second.get('edition'), edition)
Beispiel #9
0
    def test_retrieve_books_filter_author(self):
        """
        Test Filter books by author
        """
        authors = AuthorFactory.create_batch(3)
        first_author, second_author, third_author = authors
        author = third_author.pk

        BookFactory.create_batch(5, authors=[first_author])
        BookFactory.create_batch(10, authors=[second_author])
        BookFactory.create_batch(2, authors=[third_author])

        data = self._get('author', author)
        first, second = data

        self.assertEqual(len(data), 2)
        self.assertEqual(first.get('authors'), [author])
        self.assertEqual(second.get('authors'), [author])
Beispiel #10
0
    def test_retrieve_books_filter_co_author(self):
        """
        Test Filter books by auhtor and co-author
        """
        authors = AuthorFactory.create_batch(3)
        first_author, second_author, third_author = authors
        author = third_author.pk

        BookFactory.create_batch(1, authors=[first_author, author])
        BookFactory.create_batch(10, authors=[second_author])
        BookFactory.create_batch(2, authors=[author])

        data = self._get('author', author)
        first, second, third = data

        self.assertEqual(len(data), 3)
        self.assertIn(
            author,
            first.get('authors'),
        )
        self.assertIn(author, second.get('authors'))
        self.assertIn(author, third.get('authors'))
Beispiel #11
0
    def test_retrieve_books_filter_publication_year(self):
        """
        Test Filter books published in 2019
        """
        authors = AuthorFactory.create_batch(3)
        first_author = authors[0]
        publication_year = 2019

        BookFactory.create_batch(10, authors=authors, publication_year=2000)
        BookFactory.create_batch(
            3,
            authors=[first_author],
            publication_year=publication_year,
        )

        data = self._get('publication_year', publication_year)
        first, second, third = data

        self.assertEqual(len(data), 3)
        self.assertEqual(first.get('publication_year'), publication_year)
        self.assertEqual(second.get('publication_year'), publication_year)
        self.assertEqual(third.get('publication_year'), publication_year)
Beispiel #12
0
 def setUp(self):
     authors = AuthorFactory.create_batch(3)
     self.book = BookFactory(authors=authors)