parent
8301f0af78
commit
5d23bff409
@ -0,0 +1,21 @@
|
||||
|
||||
from werkzeug.wrappers import Response
|
||||
from werkzeug.exceptions import abort
|
||||
|
||||
from isso import json, models
|
||||
|
||||
|
||||
def create(app, environ, request, path):
|
||||
|
||||
try:
|
||||
rv = app.db.add(path, models.Comment.fromjson(request.data))
|
||||
except ValueError as e:
|
||||
return Response(unicode(e), 400)
|
||||
|
||||
return Response(json.dumps(app.db.get(*rv)), 201, content_type='application/json')
|
||||
|
||||
|
||||
def get(app, environ, request, path, id=None):
|
||||
|
||||
rv = list(app.db.retrieve(path)) if id is None else app.db.get(path, id)
|
||||
return Response(json.dumps(rv), 200, content_type='application/json')
|
@ -1,7 +0,0 @@
|
||||
|
||||
from werkzeug.wrappers import Response
|
||||
from werkzeug.exceptions import abort
|
||||
|
||||
|
||||
def comment(app, environ, request, path, id=None):
|
||||
return Response('', 200)
|
@ -0,0 +1,22 @@
|
||||
|
||||
import json
|
||||
from isso.models import Comment
|
||||
|
||||
# def prove(f):
|
||||
|
||||
# def dec(app, env, req, *args, **kwargs):
|
||||
|
||||
# pass
|
||||
|
||||
|
||||
# def sign(response):
|
||||
# pass
|
||||
|
||||
|
||||
class IssoEncoder(json.JSONEncoder):
|
||||
|
||||
def default(self, obj):
|
||||
if isinstance(obj, Comment):
|
||||
return dict((field, value) for field, value in obj.iteritems())
|
||||
|
||||
return json.JSONEncoder.default(self, obj)
|
@ -0,0 +1,59 @@
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from werkzeug.test import Client
|
||||
from werkzeug.wrappers import Response
|
||||
|
||||
from isso import Isso, json
|
||||
from isso.models import Comment
|
||||
|
||||
|
||||
def comment(**kw):
|
||||
return Comment.fromjson(json.dumps(kw))
|
||||
|
||||
|
||||
class TestComments(unittest.TestCase):
|
||||
|
||||
get = lambda self, *x, **z: Client(self.app, Response).get(*x, **z)
|
||||
put = lambda self, *x, **z: Client(self.app, Response).put(*x, **z)
|
||||
post = lambda self, *x, **z: Client(self.app, Response).post(*x, **z)
|
||||
|
||||
def setUp(self):
|
||||
fd, self.path = tempfile.mkstemp()
|
||||
self.app = Isso({'SQLITE': self.path})
|
||||
|
||||
def testGet(self):
|
||||
|
||||
self.post('/comment/path/new', data=json.dumps(comment(text='Lorem ipsum ...')))
|
||||
r = self.get('/comment/path/1')
|
||||
assert r.status_code == 200
|
||||
|
||||
rv = json.loads(r.data)
|
||||
|
||||
assert rv['id'] == 1
|
||||
assert rv['text'] == 'Lorem ipsum ...'
|
||||
|
||||
def testCreate(self):
|
||||
|
||||
rv = self.post('/comment/path/new', data=json.dumps(comment(text='Lorem ipsum ...')))
|
||||
|
||||
assert rv.status_code == 201
|
||||
# XXX assert cookie
|
||||
|
||||
c = Comment.fromjson(rv.data)
|
||||
|
||||
assert not c.pending
|
||||
assert not c.deleted
|
||||
assert c.text == 'Lorem ipsum ...'
|
||||
|
||||
def testCreateAndGetMultiple(self):
|
||||
|
||||
for i in range(100):
|
||||
self.post('/comment/path/new', data=json.dumps(comment(text='Spam')))
|
||||
|
||||
r = self.get('/comment/path/')
|
||||
assert r.status_code == 200
|
||||
|
||||
rv = json.loads(r.data)
|
||||
assert len(rv) == 20
|
Loading…
Reference in new issue