Using cache middleware instead of various caching micro-strategies,
[wolnelektury.git] / apps / api / tests.py
index 5a981a2..94abe53 100644 (file)
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
+# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+#
+from os import path
 
 
-from datetime import datetime
-
+from django.core.files.uploadedfile import SimpleUploadedFile
 from django.test import TestCase
 from django.test import TestCase
-from django.utils import simplejson as json
-from django.conf import settings
+from django.test.utils import override_settings
+import json
 
 
-from api.helpers import timestamp
 from catalogue.models import Book, Tag
 from catalogue.models import Book, Tag
-from picture.tests.utils import RequestFactory
 from picture.forms import PictureImportForm
 from picture.forms import PictureImportForm
-from picture.models import Picture, picture_storage
+from picture.models import Picture
 import picture.tests
 import picture.tests
-from django.core.files.uploadedfile import SimpleUploadedFile
-
-from os import path
 
 
 
 
+@override_settings(
+    NO_SEARCH_INDEX=True,
+    CACHES={'default': {
+        'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}},
+    SSIFY_CACHE_ALIASES=['default'],
+    SSIFY_RENDER=True,
+)
 class ApiTest(TestCase):
 class ApiTest(TestCase):
-
-    def setUp(self):
-        self.old_api_wait = settings.API_WAIT
-        settings.API_WAIT = -1
-
-    def tearDown(self):
-        settings.API_WAIT = self.old_api_wait
-
-
-class ChangesTest(ApiTest):
-
-    def test_basic(self):
-        book = Book(title='A Book')
-        book.save()
-        tag = Tag.objects.create(category='author', name='Author')
-        book.tags = [tag]
-        book.save()
-
-        changes = json.loads(self.client.get('/api/changes/0.json?book_fields=title&tag_fields=name').content)
-        self.assertEqual(changes['updated']['books'], 
-                         [{'id': book.id, 'title': book.title}],
-                         'Invalid book format in changes')
-        self.assertEqual(changes['updated']['tags'], 
-                         [{'id': tag.id, 'name': tag.name}],
-                         'Invalid tag format in changes')
+    def load_json(self, url):
+        content = self.client.get(url).content
+        try:
+            data = json.loads(content)
+        except ValueError:
+            self.fail('No JSON could be decoded:', content)
+        return data
 
 
 
 
-class BookChangesTests(ApiTest):
-
-    def setUp(self):
-        super(BookChangesTests, self).setUp()
-        self.book = Book.objects.create(slug='slug')
-
-    def test_basic(self):
-        # test book in book_changes.added
-        changes = json.loads(self.client.get('/api/book_changes/0.json').content)
-        self.assertEqual(len(changes['updated']),
-                         1,
-                         'Added book not in book_changes.updated')
-
-    def test_deleted_disappears(self):
-        # test deleted book disappears
-        Book.objects.all().delete()
-        changes = json.loads(self.client.get('/api/book_changes/0.json').content)
-        self.assertEqual(len(changes), 1,
-                         'Deleted book should disappear.')
-
-    def test_shelf(self):
-        changed_at = self.book.changed_at
-
-        # putting on a shelf should not update changed_at
-        shelf = Tag.objects.create(category='set', slug='shelf')
-        self.book.tags = [shelf]
-        self.assertEqual(self.book.changed_at,
-                         changed_at)
-
-class TagChangesTests(ApiTest):
-
-    def setUp(self):
-        super(TagChangesTests, self).setUp()
-        self.tag = Tag.objects.create(category='author')
-        self.book = Book.objects.create()
-        self.book.tags = [self.tag]
-        self.book.save()
-
-    def test_added(self):
-        # test tag in tag_changes.added
-        changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
-        self.assertEqual(len(changes['updated']),
-                         1,
-                         'Added tag not in tag_changes.updated')
-
-    def test_empty_disappears(self):
-        self.book.tags = []
-        self.book.save()
-        changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
-        self.assertEqual(len(changes), 1,
-                         'Empty or deleted tag should disappear.')
-
-
-
-class BookTests(TestCase):
+class BookTests(ApiTest):
 
     def setUp(self):
         self.tag = Tag.objects.create(category='author', slug='joe')
 
     def setUp(self):
         self.tag = Tag.objects.create(category='author', slug='joe')
@@ -110,23 +42,23 @@ class BookTests(TestCase):
         self.book_tagged.save()
 
     def test_book_list(self):
         self.book_tagged.save()
 
     def test_book_list(self):
-        books = json.loads(self.client.get('/api/books/').content)
+        books = self.load_json('/api/books/')
         self.assertEqual(len(books), 2,
                          'Wrong book list.')
 
     def test_tagged_books(self):
         self.assertEqual(len(books), 2,
                          'Wrong book list.')
 
     def test_tagged_books(self):
-        books = json.loads(self.client.get('/api/authors/joe/books/').content)
+        books = self.load_json('/api/authors/joe/books/')
 
         self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
                         'Wrong tagged book list.')
 
     def test_detail(self):
 
         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)
+        book = self.load_json('/api/books/a-book/')
         self.assertEqual(book['title'], self.book.title,
                         'Wrong book details.')
 
 
         self.assertEqual(book['title'], self.book.title,
                         'Wrong book details.')
 
 
-class TagTests(TestCase):
+class TagTests(ApiTest):
 
     def setUp(self):
         self.tag = Tag.objects.create(category='author', slug='joe', name='Joe')
 
     def setUp(self):
         self.tag = Tag.objects.create(category='author', slug='joe', name='Joe')
@@ -135,12 +67,12 @@ class TagTests(TestCase):
         self.book.save()
 
     def test_tag_list(self):
         self.book.save()
 
     def test_tag_list(self):
-        tags = json.loads(self.client.get('/api/authors/').content)
+        tags = self.load_json('/api/authors/')
         self.assertEqual(len(tags), 1,
                         'Wrong tag list.')
 
     def test_tag_detail(self):
         self.assertEqual(len(tags), 1,
                         'Wrong tag list.')
 
     def test_tag_detail(self):
-        tag = json.loads(self.client.get('/api/authors/joe/').content)
+        tag = self.load_json('/api/authors/joe/')
         self.assertEqual(tag['name'], self.tag.name,
                         'Wrong tag details.')
 
         self.assertEqual(tag['name'], self.tag.name,
                         'Wrong tag details.')
 
@@ -160,5 +92,4 @@ class PictureTests(ApiTest):
         if import_form.is_valid():
             import_form.save()
 
         if import_form.is_valid():
             import_form.save()
 
-        pic = Picture.objects.get(slug=slug)
-
+        Picture.objects.get(slug=slug)