def get_disruptions(self, response): """ return a map with the disruption id as key and the list of disruption + impacted object as value """ disruption_by_obj = defaultdict(list) all_disruptions = {d['id']: d for d in response['disruptions']} def disruptions_filler(_, obj): try: if 'links' not in obj: return except TypeError: return real_disruptions = [ all_disruptions[d['id']] for d in obj['links'] if d['type'] == 'disruption' ] for d in real_disruptions: disruption_by_obj[d['disruption_id']].append((d, obj)) utils.walk_dict(response, disruptions_filler) return disruption_by_obj
def get_all_disruptions(elem, response): """ return a map with the disruption id as key and the list of disruption + impacted object as value for a item of the response """ disruption_by_obj = defaultdict(list) all_disruptions = {d['id']: d for d in response['disruptions']} def disruptions_filler(_, obj): try: if 'links' not in obj: return except TypeError: return real_disruptions = [ all_disruptions[d['id']] for d in obj['links'] if d['type'] == 'disruption' ] for d in real_disruptions: disruption_by_obj[d['id']].append((d, obj)) #we import utils here else it will import jormungandr too early in the test from jormungandr import utils utils.walk_dict(elem, disruptions_filler) return disruption_by_obj
def test_walk_dict(): bob = { 'tutu': 1, 'tata': [1, 2], 'toto': { 'bob': 12, 'bobette': 13, 'nested_bob': { 'bob': 3 } }, 'tete': ('tuple1', ['ltuple1', 'ltuple2']), 'titi': [{ 'a': 1 }, { 'b': 1 }], } result = io.StringIO() def my_visitor(name, val): result.write("{}={}\n".format(name, val)) walk_dict(bob, my_visitor) expected = """titi={u'b': 1} b=1 titi={u'a': 1} a=1 tete=ltuple2 tete=ltuple1 tete=tuple1 tutu=1 toto={u'bobette': 13, u'bob': 12, u'nested_bob': {u'bob': 3}} nested_bob={u'bob': 3} bob=3 bob=12 bobette=13 tata=2 tata=1 """ assert result.getvalue() == expected result = io.StringIO() def my_stoper_visitor(name, val): result.write("{}={}\n".format(name, val)) if name == 'tete': return True walk_dict(bob, my_stoper_visitor) expected = """titi={u'b': 1} b=1 titi={u'a': 1} a=1 tete=ltuple2 """ assert result.getvalue() == expected
def check_links_(date, check_existence): links[:] = [] journey_query = journey_query_2_to_format.format(datetime=date) response_json = self.query_region(journey_query) utils.walk_dict(response_json, get_type_id) if check_existence: assert any([id_ == object_id for id_ in links]) else: assert all([id_ != object_id for id_ in links])
def _clean_old_at(self, responses): response = responses[0] def remove_messages(_, obj): try: if not obj.get( 'messages' ) or 'end_application_daily_hour' not in obj.get( 'messages')[0]: return except AttributeError: return del obj['messages'] utils.walk_dict(response, remove_messages)
def _change_links(self, responses): response = responses[0] disruption_node = response[ 'all_disruptions'] if 'all_disruptions' in response else response[ 'disruptions'] all_disruptions = {d['id']: d for d in disruption_node} def change_to_real_disruption(key, obj): try: if 'links' not in obj: return except TypeError: return real_disruptions = [ deepcopy(all_disruptions[d['id']]) for d in obj['links'] if d['type'] == 'disruption' ] obj['disruptions'] = real_disruptions utils.walk_dict(response, change_to_real_disruption)
def test_walk_dict(): bob = { 'tutu': 1, 'tata': [1, 2], 'toto': { 'bob': 12, 'bobette': 13, 'nested_bob': { 'bob': 3 } }, 'tete': ('tuple1', ['ltuple1', 'ltuple2']), 'titi': [{ 'a': 1 }, { 'b': 1 }], } result = io.StringIO() def my_first_stopper_visitor(name, val): result.write("{}={}\n".format(name, val)) if val == 'ltuple1': return True walk_dict(bob, my_first_stopper_visitor) expected_nodes = ["tete", "tuple1"] assert all(node in result.getvalue() for node in expected_nodes) def my_second_stopper_visitor(name, val): result.write("{}={}\n".format(name, val)) if val == 3: return True walk_dict(bob, my_second_stopper_visitor) expected_nodes = ["toto", "bob", "bobette", "nested_bob"] assert all(node in result.getvalue() for node in expected_nodes)
def check_internal_links(response, tester): """ We want to check that all 'internal' link are correctly linked to an element in the response for that we first collect all internal link then iterate on all node and remove a link if we find a node with * a name equals to link.'rel' * an id equals to link.'id' At the end the internal link list must be empty """ from jormungandr import utils # import late not to load it before updating the conf for the tests internal_links_id = set() internal_link_types = set() # set with the types we look for def add_link_visitor(name, val): if val and name == 'links': if 'internal' in val and bool(val['internal']): internal_links_id.add(val['id']) internal_link_types.add(val['rel']) utils.walk_dict(response, add_link_visitor) def check_node(name, val): if name in internal_link_types: if 'id' in val and val['id'] in internal_links_id: # found one ref, we can remove the link internal_links_id.remove(val['id']) utils.walk_dict(response, check_node) assert not internal_links_id, "cannot find correct ref for internal links : {}".format( [lid for lid in internal_links_id] )
def find_admin(point): visitor = FindAdminVisitor() utils.walk_dict(point, visitor) return visitor.get_admin_id(), visitor.get_admin_insee( ), visitor.get_admin_name()
def test_disruption_on_journey(self): """ We block the network and the line A, we test if there's no public_transport journey. We delete the network disruption and test if there's a journey using the line B. """ response = self.query_region(journey_basic_query) assert "journeys" in response disruptions = self.get_disruptions(response) # no disruptions on the journey for the moment assert not disruptions # some disruption are loaded on the dataset though nb_pre_loaded_disruption = len( get_not_null(self.query_region('disruptions'), 'disruptions')) assert nb_pre_loaded_disruption == 10 self.send_mock("blocking_line_disruption", "A", "line", blocking=True) self.send_mock("blocking_network_disruption", "base_network", "network", blocking=True) # Test disruption API response = self.query_region('disruptions') disruptions = get_not_null(response, 'disruptions') assert len(disruptions) - nb_pre_loaded_disruption == 2 for d in disruptions: is_valid_disruption(d) assert {"blocking_line_disruption", "blocking_network_disruption" }.issubset(set([d['disruption_uri'] for d in disruptions])) response = self.query_region(journey_basic_query + "&disruption_active=true") assert all([ len([s for s in j["sections"] if s["type"] == "public_transport"]) == 0 for j in response["journeys"] ]) # we should then not have disruptions (since we don't get any journey) disruptions = self.get_disruptions(response) assert not disruptions # we then query for the same journey but without disruptions, # so we'll have a journey (but the disruptions will be displayed response = self.query_region(journey_basic_query + "&disruption_active=false") disruptions = self.get_disruptions(response) assert disruptions assert len(disruptions) == 2 assert 'blocking_line_disruption' in disruptions assert 'blocking_network_disruption' in disruptions self.send_mock("blocking_network_disruption", "base_network", "network", blocking=True, is_deleted=True) response = self.query_region(journey_basic_query + "&disruption_active=true") links = [] def get_line_id(k, v): if k != "links" or not "type" in v or not "id" in v or v[ "type"] != "line": return links.append(v["id"]) utils.walk_dict(response, get_line_id) assert all([id_ != "A" for id_ in links]) # we also query without disruption and obviously we should not have the network disruptions response = self.query_region(journey_basic_query + "&disruption_active=false") assert 'journeys' in response disruptions = self.get_disruptions(response) assert disruptions assert len(disruptions) == 1 assert 'blocking_line_disruption' in disruptions # we also check that the journey is tagged as disrupted assert any( [j.get('status') == 'NO_SERVICE' for j in response['journeys']])
def test_disruption_on_journey(self): """ We block the network and the line A, we test if there's no public_transport journey. We delete the network disruption and test if there's a journey using the line B. """ self.wait_for_rabbitmq_cnx() response = self.query_region(journey_basic_query) assert "journeys" in response disruptions = self.get_disruptions(response) #no disruptions for the moment assert not disruptions self.send_chaos_disruption_and_sleep("blocking_line_disruption", "A", "line", blocking=True) self.send_chaos_disruption_and_sleep("blocking_network_disruption", "base_network", "network", blocking=True) response = self.query_region(journey_basic_query + "&disruption_active=true") assert all( map( lambda j: len([ s for s in j["sections"] if s["type"] == "public_transport" ]) == 0, response["journeys"])) # we should then not have disruptions (since we don't get any journey) disruptions = self.get_disruptions(response) assert not disruptions # we then query for the same journey but without disruptions, # so we'll have a journey (but the disruptions will be displayed response = self.query_region(journey_basic_query + "&disruption_active=false") disruptions = self.get_disruptions(response) assert disruptions eq_(len(disruptions), 2) assert 'blocking_line_disruption' in disruptions assert 'blocking_network_disruption' in disruptions self.send_chaos_disruption_and_sleep("blocking_network_disruption", "base_network", "network", blocking=True, is_deleted=True) response = self.query_region(journey_basic_query + "&disruption_active=true") links = [] def get_line_id(k, v): if k != "links" or not "type" in v or not "id" in v or v[ "type"] != "line": return links.append(v["id"]) utils.walk_dict(response, get_line_id) assert all(map(lambda id_: id_ != "A", links)) #we also query without disruption and obviously we should not have the network disruptions response = self.query_region(journey_basic_query + "&disruption_active=false") assert 'journeys' in response disruptions = self.get_disruptions(response) assert disruptions eq_(len(disruptions), 1) assert 'blocking_line_disruption' in disruptions #we also check that the journey is tagged as disrupted assert any( map(lambda j: j.get('status') == 'NO_SERVICE', response['journeys']))