Exemple #1
0
    def test_pkdd_dotted_get(self):
        value = "foo"
        data = {"color": {"blue": value}}

        dotted = PreserveKeysDottedDict(data)

        self.assertEqual(dotted.get("color").get("blue"), value)
Exemple #2
0
    def test_pkdd_items_format_invocation(self):
        items = [("x", 1), ("y", 2), ("z", 3)]
        dotted = PreserveKeysDottedDict(items)

        self.assertEquals(dotted.x, 1)
        self.assertEquals(dotted.y, 2)
        self.assertEquals(dotted.z, 3)
Exemple #3
0
 def test_pkdd_to_dict(self):
     dotted = PreserveKeysDottedDict(
         PreserveKeysDottedDict({
             "a": "b",
             "c": {
                 "d": "e"
             },
             "f": [{
                 "g": "h"
             }, 1]
         }))
     my_dict = dotted.to_dict()
     self.assertEquals(dotted, my_dict)
     self.assertEqual(type(dotted), PreserveKeysDottedDict)
     self.assertEqual(type(dotted.f[0]), PreserveKeysDottedDict)
     self.assertEqual(type(my_dict), dict)
     self.assertEqual(type(my_dict["f"][0]), dict)
Exemple #4
0
    def test_pkdd_setting_and_accessing(self):
        key = "x"
        value = 5
        dotted = PreserveKeysDottedDict({key: value})

        self.assertEqual(dotted[key], value)
        self.assertEqual(dotted.x, value)
        self.assertEqual(dotted.x, dotted[key])
Exemple #5
0
 def test_pkdd_repr(self):
     dotted = PreserveKeysDottedDict({
         "foo-bar": 1,
         ".foo": 1,
         "foo baz": 1,
         1: 2
     })
     assert eval(repr(dotted)) == dotted
Exemple #6
0
 def test_preserve_keys(self):
     dotted = PreserveKeysDottedDict({
         "foo-bar": 1,
         ".foo": 1,
         "foo baz": 1,
         1: 2
     })
     self.assertEquals(dotted["foo baz"], 1)
Exemple #7
0
 def test_pkdd_not_valid_identifier(self):
     dotted = PreserveKeysDottedDict({
         "lambda": 1,
         "foo-bar": 1,
         ".foo": 1,
         "foo baz": 1,
         1: 2
     })
     self.assertTrue(dotted["lambda"], 1)
     self.assertTrue(dotted["foo-bar"], 1)
     self.assertTrue(dotted[".foo"], 1)
     self.assertTrue(dotted["foo baz"], 1)
     self.assertTrue(dotted[1], 2)
Exemple #8
0
    def test_pkdd_set_via_attrib(self):
        value = 42
        meaning = PreserveKeysDottedDict()
        meaning.oflife = value

        self.assertEqual(meaning["oflife"], value)
Exemple #9
0
 def test_pkdd_none_value(self):
     value = None
     dotted = PreserveKeysDottedDict({"foo": value})
     self.assertTrue(dotted.foo is value)
Exemple #10
0
    def test_pkdd_child_dictionary_types_accessor(self):
        dotted = PreserveKeysDottedDict({"x": {"y": {"z": 5}}})

        self.assertEqual(type(dotted["x"]), PreserveKeysDottedDict)
        self.assertEqual(type(dotted["x"]["y"]), PreserveKeysDottedDict)
Exemple #11
0
    def test_pkdd_delete(self):
        dotted = PreserveKeysDottedDict({"foo": 1})
        del dotted.foo

        self.assertRaises(KeyError)
Exemple #12
0
 def test_pkdd_copy(self):
     my_dict = PreserveKeysDottedDict({"my key two": {"b": "c", 1: 2}})
     dotted = my_dict.copy()
     self.assertEquals(my_dict, dotted)
Exemple #13
0
 def test_pkdd_construction_without_data(self):
     dotted = PreserveKeysDottedDict()
     self.assertTrue(isinstance(dotted, PreserveKeysDottedDict))
Exemple #14
0
    def test_pkdd_children_equivalence(self):
        dotted = PreserveKeysDottedDict({"x": {"y": {"z": 5}}})

        self.assertTrue(dotted.x is dotted["x"])
        self.assertTrue(dotted.x.y is dotted["x"]["y"])
Exemple #15
0
 def test_pkdd_via_zip(self):
     data = ["a", "b"]
     dotted = PreserveKeysDottedDict(zip(data, data))
     self.assertEquals(dotted.a, "a")
     self.assertEquals(dotted["b"], "b")
Exemple #16
0
    def test_pkdd_missing_attribute_error(self):
        dotted = PreserveKeysDottedDict({"foo": None})

        with self.assertRaises(AttributeError):
            dotted.missing
Exemple #17
0
    def test_pkdd_child_dictionary_types(self):
        dotted = PreserveKeysDottedDict({"x": {"y": {"z": 5}}})

        self.assertEqual(type(dotted.x), PreserveKeysDottedDict)
        self.assertEqual(type(dotted.x.y), PreserveKeysDottedDict)