def test_create_path_totally_new_path(self):
     test_rickettsi = PelicanJson(self.ricketts)
     path = ['new', 'path', 'in', 1, 'object']
     with self.assertRaises(KeyError):
         test_rickettsi.get_nested_value(path)
     test_rickettsi.create_path(path, "TEST VALUE")
     self.assertEqual(test_rickettsi.get_nested_value(path), "TEST VALUE")
 def test_create_path_raise_badpath(self):
     test_rickettsi = PelicanJson(self.ricketts)
     bad_path = [[4, 'query', 'normalized', 0, 'from'],
                 [('some', 'tuple'), 'query'],
                 [{'some': 'dict'}, 'query']]
     with self.assertRaises(BadPath):
         test_rickettsi.create_path(bad_path[0], "VALUE")
         test_rickettsi.create_path(bad_path[1], "VALUE")
         test_rickettsi.create_path(bad_path[2], "VALUE")
 def test_create_path_add_item_to_list(self):
     test_item = PelicanJson(self.item)
     paths = [['attributes', 'tags', 2],
              ['attributes', 'tags', 5]]
     check_for_none = paths[1][:-1]
     check_for_none.append(4)
     test_item.create_path(paths[0], "New value inside list")
     self.assertEqual(test_item.get_nested_value(paths[0]),
                      "New value inside list")
     test_item.create_path(paths[1], "New value inside list with None")
     self.assertEqual(test_item.get_nested_value(paths[1]),
                      "New value inside list with None")
     self.assertEqual(test_item.get_nested_value(check_for_none),
                      None)
 def test_create_path_new_object_inside_list(self):
     test_rickettsi = PelicanJson(self.ricketts)
     paths = [['query', 'pages', '1422396', 'images', 7, 'title'],
              ['query', 'normalized', 10, 'NEW']]
     check_for_none = ['query', 'normalized', 5]
     test_rickettsi.create_path(paths[0], "VALUE APPENDED TO LIST")
     test_rickettsi.create_path(paths[1], "VALUE in LIST with BACKFILL")
     self.assertEqual(test_rickettsi.get_nested_value(paths[0]),
                      "VALUE APPENDED TO LIST")
     self.assertEqual(test_rickettsi.get_nested_value(paths[1]),
                      "VALUE in LIST with BACKFILL")
     self.assertEqual(test_rickettsi.get_nested_value(check_for_none),
                      None)
     self.assertEqual(len(test_rickettsi.get_nested_value(['query',
                                                           'normalized'])),
                      11)
    def test_create_path_in_dict(self):
        test_rickettsi = PelicanJson(self.ricketts)
        overwrite_path = ['query-continue', 'extlinks',
                          'eloffset', 'newdict-with-key']
        msg = "Previous value overwritten with dictionary"
        test_rickettsi.create_path(overwrite_path, msg)
        self.assertEqual(test_rickettsi.get_nested_value(overwrite_path),
                         msg)
        newdict = test_rickettsi.get_nested_value(overwrite_path[:-1])
        self.assertTrue(isinstance(newdict, PelicanJson))

        paths = [['query-continue', 'extlinks', 'newkey1'],
                 ['query-continue', 'newkey1', 'newkey2'],
                 ['newkey1', 'newkey2', 'newkey3']]
        for path in paths:
            with self.assertRaises(KeyError):
                test_rickettsi.get_nested_value(path)
            test_rickettsi.create_path(path, "NEWVALUE")
            self.assertEqual(test_rickettsi.get_nested_value(path),
                             "NEWVALUE")
 def test_create_path_raise_indexerror(self):
     test_rickettsi = PelicanJson(self.ricketts)
     bad_path = ['query', 'normalized', 'badkey', 'from']
     with self.assertRaises(IndexError):
         test_rickettsi.create_path(bad_path, "VALUE")