Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests/units/datastore.py
blob: de1e67d3711447ed9185fb92338a00fb5e073411 (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
#!/usr/bin/env python
# sugar-lint: disable

import os
from os.path import exists, join

from __init__ import tests

import active_document as ad
from sugar_network import local
from sugar_network.toolkit import sugar
from sugar_network.local import datastore
from sugar_network.resources.artifact import Artifact
from active_toolkit import coroutine


class DatastoreTest(tests.Test):

    def test_populate_NothingToImport(self):
        self.create_mountset([Artifact])
        artifacts = self.mounts.volume['artifact']
        ds_create('1', uid='1')

        datastore.populate(artifacts)
        assert not exists(sn_stamp())
        self.assertEqual(0, artifacts.find()[1].value)

        self.touch(sn_stamp())
        datastore.populate(artifacts)
        assert not exists(sn_stamp())
        self.assertEqual(0, artifacts.find()[1].value)

        self.touch(sn_stamp())
        os.utime(sn_stamp(), (0, 0))
        self.touch(ds_stamp())
        os.utime(ds_stamp(), (0, 0))
        datastore.populate(artifacts)
        assert exists(sn_stamp())
        assert exists(ds_stamp())
        self.assertEqual(0, artifacts.find()[1].value)

    def test_populate(self):
        self.create_mountset([Artifact])
        artifacts = self.mounts.volume['artifact']

        ds_create('1',
                data='data-1',
                activity='activity-1',
                activity_id='activity_id-1',
                creation_time='1',
                description='description-1',
                keep='0',
                mime_type='mime_type-1',
                mtime='fake',
                tags='tag1 tag2 tag3',
                timestamp='11',
                title='title-1',
                filesize='1',
                preview='preview-1',
                title_set_by_user='1',
                prop='value-1',
                )
        ds_create('2',
                data='data-2',
                activity='activity-2',
                activity_id='activity_id-2',
                creation_time='3',
                description='description-2',
                keep='1',
                mime_type='mime_type-2',
                mtime='fake',
                tags='tag4 tag5',
                timestamp='4',
                title='title-2',
                filesize='2',
                preview='preview-2',
                title_set_by_user='2',
                prop='value-2',
                )

        self.touch(ds_stamp())
        datastore.populate(artifacts)
        assert exists(sn_stamp())
        assert os.stat(ds_stamp()).st_mtime == os.stat(sn_stamp()).st_mtime
        self.assertEqual(
                sorted(['1', '2']),
                sorted([i.guid for i in artifacts.find()[0]]))

        self.assertEqual({
            'guid': '1',
            'context': 'activity-1',
            'activity_id': 'activity_id-1',
            'ctime': 1,
            'description': {'en': 'description-1'},
            'keep': False,
            'mime_type': 'mime_type-1',
            'tags': ['tag1', 'tag2', 'tag3'],
            'timestamp': 11,
            'mtime': 0,
            'title': {'en': 'title-1'},
            'filesize': 1,
            'traits': {'title_set_by_user': '1', 'prop': 'value-1'},
            'layer': ['public'],
            'user': [],
            'author': [],
            },
            artifacts.get('1').properties(['guid', 'context', 'activity_id', 'ctime', 'description', 'keep', 'mime_type', 'tags', 'timestamp', 'mtime', 'title', 'filesize', 'traits', 'layer', 'user', 'author']))
        self.assertEqual(
                'preview-1',
                file(artifacts.get('1').meta('preview')['path']).read())
        self.assertEqual(
                'data-1',
                file(artifacts.get('1').meta('data')['path']).read())

        self.assertEqual({
            'guid': '2',
            'context': 'activity-2',
            'activity_id': 'activity_id-2',
            'ctime': 3,
            'description': {'en': 'description-2'},
            'keep': True,
            'mime_type': 'mime_type-2',
            'tags': ['tag4', 'tag5'],
            'timestamp': 4,
            'mtime': 0,
            'title': {'en': 'title-2'},
            'filesize': 2,
            'traits': {'title_set_by_user': '2', 'prop': 'value-2'},
            'layer': ['public'],
            'user': [],
            'author': [],
            },
            artifacts.get('2').properties(['guid', 'context', 'activity_id', 'ctime', 'description', 'keep', 'mime_type', 'tags', 'timestamp', 'mtime', 'title', 'filesize', 'traits', 'layer', 'user', 'author']))
        self.assertEqual(
                'preview-2',
                file(artifacts.get('2').meta('preview')['path']).read())
        self.assertEqual(
                'data-2',
                file(artifacts.get('2').meta('data')['path']).read())


def sn_stamp():
    return local.path('datastore.index_updated')


def ds_stamp():
    return sugar.profile_path('datastore', 'index_updated')


def ds_create(guid, data=None, **props):
    root = sugar.profile_path('datastore', guid[:2], guid, 'metadata', '')

    for key, value in props.items():
        with file(join(root, key), 'wb') as f:
            f.write(value)

    if data is not None:
        with file(join(root, '..', 'data'), 'w') as f:
            f.write(data)


if __name__ == '__main__':
    tests.main()