def test_add_observer(self):
     """ Confirm observers are added as expected """
     o_dict = ObservableDictionary()
     mock_method = Mock()
     o_dict.add_observer(mock_method)
     self.assertSetEqual({mock_method}, o_dict.observers)
     self.assertEqual(1, len(o_dict.observers))
 def test_iteration(self):
     """ Confirm iteration functions as expected """
     test_data = {'key': 'value'}
     o_dict = ObservableDictionary(test_data)
     for key, value in o_dict.items():
         self.assertEqual('key', key)
         self.assertEqual('value', value)
 def test_del_absent_item(self):
     """ Confirm behavior when an absent item is deleted """
     mock_method = Mock()
     o_dict = ObservableDictionary()
     o_dict.add_observer(mock_method)
     with self.assertRaises(KeyError):
         del o_dict['not real']
     mock_method.assert_not_called()
 def test_del_existing_item(self):
     """ Confirm behavior when an item is deleted """
     mock_method = Mock()
     o_dict = ObservableDictionary()
     o_dict['key'] = 'value'
     self.assertDictEqual({'key': 'value'}, dict(o_dict))
     o_dict.add_observer(mock_method)
     del o_dict['key']
     self.assertDictEqual({}, dict(o_dict))
     mock_method.assert_called_once_with(o_dict)
 def test_update(self):
     """ Confirm update functions as expected """
     test_data = {'key': 'value'}
     o_dict = ObservableDictionary({'a': 'b'})
     mock_method = Mock()
     o_dict.add_observer(mock_method)
     o_dict.update(test_data)
     mock_method.assert_called_once_with(o_dict)
     self.assertDictEqual({'key': 'value', 'a': 'b'}, dict(o_dict))
 def test_notify_observers(self):
     """ Confirm observers are notified as expected"""
     o_dict = ObservableDictionary()
     mock_method = Mock()
     o_dict.add_observer(mock_method)
     o_dict['key'] = 'value'
     mock_method.assert_called_once_with(o_dict)
     o_dict.get('key')
     mock_method.assert_called_once_with(o_dict)
 def test_remove_absent_observer(self):
     """ Confirm behavior upon attempt to remove an absent observer """
     o_dict = ObservableDictionary()
     mock_method = Mock()
     o_dict.add_observer(mock_method)
     with self.assertRaises(KeyError):
         o_dict.remove_observer('not real')
     self.assertSetEqual({mock_method}, o_dict.observers)
     mock_method.assert_not_called()
 def test_set_get_item(self):
     """ Confirm an inserted item is accessible """
     o_dict = ObservableDictionary()
     o_dict['key'] = 'value'
     self.assertListEqual(['key'], list(o_dict.keys()))
     self.assertListEqual(['value'], list(o_dict.values()))
     self.assertDictEqual({'key': 'value'}, dict(o_dict))
     self.assertEqual(1, len(o_dict))
     self.assertEqual('value', o_dict.get('key'))
     self.assertIsNone(o_dict.get('not real'))
 def test_basic_init(self):
     """ Confirm we can initialise successfully """
     o_dict = ObservableDictionary()
     self.assertIsInstance(o_dict, ObservableDictionary)
 def test_str(self):
     """ Test conversion to a standard dictionary """
     std_dict = {'key': 'value'}
     o_dict = ObservableDictionary(std_dict)
     self.assertEqual(str(std_dict), str(o_dict))
 def test_init(self):
     """ Confirm we can initialise successfully """
     test_data = {'key': 'value'}
     o_dict = ObservableDictionary(test_data)
     self.assertIsInstance(o_dict, ObservableDictionary)
     self.assertDictEqual(test_data, dict(o_dict))
 def test_dict_conversion(self):
     """ Test conversion to a standard dictionary """
     o_dict = ObservableDictionary()
     o_dict['key'] = 'value'
     self.assertDictEqual({"key": "value"}, o_dict._dict)
     self.assertDictEqual({"key": "value"}, dict(o_dict))