-
Notifications
You must be signed in to change notification settings - Fork 68
/
test_repr.py
125 lines (92 loc) · 3.05 KB
/
test_repr.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
from __future__ import print_function
import unittest
import sqlalchemy as sa
from sqlalchemy import create_engine
from sqlalchemy.orm import Query, DeclarativeBase
from sqlalchemy.orm import Session
from sqlalchemy_mixins import ReprMixin
class Base(DeclarativeBase):
__abstract__ = True
engine = create_engine('sqlite:///:memory:', echo=False)
sess = Session(engine)
# sess = scoped_session(sessionmaker(bind=engine))
class BaseModel(Base, ReprMixin):
__abstract__ = True
# !!! IMPORTANT !!!
# include below string to make mixin work
__repr__ = ReprMixin.__repr__
pass
class User(BaseModel):
__tablename__ = 'user'
__repr_attrs__ = ['name']
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String)
posts = sa.orm.relationship('Post')
class Post(BaseModel):
__tablename__ = 'post'
__repr_attrs__ = ['body', 'user']
id = sa.Column(sa.Integer, primary_key=True)
body = sa.Column(sa.String)
user_id = sa.Column(sa.Integer, sa.ForeignKey('user.id'))
archived = sa.Column(sa.Boolean, default=False)
user = sa.orm.relationship('User')
comments = sa.orm.relationship('Comment')
class Comment(BaseModel):
__tablename__ = 'comment'
__repr_attrs__ = ['body', 'post', 'user']
id = sa.Column(sa.Integer, primary_key=True)
body = sa.Column(sa.String)
user_id = sa.Column(sa.Integer, sa.ForeignKey('user.id'))
post_id = sa.Column(sa.Integer, sa.ForeignKey('post.id'))
rating = sa.Column(sa.Integer)
user = sa.orm.relationship('User')
post = sa.orm.relationship('Post')
class TestEagerLoad(unittest.TestCase):
def test(self):
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
u1 = User(name='Bill u1', id=1)
sess.add(u1)
sess.commit()
u2 = User(name='Alex u2', id=2)
sess.add(u2)
sess.commit()
sess.commit()
p11 = Post(
id=11,
body='very very very very very long long long long long',
archived=True
)
p11.user = u1
sess.add(p11)
sess.commit()
cm11 = Comment(
id=11,
body='c11',
user=u1,
post=p11,
rating=1
)
sess.add(cm11)
sess.commit()
user_not_in_session = User()
# tests. see output in console
print(repr(u1))
self.assertIn('Bill', repr(u1))
self.assertIn('#1', repr(u1))
print(repr(u2))
self.assertIn('Alex', repr(u2))
self.assertIn('#2', repr(u2))
print(repr(p11))
self.assertIn('very', repr(p11))
self.assertIn('...', repr(p11))
print(repr(cm11))
self.assertIn('c11', repr(cm11))
self.assertIn('Bill', repr(cm11))
print(user_not_in_session)
self.assertIn('None', repr(user_not_in_session))
Comment.__repr_attrs__ = ['INCORRECT ATTR']
with self.assertRaises(KeyError):
print(repr(cm11))
if __name__ == '__main__': # pragma: no cover
unittest.main()