def test_reorder_keys(): d = OrderedDict([('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h')]) d.reorder_keys(['e', 'c', 'g', 'a']) assert_against_list(d, [('e', 'f'), ('c', 'd'), ('g', 'h'), ('a', 'b')])
class TestOrderedDict(unittest.TestCase): def setUp(self): self._test_keys = ['a', 'b', 'c'] self._test_vals = [-2, 0, 1] self.d = OrderedDict(zip(self._test_keys, self._test_vals)) def test_order(self): self.assertEquals(self.d.keys(), [key for key in self._test_keys]) def test_index(self): self.assertEquals(self.d.key_index('c'), 2) def test_reorder(self): new_order = ['c', 'a', 'b'] self.d.reorder_keys(new_order) self.assertEquals(self.d.keys(), new_order)
def test_reorder_keys_fail(): d = OrderedDict([('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h')]) with pytest.raises(ValueError): d.reorder_keys(['e', 'c', 'g', 'a', 'z']) with pytest.raises(ValueError): d.reorder_keys(['e', 'c', 'g']) with pytest.raises(ValueError): d.reorder_keys(['e', 'g', 'a', 'z']) with pytest.raises(ValueError): d.reorder_keys(['e', 'c', 'a']) with pytest.raises(ValueError): d.reorder_keys(['e', 'g', 'c', 'a', 'a'])