X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/f3e9066b40546163f0df4678702ca4d7d31f9f84..afb3cc283074103e1137e79589d15390c825ecf1:/src/api/tests/tests.py diff --git a/src/api/tests/tests.py b/src/api/tests/tests.py index 32d394e70..5486b5f53 100644 --- a/src/api/tests/tests.py +++ b/src/api/tests/tests.py @@ -35,6 +35,10 @@ class ApiTest(TestCase): good_data = json.load(f) self.assertEqual(data, good_data, json.dumps(data, indent=4)) + def assert_slugs(self, url, slugs): + have_slugs = [x['slug'] for x in self.load_json(url)] + self.assertEqual(have_slugs, slugs, have_slugs) + class BookTests(ApiTest): @@ -101,19 +105,87 @@ class PictureTests(ApiTest): Picture.objects.get(slug=slug) -class URLTests(ApiTest): +class BooksTests(ApiTest): fixtures = ['test-books.yaml'] - def test_get(self): - # book lists - self.assert_json_response('/api/audiobooks/', 'audiobooks.json') - self.assert_json_response('/api/daisy/', 'daisy.json') + def test_books(self): + self.assert_json_response('/api/books/', 'books.json') + self.assert_json_response('/api/books/?new_api=true', 'books.json') + + self.assert_slugs('/api/audiobooks/', ['parent']) + self.assert_slugs('/api/daisy/', ['parent']) + self.assert_slugs('/api/newest/', ['parent']) + self.assert_slugs('/api/parent_books/', ['parent']) + self.assert_slugs('/api/recommended/', ['parent']) + + # Book paging. + self.assert_slugs('/api/books/after/grandchild/count/1/', ['parent']) + self.assert_slugs('/api/books/?new_api=true&after=$grandchild$3&count=1', ['parent']) + + # By tag. + self.assert_slugs('/api/authors/john-doe/books/', ['parent']) + self.assert_slugs('/api/genres/sonet/books/?authors=john-doe', ['parent']) + # It is probably a mistake that this doesn't filter: + self.assert_slugs('/api/books/?authors=john-doe', ['child', 'grandchild', 'parent']) + + # Parent books by tag. + # Notice this contains a grandchild, if a child doesn't have the tag. + # This probably isn't really intended behavior and should be redefined. + self.assert_slugs('/api/genres/sonet/parent_books/', ['grandchild', 'parent']) + + def test_ebooks(self): self.assert_json_response('/api/ebooks/', 'ebooks.json') + + def test_filter_books(self): self.assert_json_response('/api/filter-books/', 'filter-books.json') - self.assert_json_response('/api/newest/', 'newest.json') + self.assert_slugs( + '/api/filter-books/?lektura=false&preview=false', + ['child', 'grandchild', 'parent']) + self.assert_slugs( + '/api/filter-books/?lektura=true', + []) - self.assert_json_response('/api/blog/', 'blog.json') - self.assert_json_response('/api/preview/', 'preview.json') - self.assert_json_response('/api/recommended/', 'recommended.json') + Book.objects.filter(slug='child').update(preview=True) + self.assert_slugs('/api/filter-books/?preview=true', ['child']) + self.assert_slugs('/api/filter-books/?preview=false', ['grandchild', 'parent']) + + self.assert_slugs('/api/filter-books/?audiobook=true', ['parent']) + self.assert_slugs('/api/filter-books/?audiobook=false', ['child', 'grandchild']) + self.assert_slugs('/api/filter-books/?genres=wiersz', ['child']) + + self.assert_slugs('/api/filter-books/?search=parent', ['parent']) + + def test_collections(self): self.assert_json_response('/api/collections/', 'collections.json') + self.assert_json_response('/api/collections/a-collection/', 'collection.json') + + def test_book(self): + self.assert_json_response('/api/books/parent/', 'books-parent.json') + self.assert_json_response('/api/books/child/', 'books-child.json') + self.assert_json_response('/api/books/grandchild/', 'books-grandchild.json') + + def test_tags(self): + # List of tags by category. + self.assert_json_response('/api/genres/', 'tags.json') + + def test_fragments(self): + # This is not supported, though it probably should be. + #self.assert_json_response('/api/books/child/fragments/', 'fragments.json') + + self.assert_json_response('/api/genres/wiersz/fragments/', 'fragments.json') + self.assert_json_response('/api/genres/wiersz/fragments/', 'fragments.json') + + self.assert_json_response('/api/books/child/fragments/an-anchor/', 'fragment.json') + + +class BlogTests(ApiTest): + def test_get(self): + self.assertEqual(self.load_json('/api/blog/'), []) + + +class PreviewTests(ApiTest): + def unauth(self): + self.assert_json_response('/api/preview/', 'preview.json') + +