Exemple #1
0
def next_passage_for_route_point_test():
    """
    test the whole next_passage_for_route_point
    mock the http call to return a good timeo response, we should get some next_passages
    """
    timeo = Timeo(id='tata',
                  timezone='UTC',
                  service_url='http://bob.com/tata',
                  service_args={
                      'a': 'bobette',
                      'b': '12'
                  })

    mock_requests = MockRequests({
        'http://bob.com/tata?a=bobette&b=12&StopDescription=?StopTimeoCode=stop_tutu&LineTimeoCode'
        '=line_toto&Way=route_tata&NextStopTimeNumber=5&StopTimeType=TR;':
        (mock_good_timeo_response(), 200)
    })

    route_point = MockRoutePoint(route_id='route_tata',
                                 line_id='line_toto',
                                 stop_id='stop_tutu')
    # we mock the http call to return the hard coded mock_response
    with mock.patch('requests.get', mock_requests.get):
        with mock.patch(
                'jormungandr.realtime_schedule.timeo.Timeo._get_direction_name',
                lambda timeo, **kwargs: None):
            passages = timeo.next_passage_for_route_point(
                route_point, current_dt=_dt("02:02"))

            assert len(passages) == 3

            assert passages[0].datetime == _dt('15:40:04')
            assert passages[1].datetime == _dt('15:55:04')
            assert passages[2].datetime == _dt('16:10:04')
Exemple #2
0
def get_passages_test():
    """
    test the next departures get from timeo's response

    the timezone is UTC for convenience
    """
    timeo = Timeo(id='tata',
                  timezone='UTC',
                  service_url='http://bob.com/tata',
                  service_args={
                      'a': 'bobette',
                      'b': '12'
                  })

    mock_response = mock_good_timeo_response()

    # we need to mock the datetime.now() because for timeo we don't have a choice but to combine
    # the current day with the timeo's response
    with mock.patch(
            'jormungandr.realtime_schedule.timeo.Timeo._get_direction_name',
            lambda timeo, **kwargs: None):
        passages = timeo._get_passages(mock_response, current_dt=_dt("02:02"))

        assert len(passages) == 3

        assert passages[0].datetime == _dt('15:40:04')
        assert passages[1].datetime == _dt('15:55:04')
        assert passages[2].datetime == _dt('16:10:04')
Exemple #3
0
def get_passages_stop_time_with_is_realtime_test():
    """
    test the next departures get from timeo's response

    the timezone is UTC for convenience
    """
    timeo = Timeo(id='tata',
                  timezone='UTC',
                  service_url='http://bob.com/tata',
                  service_args={
                      'a': 'bobette',
                      'b': '12'
                  })

    mock_response = mock_good_timeo_response(criteria=3)

    # we need to mock the datetime.now() because for timeo we don't have a choice but to combine
    # the current day with the timeo's response
    with mock.patch(
            'jormungandr.realtime_schedule.timeo.Timeo._get_direction',
            lambda timeo, **kwargs: Direction("StopPoint_Bob", "A direction"),
    ):
        passages = timeo._get_passages(MockResponse(200, 'http://bob.com/tata',
                                                    mock_response),
                                       current_dt=_dt("02:02"))

        assert len(passages) == 1

        assert passages[0].datetime == _dt('16:10:04')
Exemple #4
0
def next_passage_for_route_point_timeo_failure_test():
    """
    test the whole next_passage_for_route_point

    the timeo's response is in error (status = 404), we should get 'None'
    """
    timeo = Timeo(id='tata',
                  timezone='UTC',
                  service_url='http://bob.com/tata',
                  service_args={
                      'a': 'bobette',
                      'b': '12'
                  })

    mock_requests = MockRequests({
        'http://bob.com/tata?a=bobette&b=12&StopDescription=?StopTimeoCode=stop_tutu&LineTimeoCode'
        '=line_toto&Way=route_tata&NextStopTimeNumber=5&StopTimeType=TR;':
        (mock_good_timeo_response(), 404)
    })

    route_point = MockRoutePoint(route_id='route_tata',
                                 line_id='line_toto',
                                 stop_id='stop_tutu')
    with mock.patch('requests.get', mock_requests.get):
        passages = timeo.next_passage_for_route_point(route_point,
                                                      current_dt=_dt("02:02"))

        assert passages is None
Exemple #5
0
def get_passages_wrong_response_test():
    """
    test that if timeo returns a not valid response, we get None (and not an empty list)
    """
    timeo = Timeo(id='tata',
                  timezone='UTC',
                  service_url='http://bob.com/tata',
                  service_args={
                      'a': 'bobette',
                      'b': '12'
                  })

    mock_response = {
        "CorrelationID": "GetNextStopTimesResponse-16022016 15:30",
        "MessageResponse": [{
            "ResponseCode": "0",
            "ResponseComment": "success"
        }]
    }

    # we need to mock the datetime.now() because for timeo we don't have a choice but to combine
    # the current day with the timeo's response
    with mock.patch(
            'jormungandr.realtime_schedule.timeo.Timeo._get_direction_name',
            lambda timeo, **kwargs: None):
        passages = timeo._get_passages(mock_response, current_dt=_dt("02:02"))

        assert passages is None
Exemple #6
0
def get_passages_no_passages_test():
    """
    test that if timeo returns 0 response, we return an empty list
    """
    timeo = Timeo(id='tata',
                  timezone='UTC',
                  service_url='http://bob.com/tata',
                  service_args={
                      'a': 'bobette',
                      'b': '12'
                  })

    mock_response = {
        "CorrelationID":
        "GetNextStopTimesResponse-16022016 15:30",
        "MessageResponse": [{
            "ResponseCode": 0,
            "ResponseComment": "success"
        }],
        "StopTimesResponse": [{
            "StopID": "StopPoint_OLS01070201",
            "StopTimeoCode": "3331",
            "StopLongName": "André Malraux",
            "StopShortName": "André Malraux",
            "StopVocalName": "André Malraux",
            "ReferenceTime": "15:30:06",
            "NextStopTimesMessage": {
                "LineID": "line_id",
                "LineTimeoCode": "line_toto",
                "Way": "route_tata",
                "LineMainDirection": "Bicharderies",
                "NextExpectedStopTime": [],  # emtpy list
            },
        }],
    }

    # we need to mock the datetime.now() because for timeo we don't have a choice but to combine
    # the current day with the timeo's response
    with mock.patch(
            'jormungandr.realtime_schedule.timeo.Timeo._get_direction_name',
            lambda timeo, **kwargs: None):
        passages = timeo._get_passages(MockResponse(200, 'http://bob.com/tata',
                                                    mock_response),
                                       current_dt=_dt("02:02"))

        assert len(passages) == 0