~vds/desktopcouch/mergeable_list_not_default_anymore

« back to all changes in this revision

Viewing changes to desktopcouch/records/tests/test_record.py

  • Committer: vincenzo di somma
  • Date: 2010-11-02 23:10:39 UTC
  • Revision ID: vincenzo.di.somma@canonical.com-20101102231039-g0dw4hudmzppf3fr
Mergeable lists are not the default for lists anymore, plus clean up a bit

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009 Canonical Ltd.
 
1
# Copyright 2009-2010 Canonical Ltd.
2
2
#
3
3
# This file is part of desktopcouch.
4
4
#
15
15
# along with desktopcouch.  If not, see <http://www.gnu.org/licenses/>.
16
16
#
17
17
# Authors: Eric Casteleijn <eric.casteleijn@canonical.com>
 
18
#          Vincenzo Di Somma <vincenzo.di.somma@canonical.com>
18
19
 
19
20
"""Tests for the RecordDict object on which the Contacts API is built."""
20
21
 
55
56
        self.record = Record(self.dict)
56
57
 
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)
77
80
        db.delete_record(record_id)
78
81
 
79
82
    def test_delitem(self):
 
83
        """Test removing a field from the record"""
80
84
        def f(r):
81
85
            del r["_id"]
82
86
        self.assertRaises(KeyError, f, self.record)
84
88
        del self.record["a"]
85
89
 
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'])
89
94
 
90
95
    def test_setitem_internal(self):
 
96
        """Test it is not possible to set a private field on a record"""
91
97
        def f(r):
92
98
            r["_id"] = "new!"
93
99
        self.assertRaises(IllegalKeyException, f, self.record)
94
100
 
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, {})
97
104
 
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")
103
110
 
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"))
108
115
 
131
138
            my_app_data['foo'])
132
139
 
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"])
140
147
 
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"]))
144
151
 
145
152
    def test_mergeable_list_index(self):
212
219
        """Test missing data rmeoval"""
213
220
        self.assertRaises(ValueError, self.record["subfield_uuid"].remove, 
214
221
            "missing_data")
 
222
 
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)
219
227
        
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)
230
238
        
231
239
    def test_mergeable_list_pop_wrong_index(self):
232
240
        """Test pop when index is out or range."""
237
245
    
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)
242
250
        
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']])
249
 
 
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']])
 
257
 
 
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']])
256
264
 
291
299
          self.record.record_type)
292
300
 
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)
298
307
 
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")
310
320
 
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)