Download:
child 21:8378bc5cf2c2
parent 19:fb40c77422c5
20:5e82fdcf22ae
Anton Shestakov <engored@ya.ru>, Fri, 11 Jan 2013 14:23:54 +0900
Create and use different database for tests; also move ResourceList and CRUDResource to fruitbar.crud.

4 файлов изменено, 102 вставок(+), 86 удалений(-) [+]
.hgignore file | annotate | diff | comparison | revisions
app.py file | annotate | diff | comparison | revisions
fruitbar/crud.py file | annotate | diff | comparison | revisions
test.py file | annotate | diff | comparison | revisions
--- a/.hgignore Fri Jan 11 13:48:48 2013 +0900
+++ b/.hgignore Fri Jan 11 14:23:54 2013 +0900
@@ -3,3 +3,4 @@
*.pyo
venv/
db/
+test_db/
--- a/app.py Fri Jan 11 13:48:48 2013 +0900
+++ b/app.py Fri Jan 11 14:23:54 2013 +0900
@@ -3,29 +3,39 @@
import os
-from flask import Flask, g, request, render_template
-from flask.ext.restful import Resource, Api
-from CodernityDB.database import RecordNotFound, RecordDeleted
+from flask import Flask, g, render_template
+from flask.ext.restful import Api
from CodernityDB.database_thread_safe import ThreadSafeDatabase
from fruitbar.indexes import ProjectIndex, TaskIndex
+from fruitbar.crud import ResourceList, CRUDResource
+
+
+DEBUG = True
+DATABASE = 'db'
app = Flask(__name__)
-api = Api(app)
+app.config.from_object(__name__)
+cdb = None
-db_path = os.path.join(os.path.dirname(__file__), 'db')
-cdb = ThreadSafeDatabase(db_path)
+def init_db():
+ global cdb
+
+ db_path = os.path.join(os.path.dirname(__file__), app.config['DATABASE'])
+ cdb = ThreadSafeDatabase(db_path)
-if cdb.exists():
- cdb.open()
- cdb.reindex()
-else:
- cdb.create()
-
- cdb.add_index(ProjectIndex(cdb.path, 'project'))
- cdb.add_index(TaskIndex(cdb.path, 'task'))
+ if cdb.exists():
+ cdb.open()
+ cdb.reindex()
+
+ print list(cdb.all('id'))
+ else:
+ cdb.create()
+
+ cdb.add_index(ProjectIndex(cdb.path, 'project'))
+ cdb.add_index(TaskIndex(cdb.path, 'task'))
@app.before_request
@@ -33,67 +43,9 @@
g.db = cdb
-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 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
+@app.route('/')
+def index():
+ return render_template('index.html')
class ProjectList(ResourceList):
@@ -119,20 +71,13 @@
safe_fields = ('name', 'note', 'done')
+api = Api(app)
api.add_resource(ProjectList, '/<workspace>/projects/')
api.add_resource(Project, '/<workspace>/projects/<resource_id>/')
api.add_resource(TaskList, '/<workspace>/tasks/')
api.add_resource(Task, '/<workspace>/tasks/<resource_id>/')
-@app.route('/')
-def index():
- return render_template('index.html')
-
-
-def main():
- app.run(debug=True)
-
-
if __name__ == '__main__':
- main()
+ init_db()
+ app.run()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/fruitbar/crud.py Fri Jan 11 14:23:54 2013 +0900
@@ -0,0 +1,68 @@
+#-*- coding:utf-8 -*-
+
+from flask import g, request
+from flask.ext.restful import Resource
+from CodernityDB.database import RecordNotFound, RecordDeleted
+
+
+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 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/test.py Fri Jan 11 13:48:48 2013 +0900
+++ b/test.py Fri Jan 11 14:23:54 2013 +0900
@@ -4,7 +4,7 @@
import json
from unittest import TestCase, main
-from app import app
+from app import app, init_db
class BaseTestCase(TestCase):
@@ -118,5 +118,7 @@
if __name__ == '__main__':
app.config['TESTING'] = True
+ app.config['DATABASE'] = 'test_db'
+ init_db()
main()