示例#1
0
 def test_to_dict(self):
     doc = Document({'a': 'b', 'c': 'd', 'e': [{'f': 'g'}, {'h': 'i'}]})
     output = doc.to_dict()
     self.assertEqual(doc, output)
     self.assertIsInstance(output, dict)
     self.assertIsInstance(output['e'], list)
     self.assertIsInstance(output['e'][0], dict)
     self.assertNotIsInstance(output, Document)
     self.assertNotIsInstance(output['e'], DocumentList)
     self.assertNotIsInstance(output['e'][0], Document)
示例#2
0
 def test_reset_all_changes(self):
     doc = Document({'a': 'b', 'c': 'd', 'e': [{'f': 'g'}, {'h': 'i'}]})
     doc['a'] = 'j'
     doc['e'].append({'k': 'l'})
     doc['e'][0]['f'] = 'm'
     self.assertTrue(doc.changed)
     self.assertTrue(doc['e'][0].changed)
     doc.reset_all_changes()
     self.assertEqual({}, doc.changed)
     self.assertEqual({}, doc['e'][0].changed)
     self.assertEqual({}, doc.deleted)
示例#3
0
 def test_changed_ignores_noop(self):
     doc = Document({'a': 'b', 'c': 'd'})
     self.assertEqual({}, doc.changed)
     doc['a'] = 'b'
     self.assertEqual({}, doc.changed)
     self.assertEqual({}, doc.added)
     self.assertEqual({}, doc.deleted)
示例#4
0
 def test_pickleable(self):
     doc = Document({'a': 'b', 'c': 'd', 'e': [{'f': 'g'}, {'h': 'i'}]})
     codeced = pickle.loads(pickle.dumps(doc))
     self.assertEqual(doc, codeced)
     self.assertFalse(codeced.added)
     self.assertFalse(codeced.changed)
     self.assertFalse(codeced.deleted)
示例#5
0
 def test_added(self):
     doc = Document({'a': 'b'})
     self.assertEqual({}, doc.added)
     doc['c'] = 'd'
     doc['a'] = 'e'
     self.assertEqual({'c': 'd'}, doc.added)
     self.assertEqual({'a': 'e'}, doc.changed)
示例#6
0
 def test_changed_keeps_original_previous_value(self):
     doc = Document({'a': 'b', 'c': 'd'})
     self.assertEqual({}, doc.changed)
     doc['a'] = 'e'
     doc['a'] = 'f'
     self.assertEqual({'a': ('b', 'f')}, doc.changes)
     self.assertEqual({}, doc.added)
     self.assertEqual({}, doc.deleted)
示例#7
0
 def test_change_then_del_field(self):
     doc = Document({'a': 'b', 'c': 'd'})
     self.assertEqual({}, doc.changed)
     doc['a'] = 'e'
     del doc['a']
     self.assertEqual({}, doc.changed)
     self.assertEqual({}, doc.added)
     self.assertEqual({'a': 'b'}, doc.deleted)
示例#8
0
 def test_changed_ignores_resetting_original_value(self):
     doc = Document({'a': 'b', 'c': 'd'})
     self.assertEqual({}, doc.changed)
     doc['a'] = 'e'
     doc['a'] = 'b'
     self.assertEqual({}, doc.changed)
     self.assertEqual({}, doc.added)
     self.assertEqual({}, doc.deleted)
示例#9
0
 def test_del_then_add_same_field_with_different_value_is_a_change(self):
     doc = Document({'a': 'b', 'c': 'd'})
     self.assertEqual({}, doc.changed)
     del doc['a']
     doc['a'] = 'e'
     self.assertEqual({'a': 'e'}, doc.changed)
     self.assertEqual({}, doc.added)
     self.assertEqual({}, doc.deleted)
示例#10
0
 def test_ignores_del_then_add_same_field(self):
     doc = Document({'a': 'b', 'c': 'd'})
     self.assertEqual({}, doc.changed)
     del doc['a']
     doc['a'] = 'b'
     self.assertEqual({}, doc.changed)
     self.assertEqual({}, doc.added)
     self.assertEqual({}, doc.deleted)
示例#11
0
 def test_to_dict(self):
     doc = Document({
         'a': 'b',
         'c': 'd',
         'e': [
             {'f': 'g'},
             {'h': 'i'}
         ]
     })
     output = doc.to_dict()
     self.assertEquals(doc, output)
     self.assertIsInstance(output, dict)
     self.assertIsInstance(output['e'], list)
     self.assertIsInstance(output['e'][0], dict)
     self.assertNotIsInstance(output, Document)
     self.assertNotIsInstance(output['e'], DocumentList)
     self.assertNotIsInstance(output['e'][0], Document)
示例#12
0
 def test_reset_all_changes(self):
     doc = Document({
         'a': 'b',
         'c': 'd',
         'e': [
             {'f': 'g'},
             {'h': 'i'}
         ]
     })
     doc['a'] = 'j'
     doc['e'].append({'k': 'l'})
     doc['e'][0]['f'] = 'm'
     self.assertTrue(doc.changed)
     self.assertTrue(doc['e'][0].changed)
     doc.reset_all_changes()
     self.assertEquals({}, doc.changed)
     self.assertEquals({}, doc['e'][0].changed)
     self.assertEquals({}, doc.deleted)
示例#13
0
 def test_set_dict_as_item(self):
     doc = Document()
     doc['a'] = {'b': 'c'}
     self.assertEqual({'a': {'b': 'c'}}, doc)
     self.assertIsInstance(doc['a'], Document)
     self.assertEqual(doc['a']['b'], 'c')
示例#14
0
 def test_init_with_nested_list_and_dict(self):
     doc = Document({'a': [{'b': 'c'}]})
     self.assertEqual({'a': [{'b': 'c'}]}, doc)
     self.assertIsInstance(doc['a'], DocumentList)
     self.assertIsInstance(doc['a'][0], Document)
     self.assertEqual(doc['a'][0]['b'], 'c')
示例#15
0
 def test_copies_initial_dict(self):
     spec = {'a': {'b': 'c'}}
     doc = Document(spec)
     spec['d'] = 'e'
     self.assertEqual({'a': {'b': 'c'}}, doc)
示例#16
0
 def test_equals_dict(self):
     doc = Document({'a': 'b', 'c': 'd'})
     self.assertEqual(doc, {'a': 'b', 'c': 'd'})
示例#17
0
 def test_setdefault(self):
     doc = Document({'a': 'b'})
     self.assertEqual(doc.setdefault('c', {'d': 'e'}), {'d': 'e'})
     self.assertIsInstance(doc['c'], Document)
示例#18
0
 def test_init_with_nested_dict(self):
     doc = Document({'a': {'b': 'c'}})
     self.assertEqual({'a': {'b': 'c'}}, doc)
     self.assertIsInstance(doc['a'], Document)
     self.assertEqual(doc['a']['b'], 'c')
示例#19
0
 def test_update(self):
     doc = Document()
     doc.update({'a': 'b', 'c': 'd'})
     self.assertEqual({'a': 'b', 'c': 'd'}, doc)
示例#20
0
 def test_update_with_multi_character_kwargs(self):
     doc = Document()
     doc.update({'a': 'b'}, cc='d')
     self.assertEqual({'a': 'b', 'cc': 'd'}, doc)
示例#21
0
 def test_init_with_kwargs_only(self):
     doc = Document(a='b', c='d')
     self.assertEqual({'a': 'b', 'c': 'd'}, doc)
示例#22
0
 def test_set_get(self):
     doc = Document({'a': 'b'})
     self.assertEqual('b', doc['a'])
     doc['a'] = 'c'
     self.assertEqual('c', doc['a'])
示例#23
0
 def test_delete_from_document_list(self):
     doc = Document({'a': [{'b': 'c'}]})
     del doc['a'][0]
     self.assertEqual(doc, {'a': []})
示例#24
0
 def test_append_dict_to_list(self):
     doc = Document({'a': []})
     doc['a'].append({'b': 'c'})
     self.assertEqual({'a': [{'b': 'c'}]}, doc)
     self.assertIsInstance(doc['a'][0], Document)
     self.assertEqual(doc['a'][0]['b'], 'c')
示例#25
0
 def test_changes(self):
     doc = Document({'a': 'b', 'c': 'd'})
     self.assertEqual({}, doc.changes)
     doc['a'] = 'e'
     self.assertEqual({'a': ('b', 'e')}, doc.changes)
示例#26
0
 def test_init_with_initial_keyword(self):
     doc = Document(initial={'a': 'b', 'c': 'd'})
     self.assertEqual(doc, {'a': 'b', 'c': 'd'})
示例#27
0
 def test_deleted(self):
     doc = Document({'a': 'b', 'c': 'd'})
     self.assertEqual({}, doc.deleted)
     del doc['c']
     doc['a'] = 'e'
     self.assertEqual({'c': 'd'}, doc.deleted)
示例#28
0
 def test_delete_from_document(self):
     doc = Document({'a': 'b', 'c': 'd'})
     del doc['c']
     self.assertEqual(doc, {'a': 'b'})
示例#29
0
 def test_changed_exposes_wrapped_value(self):
     doc = Document({'a': {'c': 'd'}})
     self.assertEqual({}, doc.changed)
     doc['a'] = {'e': 'f'}
     self.assertEqual({'a': {'e': 'f'}}, doc.changed)
     self.assertIsInstance(doc.changed['a'], Document)
示例#30
0
 def test_extend_doc_list(self):
     doc = Document({'a': [{'b': 'c'}]})
     doc['a'].extend([{'d': 'e'}, {'f': 'g'}])
     self.assertEqual(doc, {'a': [{'b': 'c'}, {'d': 'e'}, {'f': 'g'}]})
     self.assertIsInstance(doc['a'][1], Document)
     self.assertIsInstance(doc['a'][2], Document)
示例#31
0
 def test_init_with_kwargs(self):
     doc = Document({'a': 'b', 'c': 'd'}, e='f', g='h')
     self.assertEqual({'a': 'b', 'c': 'd', 'e': 'f', 'g': 'h'}, doc)
示例#32
0
 def test_update(self):
     doc = Document()
     doc.update({'a': 'b', 'c': 'd'})
     self.assertEquals({'a': 'b', 'c': 'd'}, doc)
示例#33
0
 def test_init_with_initial_keyword_and_kwargs(self):
     doc = Document(initial={'a': 'b', 'c': 'd'}, e='f', g='h')
     self.assertEqual(doc, {'a': 'b', 'c': 'd', 'e': 'f', 'g': 'h'})
示例#34
0
 def test_setdefault(self):
     doc = Document({'a': 'b'})
     self.assertEquals(doc.setdefault('c', {'d': 'e'}), {'d': 'e'})
     self.assertIsInstance(doc['c'], Document)
示例#35
0
 def test_update_with_multi_character_kwargs(self):
     doc = Document()
     doc.update({'a': 'b'}, cc='d')
     self.assertEquals({'a': 'b', 'cc': 'd'}, doc)