-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.py
152 lines (116 loc) · 3.59 KB
/
test.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
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
from schemalchemy import Base
from schematics.types import IntType, StringType
from schematics.transforms import whitelist
from schematics.types.serializable import serializable
from sqlalchemy import Table, Column, ForeignKey, Integer, String
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, relationship, synonym
DATABASE = 'test.db'
class Person(Base):
"""
Example using a SQLAlchemy Table definition.
Note the Column name strings must match the Schematics properties.
"""
class Options:
roles = {
'public': whitelist('name', 'company_name')
}
__table__ = Table('person', Base.metadata,
Column('id', Integer, primary_key=True),
Column('name', String(50)),
Column('company_id', Integer, ForeignKey('company.id'))
)
id = IntType(default=1)
name = StringType()
company_id = synonym('_company_id')
@serializable
def company_name(self):
return self.company.name
class Company(Base):
"""
Example of setting the columns directly.
Note the Column properties must match the Schematics properties with
the column_prefix.
"""
__tablename__ = 'company'
_id = Column('id', Integer, primary_key=True)
_name = Column('name', String(50))
people = relationship('Person', backref='company')
id = IntType(default=1)
name = StringType()
def init_db():
engine = create_engine('sqlite:///' + DATABASE)
session = sessionmaker(bind=engine)
Base.metadata.create_all(engine)
return session
def fixture_person():
p = Person({'name': 'Paul Eipper'})
return p
def fixture_company():
c = Company()
c.id = 2
c.name = 'nKey'
c.validate()
return c
def test_insert_data(session):
p = fixture_person()
c = fixture_company()
p.company = c
session.add(p)
session.add(c)
session.commit()
fp = fixture_person()
fc = fixture_company()
fc.people.append(fp)
assert_equal(p.serialize(), fp.serialize())
assert_equal(c.serialize(), fc.serialize())
assert_equal(p._company_id, p.company_id)
assert_equal(p.company, c)
assert_in(p, c.people)
print 'Created:'
print '\tPerson ', p.serialize()
print '\tCompany', c.serialize()
def test_load_data(session):
p = session.query(Person).first()
c = session.query(Company).first()
fp = fixture_person()
fc = fixture_company()
fc.people.append(fp)
assert_equal(p.serialize(), fp.serialize())
assert_equal(c.serialize(), fc.serialize())
print 'Loaded:'
print '\tPerson ', p.serialize()
print '\tCompany', c.serialize()
def test_update_data(Session):
session = Session()
p = session.query(Person).first()
c = session.query(Company).first()
p.id = 10
c.id = 20
p._company_id = c.id
p.name = 'Paul E.'
c.name = 'Another'
p_serialized = p.serialize()
c_serialized = c.serialize()
session.commit()
session = Session()
p = session.query(Person).first()
c = session.query(Company).first()
assert_equal(p_serialized, p.serialize())
assert_equal(c_serialized, c.serialize())
print 'Updated:'
print '\tPerson ', p.serialize()
print '\tCompany', c.serialize()
def assert_equal(v1, v2):
assert v1 == v2, '%s != %s' % (v1, v2)
def assert_in(v1, v2):
assert v1 in v2, '%s not in %s' % (v1, v2)
if __name__ == '__main__':
import os
if os.path.exists(DATABASE):
os.remove(DATABASE)
Session = init_db()
test_insert_data(Session())
test_load_data(Session())
test_update_data(Session)
print 'All tests OK'