-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtest_indexes.py
More file actions
100 lines (71 loc) · 3.02 KB
/
Copy pathtest_indexes.py
File metadata and controls
100 lines (71 loc) · 3.02 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
import pytest
from sqlobject import DatabaseIndex, ForeignKey, IntCol, MultipleJoin, \
SQLObject, StringCol
from sqlobject.dberrors import DatabaseError, IntegrityError, \
OperationalError, ProgrammingError
from sqlobject.tests.dbtest import raises, setupClass, supports
########################################
# Indexes
########################################
class SOIndex1(SQLObject):
name = StringCol(length=100)
number = IntCol()
nameIndex = DatabaseIndex('name', unique=True)
nameIndex2 = DatabaseIndex(name, number)
nameIndex3 = DatabaseIndex({'column': name,
'length': 3})
class SOIndex2(SQLObject):
name = StringCol(length=100)
nameIndex = DatabaseIndex({'expression': 'lower(name)'})
def test_indexes_1():
setupClass(SOIndex1)
n = 0
for name in 'blah blech boring yep yort snort'.split():
n += 1
SOIndex1(name=name, number=n)
mod = SOIndex1._connection.module
raises(
(mod.ProgrammingError, mod.IntegrityError,
mod.OperationalError, mod.DatabaseError,
ProgrammingError, IntegrityError, OperationalError, DatabaseError),
SOIndex1, name='blah', number=0)
def test_indexes_2():
if not supports('expressionIndex'):
pytest.skip("expressionIndex isn't supported")
setupClass(SOIndex2)
SOIndex2(name='')
class PersonIndexGet(SQLObject):
firstName = StringCol(length=100)
lastName = StringCol(length=100)
age = IntCol(alternateID=True)
nameIndex = DatabaseIndex(firstName, lastName, unique=True)
def test_index_get_1():
setupClass(PersonIndexGet, force=True)
PersonIndexGet(firstName='Eric', lastName='Idle', age=62)
PersonIndexGet(firstName='Terry', lastName='Gilliam', age=65)
PersonIndexGet(firstName='John', lastName='Cleese', age=66)
PersonIndexGet.get(1)
PersonIndexGet.nameIndex.get('Terry', 'Gilliam')
PersonIndexGet.nameIndex.get(firstName='John', lastName='Cleese')
raises(Exception, PersonIndexGet.nameIndex.get,
firstName='Graham', lastName='Chapman')
raises(Exception, PersonIndexGet.nameIndex.get,
'Terry', lastName='Gilliam')
raises(Exception, PersonIndexGet.nameIndex.get, 'Terry', 'Gilliam', 65)
raises(Exception, PersonIndexGet.nameIndex.get, 'Terry')
class PersonIndexGet2(SQLObject):
name = StringCol(alternateID=True, length=100)
age = IntCol()
addresses = MultipleJoin('AddressIndexGet2')
class AddressIndexGet2(SQLObject):
person = ForeignKey('PersonIndexGet2', notNone=True)
type = StringCol(notNone=True, length=100)
street = StringCol(notNone=True)
pk = DatabaseIndex(person, type, unique=True)
def test_index_get_2():
setupClass([PersonIndexGet2, AddressIndexGet2])
p = PersonIndexGet2(name='Terry Guilliam', age=64)
AddressIndexGet2(person=p, type='home', street='Terry Street 234')
AddressIndexGet2(person=p, type='work', street='Guilliam Street 234')
AddressIndexGet2.pk.get(p, 'work')
AddressIndexGet2.pk.get(person=p, type='work')