예제 #1
0
    def setUp(self):
        current_dir = os.path.abspath(os.path.dirname(__file__))
        self.fixture_dir = os.path.join(current_dir, 'fixtures')
        self.test_url = 'http://127.0.0.1:8080/'
        entry_point = 'http://127.0.0.1:8080/?json_response={}'

        # For various navigation tests
        self.nav_values = {'next': "http://127.0.0.1:8080/docs?tag=npr_api&profile=someGUIDvalue&offset=10",
                           'prev': "http://127.0.0.1:8080/docs?",
                           'first': "http://127.0.0.1:8080/docs?tag=npr_api&profile=someGUIDvalue",
                           'last': "http://127.0.0.1:8080/docs?tag=npr_api&profile=someGUIDvalue&offset=13130",
                           'current': "http://127.0.0.1:8080/docs?tag=npr_api&profile=someGUIDvalue"}

        # Fixture locations
        self.home_doc = os.path.join(self.fixture_dir, 'homedoc.json')
        self.auth_doc = os.path.join(self.fixture_dir, 'authdetails.json')
        self.data_doc = os.path.join(self.fixture_dir, 'datadoc.json')

        # Can also return JSON via these urls and testing server
        self.server_process = Process(target=run_forever)
        self.server_process.start()
        self.test_entry_point = entry_point.format(self.home_doc)
        self.auth_url = entry_point.format(self.auth_doc)
        self.data_url = entry_point.format(self.data_doc)

        with open(self.home_doc, 'r') as jfile:
            home = PelicanJson(json.loads(jfile.read()))
        home.set_nested_value(['links', 'auth', 3, 'href'],
                              self.auth_url)
        self.home_values = home.convert()
예제 #2
0
def edit_profile(profile, new_data):
    """EXPERIMENTAL function for cramming data into a profile.
    """
    pelican_profile = PelicanJson(profile)
    pelican_data = PelicanJson(new_data)
    for path, value in pelican_data.enumerate():
        pelican_profile.set_nested_value(path, value, force=True)
    return pelican_profile.convert()
 def test_set_nested_value_force_add_to_list(self):
     path = ['attributes', 'tags', 4]
     test_pelican = PelicanJson(self.item)
     test_pelican.set_nested_value(path, 'New Tag', force=True)
     new_tag = test_pelican.get_nested_value(path)
     self.assertEqual(new_tag, 'New Tag')
     none_placeholder = ['attributes', 'tags', 3]
     self.assertEqual(test_pelican.get_nested_value(none_placeholder),
                      None)
 def test_set_nested_value_force_previous_index_error(self):
     test_rickettsi = PelicanJson(self.ricketts)
     success_msg = "Should now work"
     # IndexErrors overridden: paths created
     index_error_paths = [['query', 'normalized', 1, 'from'],
                          ['query', 'normalized', 1, 'to']]
     for path in index_error_paths:
         test_rickettsi.set_nested_value(path, success_msg, force=True)
         self.assertEqual(test_rickettsi.get_nested_value(path),
                          success_msg)
예제 #5
0
def strip_guids(json_result):
    guidpat = re.compile(r'\w{8}-\w{4}-\w{4}-\w{4}-\w{12}')
    pelican = PelicanJson(json_result)
    for path, value in pelican.enumerate():
        if type(value) == str and guidpat.search(value):
            pelican.set_nested_value(path,
                                     re.sub(guidpat, 'someGUIDvalue',
                                            value))

    return pelican.convert()
예제 #6
0
def clean_urls(json_result):
    urlpat = re.compile(r'https://[api|publish]-sandbox.pmp.io')
    pelican = PelicanJson(json_result)
    for path, value in pelican.enumerate():
        if type(value) == str and urlpat.search(value):
            pelican.set_nested_value(path,
                                     re.sub(urlpat, 'http://127.0.0.1:8080',
                                            value))

    return pelican.convert()
 def test_set_nested_value_force_type_error(self):
     test_rickettsi = PelicanJson(self.ricketts)
     success_msg = "Should now work"
     # TypeErrors overridden: path created
     type_error_path = ['query-continue', 'extlinks',
                        'eloffset', 'newdict-with-key']
     test_rickettsi.set_nested_value(type_error_path,
                                     success_msg,
                                     force=True)
     self.assertEqual(test_rickettsi.get_nested_value(type_error_path),
                      success_msg)
 def test_set_nested_value_force_key_error(self):
     test_rickettsi = PelicanJson(self.ricketts)
     success_msg = "Should now work"
     # KeyErrors overidden
     key_error_paths = [['unknownKey', 'unknownKey2'],
                        ['query-continue', 'unknownKey']]
     for path in key_error_paths:
         test_rickettsi.set_nested_value(path,
                                         success_msg,
                                         force=True)
         self.assertEqual(test_rickettsi.get_nested_value(path),
                          success_msg)
    def test_set_nested_value(self):
        new_path = ['ISBN:9780804720687', 'book_title']
        test_book = PelicanJson(self.book)
        test_book.set_nested_value(new_path, 'Between Pacific Tides')
        self.assertEqual(test_book.get_nested_value(new_path),
                         'Between Pacific Tides')

        test_pelican = PelicanJson(self.pelecanus_occidentalis)
        values = []
        self.assertEqual(len(list(test_pelican.values())), 5)
        for path, value in test_pelican.enumerate():
            values.append(value)
            test_pelican.set_nested_value(path, None)
        self.assertEqual(len(set(test_pelican.values())), 1)
        for path in test_pelican.search_value(None):
            test_pelican.set_nested_value(path, values.pop())
        self.assertEqual(len(list(test_pelican.values())), 5)

        pelican_item = PelicanJson(self.item)
        pelican_item.set_nested_value(['href'], None)
        self.assertEqual(pelican_item['href'], None)
    def test_set_nested_value_raises_error(self):
        # Wish to raise IndexError, KeyError, TypeError
        test_rickettsi = PelicanJson(self.ricketts)
        key_error_paths = [['unknownKey', 'unknownKey2'],
                           ['query-continue', 'unknownKey']]
        # KeyErrors
        with self.assertRaises(KeyError):
            for path in key_error_paths:
                test_rickettsi.set_nested_value(path, "Shouldn't Work")

        # TypeErrors
        type_error_path = ['query-continue', 'extlinks',
                           'eloffset', 'newdict-with-key']
        with self.assertRaises(TypeError):
            test_rickettsi.set_nested_value(type_error_path,
                                            "Shouldn't Work")

        # IndexErrors
        index_error_paths = [['query', 'normalized', 1, 'from'],
                             ['query', 'normalized', 1, 'to']]
        with self.assertRaises(IndexError):
            for path in index_error_paths:
                test_rickettsi.set_nested_value(path, "Shouldn't Work")