def format_impacts(self, impacts): navitia = Navitia(self.navitia_url, self.coverage, self.token) columns = impacts.keys() rows = [] for sub_dict in impacts.fetchall(): row = [] for column in columns: val = sub_dict[column] if column == 'pt_object_name' : val = navitia.find_tc_object_name(sub_dict['pt_object_uri'], sub_dict['pt_object_type']) elif column == 'periodicity': val = 'yes' if val else 'no' elif column == 'status' and val == 'archived': val = 'deleted' elif isinstance(val, datetime.date): val = utils.utc_to_local(val, self.time_zone) row.append(utils.sanitize_csv_data(val)) rows.append(row) return {'columns' : columns, 'rows' : rows}
def format_impacts(self, impacts): navitia = Navitia(self.navitia_url, self.coverage, self.token) columns = impacts.keys() rows = [] for sub_dict in impacts.fetchall(): row = [] for column in columns: val = sub_dict[column] if column == 'pt_object_name': val = navitia.find_tc_object_name( sub_dict['pt_object_uri'], sub_dict['pt_object_type']) elif column == 'periodicity': val = 'yes' if val else 'no' elif column == 'status' and val == 'archived': val = 'deleted' elif isinstance(val, datetime.date): val = utils.utc_to_local(val, self.time_zone) row.append(utils.sanitize_csv_data(val)) rows.append(row) return {'columns': columns, 'rows': rows}
def test_navitia_unauthorized(): n = Navitia('http://api.navitia.io', 'jdr') with HTTMock(navitia_mock_navitia_unauthorized): try: n._navitia_caller('http://api.navitia.io') except exceptions.Unauthorized as e: eq_(e.message, 'test unauthorized') raise
def test_navitia_retry(): n = Navitia('http://api.navitia.io', 'jdr') def navitia_timeout_response(*args, **kwargs): global navitia_timeout_count navitia_timeout_count += 1 raise requests.exceptions.Timeout requests.get = navitia_timeout_response n._navitia_caller('http://api.navitia.io') eq_(navitia_timeout_count, 3)
def output(self, key, obj): if not obj: return None if obj.type == 'line_section': return None navitia = Navitia(current_app.config['NAVITIA_URL'], get_coverage(request), get_token(request)) response = navitia.get_pt_object(obj.uri, obj.type) if response and 'name' in response: return response['name'] return 'Unable to find object'
def output(self, key, obj): if not obj: return None if obj.type == 'line_section': return None navitia = Navitia( current_app.config['NAVITIA_URL'], get_coverage(request), get_token(request)) response = navitia.get_pt_object(obj.uri, obj.type) if response and 'name' in response: return response['name'] return 'Unable to find object'
def test_get_pt_object(): n = Navitia('http://api.navitia.io', 'jdr') mock = NavitiaMock(200, {'networks': [{'id': 'network:foo', 'name': 'reseau foo'}]}, assert_url='http://api.navitia.io/v1/coverage/jdr/networks/network:foo?depth=0') with HTTMock(mock): eq_(n.get_pt_object('network:foo', 'network'), {'id': 'network:foo', 'name': 'reseau foo'}) mock = NavitiaMock(200, {'networks': []}) with HTTMock(mock): eq_(n.get_pt_object('network:foo', 'network'), None) mock = NavitiaMock(404, {}) with HTTMock(mock): eq_(n.get_pt_object('network:foo', 'network'), None)
def output(self, key, obj): if not obj: return None if isinstance(obj, dict) and 'uri' in obj and 'type' in obj: obj_uri = obj['uri'] obj_type = obj['type'] else: obj_uri = obj.uri obj_type = obj.type navitia = Navitia( current_app.config['NAVITIA_URL'], get_coverage(request), get_token(request)) return navitia.find_tc_object_name(obj_uri, obj_type)
def output(self, key, obj): to_return = [] # for history if hasattr(obj, 'history_localization') and isinstance(obj.localizations, list): return self._clean_localizations(obj.history_localization) navitia = Navitia(current_app.config['NAVITIA_URL'], get_coverage(request), get_token(request)) if isinstance(obj, dict) and 'localizations' in obj: for localization in obj['localizations']: response = navitia.get_pt_object( localization['uri'], localization['type']) if response and 'name' in response: response["type"] = localization['type'] to_return.append(response) else: to_return.append( { "id": localization['uri'], "name": "Unable to find object", "type": localization['type'] } ) elif obj.localizations: for localization in obj.localizations: response = navitia.get_pt_object( localization.uri, localization.type) if response and 'name' in response: response["type"] = localization.type to_return.append(response) else: to_return.append( { "id": localization.uri, "name": "Unable to find object", "type": localization.type } ) return self._clean_localizations(to_return)
def output(self, key, obj): to_return = [] navitia = Navitia(current_app.config['NAVITIA_URL'], get_coverage(request), get_token(request)) for localization in obj.localizations: response = navitia.get_pt_object(localization.uri, localization.type) if response and 'name' in response: response["type"] = localization.type to_return.append(response) else: to_return.append({ "id": localization.uri, "name": "Unable to find object", "type": localization.type }) return to_return
def wrapper(*args, **kwargs): try: coverage = get_coverage(request) token = get_token(request) except exceptions.HeaderAbsent as e: return marshal({'error': { 'message': utils.parse_error(e) }}, fields.error_fields), 400 nav = Navitia(current_app.config['NAVITIA_URL'], coverage, token) return func(*args, navitia=nav, **kwargs)
def output(self, key, obj): # for history if hasattr(obj, 'name'): return obj.name if not obj: return None if isinstance(obj, dict) and 'uri' in obj and 'type' in obj: obj_uri = obj['uri'] obj_type = obj['type'] else: obj_uri = obj.uri obj_type = obj.type navitia = Navitia(current_app.config['NAVITIA_URL'], get_coverage(request), get_token(request)) return navitia.find_tc_object_name(obj_uri, obj_type)
def output(self, key, obj): to_return = [] navitia = Navitia(current_app.config['NAVITIA_URL'], get_coverage(request), get_token(request)) if isinstance(obj, dict) and 'localizations' in obj: for localization in obj['localizations']: response = navitia.get_pt_object( localization['uri'], localization['type']) if response and 'name' in response: response["type"] = localization['type'] to_return.append(response) else: to_return.append( { "id": localization['uri'], "name": "Unable to find object", "type": localization['type'] } ) elif obj.localizations: for localization in obj.localizations: response = navitia.get_pt_object( localization.uri, localization.type) if response and 'name' in response: response["type"] = localization.type to_return.append(response) else: to_return.append( { "id": localization.uri, "name": "Unable to find object", "type": localization.type } ) return to_return
def test_get_pt_object(): n = Navitia('http://api.navitia.io', 'jdr') mock = NavitiaMock( 200, {'networks': [{ 'id': 'network:foo', 'name': 'reseau foo' }]}, assert_url= 'http://api.navitia.io/v1/coverage/jdr/networks/network:foo?depth=0') with HTTMock(mock): eq_(n.get_pt_object('network:foo', 'network'), { 'id': 'network:foo', 'name': 'reseau foo' }) mock = NavitiaMock(200, {'networks': []}) with HTTMock(mock): eq_(n.get_pt_object('network:foo', 'network'), None) mock = NavitiaMock(404, {}) with HTTMock(mock): eq_(n.get_pt_object('network:foo', 'network'), None)
def test_query_formater(): n = Navitia('http://api.navitia.io', 'jdr') eq_( n.query_formater('uri', 'network'), 'http://api.navitia.io/v1/coverage/jdr/networks/uri?depth=0&disable_disruption=true' )
def test_query_formater_all(): n = Navitia('http://api.navitia.io', 'jdr') eq_(n.query_formater('uri', 'network', 'networks'), 'http://api.navitia.io/v1/coverage/jdr/networks/uri/networks?depth=0')
def test_query_formater_line_section(): n = Navitia('http://api.navitia.io', 'jdr') eq_(n.query_formater('uri', 'line_section'), None)
def test_query_formater_all_objects_invalid(): n = Navitia('http://api.navitia.io', 'jdr') eq_(n.query_formater('uri', 'network', 'stop_areas'), 'http://api.navitia.io/v1/coverage/jdr/networks/uri/networks?depth=0')
def test_query_formater_all_objects_invalid(): n = Navitia('http://api.navitia.io', 'jdr') eq_( n.query_formater('uri', 'network', 'stop_areas'), 'http://api.navitia.io/v1/coverage/jdr/networks/uri/networks?depth=0&disable_disruption=true' )
def test_navitia_unknown_object_type(): n = Navitia('http://api.navitia.io', 'jdr') with HTTMock(navitia_mock_unknown_object_type): n.get_pt_object('network:foo', 'aaaaaaaa')
def test_navitia_request_error(): n = Navitia('http://api.navitia.io', 'jdr') with HTTMock(navitia_mock_navitia_error): n.get_pt_object('network:foo','network')
def test_navitia_request_error(): n = Navitia('http://api.navitia.io', 'jdr') with HTTMock(navitia_mock_navitia_error): n.get_pt_object('network:foo3', 'network')
def test_query_formater(): n = Navitia('http://api.navitia.io', 'jdr') eq_(n.query_formater('uri', 'network'), 'http://api.navitia.io/v1/coverage/jdr/networks/uri?depth=0&disable_disruption=true')
def test_navitia_unknown_object_type(): n = Navitia('http://api.navitia.io', 'jdr') with HTTMock(navitia_mock_unknown_object_type): n.get_pt_object('network:foo4', 'aaaaaaaa')