def test_equal_to_dict(self): d = PermaDict() self.assertNotEqual(d, {4: "the number four"}) d[4] = "the number four" self.assertEqual(d, {4: "the number four"}) self.assertNotEqual(d, {4: "the number five"}) self.assertEqual(PermaDict({1: 2, 3: 4}), {1: 2, 3: 4})
def test_raise_exception(self): locations = PermaDict([('Cos', "Houston"), ('Bla', 1)]) with pytest.raises(KeyError) as e_info: locations["Cos"] = 33 assert e_info.value.args[0] == "'Cos' already in dictionary." with pytest.raises(KeyError) as update_error: locations.update(Cos=55) assert update_error.value.args[0] == "'Cos' already in dictionary."
def test_silent_flag_to_initializer(self): d = PermaDict({1: 2, 3: 4}, silent=True) d.update([(5, 6), (1, 8), (7, 8)]) self.assertEqual(d, {1: 2, 3: 4, 5: 6, 7: 8}) d[3] = 6 d[9] = 10 self.assertEqual(d, {1: 2, 3: 4, 5: 6, 7: 8, 9: 10}) e = PermaDict(silent=True, not_silent=False, super_silent=True) self.assertEqual(e, {'not_silent': False, 'super_silent': True})
def test_force_argument_to_update(self): d = PermaDict({1: 2, 3: 4}, silent=True) d.update([(5, 6), (1, 8), (7, 8)], force=True) self.assertEqual(d, {1: 8, 3: 4, 5: 6, 7: 8}) e = PermaDict() e.update(a=1, b=2, force=True) self.assertEqual(e, {'a': 1, 'b': 2})
def test_can_update_with_new_keys(self): d = PermaDict() d.update({'a': 1}) self.assertEqual(d, {'a': 1}) d.update([('b', 2)]) self.assertEqual(d, {'a': 1, 'b': 2}) d.update(c=3) self.assertEqual(d, {'a': 1, 'b': 2, 'c': 3})
def test_force_set(self): locations = PermaDict({'David': "Boston"}) locations.force_set('David', "Amsterdam") locations.force_set('Asheesh', "Boston") locations.force_set('Asheesh', "San Francisco") assert dict(locations) == { 'David': 'Amsterdam', 'Asheesh': 'San Francisco' }
def test_can_add_key(self): d = PermaDict() with self.assertRaises(KeyError): d[4] d[4] = "the number four" self.assertEqual(d[4], "the number four")
def test_force_set_method(self): d = PermaDict({1: 2, 3: 4}) d.force_set(3, 6) d.force_set(5, 6) self.assertEqual(d, {1: 2, 3: 6, 5: 6})
def test_error_when_updating_value(self): d = PermaDict({1: 2, 3: 4}) with self.assertRaises(KeyError): d.update([(5, 6), (1, 8), (7, 8)]) self.assertEqual(d, {1: 2, 3: 4, 5: 6})
def test_error_when_changing_value(self): d = PermaDict() d[4] = "the number four" with self.assertRaises(KeyError): d[4] = "the number 4" self.assertEqual(d, {4: "the number four"})
def test_can_pop_key(self): d = PermaDict() d[4] = "the number four" self.assertEqual(d, {4: "the number four"}) self.assertEqual(d.pop(4), "the number four") self.assertEqual(d, {})
def test_can_iterate(self): d = PermaDict({'a': 'b', 'c': 'd'}) self.assertEqual(set(d), {'a', 'c'})
def test_BasicPart(self): locations = PermaDict([('Cos', "Houston"), ('Bla', 1)]) assert list(locations) == ["Cos", "Bla"] assert list(locations.values()) == ["Houston", 1] assert list(locations.items()) == [('Cos', "Houston"), ('Bla', 1)]
def test_forced_update(self): locations = PermaDict({'David': "Boston"}) locations.update([('David', 'Amsterdam'), ('Asheesh', 'SF')], force=True) assert dict(locations) == {'David': 'Amsterdam', 'Asheesh': 'SF'}
def test_silent_argument(self): locations = PermaDict({'David': "Boston"}, silent=True) locations['David'] = "Amsterdam" locations['Asheesh'] = "Boston" print(dict(locations)) assert dict(locations) == {'David': 'Boston', 'Asheesh': 'Boston'}
def test_has_keys_values_and_items(self): d = PermaDict({'a': 'b', 'c': 'd'}) self.assertEqual(set(d.keys()), {'a', 'c'}) self.assertEqual(set(d.values()), {'b', 'd'}) self.assertEqual(set(d.items()), {('a', 'b'), ('c', 'd')})
def test_dict_subclass(self): locations = PermaDict([('Cos', "Houston"), ('Bla', 1)]) assert hasattr(locations, "__setitem__") assert hasattr(locations, "__getitem__") assert hasattr(locations, "force_set")