def test_equal(): d1 = OrderedDict([('a', 'b'), ('e', 'f'), ('c', 'd'), ('g', 'h')]) d2 = OrderedDict([('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h')]) d3 = OrderedDict([('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h')]) assert not d1 == d2 assert d3 == d2
def test_key_index(): d = OrderedDict([('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h')]) assert d.key_index('a') == 0 assert d.key_index('c') == 1 assert d.key_index('e') == 2 assert d.key_index('g') == 3
def test_update_with_insert(): d = OrderedDict([('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h')]) d.insert('c', 'z', 3) assert_against_list(d, [('a', 'b'), ('e', 'f'), ('g', 'h'), ('c', 'z')])
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')])
def test_insert(): scenario = [(('a', 'b', 2), 0), (('c', 'd', 2), 1), (('e', 'f', 2), 2), (('g', 'h', 2), 2), (('i', 'j', 2), 2), (('k', 'l', 10), 5), (('m', 'n', 4), 4)] d = OrderedDict() for data, result in scenario: d.insert(*data) assert d[data[0]] == data[1] assert d.keys()[result] == data[0] assert_against_list(d, [('a', 'b'), ('c', 'd'), ('i', 'j'), ('g', 'h'), ('m', 'n'), ('e', 'f'), ('k', 'l')])
def test_get(): d = OrderedDict([('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h')]) assert d['a'] == 'b' assert d['c'] == 'd' assert d['e'] == 'f' assert d['g'] == 'h'
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_delete(): d = OrderedDict([('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h')]) del d['c'] assert_against_list(d, [('a', 'b'), ('e', 'f'), ('g', 'h')]) d['c'] = 'z' assert_against_list(d, [('a', 'b'), ('e', 'f'), ('g', 'h'), ('c', 'z')])
def groups(self): groups = OrderedDict() for compo in self.components: if getattr(compo, 'menu_group', None): groups.setdefault(compo.menu_group, {}).setdefault('components', []).append(compo) groups[compo.menu_group]['icon'] = getattr(compo, 'icon', None) groups[compo.menu_group]['type'] = 'group' group_name = compo.menu_group if type(compo.menu_group) is tuple: group_name, groups[compo.menu_group]['selection'] = group_name groups[compo.menu_group]['name'] = group_name else: groups.setdefault(compo.cid, {}).setdefault('components', []).append(compo) groups[compo.cid]['type'] = 'entry' return groups.values()
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_check(self): od = OrderedDict([('c', 1), ('b', 0), ('a', -2)]) self.assertNotEquals(self.d, od)
def test_value_check(self): od = OrderedDict([('a', -2), ('b', 0), ('c', 3)]) self.assertNotEquals(self.d, od)
def test_init(): base_list = [('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h')] d = OrderedDict(base_list) assert_against_list(d, base_list)
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'])
def test_representation(): d = OrderedDict([('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h')]) assert str(d) == "[('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h')]"