+
+
+
+class BookTests(TestCase):
+
+ def setUp(self):
+ self.tag = Tag.objects.create(category='author', slug='joe')
+ self.book = Book.objects.create(title='A Book', slug='a-book')
+ self.book_tagged = Book.objects.create(title='Tagged Book', slug='tagged-book')
+ self.book_tagged.tags = [self.tag]
+ self.book_tagged.save()
+
+ def test_book_list(self):
+ books = json.loads(self.client.get('/api/books/').content)
+ self.assertEqual(len(books), 2,
+ 'Wrong book list.')
+
+ def test_tagged_books(self):
+ books = json.loads(self.client.get('/api/authors/joe/books/').content)
+
+ self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
+ 'Wrong tagged book list.')
+
+ def test_detail(self):
+ book = json.loads(self.client.get('/api/books/a-book/').content)
+ self.assertEqual(book['title'], self.book.title,
+ 'Wrong book details.')
+
+
+class TagTests(TestCase):
+
+ def setUp(self):
+ self.tag = Tag.objects.create(category='author', slug='joe', name='Joe')
+ self.book = Book.objects.create(title='A Book', slug='a-book')
+ self.book.tags = [self.tag]
+ self.book.save()
+
+ def test_tag_list(self):
+ tags = json.loads(self.client.get('/api/authors/').content)
+ self.assertEqual(len(tags), 1,
+ 'Wrong tag list.')
+
+ def test_tag_detail(self):
+ tag = json.loads(self.client.get('/api/authors/joe/').content)
+ self.assertEqual(tag['name'], self.tag.name,
+ 'Wrong tag details.')