Download:
child 20:5e82fdcf22ae
parent 18:fc0c0cc25502
19:fb40c77422c5
Anton Shestakov <engored@ya.ru>, Fri, 11 Jan 2013 13:48:48 +0900
Simple tests, using specific workspace on production db.

1 файлов изменено, 122 вставок(+), 0 удалений(-) [+]
test.py file | annotate | diff | comparison | revisions
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test.py Fri Jan 11 13:48:48 2013 +0900
@@ -0,0 +1,122 @@
+#!/usr/bin/env python
+#-*- coding:utf-8 -*-
+
+import json
+from unittest import TestCase, main
+
+from app import app
+
+
+class BaseTestCase(TestCase):
+ workspace = 'TESTING'
+
+ def setUp(self):
+ self.c = app.test_client()
+
+
+class IndexTestCase(BaseTestCase):
+ def test_index(self):
+ response = self.c.get('/')
+ assert 'Fruit Bar' in response.data
+
+
+class ResourceTestCase(BaseTestCase):
+ new_resource = {}
+ resource_update = {}
+ resource_type = ''
+ list_url = ''
+ item_url = ''
+
+ def create_resource(self):
+ url = self.list_url % self.workspace
+ response = self.c.post(url, content_type='application/json', data=json.dumps(self.new_resource))
+ data = json.loads(response.data)
+
+ self.assertIn('_t', data)
+ self.assertEqual(data.pop('_t'), self.resource_type)
+
+ self.assertIn('workspace', data)
+ self.assertEqual(data.pop('workspace'), self.workspace)
+
+ resource_id = data.pop('_id')
+ data.pop('_rev')
+ self.assertDictEqual(self.new_resource, data)
+
+ return resource_id
+
+ def read_resource(self, resource_id):
+ url = self.item_url % (self.workspace, resource_id)
+ response = self.c.get(url)
+ data = json.loads(response.data)
+
+ self.assertEqual(data['_id'], resource_id)
+
+ url = self.list_url % self.workspace
+ response = self.c.get(url)
+ data = json.loads(response.data)
+
+ self.assertTrue(any(resource.get('_id', None) == resource_id for resource in data))
+
+ def update_resource(self, resource_id):
+ url = self.item_url % (self.workspace, resource_id)
+ response = self.c.put(url, content_type='application/json', data=json.dumps(self.resource_update))
+ data = json.loads(response.data)
+
+ self.assertEqual(data['_id'], resource_id)
+ for key, value in self.resource_update.items():
+ self.assertEqual(data[key], value)
+
+ def delete_resource(self, resource_id):
+ url = self.item_url % (self.workspace, resource_id)
+ response = self.c.delete(url)
+ data = json.loads(response.data)
+
+ response = self.c.get(url)
+ self.assertEqual(response.status_code, 404)
+
+
+class ProjectsTestCase(ResourceTestCase):
+ new_resource = {
+ 'name': 'Test Project',
+ 'color': 'success'
+ }
+ resource_update = {
+ 'name': 'Renamed Project'
+ }
+ resource_type = 'project'
+ list_url = '/%s/projects/'
+ item_url = '/%s/projects/%s/'
+
+ def test_project(self):
+ project_id = self.create_resource()
+ self.read_resource(project_id)
+ self.update_resource(project_id)
+ self.delete_resource(project_id)
+
+
+class TasksTestCase(ResourceTestCase):
+ new_resource = {
+ 'name': 'Test Task',
+ 'note': 'test',
+ 'done': False,
+ 'project_id': 'fake'
+ }
+ resource_update = {
+ 'name': 'Renamed Task',
+ 'note': 'the note'
+ }
+ resource_type = 'task'
+ list_url = '/%s/tasks/'
+ item_url = '/%s/tasks/%s/'
+
+ def test_task(self):
+ task_id = self.create_resource()
+ self.read_resource(task_id)
+ self.update_resource(task_id)
+ self.delete_resource(task_id)
+
+
+if __name__ == '__main__':
+ app.config['TESTING'] = True
+ main()
+