~saurabhanandiit/gtg/exportFixed

« back to all changes in this revision

Viewing changes to GTG/tests/test_syncengine.py

Merge of my work on liblarch newbase and all the backends ported to liblarch
(which mainly means porting the datastore).
One failing test, will check it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# -----------------------------------------------------------------------------
 
3
# Gettings Things Gnome! - a personal organizer for the GNOME desktop
 
4
# Copyright (c) 2008-2009 - Lionel Dricot & Bertrand Rousseau
 
5
#
 
6
# This program is free software: you can redistribute it and/or modify it under
 
7
# the terms of the GNU General Public License as published by the Free Software
 
8
# Foundation, either version 3 of the License, or (at your option) any later
 
9
# version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but WITHOUT
 
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
13
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 
14
# details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along with
 
17
# this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
# -----------------------------------------------------------------------------
 
19
 
 
20
'''
 
21
Tests for the SyncEngine class
 
22
'''
 
23
 
 
24
import unittest
 
25
import uuid
 
26
 
 
27
from GTG.backends.syncengine import SyncEngine
 
28
 
 
29
 
 
30
 
 
31
class TestSyncEngine(unittest.TestCase):
 
32
    '''
 
33
    Tests for the SyncEngine object.
 
34
    '''
 
35
    
 
36
    def setUp(self):
 
37
        self.ftp_local = FakeTaskProvider()
 
38
        self.ftp_remote = FakeTaskProvider()
 
39
        self.sync_engine = SyncEngine()
 
40
    
 
41
    def test_analyze_element_and_record_and_break_relationship(self):
 
42
        '''
 
43
        Test for the _analyze_element, analyze_remote_id, analyze_local_id,
 
44
        record_relationship, break_relationship
 
45
        '''
 
46
        #adding a new local task
 
47
        local_id = uuid.uuid4()
 
48
        self.ftp_local.fake_add_task(local_id)
 
49
        self.assertEqual(self.sync_engine.analyze_local_id(local_id, \
 
50
                       self.ftp_local.has_task, self.ftp_remote.has_task), \
 
51
                         (SyncEngine.ADD, None))
 
52
        #creating the related remote task
 
53
        remote_id = uuid.uuid4()
 
54
        self.ftp_remote.fake_add_task(remote_id)
 
55
        #informing the sync_engine about that
 
56
        self.sync_engine.record_relationship(local_id, remote_id, object())
 
57
        #verifying that it understood that
 
58
        self.assertEqual(self.sync_engine.analyze_local_id(local_id, \
 
59
                       self.ftp_local.has_task, self.ftp_remote.has_task), \
 
60
                         (SyncEngine.UPDATE, remote_id))
 
61
        self.assertEqual(self.sync_engine.analyze_remote_id(remote_id, \
 
62
                       self.ftp_local.has_task, self.ftp_remote.has_task), \
 
63
                         (SyncEngine.UPDATE, local_id))
 
64
        #and not the reverse
 
65
        self.assertEqual(self.sync_engine.analyze_remote_id(local_id, \
 
66
                       self.ftp_local.has_task, self.ftp_remote.has_task), \
 
67
                         (SyncEngine.ADD, None))
 
68
        self.assertEqual(self.sync_engine.analyze_local_id(remote_id, \
 
69
                       self.ftp_local.has_task, self.ftp_remote.has_task), \
 
70
                         (SyncEngine.ADD, None))
 
71
        #now we remove the remote task
 
72
        self.ftp_remote.fake_remove_task(remote_id)
 
73
        self.assertEqual(self.sync_engine.analyze_local_id(local_id, \
 
74
                       self.ftp_local.has_task, self.ftp_remote.has_task), \
 
75
                         (SyncEngine.REMOVE, None))
 
76
        self.sync_engine.break_relationship(local_id = local_id)
 
77
        self.assertEqual(self.sync_engine.analyze_local_id(local_id, \
 
78
                       self.ftp_local.has_task, self.ftp_remote.has_task), \
 
79
                         (SyncEngine.ADD, None))
 
80
        self.assertEqual(self.sync_engine.analyze_remote_id(remote_id, \
 
81
                       self.ftp_local.has_task, self.ftp_remote.has_task), \
 
82
                         (SyncEngine.ADD, None))
 
83
        #we add them back and remove giving the remote id as key to find what to
 
84
        #delete
 
85
        self.ftp_local.fake_add_task(local_id)
 
86
        self.ftp_remote.fake_add_task(remote_id)
 
87
        self.ftp_remote.fake_remove_task(remote_id)
 
88
        self.sync_engine.record_relationship(local_id, remote_id, object)
 
89
        self.sync_engine.break_relationship(remote_id = remote_id)
 
90
        self.assertEqual(self.sync_engine.analyze_local_id(local_id, \
 
91
                       self.ftp_local.has_task, self.ftp_remote.has_task), \
 
92
                         (SyncEngine.ADD, None))
 
93
        self.assertEqual(self.sync_engine.analyze_remote_id(remote_id, \
 
94
                       self.ftp_local.has_task, self.ftp_remote.has_task), \
 
95
                         (SyncEngine.ADD, None))
 
96
 
 
97
    def test_syncability(self):
 
98
        '''
 
99
        Test for the _analyze_element, analyze_remote_id, analyze_local_id.
 
100
        Checks that the is_syncable parameter is used correctly
 
101
        '''
 
102
        #adding a new local task unsyncable
 
103
        local_id = uuid.uuid4()
 
104
        self.ftp_local.fake_add_task(local_id)
 
105
        self.assertEqual(self.sync_engine.analyze_local_id(local_id, \
 
106
                       self.ftp_local.has_task, self.ftp_remote.has_task,
 
107
                                                           False), \
 
108
                         (None, None))
 
109
        #adding a new local task, syncable
 
110
        local_id = uuid.uuid4()
 
111
        self.ftp_local.fake_add_task(local_id)
 
112
        self.assertEqual(self.sync_engine.analyze_local_id(local_id, \
 
113
                       self.ftp_local.has_task, self.ftp_remote.has_task), \
 
114
                         (SyncEngine.ADD, None))
 
115
        #creating the related remote task
 
116
        remote_id = uuid.uuid4()
 
117
        self.ftp_remote.fake_add_task(remote_id)
 
118
        #informing the sync_engine about that
 
119
        self.sync_engine.record_relationship(local_id, remote_id, object())
 
120
        #checking that it behaves correctly with established relationships
 
121
        self.assertEqual(self.sync_engine.analyze_local_id(local_id, \
 
122
                       self.ftp_local.has_task, self.ftp_remote.has_task,
 
123
                                                          True), \
 
124
                         (SyncEngine.UPDATE, remote_id))
 
125
        self.assertEqual(self.sync_engine.analyze_local_id(local_id, \
 
126
                       self.ftp_local.has_task, self.ftp_remote.has_task,
 
127
                                                          False), \
 
128
                         (SyncEngine.LOST_SYNCABILITY, remote_id))
 
129
        self.assertEqual(self.sync_engine.analyze_remote_id(remote_id, \
 
130
                       self.ftp_local.has_task, self.ftp_remote.has_task,
 
131
                                                           True), \
 
132
                         (SyncEngine.UPDATE, local_id))
 
133
        self.assertEqual(self.sync_engine.analyze_remote_id(remote_id, \
 
134
                       self.ftp_local.has_task, self.ftp_remote.has_task,
 
135
                                                           False), \
 
136
                         (SyncEngine.LOST_SYNCABILITY, local_id))
 
137
        #now we remove the remote task
 
138
        self.ftp_remote.fake_remove_task(remote_id)
 
139
        self.assertEqual(self.sync_engine.analyze_local_id(local_id, \
 
140
                       self.ftp_local.has_task, self.ftp_remote.has_task, 
 
141
                                                          True), \
 
142
                         (SyncEngine.REMOVE, None))
 
143
        self.assertEqual(self.sync_engine.analyze_local_id(local_id, \
 
144
                       self.ftp_local.has_task, self.ftp_remote.has_task, 
 
145
                                                          False), \
 
146
                         (SyncEngine.REMOVE, None))
 
147
        self.sync_engine.break_relationship(local_id = local_id)
 
148
        self.assertEqual(self.sync_engine.analyze_local_id(local_id, \
 
149
                       self.ftp_local.has_task, self.ftp_remote.has_task,
 
150
                                                          True), \
 
151
                         (SyncEngine.ADD, None))
 
152
        self.assertEqual(self.sync_engine.analyze_local_id(local_id, \
 
153
                       self.ftp_local.has_task, self.ftp_remote.has_task,
 
154
                                                          False), \
 
155
                         (None, None))
 
156
        self.assertEqual(self.sync_engine.analyze_remote_id(remote_id, \
 
157
                       self.ftp_local.has_task, self.ftp_remote.has_task,
 
158
                                                           True), \
 
159
                         (SyncEngine.ADD, None))
 
160
        self.assertEqual(self.sync_engine.analyze_remote_id(remote_id, \
 
161
                       self.ftp_local.has_task, self.ftp_remote.has_task,
 
162
                                                           False), \
 
163
                         (None, None))
 
164
 
 
165
def test_suite():
 
166
    return unittest.TestLoader().loadTestsFromTestCase(TestSyncEngine)
 
167
 
 
168
 
 
169
class FakeTaskProvider(object):
 
170
 
 
171
    def __init__(self):
 
172
        self.dic = {}
 
173
 
 
174
    def has_task(self, tid):
 
175
        return self.dic.has_key(tid)
 
176
 
 
177
###############################################################################
 
178
### Function with the fake_ prefix are here to assist in testing, they do not
 
179
### need to be present in the real class
 
180
###############################################################################
 
181
 
 
182
    def fake_add_task(self, tid):
 
183
        self.dic[tid] = "something"
 
184
 
 
185
    def fake_get_task(self, tid):
 
186
        return self.dic[tid]
 
187
 
 
188
    def fake_remove_task(self, tid):
 
189
        del self.dic[tid]