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
|
import unittest
from testutils import tmpData
from olpc.datastore import DataStore
from olpc.datastore import model, backingstore
import datetime
import os
DEFAULT_STORE = '/tmp/test_ds'
class Test(unittest.TestCase):
def setUp(self): os.system('rm -rf %s' % DEFAULT_STORE)
def tearDown(self): os.system('rm -rf %s' % DEFAULT_STORE)
def test_dateproperty(self):
n = datetime.datetime.now()
# we have to kill the microseconds as
# time.strptime which we must use in 2.4 doesn't parse it
n = n.replace(microsecond=0)
p = model.Property('ctime', n.isoformat(), 'date')
assert p.key == "ctime"
assert p.value == n.isoformat()
p.value = p.value
assert p.value == n.isoformat()
def test_binaryproperty(self):
ds = DataStore()
ds.registerBackend(backingstore.FileBackingStore)
#add a custom field to the model
dm = model.defaultModel.copy().addField('thumbnail', 'binary')
ds.mount(DEFAULT_STORE, {'indexmanager.model' : dm})
n = datetime.datetime.now()
data = open('test.jpg', 'r').read()
# binary data with \0's in it can cause dbus errors here
fn = tmpData("with image\0\0 prop")
# The key types are looked up in the model now
uid = ds.create({'title' : "Document 1", 'thumbnail' : data, 'ctime' : n.isoformat()}, fn)
ds.complete_indexing()
c = ds.get(uid)
assert c.get_property('thumbnail') == data
# I don't care about the microsecond issue now, the typelib
# patch later can fix that
assert c.get_property('ctime')[:19] == n.isoformat()[:19]
ds.stop()
def test_intproperty(self):
p = model.Property('keep', 1, 'int')
assert p.value == '1'
p.value = 0
assert p.value == '0'
p.value = '1'
assert p.value == '1'
p.value = '0'
assert p.value == '0'
ds = DataStore()
ds.registerBackend(backingstore.FileBackingStore)
ds.mount(DEFAULT_STORE)
uid = ds.create({'title' : "Document 1", 'keep' : 1},)
ds.complete_indexing()
c = ds.get(uid)
assert c.get_property('keep') == 1
ds.update(uid, {'title' : "Document 1", 'keep' : 0})
ds.complete_indexing()
c = ds.get(uid)
assert c.get_property('keep') == 0
ds.update(uid, {'title' : "Document 1", 'keep' : '1'})
ds.complete_indexing()
c = ds.get(uid)
assert c.get_property('keep') == 1
ds.update(uid, {'title' : "Document 1", 'keep' : '0'})
ds.complete_indexing()
c = ds.get(uid)
assert c.get_property('keep') == 0
ds.stop()
def test_randomproperty(self):
# specifying items not in the model, with and w/o type
# qualifiers
ds = DataStore()
ds.registerBackend(backingstore.FileBackingStore)
ds.mount(DEFAULT_STORE)
uid = ds.create({'title' : 'Random Extras', 'foobar' : 'baz',
'incept:date' : datetime.datetime.now().isoformat()})
ds.complete_indexing()
ds.update(uid, {'title' : 'Random Extras the sequel',
'foobar' : 'whodofoodo',
'incept:date' : datetime.datetime.now().isoformat()})
ds.complete_indexing()
# ignored w/o prefix
assert ds.find('whodofoodo')[1] == 0
# found with it
assert ds.find('foobar:whodofoodo')[1] == 1
c = ds.get_properties(uid)
assert 'foobar' in c
assert 'title' in c
# maps back w/o the type
assert 'incept' in c
ds.update(uid, {'title' : 'Random Extras the sequel',
'foobar' : '',
'incept:date' : datetime.datetime.now().isoformat()})
ds.complete_indexing()
assert ds.find('whodofoodo')[1] == 0
# found with it
assert ds.find('foobar:whodofoodo')[1] == 0
c = ds.get_properties(uid)
assert 'title' in c
# maps back w/o the type
assert 'incept' in c
assert c['foobar'] == ''
ds.stop()
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(Test))
return suite
if __name__ == "__main__":
unittest.main()
|