-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
120 lines (86 loc) · 3.2 KB
/
tests.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
#!/usr/bin/env python
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import unittest
from alchmanager import ManagedQuery, ManagedSession
engine = create_engine('sqlite:///:memory:')
session = sessionmaker(query_cls=ManagedQuery,
class_=ManagedSession,
bind=engine)()
Base = declarative_base()
@session.load_manager()
class MainSessionManager:
@staticmethod
def published(query):
return query.filter_by(is_public=True)
@staticmethod
def has_index(query):
return query.filter_by(is_index=True)
class MainManager:
@staticmethod
def is_index(query):
return query.filter_by(is_index=True)
@staticmethod
def is_public(query):
return query.filter_by(is_public=True)
class Main(Base):
__tablename__ = 'main'
id = Column(Integer, primary_key=True)
child = Column(Integer, index=True)
preview = Column(String(50))
typeMedia = Column(Integer)
is_index = Column(Boolean, default=False)
is_public = Column(Boolean, default=False)
__manager__ = MainManager
__mapper_args__ = {'polymorphic_on': typeMedia}
class Video(Main):
__tablename__ = 'video'
videoid = Column(Integer, ForeignKey(Main.child), primary_key=True)
movie = Column(String(50))
__mapper_args__ = {'polymorphic_identity': 1,
'inherit_condition': (Main.typeMedia == 1) &
(Main.child == videoid)}
class Test(Base):
__tablename__ = 'test'
id = Column(Integer, primary_key=True)
child = Column(Integer, index=True)
is_public = Column(Boolean, default=False)
is_index = Column(Boolean)
Base.metadata.create_all(engine)
items = [Video(movie='test',
preview='test'),
Video(movie='test',
preview='testmovie',
is_index=True),
Test(child=1,
is_public=True,
is_index=True),
Test(child=2)]
session.add_all(items)
session.commit()
class TestsQueryManager(unittest.TestCase):
def test_queires(self):
standart_query = session.query(Video).filter_by(is_index=True).all()
managed_query = session.query(Video).is_index().all()
self.assertEquals(standart_query, managed_query)
def test_subclass_query(self):
self.assertTrue(hasattr(session.query(Video), 'is_index'))
self.assertTrue(session.query(Video).is_index())
self.assertTrue(
session.query(Video).is_index().filter_by(child=1).is_public()
)
def test_without_manager(self):
self.assertFalse(hasattr(session.query(Test), 'is_index'))
class TestsSessionManager(unittest.TestCase):
def test_is_loaded(self):
self.assertTrue(hasattr(session.query(Test), 'published'))
def test_queries(self):
self.assertEquals(session.query(Test).published().count(), 1)
self.assertEquals(session.query(Test).count(), 2)
def test_with_many_calls(self):
query = session.query(Test).has_index().published().count()
self.assertEquals(query, 1)
if __name__ == '__main__':
unittest.main()