def setUp(self):
     self.file = open('tests/test_data/test.json', 'r')
     self.test_data1 = json.load(self.file)
     self.file2 = open('tests/test_data/test2.json', 'r')
     self.test_data2 = json.load(self.file2)
     self.tk = JsonToolKit()
     self.tk_impl = JsonToolKit(self.test_data1)
示例#2
0
class DelItemTestCase(unittest.TestCase):
    def setUp(self):
        self.file = open('tests/test_data/test.json', 'r')
        self.test_data1 = json.load(self.file)
        self.file2 = open('tests/test_data/test2.json', 'r')
        self.test_data2 = json.load(self.file2)
        self.tk = JsonToolKit()
        self.tk_impl = JsonToolKit(self.test_data1)

    def test_del_item_explicit(self):
        data1=self.tk.del_item("company1.department2.janitor", data=self.test_data1)
        test_data1_result={'head': {'name': 'Mike Steffens'}}
        data2 = self.tk.del_item("p.contents.strong.string", data=self.test_data2)
        test_data2_result = [{'p': {'string': '',
                                    'contents': [
                                        {'strong': {}},
                                        {'p': {'string': 'Foo Bar'}}
                                    ]}},
                             {'p': {'string': '',
                                    'contents': [
                                        {'strong': {}},
                                        {'strong': {}},
                                        {'p': {'string': 'some text, again'}}
                                    ]}}
                             ]

        self.assertEqual(self.tk.get_value("company1.department2", data1),
                         test_data1_result)
        self.assertEqual(data2, test_data2_result)


    def test_del_item_implicit(self):
        self.tk_impl.del_item("company1.department2.janitor")
        test_data1_result = {'head': {'name': 'Mike Steffens'}}
        self.assertEqual(self.tk_impl.get_value("company1.department2"),
                         test_data1_result)


    def tearDown(self):
        self.file.close()
        self.file2.close()
class GetKeyPathsTestCase(unittest.TestCase):

    def setUp(self):
        self.file = open('tests/test_data/test.json', 'r')
        self.test_data1=json.load(self.file)
        self.file2 = open('tests/test_data/test2.json', 'r')
        self.test_data2=json.load(self.file2)
        self.tk = JsonToolKit()

    def test_get_keypaths(self):
        test_data1_keypaths=['company1.department2.head.name',
                                  'company2.department2.janitor',
                                  'company1.department2', 'company2.department1',
                                  'company2.department1.head',
                                  'company2.department2.janitor.name',
                                  'company1.department1.team.age',
                                  'company2.department2.head.name',
                                  'company2.department1.head.name',
                                  'company1.department1.team.name',
                                  'company1.department1.head',
                                  'company1.department1.janitor',
                                  'company2.department1.janitor',
                                  'company2.department1.janitor.name',
                                  'company2.department2',
                                  'company1.department1.team.skills',
                                  'company1.department1', 'company2.department2.head',
                                  'company1.department2.janitor.name',
                                  'company1.department1.team',
                                  'company1.department2.head',
                                  'company1.department1.head.name',
                                  'company1.department1.janitor.name',
                                  'company1.department2.janitor', 'company2', 'company1']
        test_data2_keypaths=['p.contents.strong', 'p.contents.p.string',
                                  'p.contents', 'p.contents.strong.string',
                                  'p.contents.p', 'p.string', 'p']

        self.assertListEqual(sorted(self.tk.get_keypaths(self.test_data1)),
                      sorted(test_data1_keypaths))
        self.assertListEqual(sorted(self.tk.get_keypaths(self.test_data2)),
                             sorted(test_data2_keypaths))

    def test_get_keypaths_hide_no_arrays(self):
        test_data1_keypaths=['company1.department1.janitor',
                             'company2.department2.head',
                             'company1.department2.janitor.name',
                             'company2.department1.head',
                             'company2.department1.janitor.name',
                             'company2.department1', 'company2',
                             'company1.department2.janitor',
                             'company2.department2.janitor', 'company1.department1',
                             'company2.department1.janitor', 'company1',
                             'company2.department1.head.name',
                             'company1.department1.head.name',
                             'company1.department1.janitor.name',
                             'company1.department2.head.name', 'company2.department2',
                             'company1.department1.team',
                             'company2.department2.head.name',
                             'company1.department1.team[1].name',
                             'company1.department2',
                             'company1.department1.team[0].name',
                             'company1.department1.team[0].skills',
                             'company1.department1.team[0].age',
                             'company1.department1.head',
                             'company1.department1.team[1].age',
                             'company2.department2.janitor.name',
                             'company1.department2.head']
        test_data2_keypaths=['[0].p.string', '[0].p.contents[1].p.string',
                             '[1].p.contents[2].p', '[1].p.contents[1].strong.string',
                             '[0].p.contents[1].p', '[1].p.contents[1].strong',
                             '[1].p.contents[2].p.string', '[0].p.contents',
                             '[1].p.contents[0].strong.string', '[1].p',
                             '[1].p.contents', '[0].p',
                             '[0].p.contents[0].strong.string',
                             '[1].p.contents[0].strong', '[1].p.string',
                             '[0].p.contents[0].strong']
        self.assertListEqual(sorted(self.tk.get_keypaths(self.test_data1,
                                                     hide_arrays=False)),
                             sorted(test_data1_keypaths))
        self.assertListEqual(sorted(self.tk.get_keypaths(self.test_data2,
                                                     hide_arrays=False)),
                             sorted(test_data2_keypaths))

    def test_get_keypaths_startkey(self):
        test_data1_keypaths=['head.name', 'janitor.name', 'team.skills', 'head',
                                  'team', 'janitor', 'team.age', 'team.name']
        test_data2_keypaths=['p', 'p.string', 'strong', 'strong.string']
        self.assertListEqual(sorted(self.tk.get_keypaths(self.test_data1,
                                                  start_key="company1.department1")),
                             sorted(test_data1_keypaths))
        self.assertListEqual(sorted(self.tk.get_keypaths(self.test_data2,
                                                     start_key="p.contents")),
                             sorted(test_data2_keypaths))

    def tearDown(self):
        self.file.close()
        self.file2.close()
class GetValueTestCase(unittest.TestCase):

    def setUp(self):
        self.file = open('tests/test_data/test.json', 'r')
        self.test_data1 = json.load(self.file)
        self.file2 = open('tests/test_data/test2.json', 'r')
        self.test_data2 = json.load(self.file2)
        self.tk = JsonToolKit()
        self.tk = JsonToolKit()
        self.tk_impl = JsonToolKit(self.test_data1)
        self.tk_impl2 = JsonToolKit(self.test_data2)

    def test_get_value_impl(self):
        ret_val = self.tk.get_value("company1", self.test_data1)
        test_val1 = {"department1": {"head": {"name": "Jacky Lynch"},
                                     "janitor": {"name": "Paul Meier"},
                                     "team": [{"name": "Lodger Reimann", "skills": [
                                         "C#", "JavaScript", "Python"], "age": 34},
                                              {"name": "Maria Benz", "age": 21}]
                                     },
                     "department2": {"head": {"name": "Mike Steffens"},
                                     "janitor": {"name": "Johanna Simon"}
                                     }
                     }
        self.assertEqual(ret_val, test_val1)

        ret_val = self.tk.get_value("company1.department1", self.test_data1)
        test_val2 = {"head": {"name": "Jacky Lynch"},
                     "janitor": {"name": "Paul Meier"},
                     "team": [{"name": "Lodger Reimann",
                               "skills": ["C#", "JavaScript", "Python"],
                               "age": 34},
                              {"name": "Maria Benz", "age": 21}]
                     }
        self.assertEqual(ret_val, test_val2)
        ret_val = self.tk.get_value("company1", self.test_data1)
        self.assertEqual(ret_val, test_val1)

    def test_get_value_expl(self):
        ret_val = self.tk_impl.get_value("company1")
        test_val1 = {"department1": {"head": {"name": "Jacky Lynch"},
                                     "janitor": {"name": "Paul Meier"},
                                     "team": [{"name": "Lodger Reimann", "skills": [
                                         "C#", "JavaScript", "Python"], "age": 34},
                                              {"name": "Maria Benz", "age": 21}]
                                     },
                     "department2": {"head": {"name": "Mike Steffens"},
                                     "janitor": {"name": "Johanna Simon"}
                                     }
                     }
        self.assertEqual(ret_val, test_val1)
        ret_val = self.tk_impl.get_value("company1.department1.head.name")
        test_val2 = "Jacky Lynch"
        self.assertEqual(ret_val, test_val2)
        ret_val = self.tk_impl.get_value("company1")
        self.assertEqual(ret_val, test_val1)

    def test_get_root_array_member(self):
        ret_val = self.tk_impl2.get_value("p")
        test_val1 = [{"string": "",
                      "contents": [
                          {"strong": {"string": "This is a string text"}},
                          {"p": {"string": "Foo Bar"}}]
                      },
                     {"string": "",
                      "contents": [
                          {"strong": {"string": "This is another strong text"}},
                          {"strong": {"string": "another text"}},
                          {"p": {"string": "some text, again"}}]
                      }
                     ]
        self.assertEqual(ret_val, test_val1)
        ret_val = self.tk_impl2.get_value("strong")
        test_val2 = []
        self.assertEqual(ret_val, test_val2)

    def test_get_single_root_array_member(self):
        ret_val = self.tk_impl2.get_value("[0].p")
        test_val1 = {"string": "",
                      "contents": [
                          {"strong": {"string": "This is a string text"}},
                          {"p": {"string": "Foo Bar"}}]
                      }

        self.assertEqual(ret_val, test_val1)
        ret_val = self.tk_impl2.get_value("[0].strong")
        test_val2 = {}
        self.assertEqual(ret_val, test_val2)
        ret_val = self.tk_impl2.get_value("[0].p")
        self.assertEqual(ret_val, test_val1)
        self.assertEqual(self.tk_impl2.get_json(),self.test_data2)

    def test_get_array(self):
        ret_val = self.tk_impl2.get_value("p.contents")
        test_val1 = [{"strong": {"string": "This is a string text"}},
                    {"p": {"string": "Foo Bar"}},
                    {"strong": {"string": "This is another strong text"}},
                    {"strong": {"string": "another text"}},
                    {"p": {"string": "some text, again"}}
                    ]
        self.assertEqual(ret_val, test_val1)

    def test_get_filtered_array(self):
        ret_val = self.tk_impl2.get_value("p.contents.strong")
        test_val1 = [{"string": "This is a string text"},
                    {"string": "This is another strong text"},
                    {"string": "another text"},
                    ]
        self.assertEqual(ret_val, test_val1)

    def test_get_array_member(self):
        ret_val = self.tk_impl2.get_value("p.contents[0]")
        test_val1 = [{"strong": {"string": "This is a string text"}},
                     {"strong": {"string": "This is another strong text"}},
                     ]
        self.assertEqual(ret_val, test_val1)

    def test_get_filtered_array_member(self):
        ret_val = self.tk_impl2.get_value("p.contents[1].strong")
        test_val1 = [{"string": "another text"}]
        self.assertEqual(ret_val, test_val1)

    def tearDown(self):
        self.file.close()
        self.file2.close()