Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests/units/dbus_datastore.py
blob: bbf8bcc92aaa28edad223ba2f4883eaf2968fdb6 (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/env python
# sugar-lint: disable

import os
import sys
import time
from os.path import exists, abspath, dirname

arg0 = abspath(__file__).replace('.pyc', '.py')

import dbus
import gobject
from dbus.mainloop.glib import threads_init, DBusGMainLoop

from __init__ import tests

import active_document as ad
from sugar_network.resources.volume import Volume
from sugar_network.resources.artifact import Artifact
from sugar_network.local.mounts import HomeMount
from sugar_network.local.dbus_datastore import Datastore
from sugar_network.toolkit import dbus_thread
from active_toolkit import coroutine


gobject.threads_init()
threads_init()
DBusGMainLoop(set_as_default=True)


class DbusDatastoreTest(tests.Test):

    def setUp(self, fork_num=0):
        tests.Test.setUp(self, fork_num)

        if fork_num:
            return

        self.fork(os.execvp, arg0, [arg0, self.id().split('.')[-1], 'fork'])
        self.ds = dbus.Interface(
                dbus.SessionBus().get_object(
                    'org.laptop.sugar.DataStore',
                    '/org/laptop/sugar/DataStore'),
                'org.laptop.sugar.DataStore')

    def test_create_Empty(self):
        guid = self.ds.create({}, '', False, timeout=3)
        self.assertEqual(5, len(guid.split('-')))
        self.assertEqual({
            'uid': guid,
            'activity': '',
            'keep': '0',
            'mime_type': '',
            'title': 'Unnamed',
            'description': '',
            'activity_id': '',
            'filesize': '0',
            'creation_time': '1',
            'timestamp': '1',
            'mtime': '1970-01-01T00:00:01',
            'tags': '',
            },
            self.ds.get_properties(guid, timeout=3))

    def test_create_SNProps(self):
        guid = self.ds.create({
            'activity': 'activity',
            'activity_id': 'activity_id',
            'creation_time': '-1',
            'description': 'description',
            'keep': '1',
            'mime_type': 'mime_type',
            'mtime': '-1',
            'tags': 'tags',
            'timestamp': '-1',
            'title': 'title',
            'filesize': '-1',
            },
            '', False, timeout=3)

        self.assertEqual({
            'uid': guid,
            'activity': 'activity',
            'keep': '1',
            'mime_type': 'mime_type',
            'title': 'title',
            'description': 'description',
            'activity_id': 'activity_id',
            'filesize': '0',
            'creation_time': '1',
            'timestamp': '-1',
            'mtime': '1970-01-01T00:00:01',
            'tags': 'tags',
            },
            self.ds.get_properties(guid, timeout=3))

    def test_create_ExtraProps(self):
        guid = self.ds.create({
            'share-scope': 'share-scope',
            'title_set_by_user': '1',
            'arbitrary_prop': 'arbitrary_value',
            },
            '', False, timeout=3)

        self.assertEqual({
            'uid': guid,
            'activity': '',
            'keep': '0',
            'mime_type': '',
            'title': 'Unnamed',
            'description': '',
            'activity_id': '',
            'filesize': '0',
            'creation_time': '1',
            'timestamp': '1',
            'mtime': '1970-01-01T00:00:01',
            'tags': '',
            'share-scope': 'share-scope',
            'title_set_by_user': '1',
            'arbitrary_prop': 'arbitrary_value',
            },
            self.ds.get_properties(guid, timeout=3))

    def test_update_SNProps(self):
        guid = self.ds.create({}, '', False, timeout=3)

        props = self.ds.get_properties(guid, timeout=3)
        self.assertEqual('', props['activity'])
        self.assertEqual('0', props['keep'])

        self.ds.update(guid, {'activity': 'activity-1', 'keep': '1'}, '', False, timeout=3)

        props = self.ds.get_properties(guid, timeout=3)
        self.assertEqual('activity-1', props['activity'])
        self.assertEqual('1', props['keep'])

        self.ds.update(guid, {'activity': 'activity-2', 'keep': '0'}, '', False, timeout=3)

        props = self.ds.get_properties(guid, timeout=3)
        self.assertEqual('activity-2', props['activity'])
        self.assertEqual('0', props['keep'])

    def test_update_ExtraProps(self):
        guid = self.ds.create({}, '', False, timeout=3)

        props = self.ds.get_properties(guid, timeout=3)
        self.assertEqual(None, props.get('prop_1'))
        self.assertEqual(None, props.get('prop_2'))

        self.ds.update(guid, {'prop_1': 'value_1', 'prop_2': 'value_2'}, '', False, timeout=3)

        props = self.ds.get_properties(guid, timeout=3)
        self.assertEqual('value_1', props.get('prop_1'))
        self.assertEqual('value_2', props.get('prop_2'))

        self.ds.update(guid, {'prop_1': 'value_3', 'prop_2': 'value_4'}, '', False, timeout=3)

        props = self.ds.get_properties(guid, timeout=3)
        self.assertEqual('value_3', props.get('prop_1'))
        self.assertEqual('value_4', props.get('prop_2'))

    def test_update_TryToUpdateGuid(self):
        guid = self.ds.create({}, '', False, timeout=3)

        self.ds.update(guid, {'uid': 'new', 'prop': 'value'}, '', False, timeout=3)

        props = self.ds.get_properties(guid, timeout=3)
        self.assertEqual(guid, props.get('uid'))
        self.assertEqual('value', props.get('prop'))

    def test_find(self):
        entries, total = self.ds.find({}, ['uid', 'title', 'term'], timeout=3)
        self.assertEqual(0, total)
        self.assertEqual(
                sorted([
                    ]),
                entries)

        guid_1 = self.ds.create({'title': 'title-1', 'term': 'value-1'}, '', False, timeout=3)
        guid_2 = self.ds.create({'title': 'title-2', 'term': 'value-2'}, '', False, timeout=3)
        guid_3 = self.ds.create({'title': 'title-3', 'term': 'value-3'}, '', False, timeout=3)

        # All entries
        entries, total = self.ds.find({}, ['uid', 'title', 'term'], timeout=3)
        self.assertEqual(3, total)
        self.assertEqual(
                sorted([
                    {'uid': guid_1, 'title': 'title-1', 'term': 'value-1'},
                    {'uid': guid_2, 'title': 'title-2', 'term': 'value-2'},
                    {'uid': guid_3, 'title': 'title-3', 'term': 'value-3'},
                    ]),
                sorted(entries))

        # offset/limit
        entries, total = self.ds.find({'offset': 0, 'limit': 2}, ['uid', 'title', 'term'], timeout=3)
        self.assertEqual(3, total)
        self.assertEqual(2, len(entries))
        entries, total = self.ds.find({'offset': 2, 'limit':2}, ['uid', 'title', 'term'], timeout=3)
        self.assertEqual(3, total)
        self.assertEqual(1, len(entries))

        # fulltext search
        entries, total = self.ds.find({'query': 'title'}, ['uid', 'title', 'term'], timeout=3)
        self.assertEqual(3, total)
        self.assertEqual(
                sorted([
                    {'uid': guid_1, 'title': 'title-1', 'term': 'value-1'},
                    {'uid': guid_2, 'title': 'title-2', 'term': 'value-2'},
                    {'uid': guid_3, 'title': 'title-3', 'term': 'value-3'},
                    ]),
                sorted(entries))
        entries, total = self.ds.find({'query': 'title-2'}, ['uid', 'title', 'term'], timeout=3)
        self.assertEqual(1, total)
        self.assertEqual(
                sorted([
                    {'uid': guid_2, 'title': 'title-2', 'term': 'value-2'},
                    ]),
                sorted(entries))

        # search by properties
        entries, total = self.ds.find({'title': 'title-1'}, ['uid', 'title', 'term'], timeout=3)
        self.assertEqual(1, total)
        self.assertEqual(
                sorted([
                    {'uid': guid_1, 'title': 'title-1', 'term': 'value-1'},
                    ]),
                sorted(entries))

        # search by guid
        entries, total = self.ds.find({'uid': guid_3}, ['uid', 'title', 'term'], timeout=3)
        self.assertEqual(1, total)
        self.assertEqual(
                sorted([
                    {'uid': guid_3, 'title': 'title-3', 'term': 'value-3'},
                    ]),
                sorted(entries))

        # order by mapped property
        entries, total = self.ds.find({'order_by': 'uid'}, ['uid'], timeout=3)
        self.assertEqual(3, total)
        self.assertEqual(
                sorted([{'uid': guid_1}, {'uid': guid_2}, {'uid': guid_3}]),
                entries)
        entries, total = self.ds.find({'order_by': '-uid'}, ['uid'], timeout=3)
        self.assertEqual(3, total)
        self.assertEqual(
                [i for i in sorted([{'uid': guid_1}, {'uid': guid_2}, {'uid': guid_3}])],
                entries)

        # order by not mapped property
        entries, total = self.ds.find({'order_by': 'title'}, ['uid'], timeout=3)
        self.assertEqual(3, total)
        self.assertEqual(
                [{'uid': guid_1}, {'uid': guid_2}, {'uid': guid_3}],
                entries)
        entries, total = self.ds.find({'order_by': ['+title']}, ['uid'], timeout=3)
        self.assertEqual(3, total)
        self.assertEqual(
                [{'uid': guid_3}, {'uid': guid_2}, {'uid': guid_1}],
                entries)
        entries, total = self.ds.find({'order_by': '-title'}, ['uid'], timeout=3)
        self.assertEqual(3, total)
        self.assertEqual(
                [{'uid': guid_1}, {'uid': guid_2}, {'uid': guid_3}],
                entries)

    def test_get_uniquevaluesfor(self):
        guid_1 = self.ds.create({'title': '1', 'description': '3'}, '', False, timeout=3)
        guid_2 = self.ds.create({'title': '1', 'description': '3'}, '', False, timeout=3)
        guid_3 = self.ds.create({'title': '2', 'description': '4'}, '', False, timeout=3)

        self.assertEqual(
                sorted(['1', '2']),
                sorted(self.ds.get_uniquevaluesfor('title', {})))
        self.assertEqual(
                sorted(['3', '4']),
                sorted(self.ds.get_uniquevaluesfor('description', {})))
        self.assertEqual(
                sorted(['1']),
                sorted(self.ds.get_uniquevaluesfor('title', {'title': '1'})))

    def test_BLOBs(self):
        guid = self.ds.create({'preview': 'preview-1'}, '', False, timeout=3)
        self.assertEqual('preview-1', self.ds.get_properties(guid, timeout=3, byte_arrays=True)['preview'])
        self.ds.update(guid, {'preview': 'preview-2'}, '', False, timeout=3)
        self.assertEqual('preview-2', self.ds.get_properties(guid, timeout=3, byte_arrays=True)['preview'])

    def test_Data(self):
        data_1 = 'data-1'
        self.touch(('file-1', data_1))
        guid_1 = self.ds.create({}, tests.tmpdir + '/file-1', False, timeout=3)
        assert exists(tests.tmpdir + '/file-1')
        self.assertEqual(str(len(data_1)), self.ds.get_properties(guid_1, timeout=3, byte_arrays=True)['filesize'])
        data_path = self.ds.get_filename(guid_1)
        self.assertEqual(tests.tmpdir + '/.sugar/default/data', dirname(data_path))
        self.assertEqual(data_1, file(data_path).read())

        data_2 = 'data-2'
        self.touch(('file-2', data_2))
        self.ds.update(guid_1, {}, tests.tmpdir + '/file-2', False, timeout=3)
        assert exists(tests.tmpdir + '/file-2')
        self.assertEqual(str(len(data_2)), self.ds.get_properties(guid_1, timeout=3, byte_arrays=True)['filesize'])
        data_path = self.ds.get_filename(guid_1)
        self.assertEqual(tests.tmpdir + '/.sugar/default/data', dirname(data_path))
        self.assertEqual(data_2, file(data_path).read())

        self.touch(('file-1', data_1))
        guid_2 = self.ds.create({}, tests.tmpdir + '/file-1', True, timeout=3)
        assert not exists(tests.tmpdir + '/file-1')
        self.assertEqual(data_1, file(self.ds.get_filename(guid_2)).read())

        self.touch(('file-2', data_2))
        self.ds.update(guid_2, {}, tests.tmpdir + '/file-2', True, timeout=3)
        assert not exists(tests.tmpdir + '/file-2')
        self.assertEqual(data_2, file(self.ds.get_filename(guid_2)).read())

    def test_Delete(self):
        guid_1 = self.ds.create({}, '', False, timeout=3)
        guid_2 = self.ds.create({}, '', False, timeout=3)
        guid_3 = self.ds.create({}, '', False, timeout=3)

        self.assertEqual(
                sorted([{'uid': guid_1}, {'uid': guid_2}, {'uid': guid_3}]),
                sorted(self.ds.find({}, ['uid'], timeout=3)[0]))

        self.ds.delete(guid_2)

        self.assertEqual(
                sorted([{'uid': guid_1}, {'uid': guid_3}]),
                sorted(self.ds.find({}, ['uid'], timeout=3)[0]))

        self.ds.delete(guid_3)

        self.assertEqual(
                sorted([{'uid': guid_1}]),
                sorted(self.ds.find({}, ['uid'], timeout=3)[0]))

        self.ds.delete(guid_1)

        self.assertEqual(
                sorted([]),
                sorted(self.ds.find({}, ['uid'], timeout=3)[0]))

    def test_Events(self):
        Created = []
        Updated = []
        Deleted = []

        self.ds.connect_to_signal('Created', Created.append)
        self.ds.connect_to_signal('Updated', Updated.append)
        self.ds.connect_to_signal('Deleted', Deleted.append)

        guid = self.ds.create({}, '', False, timeout=3)
        self.ds.update(guid, {}, '', False, timeout=3)
        self.ds.delete(guid, timeout=3)

        mainloop = gobject.MainLoop()
        gobject.timeout_add(1000, mainloop.quit)
        mainloop.run()

        self.assertEqual([guid], Created)
        self.assertEqual([guid], Updated)
        self.assertEqual([guid], Deleted)


if __name__ == '__main__':
    if len(sys.argv) > 1 and sys.argv[-1] == 'fork':
        self = DbusDatastoreTest(sys.argv[1])
        self.setUp(fork_num=1)
        self.override(time, 'time', lambda: 1)
        self.create_mountset([Artifact])
        dbus_thread.spawn_service(Datastore)
        dbus_thread.start(self.mounts)
    else:
        tests.main()