class TestValueNotExist(unittest.TestCase, ExtraAssertions): def setUp(self): self.tdict = TwoWayOrderedDict([('a', 1), ('b', 'b')]) self.value = 3 # Hold the expected dict for each test case. Used on tearDown self.expected_dict = {} def test_set_item_key_not_exist(self): self.tdict['c'] = self.value self.assertViewEqualO(self.tdict.items(), [('a', 1), ('b', 'b'), ('c', 3)]) self.expected_dict = {'a': 1, 1: 'a', 'b': 'b', 'c': 3, 3: 'c'} def test_set_item_key_exist_as_key(self): self.tdict['a'] = self.value self.assertViewEqualO(self.tdict.items(), [('a', 3), ('b', 'b')]) self.expected_dict = {'a': 3, 3: 'a', 'b': 'b'} def test_set_item_key_exist_as_value(self): self.tdict[1] = self.value self.assertViewEqualO(self.tdict.items(), [('b', 'b'), (1, 3)]) self.expected_dict = {'b': 'b', 1: 3, 3: 1} def test_set_item_key_exist_as_both(self): self.tdict['b'] = self.value self.assertViewEqualO(self.tdict.items(), [('a', 1), ('b', 3)]) self.expected_dict = {'a': 1, 1: 'a', 'b': 3, 3: 'b'}
class TestUpdate(unittest.TestCase, ExtraAssertions): """Test case for the TwoWayOrderedDict update method.""" def setUp(self): self.tdict = TwoWayOrderedDict([('a', 1), ('b', 2)]) def test_update_ordered(self): self.tdict.update([('a', 10), ('c', 3), ('d', 4), ('e', 5)]) self.assertViewEqualO(self.tdict.items(), [('a', 10), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]) def test_update_unordered(self): self.tdict.update({'a': 10, 'c': 3, 'd': 4, 'e': 5}) self.assertViewEqualU(self.tdict.items(), [('a', 10), ('b', 2), ('c', 3), ('e', 5), ('d', 4)]) def test_update_raises(self): self.assertRaises(TypeError, self.tdict.update, [('a', 10)], [('b', 20)]) def test_update_from_other(self): other = TwoWayOrderedDict([('a', 10), ('b', 20), ('c', 30)]) self.tdict.update(other) self.assertViewEqualO(self.tdict.items(), [('a', 10), ('b', 20), ('c', 30)])
class TestPopMethods(unittest.TestCase, ExtraAssertions): """Test case for the TwoWayOrderedDict pop() & popitem() methods.""" def setUp(self): self.tdict = TwoWayOrderedDict([('a', 1), ('b', 2), ('c', 3)]) def test_pop_by_key(self): self.assertEqual(self.tdict.pop('a'), 1) self.assertViewEqualO(self.tdict.items(), [('b', 2), ('c', 3)]) def test_pop_by_value(self): self.assertEqual(self.tdict.pop(1), 'a') self.assertViewEqualO(self.tdict.items(), [('b', 2), ('c', 3)]) def test_pop_raises(self): self.assertRaises(KeyError, self.tdict.pop, 'd') def test_pop_with_default_value(self): self.assertIsNone(self.tdict.pop('d', None)) def test_popitem_last(self): self.assertEqual(self.tdict.popitem(), ('c', 3)) self.assertViewEqualO(self.tdict.items(), [('a', 1), ('b', 2)]) def test_popitem_first(self): self.assertEqual(self.tdict.popitem(last=False), ('a', 1)) self.assertViewEqualO(self.tdict.items(), [('b', 2), ('c', 3)]) def test_popitem_raises(self): while self.tdict: self.tdict.popitem() self.assertRaises(KeyError, self.tdict.popitem)
class TestDelItem(unittest.TestCase, ExtraAssertions): """Test case for the TwoWayOrderedDict __delitem__ method.""" def setUp(self): self.tdict = TwoWayOrderedDict([('a', 1), ('b', 'b'), ('c', 3)]) def test_del_item_by_key(self): del self.tdict['a'] self.assertViewEqualO(self.tdict.items(), [('b', 'b'), ('c', 3)]) def test_del_item_by_value(self): del self.tdict[3] self.assertViewEqualO(self.tdict.items(), [('a', 1), ('b', 'b')]) def test_del_item_not_exist(self): self.assertRaises(KeyError, self.tdict.__delitem__, 'd') def test_del_item_key_equals_value(self): del self.tdict['b'] self.assertViewEqualO(self.tdict.items(), [('a', 1), ('c', 3)])
class TestSetDefault(unittest.TestCase, ExtraAssertions): """Test case for the TwoWayOrderedDict setdefault method.""" def setUp(self): self.tdict = TwoWayOrderedDict([('a', 1), ('b', 2)]) def test_setdefault_by_key(self): self.assertEqual(self.tdict.setdefault('a'), 1) def test_setdefault_by_value(self): self.assertEqual(self.tdict.setdefault(1), 'a') def test_setdefault_not_exist(self): self.assertIsNone(self.tdict.setdefault('c')) self.assertEqual(self.tdict.setdefault('d', 'd'), 'd') self.assertViewEqualO(self.tdict.items(), [('a', 1), ('b', 2), ('c', None), ('d', 'd')])
def test_init_unordered_dict(self): tdict = TwoWayOrderedDict({'a': 1, 'b': 2, 'c': 3}) self.assertViewEqualU(tdict.items(), [('a', 1), ('c', 3), ('b', 2)])
def test_init_unordered_kwargs(self): tdict = TwoWayOrderedDict(a=1, b=2, c=3) self.assertViewEqualU(tdict.items(), [('a', 1), ('c', 3), ('b', 2)])
def test_init_ordered(self): tdict = TwoWayOrderedDict([('a', 1), ('b', 2), ('c', 3)]) self.assertViewEqualO(tdict.items(), [('a', 1), ('b', 2), ('c', 3)])