Updated API tests.
[redakcja.git] / apps / api / tests / __init__.py
index 671978a..2292e7d 100644 (file)
@@ -1,10 +1,3 @@
-"""
-This file demonstrates two different styles of tests (one doctest and one
-unittest). These will both pass when you run "manage.py test".
-
-Replace these with more appropriate tests for your application.
-"""
-
 from django.test import TestCase
 from django.test.client import Client
 from django.core.urlresolvers import reverse
@@ -22,11 +15,15 @@ import tempfile
 REPO_TEMPLATES = join(dirname(__file__), 'data')
 
 def temprepo(name):
-    def decorator(func):   
+    from functools import wraps
+
+    def decorator(func):
+
+        @wraps(func)
         def decorated(self, *args, **kwargs):
             clean = False
             try:
-                temp = tempfile.mkdtemp("", "testdir_" )
+                temp = tempfile.mkdtemp("-test", func.__name__)
                 shutil.copytree(join(REPO_TEMPLATES, name), join(temp, 'repo'), False)
                 settings.REPOSITORY_PATH = join(temp, 'repo')
                 func(self, *args, **kwargs)
@@ -37,9 +34,9 @@ def temprepo(name):
                     print self.response
                     print "<<<"
 
-                shutil.rmtree(temp, True)
+                shutil.rmtree(temp, True)
                 settings.REPOSITORY_PATH = ''
-            
+           
         return decorated
     return decorator   
     
@@ -51,27 +48,23 @@ class SimpleTest(TestCase):
         u = User.objects.create_user('admin', 'test@localhost', 'admin')
         u.save()        
 
-    @temprepo('empty')
+    @temprepo('clean')
     def test_documents_get_anonymous(self):
         self.response = self.client.get( reverse("document_list_view") )
-        self.assert_json_response({
-            u'latest_rev': u'e56b2a7e06a97d7c3697fc4295974e0f20a66190',
-            u'documents': [],
-            u'cabinet': u'default',
-        }, exclude=['latest_shared_rev'])
+        self.assert_json_response({            
+            u'documents': [],            
+        })
 
-    @temprepo('empty')
+    @temprepo('clean')
     def test_documents_get_with_login(self):
         self.assertTrue(self.client.login(username='admin', password='admin'))
         
         self.response = self.client.get( reverse("document_list_view") )
-        self.assert_json_response({            
-            u'latest_rev': u'e56b2a7e06a97d7c3697fc4295974e0f20a66190',
-            u'documents': [],
-            u'cabinet': u'default',
+        self.assert_json_response({                        
+            u'documents': [],            
         })    
 
-    @temprepo('empty')
+    @temprepo('clean')
     def test_documents_post(self):
         self.assertTrue(self.client.login(username='admin', password='admin'))
 
@@ -79,8 +72,8 @@ class SimpleTest(TestCase):
         infile.write('0123456789')
         infile.flush()
         infile.seek(0)
-        
-        self.response = self.client.post( reverse("document_list_view"),               
+
+        self.response = self.client.post( reverse("document_list_view"),
         data = {
              'bookname': 'testbook',
              'ocr': infile,
@@ -88,15 +81,15 @@ class SimpleTest(TestCase):
         })
 
         infile.close()
-        
-        result = self.assert_json_response({
+
+        self.assert_json_response({
             'url': reverse('document_view', args=['testbook']),
             'name': 'testbook',
-            'size': 10,
+            'size': 10,
             # can't test revision number, 'cause it's random
         },)
 
-    @temprepo('empty')        
+    @temprepo('clean')
     def test_document_creation(self):
         self.assertTrue(self.client.login(username='admin', password='admin'))
 
@@ -111,73 +104,82 @@ class SimpleTest(TestCase):
              'ocr': infile,
              'generate_dc': False,
         })
-        
+
         r = self.assert_json_response({
             'url': reverse('document_view', args=['testbook']),
-            'name': 'testbook',
-            'size': 15,
+            'name': 'testbook',            
             # can't test revision number, 'cause it's random
         })
 
         created_rev = r['revision']
 
         self.response = self.client.get( \
-            reverse("document_view", args=["testbook"])+'?autocabinet=true' )
+            reverse("document_view", args=["testbook"]) )
 
         result = self.assert_json_response({
             u'latest_shared_rev': created_rev,
-            u'size': 15,
+            u'size': 15,
         })
 
 
-    @temprepo('testone')
+    @temprepo('simple')
     def test_document_meta_get_with_login(self):
         self.assertTrue(self.client.login(username='admin', password='admin'))
 
         self.response = self.client.get( reverse("document_list_view") )
         self.assert_json_response({
-            u'latest_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
-            u'documents': [{u'url': u'/api/documents/testfile', u'name': u'testfile'}],
-            u'cabinet': u'default',
+            u'latest_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
+            u'documents': [{u'url': u'/api/documents/sample', u'name': u'sample'},
+                {u'url': u'/api/documents/sample_pl', u'name': u'sample_pl'}],
         })
 
         self.response = self.client.get( \
-            reverse("document_view", args=['testfile'])+'?autocabinet=true' )
+            reverse("document_view", args=['sample']) )
 
         self.assert_json_response({
-            u'latest_shared_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
-            u'text_url': reverse("doctext_view", args=[u'testfile']),
-            u'dc_url': reverse("docdc_view", args=[u'testfile']),
-            u'parts_url': reverse("docparts_view", args=[u'testfile']),
-            u'name': u'testfile',
-            u'size': 20,
+            #u'latest_shared_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
+            u'text_url': reverse("doctext_view", args=[u'sample']),
+            u'dc_url': reverse("docdc_view", args=[u'sample']),
+            # u'parts_url': reverse("docparts_view", args=[u'sample']),
+            u'name': u'sample',
+            u'size': 20,
         })
-        
-        
-    @temprepo('test2')
-    def test_document_text_with_login(self):
-        self.assertTrue(self.client.login(username='admin', password='admin'))
-        
-        self.response = self.client.get( \
-            reverse("doctext_view", args=['testfile']) )
-
-        self.assertEqual(self.response.status_code, 200)
-        self.assertEqual(self.response.content, "Test file contents.\n")
 
 
-    @temprepo('test2')
-    def test_document_text_update(self):
+    @temprepo('simple')
+    def test_document_text_with_login(self):
         self.assertTrue(self.client.login(username='admin', password='admin'))
-        TEXT = u"Ala ma kota i psa"
-        
-        self.response = self.client.put( \
-            reverse("doctext_view", args=['testfile']), {'contents': TEXT })
-        self.assertEqual(self.response.status_code, 200)
 
         self.response = self.client.get( \
-            reverse("doctext_view", args=['testfile']) )
+            reverse("document_view", args=['sample']) )
+
+        resp = self.assert_json_response({
+            #u'latest_shared_rev': u'f94a263812dbe46a3a13d5209bb119988d0078d5',
+            u'text_url': reverse("doctext_view", args=[u'sample']),
+            u'dc_url': reverse("docdc_view", args=[u'sample']),
+            # u'parts_url': reverse("docparts_view", args=[u'sample']),
+            u'name': u'sample',
+            # u'size': 20,
+        })
+
+        self.response = self.client.get(resp['text_url'])
         self.assertEqual(self.response.status_code, 200)
-        self.assertEqual(self.response.content, TEXT)
+        self.assertEqual(self.response.content, "Ala ma kota\n")
+#
+#
+#    @temprepo('simple')
+#    def test_document_text_update(self):
+#        self.assertTrue(self.client.login(username='admin', password='admin'))
+#        TEXT = u"Ala ma kota i psa"
+#
+#        self.response = self.client.put( \
+#            reverse("doctext_view", args=['testfile']), {'contents': TEXT })
+#        self.assertEqual(self.response.status_code, 200)
+#
+#        self.response = self.client.get( \
+#            reverse("doctext_view", args=['testfile']) )
+#        self.assertEqual(self.response.status_code, 200)
+#        self.assertEqual(self.response.content, TEXT)
 
     def assert_json_response(self, must_have={}, exclude=[]):
         self.assertEqual(self.response.status_code, 200)