-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_foreignKey.py
More file actions
94 lines (63 loc) · 2.69 KB
/
Copy pathtest_foreignKey.py
File metadata and controls
94 lines (63 loc) · 2.69 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
from sqlobject import ForeignKey, SQLObject, StringCol
from sqlobject.tests.dbtest import setupClass
from sqlobject.inheritance import InheritableSQLObject
class Note(SQLObject):
text = StringCol()
class PersonWithNotes(InheritableSQLObject):
firstName = StringCol()
lastName = StringCol()
note = ForeignKey("Note", default=None)
class Paper(SQLObject):
content = StringCol()
class EmployeeWithNotes(PersonWithNotes):
_inheritable = False
paper = ForeignKey("Paper", default=None)
def test_foreignKey():
setupClass([Note, PersonWithNotes, Paper, EmployeeWithNotes], force=True)
note = Note(text="person")
PersonWithNotes(firstName='Oneof', lastName='Authors', note=note)
note = Note(text="employee")
EmployeeWithNotes(firstName='Project', lastName='Leader', note=note)
paper = Paper(content="secret")
EmployeeWithNotes(firstName='Senior', lastName='Clerk', paper=paper)
PersonWithNotes(firstName='Some', lastName='Person')
person = PersonWithNotes.get(1)
assert isinstance(person, PersonWithNotes) and \
not isinstance(person, EmployeeWithNotes)
assert person.note.text == "person"
employee = EmployeeWithNotes.get(2)
assert isinstance(employee, EmployeeWithNotes)
assert employee.note.text == "employee"
save_employee = employee
# comparison to None needed to build the right SQL expression
persons = PersonWithNotes.select(PersonWithNotes.q.noteID != None) # noqa
assert persons.count() == 2
persons = PersonWithNotes.selectBy(noteID=person.note.id)
assert persons.count() == 1
# comparison to None needed to build the right SQL expression
employee = EmployeeWithNotes.select(
PersonWithNotes.q.noteID != None) # noqa
assert employee.count() == 1
persons = PersonWithNotes.selectBy(noteID=person.note.id)
assert persons.count() == 1
persons = PersonWithNotes.selectBy(note=person.note)
assert persons.count() == 1
persons = PersonWithNotes.selectBy(note=None)
assert persons.count() == 2
employee = EmployeeWithNotes.selectBy(paperID=None)
assert employee.count() == 1
employee = EmployeeWithNotes.selectBy(paper=None)
assert employee.count() == 1
employee = EmployeeWithNotes.selectBy(note=save_employee.note,
paper=save_employee.paper)
assert employee.count() == 1
employee = EmployeeWithNotes.selectBy()
assert employee.count() == 2
class SOTestInhBase(InheritableSQLObject):
pass
class SOTestInhFKey(SOTestInhBase):
base = ForeignKey("SOTestInhBase")
def test_foreignKey2():
setupClass([SOTestInhBase, SOTestInhFKey])
test = SOTestInhBase()
SOTestInhFKey(base=test)