1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
import datetime
try:
import json
except ImportError:
import simplejson as json
from webtest import TestApp
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, Unicode, Date, ForeignKey
from sqlalchemy.orm import relation
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from wsme import WSRoot
import wsme.types
from wsmeext.sqlalchemy.types import generate_types
from wsmeext.sqlalchemy.controllers import CRUDController
from six import u
engine = create_engine('sqlite:///')
DBSession = scoped_session(sessionmaker(autocommit=False, autoflush=False,
bind=engine))
DBBase = declarative_base()
registry = wsme.types.Registry()
class DBPerson(DBBase):
__tablename__ = 'person'
id = Column(Integer, primary_key=True)
name = Column(Unicode(50))
birthdate = Column(Date)
addresses = relation('DBAddress')
class DBAddress(DBBase):
__tablename__ = 'address'
id = Column(Integer, primary_key=True)
_person_id = Column('person_id', ForeignKey(DBPerson.id))
street = Column(Unicode(50))
city = Column(Unicode(50))
person = relation(DBPerson)
globals().update(
generate_types(DBPerson, DBAddress, makename=lambda s: s[2:],
registry=registry))
class PersonController(CRUDController):
__saclass__ = DBPerson
__dbsession__ = DBSession
__registry__ = registry
class AddressController(CRUDController):
__saclass__ = DBAddress
__dbsession__ = DBSession
__registry__ = registry
class Root(WSRoot):
__registry__ = registry
person = PersonController()
address = AddressController()
class TestCRUDController():
def setUp(self):
DBBase.metadata.create_all(DBSession.bind)
self.root = Root()
self.root.getapi()
self.root.addprotocol('restjson')
self.app = TestApp(self.root.wsgiapp())
def tearDown(self):
DBBase.metadata.drop_all(DBSession.bind)
def test_create(self):
data = dict(data=dict(
name=u('Pierre-Joseph'),
birthdate=u('1809-01-15')
))
r = self.app.post('/person/create', json.dumps(data),
headers={'Content-Type': 'application/json'})
r = json.loads(r.text)
print(r)
assert r['name'] == u('Pierre-Joseph')
assert r['birthdate'] == u('1809-01-15')
def test_PUT(self):
data = dict(data=dict(
name=u('Pierre-Joseph'),
birthdate=u('1809-01-15')
))
r = self.app.put('/person', json.dumps(data),
headers={'Content-Type': 'application/json'})
r = json.loads(r.text)
print(r)
assert r['name'] == u('Pierre-Joseph')
assert r['birthdate'] == u('1809-01-15')
def test_read(self):
p = DBPerson(
name=u('Pierre-Joseph'),
birthdate=datetime.date(1809, 1, 15))
DBSession.add(p)
DBSession.flush()
pid = p.id
r = self.app.post('/person/read', '{"ref": {"id": %s}}' % pid,
headers={'Content-Type': 'application/json'})
r = json.loads(r.text)
print(r)
assert r['name'] == u('Pierre-Joseph')
assert r['birthdate'] == u('1809-01-15')
def test_GET(self):
p = DBPerson(
name=u('Pierre-Joseph'),
birthdate=datetime.date(1809, 1, 15))
DBSession.add(p)
DBSession.flush()
pid = p.id
r = self.app.get('/person?ref.id=%s' % pid,
headers={'Content-Type': 'application/json'})
r = json.loads(r.text)
print(r)
assert r['name'] == u('Pierre-Joseph')
assert r['birthdate'] == u('1809-01-15')
def test_update(self):
p = DBPerson(
name=u('Pierre-Joseph'),
birthdate=datetime.date(1809, 1, 15))
DBSession.add(p)
DBSession.flush()
pid = p.id
data = {
"id": pid,
"name": u('Pierre-Joseph Proudon')
}
r = self.app.post('/person/update', json.dumps(dict(data=data)),
headers={'Content-Type': 'application/json'})
r = json.loads(r.text)
print(r)
assert r['name'] == u('Pierre-Joseph Proudon')
assert r['birthdate'] == u('1809-01-15')
def test_POST(self):
p = DBPerson(
name=u('Pierre-Joseph'),
birthdate=datetime.date(1809, 1, 15))
DBSession.add(p)
DBSession.flush()
pid = p.id
data = {
"id": pid,
"name": u('Pierre-Joseph Proudon')
}
r = self.app.post('/person', json.dumps(dict(data=data)),
headers={'Content-Type': 'application/json'})
r = json.loads(r.text)
print(r)
assert r['name'] == u('Pierre-Joseph Proudon')
assert r['birthdate'] == u('1809-01-15')
def test_delete(self):
p = DBPerson(
name=u('Pierre-Joseph'),
birthdate=datetime.date(1809, 1, 15))
DBSession.add(p)
DBSession.flush()
pid = p.id
r = self.app.post('/person/delete', json.dumps(
dict(ref=dict(id=pid))),
headers={
'Content-Type': 'application/json'
})
print(r)
assert DBSession.query(DBPerson).get(pid) is None
def test_DELETE(self):
p = DBPerson(
name=u('Pierre-Joseph'),
birthdate=datetime.date(1809, 1, 15))
DBSession.add(p)
DBSession.flush()
pid = p.id
r = self.app.delete('/person?ref.id=%s' % pid,
headers={'Content-Type': 'application/json'})
print(r)
assert DBSession.query(DBPerson).get(pid) is None
def test_nothing(self):
pass
|