15
15
# along with desktopcouch. If not, see <http://www.gnu.org/licenses/>.
17
17
# Authors: Eric Casteleijn <eric.casteleijn@canonical.com>
18
# Vincenzo Di Somma <vincenzo.di.somma@canonical.com>
19
20
"""Tests for the RecordDict object on which the Contacts API is built."""
55
56
self.record = Record(self.dict)
57
58
def test_revision(self):
59
"""Test document always has a revision field and that the revision
60
changes when the document is updated"""
58
61
self.assertEquals(self.record.record_revision, None)
59
62
def set_rev(rec): rec.record_revision = "1"
60
63
self.assertRaises(AttributeError, set_rev, self.record)
84
88
del self.record["a"]
86
90
def test_iter(self):
91
"""Tests it is possible to iterate over the record fields"""
87
92
self.assertEquals(sorted(list(iter(self.record))),
88
93
['a', 'b', 'record_type', 'subfield', 'subfield_uuid'])
90
95
def test_setitem_internal(self):
96
"""Test it is not possible to set a private field on a record"""
93
99
self.assertRaises(IllegalKeyException, f, self.record)
95
101
def test_no_record_type(self):
102
"""Test that creating a record with no record type fails"""
96
103
self.assertRaises(NoRecordTypeSpecified, Record, {})
98
105
def test_get_item(self):
99
"Does a RecordDict basically wrap a dict properly?"
106
"""Does a RecordDict basically wrap a dict properly?"""
100
107
self.assertEqual(self.dict["a"], self.record["a"])
101
108
self.assertRaises(KeyError,
102
109
self.record.__getitem__, "application_annotations")
104
111
def test_get(self):
105
"Does a RecordDict get() work?"
112
"""Does a RecordDict get() work?"""
106
113
self.assertEqual(self.dict["a"], self.record.get("a"))
107
114
self.assertEqual(None, self.record.get("application_annotations"))
131
138
my_app_data['foo'])
133
140
def test_loads_dict_subdict(self):
134
"Are subdicts supported?"
141
"""Are subdicts supported?"""
135
142
self.assertEqual(2, len(self.record["subfield"]))
136
143
subfield_single = self.record["subfield"]
137
144
self.assertEqual(
139
146
self.dict["subfield"]["field11s"])
141
148
def test_loads_dict_multi_subdict(self):
142
"Are subdicts with multiple entries supported?"
149
"""Are subdicts with multiple entries supported?"""
143
150
self.assertEqual(2, len(self.record["subfield_uuid"]))
145
152
def test_mergeable_list_index(self):
212
219
"""Test missing data rmeoval"""
213
220
self.assertRaises(ValueError, self.record["subfield_uuid"].remove,
215
223
def test_mergeable_list_remove_last(self):
216
224
"""Test that exception is raised when removing last item."""
217
self.record["subfield_uuid"] = [1]
225
self.record["subfield_uuid"] = MergeableList.from_list([1])
218
226
self.assertRaises(ValueError, self.record["subfield_uuid"].remove, 1)
220
228
def test_mergeable_list_pop_correct_index(self):
221
229
"""Test the pop method when working with a correct index."""
222
230
value = [1, 2, 3, 4, 5]
223
self.record["subfield_uuid"] = value
231
self.record["subfield_uuid"] = MergeableList.from_list(value)
224
232
# test with negative index
225
poped_value = self.record["subfield_uuid"].pop(-2)
226
self.assertEqual(value[-2], poped_value)
233
popped_value = self.record["subfield_uuid"].pop(-2)
234
self.assertEqual(value[-2], popped_value)
227
235
# test with positive index
228
236
poped_value = self.record["subfield_uuid"].pop(1)
229
self.assertEqual(value[1], poped_value)
237
self.assertEqual(value[1], popped_value)
231
239
def test_mergeable_list_pop_wrong_index(self):
232
240
"""Test pop when index is out or range."""
238
246
def test_mergeable_list_pop_last(self):
239
247
"""Test that exception is raised when poping last item"""
240
self.record["subfield_uuid"] = [1]
248
self.record["subfield_uuid"] = MergeableList.from_list([1])
241
249
self.assertRaises(ValueError, self.record["subfield_uuid"].pop, 0)
243
def test_tuple(self):
244
"""Test assigning tuples to a key results in mergeable lists."""
245
rec = Record({'record_type': 'http://fnord.org/smorgasbord'})
246
rec['key'] = (1, 2, 3, 4)
247
self.assert_(isinstance(rec['key'], MergeableList))
248
self.assertEqual([1, 2, 3, 4], [value for value in rec['key']])
250
251
def test_list(self):
251
252
"""Test assigning lists to a key results in mergeable lists."""
252
253
rec = Record({'record_type': 'http://fnord.org/smorgasbord'})
253
254
rec['key'] = [1, 2, 3, 4]
255
self.assert_(isinstance(rec['key'], list))
256
self.assertEqual([1, 2, 3, 4], [value for value in rec['key']])
258
def test_mergeable_list(self):
259
"""Test assigning lists to a key results in mergeable lists."""
260
rec = Record({'record_type': 'http://fnord.org/smorgasbord'})
261
rec['key'] = MergeableList.from_list([1, 2, 3, 4])
254
262
self.assert_(isinstance(rec['key'], MergeableList))
255
263
self.assertEqual([1, 2, 3, 4], [value for value in rec['key']])
291
299
self.record.record_type)
293
301
def test_run_doctests(self):
302
"""Run all doc tests from here to set the proper context (ctx)"""
294
303
ctx = test_environment.test_context
295
304
globs = { "db": CouchDatabase('testing', create=True, ctx=ctx) }
296
305
results = doctest.testfile('../doc/records.txt', globs=globs)
297
306
self.assertEqual(0, results.failed)
299
308
def test_record_id(self):
309
"""Test all passible way to assign a record id"""
300
310
data = {"_id":"recordid"}
301
311
record = Record(data, record_type="url")
302
312
self.assertEqual(data["_id"], record.record_id)
309
319
Record, data, record_id=record_id, record_type="url")
311
321
def test_record_type_version(self):
322
"""Test record type version support"""
312
323
data = {"_id":"recordid"}
313
324
record1 = Record(data, record_type="url")
314
325
self.assertIs(None, record1.record_type_version)