This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
/
test_mapper.py
83 lines (65 loc) · 2.29 KB
/
test_mapper.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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
----------------------
Object-document mapper
----------------------
>>> def text_validator(node, value):
... if value.startswith('Banana'):
... raise BaseException, "You can't start a text with 'Banana'"
...
>>> def colon_validator(node, value):
... for author in value:
... if ',' not in author:
... raise BaseException, "Authors's name must be in 'LastName, FirstName' format"
>>> class Book(Document):
... title = TextProperty(required=True, validator=text_validator)
... authors = MultiTextProperty(required=False, validator=colon_validator)
... pages = TextProperty()
...
>>> class Book2(Document):
... title = TextProperty(required=True, validator=text_validator)
... authors = comp = CompositeTextProperty(required=False, subkeys='fl')
...
Instantiating a Book object::
>>> book1 = Book(title='Godel, Escher, Bach',
... authors=(u'Hofstadter, Douglas',),
... pages='777')
...
>>> book2 = Book2(title='Godel, Escher, Bach',
... authors=u'^lHofstadter^fDouglas')
...
Manipulating its attributes::
>>> book1.title
u'Godel, Escher, Bach'
>>> book1.authors[0]
u'Hofstadter, Douglas'
>>> book1.authors = (u'Hofstadter Douglas',)
Traceback (most recent call last):
...
BaseException: Authors's name must be in 'LastName, FirstName' format
>>> book1.authors[0]
u'Hofstadter, Douglas'
>>> book1.authors += (u'Daiana Rose',)
Traceback (most recent call last):
...
BaseException: Authors's name must be in 'LastName, FirstName' format
>>> book1.authors += (u'Rose, Daiana',)
>>> book1.authors
(u'Hofstadter, Douglas', u'Rose, Daiana')
>>> print book2.authors
^lHofstadter^fDouglas
>>> book2.authors['f']
u'Douglas'
>>> book2.authors['j']
Traceback (most recent call last):
...
KeyError: 'j'
"""
from isis.model.mapper import Document
from isis.model.mapper import TextProperty, MultiTextProperty, CompositeTextProperty
def test():
import doctest
doctest.testmod()
if __name__=='__main__':
test()