be more robust
[wolnelektury.git] / apps / api / tests.py
index 41df4c6..fe8e70f 100644 (file)
@@ -1,23 +1,26 @@
 # -*- coding: utf-8 -*-
 
 # -*- coding: utf-8 -*-
 
-from datetime import datetime
+from os import path
 
 
+from django.core.files.uploadedfile import SimpleUploadedFile
 from django.test import TestCase
 from django.test import TestCase
+from django.test.utils import override_settings
 from django.utils import simplejson as json
 from django.utils import simplejson as json
-from django.conf import settings
 
 
-from api.helpers import timestamp
 from catalogue.models import Book, Tag
 from catalogue.models import Book, Tag
+from picture.forms import PictureImportForm
+from picture.models import Picture
+import picture.tests
 
 
 
 
+@override_settings(
+    API_WAIT=-1, 
+    CACHES = {'api': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'},
+              'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'},
+              'permanent': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}
+)
 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
+    pass
 
 
 class ChangesTest(ApiTest):
 
 
 class ChangesTest(ApiTest):
@@ -60,7 +63,6 @@ class BookChangesTests(ApiTest):
 
     def test_shelf(self):
         changed_at = self.book.changed_at
 
     def test_shelf(self):
         changed_at = self.book.changed_at
-        print changed_at
 
         # putting on a shelf should not update changed_at
         shelf = Tag.objects.create(category='set', slug='shelf')
 
         # putting on a shelf should not update changed_at
         shelf = Tag.objects.create(category='set', slug='shelf')
@@ -90,3 +92,68 @@ class TagChangesTests(ApiTest):
         changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
         self.assertEqual(len(changes), 1,
                          'Empty or deleted tag should disappear.')
         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):
+
+    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.')
+
+
+class PictureTests(ApiTest):
+    def test_publish(self):
+        slug = "kandinsky-composition-viii"
+        xml = SimpleUploadedFile('composition8.xml', open(path.join(picture.tests.__path__[0], "files", slug + ".xml")).read())
+        img = SimpleUploadedFile('kompozycja-8.png', open(path.join(picture.tests.__path__[0], "files", slug + ".png")).read())
+
+        import_form = PictureImportForm({}, {
+            'picture_xml_file': xml,
+            'picture_image_file': img
+            })
+
+        assert import_form.is_valid()
+        if import_form.is_valid():
+            import_form.save()
+
+        pic = Picture.objects.get(slug=slug)
+