Example #1
0
    def test_resolve_jsonpointer_empty_path(self):
        doc = {}
        path = ""
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), doc)

        doc = {"foo": 1}
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), doc)
Example #2
0
    def test_resolve_jsonpointer_empty_path(self):
        doc = {}
        path = ''
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), doc)

        doc = {'foo': 1}
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), doc)
Example #3
0
 def test_set_jsonpointer_append_path_preserves_intermediate(self):
     doc = {'foo': {'bar': 1}, 1: 2}
     path = '/foo/foo2'
     value = 'value'
     pandata.set_jsonpointer(doc, path, value)
     print(doc)
     self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
     self.assertEqual(doc[1], 2)
     self.assertEqual(pandata.resolve_jsonpointer(doc, '/foo/bar'), 1)
Example #4
0
 def test_set_jsonpointer_append_path_preserves_intermediate(self):
     doc = {"foo": {"bar": 1}, 1: 2}
     path = "/foo/foo2"
     value = "value"
     pandata.set_jsonpointer(doc, path, value)
     print(doc)
     self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
     self.assertEqual(doc[1], 2)
     self.assertEqual(pandata.resolve_jsonpointer(doc, "/foo/bar"), 1)
Example #5
0
    def test_resolve_jsonpointer_existing(self):
        doc = {"foo": 1, "bar": [11, {"a": 222}]}

        path = "/foo"
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), 1)

        path = "/bar/0"
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), 11)

        path = "/bar/1/a"
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), 222)
Example #6
0
    def test_resolve_jsonpointer_root_path_only(self):
        doc = {}
        path = "/"
        with self.assertRaises(RefResolutionError):
            self.assertEqual(pandata.resolve_jsonpointer(doc, path), doc)

        doc = {"foo": 1}
        with self.assertRaises(RefResolutionError):
            self.assertEqual(pandata.resolve_jsonpointer(doc, path), doc)

        doc = {"": 1}
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), doc[""])
Example #7
0
    def test_set_jsonpointer_empty_doc(self):
        doc = {}
        path = "/foo"
        value = "value"
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)

        doc = {}
        path = "/foo/bar"
        value = "value"
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
Example #8
0
    def test_set_jsonpointer_sequence(self):
        doc = [1, 2]
        path = '/1'
        value = 'value'
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)

        doc = [1, 2]
        path = '/1/foo/bar'
        value = 'value'
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
Example #9
0
    def test_resolve_jsonpointer_root_path_only(self):
        doc = {}
        path = '/'
        with self.assertRaises(RefResolutionError):
            self.assertEqual(pandata.resolve_jsonpointer(doc, path), doc)

        doc = {'foo': 1}
        with self.assertRaises(RefResolutionError):
            self.assertEqual(pandata.resolve_jsonpointer(doc, path), doc)

        doc = {'': 1}
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), doc[''])
Example #10
0
    def test_set_jsonpointer_empty_doc(self):
        doc = {}
        path = '/foo'
        value = 'value'
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)

        doc = {}
        path = '/foo/bar'
        value = 'value'
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
Example #11
0
    def test_set_jsonpointer_sequence(self):
        doc = [1, 2]
        path = "/1"
        value = "value"
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)

        doc = [1, 2]
        path = "/1/foo/bar"
        value = "value"
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
Example #12
0
    def test_resolve_jsonpointer_sequence(self):
        doc = [1, [22, 33]]

        path = "/0"
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), 1)

        path = "/1"
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), [22, 33])

        path = "/1/0"
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), 22)
        path = "/1/1"
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), 33)
Example #13
0
    def test_resolve_jsonpointer_sequence(self):
        doc = [1, [22, 33]]

        path = '/0'
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), 1)

        path = '/1'
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), [22, 33])

        path = '/1/0'
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), 22)
        path = '/1/1'
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), 33)
Example #14
0
    def test_set_jsonpointer_missing(self):
        doc = {"foo": 1, 1: 2}
        path = "/foo/bar"
        value = "value"
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)

        doc = {"foo": 1, 1: 2}
        path = "/foo/bar/some/other"
        value = "value"
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)
Example #15
0
    def test_set_jsonpointer_missing(self):
        doc = {'foo': 1, 1: 2}
        path = '/foo/bar'
        value = 'value'
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)

        doc = {'foo': 1, 1: 2}
        path = '/foo/bar/some/other'
        value = 'value'
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)
Example #16
0
    def test_resolve_jsonpointer_existing(self):
        doc = {
            'foo': 1,
            'bar': [11, {'a': 222}]
        }

        path = '/foo'
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), 1)

        path = '/bar/0'
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), 11)

        path = '/bar/1/a'
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), 222)
Example #17
0
 def test_resolve_jsonpointer_examples_from_spec(self):
     def _doc():
         return {
             r"foo": ["bar", r"baz"],
             r"": 0,
             r"a/b": 1,
             r"c%d": 2,
             r"e^f": 3,
             r"g|h": 4,
             r"i\\j": 5,
             r"k\"l": 6,
             r" ": 7,
             r"m~n": 8
         }
     cases = [
         (r"",            _doc()),
         (r"/foo",        ["bar", "baz"]),
         (r"/foo/0",      "bar"),
         (r"/",           0),
         (r"/a~1b",       1),
         (r"/c%d",      2),
         (r"/e^f",      3),
         (r"/g|h",      4),
         (r"/i\\j",      5),
         (r"/k\"l",      6),
         (r"/ ",        7),
         (r"/m~0n",       8),
     ]
     for path, exp in cases:
         doc = _doc()
         self.assertEqual(pandata.resolve_jsonpointer(doc, path), exp)
Example #18
0
    def test_resolve_jsonpointer_examples_from_spec(self):
        def _doc():
            return {
                r"foo": ["bar", r"baz"],
                r"": 0,
                r"a/b": 1,
                r"c%d": 2,
                r"e^f": 3,
                r"g|h": 4,
                r"i\\j": 5,
                r"k\"l": 6,
                r" ": 7,
                r"m~n": 8,
            }

        cases = [
            (r"", _doc()),
            (r"/foo", ["bar", "baz"]),
            (r"/foo/0", "bar"),
            (r"/", 0),
            (r"/a~1b", 1),
            (r"/c%d", 2),
            (r"/e^f", 3),
            (r"/g|h", 4),
            (r"/i\\j", 5),
            (r"/k\"l", 6),
            (r"/ ", 7),
            (r"/m~0n", 8),
        ]
        for path, exp in cases:
            doc = _doc()
            self.assertEqual(pandata.resolve_jsonpointer(doc, path), exp)
Example #19
0
    def test_set_jsonpointer_replace_value(self):
        doc = {"foo": "bar", 1: 2}
        path = "/foo"
        value = "value"
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)

        doc = {"foo": 1, 1: 2}
        path = "/foo"
        value = "value"
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)

        doc = {"foo": {"bar": 1}, 1: 2}
        path = "/foo"
        value = 2
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)
Example #20
0
    def test_set_jsonpointer_replace_value(self):
        doc = {'foo': 'bar', 1: 2}
        path = '/foo'
        value = 'value'
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)

        doc = {'foo': 1, 1: 2}
        path = '/foo'
        value = 'value'
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)

        doc = {'foo': {'bar': 1}, 1: 2}
        path = '/foo'
        value = 2
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
        self.assertEqual(doc[1], 2)
Example #21
0
def store_model_parts(mdl, outfiles):
    for filespec in outfiles:
        try:
            try:
                part = pandel.resolve_jsonpointer(mdl, filespec.path)
            except RefResolutionError:
                log.warning(
                    "Nothing found at model(%s) to write to file(%s).",
                    filespec.path,
                    filespec.fname,
                )
            else:
                store_part_as_df(filespec, part)
        except Exception as ex:
            raise Exception("Failed storing %s due to: %s" % (filespec, ex)) from ex
Example #22
0
    def test_set_jsonpointer_sequence_insert_end(self):
        doc = [0, 1]
        path = '/2'
        value = 'value'
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, '/0'), 0)
        self.assertEqual(pandata.resolve_jsonpointer(doc, '/1'), 1)

        doc = [0, 1]
        path = '/-'
        value = 'value'
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, '/2'), value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, '/0'), 0)
        self.assertEqual(pandata.resolve_jsonpointer(doc, '/1'), 1)
Example #23
0
    def test_set_jsonpointer_sequence_insert_end(self):
        doc = [0, 1]
        path = "/2"
        value = "value"
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, path), value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, "/0"), 0)
        self.assertEqual(pandata.resolve_jsonpointer(doc, "/1"), 1)

        doc = [0, 1]
        path = "/-"
        value = "value"
        pandata.set_jsonpointer(doc, path, value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, "/2"), value)
        self.assertEqual(pandata.resolve_jsonpointer(doc, "/0"), 0)
        self.assertEqual(pandata.resolve_jsonpointer(doc, "/1"), 1)
Example #24
0
    def test_resolve_jsonpointer_missing_screams(self):
        doc = {}

        path = "/foo"
        with self.assertRaises(RefResolutionError):
            pandata.resolve_jsonpointer(doc, path)
Example #25
0
    def test_resolve_jsonpointer_missing_screams(self):
        doc = {}

        path = '/foo'
        with self.assertRaises(RefResolutionError):
            pandata.resolve_jsonpointer(doc, path)