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

import os
import socket
from os.path import exists, abspath

from __init__ import tests

from active_toolkit import sockets, coroutine
from sugar_network.resources.report import Report
from sugar_network import IPCClient


class HomeMountTest(tests.Test):

    def test_create(self):
        self.start_server()
        local = IPCClient(mountpoint='~')

        guid = local.post(['context'], {
            'type': 'activity',
            'title': 'title',
            'summary': 'summary',
            'description': 'description',
            })
        self.assertNotEqual(None, guid)

        res = local.get(['context', guid], reply=['guid', 'title', 'keep', 'keep_impl', 'position'])
        self.assertEqual(guid, res['guid'])
        self.assertEqual('title', res['title'])
        self.assertEqual(False, res['keep'])
        self.assertEqual(0, res['keep_impl'])
        self.assertEqual([-1, -1], res['position'])

    def test_update(self):
        self.start_server()
        local = IPCClient(mountpoint='~')

        guid = local.post(['context'], {
            'type': 'activity',
            'title': 'title',
            'summary': 'summary',
            'description': 'description',
            })

        local.put(['context', guid], {
            'title': 'title_2',
            'keep': True,
            'position': (2, 3),
            })

        context = local.get(['context', guid], reply=['title', 'keep', 'position'])
        self.assertEqual('title_2', context['title'])
        self.assertEqual(True, context['keep'])
        self.assertEqual([2, 3], context['position'])

    def test_find(self):
        self.start_server()
        local = IPCClient(mountpoint='~')

        guid_1 = local.post(['context'], {
            'type': 'activity',
            'title': 'title_1',
            'summary': 'summary',
            'description': 'description',
            })
        guid_2 = local.post(['context'], {
            'type': 'activity',
            'title': 'title_2',
            'summary': 'summary',
            'description': 'description',
            })
        guid_3 = local.post(['context'], {
            'type': 'activity',
            'title': 'title_3',
            'summary': 'summary',
            'description': 'description',
            })

        cursor = local.get(['context'], reply=['guid', 'title', 'keep', 'keep_impl', 'position'])
        self.assertEqual(3, cursor['total'])
        self.assertEqual(
                sorted([
                    (guid_1, 'title_1', False, 0, [-1, -1]),
                    (guid_2, 'title_2', False, 0, [-1, -1]),
                    (guid_3, 'title_3', False, 0, [-1, -1]),
                    ]),
                sorted([(i['guid'], i['title'], i['keep'], i['keep_impl'], i['position']) for i in cursor['result']]))

    def test_upload_blob(self):
        self.start_server()
        local = IPCClient(mountpoint='~')

        guid = local.post(['context'], {
            'type': 'activity',
            'title': 'title',
            'summary': 'summary',
            'description': 'description',
            })

        self.touch(('file', 'blob'))
        local.put(['context', guid, 'preview'], cmd='upload_blob', path=abspath('file'))
        blob = local.get(['context', guid, 'preview'], cmd='get_blob')
        self.assertEqual('blob', file(blob['path']).read())

        self.touch(('file2', 'blob2'))
        local.put(['context', guid, 'preview'], cmd='upload_blob', path=abspath('file2'), pass_ownership=True)
        blob = local.get(['context', guid, 'preview'], cmd='get_blob')
        self.assertEqual('blob2', file(blob['path']).read())
        assert not exists('file2')

    def test_GetAbsetnBLOB(self):
        self.start_server([Report])
        local = IPCClient(mountpoint='~')

        guid = local.post(['report'], {
            'context': 'context',
            'implementation': 'implementation',
            'description': 'description',
            })

        self.assertEqual(None, local.get(['report', guid, 'data'], cmd='get_blob'))

    def test_GetDefaultBLOB(self):
        self.start_server()
        local = IPCClient(mountpoint='~')

        guid = local.post(['context'], {
            'type': 'activity',
            'title': 'title',
            'summary': 'summary',
            'description': 'description',
            })

        blob = local.get(['context', guid, 'icon'], cmd='get_blob')
        assert blob['path'].endswith('missing.png')
        assert exists(blob['path'])

    def test_Subscription(self):
        self.start_server()
        local = IPCClient(mountpoint='~')
        events = []

        def read_events():
            for event in local.subscribe():
                if 'props' in event:
                    event.pop('props')
                events.append(event)
        job = coroutine.spawn(read_events)

        guid = local.post(['context'], {
            'type': 'activity',
            'title': 'title',
            'summary': 'summary',
            'description': 'description',
            })
        coroutine.dispatch()
        local.put(['context', guid], {
            'title': 'title_2',
            })
        coroutine.dispatch()
        local.delete(['context', guid])
        coroutine.sleep(.5)
        job.kill()

        self.assertEqual([
            {'guid': guid, 'seqno': 1, 'document': 'context', 'event': 'create'},
            {'guid': guid, 'seqno': 2, 'document': 'context', 'event': 'update', 'mountpoint': '~'},
            {'guid': guid, 'event': 'delete', 'document': 'context', 'mountpoint': '~'},
            ],
            events)


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