Download:
child 97:58a14a6318d1
parent 95:ba60cdc7a535
96:5e7f52c8da36
Anton Shestakov <av6@dwimlabs.net>, Mon, 18 Jul 2016 21:16:47 +0800
fruitbar: remove trailing white space

3 файлов изменено, 43 вставок(+), 43 удалений(-) [+]
fruitbar/crud.py file | annotate | diff | comparison | revisions
fruitbar/indexes.py file | annotate | diff | comparison | revisions
test.py file | annotate | diff | comparison | revisions
--- a/fruitbar/crud.py Thu Jun 30 21:47:54 2016 +0800
+++ b/fruitbar/crud.py Mon Jul 18 21:16:47 2016 +0800
@@ -8,74 +8,74 @@
class ResourceList(Resource):
db_index = 'id'
doc_stub = {}
-
+
def get(self, workspace):
return [project['doc'] for project in g.db.get_many(self.db_index, workspace, with_doc=True)]
-
+
def post(self, workspace):
doc = request.json.copy()
doc.update(self.doc_stub)
doc.update({'workspace': workspace})
-
+
response = g.db.insert(doc)
-
+
return g.db.get('id', response['_id'], with_doc=True)
class ResourceCombiner(Resource):
combine = {}
-
+
def get(self, workspace):
result = {}
-
+
for key, resource_class in self.combine.items():
resource = resource_class()
result[key] = resource.get(workspace)
-
+
return result
class CRUDResource(Resource):
""" CRUD? More like RUD!
"""
-
+
safe_fields = tuple()
-
+
def get(self, workspace, resource_id):
try:
doc = g.db.get('id', resource_id, with_doc=True)
-
+
if doc['workspace'] != workspace:
raise RecordNotFound
except (RecordNotFound, RecordDeleted):
return '', 404
-
+
return doc
-
+
def put(self, workspace, resource_id):
try:
doc = g.db.get('id', resource_id, with_doc=True)
-
+
if doc['workspace'] != workspace:
raise RecordNotFound
except (RecordNotFound, RecordDeleted):
return '', 404
-
+
userdata = dict((k, v) for (k, v) in request.json.items() if k in self.safe_fields)
doc.update(userdata)
response = g.db.update(doc)
-
+
return self.get(workspace, response['_id'])
-
+
def delete(self, workspace, resource_id):
try:
doc = g.db.get('id', resource_id, with_doc=True)
-
+
if doc['workspace'] != workspace:
raise RecordNotFound
except (RecordNotFound, RecordDeleted):
return '', 404
-
+
g.db.delete(doc)
-
+
return '', 200
--- a/fruitbar/indexes.py Thu Jun 30 21:47:54 2016 +0800
+++ b/fruitbar/indexes.py Mon Jul 18 21:16:47 2016 +0800
@@ -9,11 +9,11 @@
def __init__(self, *args, **kwargs):
kwargs['key_format'] = '16s'
super(ProjectIndex, self).__init__(*args, **kwargs)
-
+
def make_key_value(self, data):
if data['_t'] == 'project':
return md5(data['workspace'].encode('utf-8')).digest(), None
-
+
def make_key(self, key):
return md5(key).digest()
@@ -22,14 +22,14 @@
def __init__(self, *args, **kwargs):
kwargs['key_format'] = '16s'
super(TaskIndex, self).__init__(*args, **kwargs)
-
+
def make_key_value(self, data):
if data['_t'] == 'task':
return md5(data['workspace'].encode('utf-8')).digest(), None
-
+
def make_key(self, key):
return md5(key).digest()
-
+
def run_delete_for_project(self, db, workspace, project_id):
for task in db.get_many('task', workspace, with_doc=True):
if task['doc']['project_id'] == project_id:
--- a/test.py Thu Jun 30 21:47:54 2016 +0800
+++ b/test.py Mon Jul 18 21:16:47 2016 +0800
@@ -10,10 +10,10 @@
class BaseTestCase(TestCase):
workspace = 'TESTING'
-
+
def setUp(self):
self.c = app.test_client()
-
+
class IndexTestCase(BaseTestCase):
def test_index(self):
@@ -29,58 +29,58 @@
list_url = ''
item_url = ''
all_url = '/%s/all/'
-
+
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))
-
+
url = self.all_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 chain(*data.values())))
-
+
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)
self.assertEqual(data, '')
-
+
response = self.c.get(url)
self.assertEqual(response.status_code, 404)
@@ -96,7 +96,7 @@
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)
@@ -118,7 +118,7 @@
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)