-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_inheritance.py
More file actions
164 lines (119 loc) · 5.05 KB
/
Copy pathtest_inheritance.py
File metadata and controls
164 lines (119 loc) · 5.05 KB
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
import pytest
from pytest import raises
from sqlobject import IntCol, StringCol
from sqlobject.inheritance import InheritableSQLObject
from sqlobject.tests.dbtest import getConnection, setupClass
try:
connection = getConnection()
except (AttributeError, NameError):
# The module was imported during documentation building
pass
else:
if connection.module.__name__ == 'mysql.connector' \
and connection.connector_type == 'mysql.connector-python':
pytestmark = pytest.mark.skip(
"connector-python falls into an infinite loop here")
########################################
# Inheritance
########################################
class InheritablePerson(InheritableSQLObject):
firstName = StringCol(length=100)
lastName = StringCol(alternateID=True, length=100)
class Employee(InheritablePerson):
_inheritable = False
so_position = StringCol(length=100)
def setup():
setupClass(InheritablePerson)
setupClass(Employee)
Employee(firstName='Project', lastName='Leader',
so_position='Project leader')
InheritablePerson(firstName='Oneof', lastName='Authors')
def test_creation_fail():
setup()
kwargs = {'firstName': 'John', 'lastname': 'Doe'}
raises(TypeError, Employee, **kwargs)
persons = InheritablePerson.select(InheritablePerson.q.firstName == 'John')
assert persons.count() == 0
def test_inheritance():
setup()
persons = InheritablePerson.select() # all
for person in persons:
assert isinstance(person, InheritablePerson)
if isinstance(person, Employee):
assert not hasattr(person, "childName")
else:
assert hasattr(person, "childName")
assert not person.childName
def test_inheritance_select():
setup()
# comparison to None needed to build the right SQL expression
persons = InheritablePerson.select(
InheritablePerson.q.firstName != None) # noqa
assert persons.count() == 2
persons = InheritablePerson.select(InheritablePerson.q.firstName == "phd")
assert persons.count() == 0
# comparison to None needed to build the right SQL expression
employees = Employee.select(Employee.q.firstName != None) # noqa
assert employees.count() == 1
employees = Employee.select(Employee.q.firstName == "phd")
assert employees.count() == 0
# comparison to None needed to build the right SQL expression
employees = Employee.select(Employee.q.so_position != None) # noqa
assert employees.count() == 1
persons = InheritablePerson.selectBy(firstName="Project")
assert persons.count() == 1
assert isinstance(persons[0], Employee)
persons = Employee.selectBy(firstName="Project")
assert persons.count() == 1
try:
person = InheritablePerson.byLastName("Oneof")
except Exception:
pass
else:
raise RuntimeError("unknown person %s" % person)
person = InheritablePerson.byLastName("Leader")
assert person.firstName == "Project"
person = Employee.byLastName("Leader")
assert person.firstName == "Project"
persons = list(InheritablePerson.select(
orderBy=InheritablePerson.q.lastName))
assert len(persons) == 2
persons = list(InheritablePerson.select(orderBy=(
InheritablePerson.q.lastName, InheritablePerson.q.firstName)))
assert len(persons) == 2
persons = list(Employee.select(orderBy=Employee.q.lastName))
assert len(persons) == 1
persons = list(Employee.select(orderBy=(Employee.q.lastName,
Employee.q.firstName)))
assert len(persons) == 1
persons = list(Employee.select(orderBy=Employee.q.so_position))
assert len(persons) == 1
persons = list(Employee.select(orderBy=(Employee.q.so_position,
Employee.q.lastName)))
assert len(persons) == 1
def test_addDelColumn():
setup()
assert hasattr(InheritablePerson, "firstName")
assert hasattr(Employee, "firstName")
assert hasattr(InheritablePerson.q, "firstName")
assert hasattr(Employee.q, "firstName")
Employee.sqlmeta.addColumn(IntCol('runtime', default=None))
assert not hasattr(InheritablePerson, 'runtime')
assert hasattr(Employee, 'runtime')
assert not hasattr(InheritablePerson.q, 'runtime')
assert hasattr(Employee.q, 'runtime')
InheritablePerson.sqlmeta.addColumn(IntCol('runtime2', default=None))
assert hasattr(InheritablePerson, 'runtime2')
assert hasattr(Employee, 'runtime2')
assert hasattr(InheritablePerson.q, 'runtime2')
assert hasattr(Employee.q, 'runtime2')
Employee.sqlmeta.delColumn('runtime')
assert not hasattr(InheritablePerson, 'runtime')
assert not hasattr(Employee, 'runtime')
assert not hasattr(InheritablePerson.q, 'runtime')
assert not hasattr(Employee.q, 'runtime')
InheritablePerson.sqlmeta.delColumn('runtime2')
assert not hasattr(InheritablePerson, 'runtime2')
assert not hasattr(Employee, 'runtime2')
assert not hasattr(InheritablePerson.q, 'runtime2')
assert not hasattr(Employee.q, 'runtime2')