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
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
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
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
# -----------------------------------------------------------------------------
21
Tests for the SyncEngine class
27
from GTG.backends.syncengine import SyncEngine
31
class TestSyncEngine(unittest.TestCase):
33
Tests for the SyncEngine object.
37
self.ftp_local = FakeTaskProvider()
38
self.ftp_remote = FakeTaskProvider()
39
self.sync_engine = SyncEngine()
41
def test_analyze_element_and_record_and_break_relationship(self):
43
Test for the _analyze_element, analyze_remote_id, analyze_local_id,
44
record_relationship, break_relationship
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))
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
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))
97
def test_syncability(self):
99
Test for the _analyze_element, analyze_remote_id, analyze_local_id.
100
Checks that the is_syncable parameter is used correctly
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,
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,
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,
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,
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,
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,
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,
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,
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,
156
self.assertEqual(self.sync_engine.analyze_remote_id(remote_id, \
157
self.ftp_local.has_task, self.ftp_remote.has_task,
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,
166
return unittest.TestLoader().loadTestsFromTestCase(TestSyncEngine)
169
class FakeTaskProvider(object):
174
def has_task(self, tid):
175
return self.dic.has_key(tid)
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
###############################################################################
182
def fake_add_task(self, tid):
183
self.dic[tid] = "something"
185
def fake_get_task(self, tid):
188
def fake_remove_task(self, tid):