/
test_jsonify_sqlalchemy.py
102 lines (80 loc) · 3.04 KB
/
test_jsonify_sqlalchemy.py
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
from nose.tools import raises
from tg import jsonify
try:
try:
import sqlite3
except:
import pysqlite2
from sqlalchemy import (MetaData, Table, Column, ForeignKey,
Integer, String)
from sqlalchemy.orm import create_session, mapper, relation
metadata = MetaData('sqlite:///:memory:')
test1 = Table('test1', metadata,
Column('id', Integer, primary_key=True),
Column('val', String(8)))
test2 = Table('test2', metadata,
Column('id', Integer, primary_key=True),
Column('test1id', Integer, ForeignKey('test1.id')),
Column('val', String(8)))
test3 = Table('test3', metadata,
Column('id', Integer, primary_key=True),
Column('val', String(8)))
test4 = Table('test4', metadata,
Column('id', Integer, primary_key=True),
Column('val', String(8)))
metadata.create_all()
class Test2(object):
pass
mapper(Test2, test2)
class Test1(object):
pass
mapper(Test1, test1, properties={'test2s': relation(Test2)})
class Test3(object):
def __json__(self):
return {'id': self.id, 'val': self.val, 'customized': True}
mapper(Test3, test3)
class Test4(object):
pass
mapper(Test4, test4)
test1.insert().execute({'id': 1, 'val': 'bob'})
test2.insert().execute({'id': 1, 'test1id': 1, 'val': 'fred'})
test2.insert().execute({'id': 2, 'test1id': 1, 'val': 'alice'})
test3.insert().execute({'id': 1, 'val': 'bob'})
test4.insert().execute({'id': 1, 'val': 'alberto'})
except ImportError:
from warnings import warn
warn('SQLAlchemy or PySqlite not installed - cannot run these tests.')
else:
def test_saobj():
s = create_session()
t = s.query(Test1).get(1)
encoded = jsonify.encode(t)
assert encoded == '{"id": 1, "val": "bob"}'
@raises(jsonify.JsonEncodeError)
def test_salist():
s = create_session()
t = s.query(Test1).get(1)
encoded = jsonify.encode(t.test2s)
assert encoded == '{rows: [{"test1id": 1, "id": 1, "val": "fred"},' \
' {"test1id": 1, "id": 2, "val": "alice"}]', encoded
@raises(jsonify.JsonEncodeError)
def test_select_row():
s = create_session()
t = test1.select().execute()
encoded = jsonify.encode(t)
# this may be added back later on
# assert encoded == """{"count": -1, "rows": [{"count": 1, "rows": {"id": 1, "val": "bob"}}]}""", encoded
@raises(jsonify.JsonEncodeError)
def test_select_rows():
s = create_session()
t = test2.select().execute()
encoded = jsonify.encode(t)
# this may be added back later
#
assert encoded == '{"count": -1, "rows": [{"count": 1, "rows": {"test1id": 1, "id": 1, "val": "fred"}},\
{"count": 1, "rows": {"test1id": 1, "id": 2, "val": "alice"}}]}', encoded
def test_explicit_saobj():
s = create_session()
t = s.query(Test3).get(1)
encoded = jsonify.encode(t)
assert encoded == '{"id": 1, "val": "bob", "customized": true}'