Exemplo n.º 1
0
def make_url_count_and_dt_test():
    """
    same as make_url_test but with a count and a from_dt
    """
    timeo = Timeo(
        id='tata',
        timezone='UTC',
        service_url='http://bob.com/tata',
        service_args={
            'a': 'bobette',
            'b': '12'
        },
        from_datetime_step=30,
    )

    url = timeo._make_url(
        MockRoutePoint(route_id='route_tata',
                       line_id='line_toto',
                       stop_id='stop_tutu'),
        count=2,
        from_dt=_timestamp("12:00"),
    )

    # it should be a valid url
    assert validators.url(url)

    assert url.startswith('http://bob.com')
    assert 'a=bobette' in url
    assert 'b=12' in url
    assert 'StopTimeoCode=stop_tutu' in url
    assert 'LineTimeoCode=line_toto' in url
    assert 'Way=route_tata' in url
    # we should have the param we provided
    assert 'NextStopTimeNumber=2' in url
    assert 'NextStopReferenceTime=2016-02-07T12:00:00' in url

    # same as before we only update the seconds of dt
    url = timeo._make_url(
        MockRoutePoint(route_id='route_tata',
                       line_id='line_toto',
                       stop_id='stop_tutu'),
        count=2,
        from_dt=_timestamp("12:00:04"),
    )
    assert 'NextStopReferenceTime=2016-02-07T12:00:00' in url

    url = timeo._make_url(
        MockRoutePoint(route_id='route_tata',
                       line_id='line_toto',
                       stop_id='stop_tutu'),
        count=2,
        from_dt=_timestamp("12:00:59"),
    )
    assert 'NextStopReferenceTime=2016-02-07T12:00:30' in url
Exemplo n.º 2
0
def make_url_test():
    timeo = Timeo(id='tata',
                  timezone='Europe/Paris',
                  service_url='http://bob.com/tata',
                  service_args={
                      'a': 'bobette',
                      'b': '12'
                  })

    url = timeo._make_url(
        MockRoutePoint(route_id='route_tata',
                       line_id='line_toto',
                       stop_id='stop_tutu'))

    # it should be a valid url
    assert validators.url(url)

    assert url.startswith('http://bob.com')
    assert 'a=bobette' in url
    assert 'b=12' in url
    assert 'StopTimeoCode=stop_tutu' in url
    assert 'LineTimeoCode=line_toto' in url
    assert 'Way=route_tata' in url
    # we did not provide a datetime, we should not have it in the url
    assert 'NextStopReferenceTime' not in url
    # we did not provide a count, we should have the default value
    assert 'NextStopTimeNumber=5' in url
Exemplo n.º 3
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
Exemplo n.º 4
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')
Exemplo n.º 5
0
def next_passage_for_route_point_good_response_with_error_test():
    """
    mock the http call to return a good response that also contains an error, we should get some next_passages
    """
    siri = Siri(id='tata',
                service_url='http://bob.com/',
                requestor_ref='Stibada')
    route_point = MockRoutePoint(route_id='route_tata',
                                 line_id='line_toto',
                                 stop_id='stop_tutu')

    with requests_mock.Mocker() as m:
        m.post('http://bob.com/', text=mock_good_with_error_response())
        passages = siri._get_next_passage_for_route_point(
            route_point,
            from_dt=_timestamp("12:00"),
            current_dt=_timestamp("12:00"),
            count=1)
        assert m.called
        assert len(passages) == 1
        assert passages[0].datetime == datetime.datetime(2016,
                                                         3,
                                                         29,
                                                         13,
                                                         37,
                                                         tzinfo=pytz.UTC)
Exemplo n.º 6
0
def next_passage_for_route_point_test():
    """
    test the whole next_passage_for_route_point
    mock the http call to return a good response, we should get some next_passages
    """
    siri = Siri(id='tata',
                service_url='http://bob.com/',
                requestor_ref='Stibada')
    mock_requests = MockRequests(
        {'http://bob.com/': (mock_good_response(), 200)})
    route_point = MockRoutePoint(route_id='route_tata',
                                 line_id='line_toto',
                                 stop_id='stop_tutu')

    with mock.patch('requests.post', mock_requests.post):
        passages = siri._get_next_passage_for_route_point(
            route_point,
            from_dt=_timestamp("12:00"),
            current_dt=_timestamp("12:00"),
            count=1)
        assert len(passages) == 1
        assert passages[0].datetime == datetime.datetime(2016,
                                                         3,
                                                         29,
                                                         13,
                                                         37,
                                                         tzinfo=pytz.UTC)
Exemplo n.º 7
0
def make_url_invalid_code_test():
    """
    test make_url when RoutePoint does not have a mandatory code

    we should not get any url
    """
    synthese = Synthese(id='tata', timezone='Europe/Paris', service_url='http://bob.com/')

    url = synthese._make_url(MockRoutePoint(route_id='route_tata', line_id='line_toto', stop_id=None))

    assert url is None
Exemplo n.º 8
0
def make_url_invalid_code_test():
    """
    test make_url when RoutePoint does not have a mandatory code

    we should not get any url
    """
    timeo = Timeo(id='tata', timezone='Europe/Paris', service_url='http://bob.com/tata',
                  service_args={'a': 'bobette', 'b': '12'})

    url = timeo._make_url(MockRoutePoint(route_id='route_tata', line_id='line_toto', stop_id=None))

    assert url is None
Exemplo n.º 9
0
def make_url_test():
    synthese = Synthese(id='tata', timezone='Europe/Paris', service_url='http://bob.com/')

    url = synthese._make_url(MockRoutePoint(route_id='route_tata', line_id='line_toto', stop_id='stop_tutu'))

    # it should be a valid url
    assert validators.url(url)

    assert url.startswith('http://bob.com/')
    assert 'SERVICE=tdg' in url
    assert 'roid=stop_tutu' in url
    assert 'rn=' not in url  # we should not have any limit
    assert 'date=' not in url  # we should not have any custom date
Exemplo n.º 10
0
def make_url_date_and_count_test():
    synthese = Synthese(id='tata', timezone='UTC', service_url='http://bob.com/')

    url = synthese._make_url(MockRoutePoint(route_id='route_tata', line_id='line_toto', stop_id='stop_tutu'),
                             count=2, from_dt=_timestamp("12:00"))

    # it should be a valid url
    assert validators.url(url)

    assert url.startswith('http://bob.com/')
    assert 'SERVICE=tdg' in url
    assert 'roid=stop_tutu' in url
    # we should have the param we provided
    assert 'rn=2' in url
    assert 'date=2016-02-07 12:00' in url
Exemplo n.º 11
0
def next_passage_for_route_point_failure_test():
    """
    test the whole next_passage_for_route_point

    the siri's response is in error (status = 404), we should get 'None'
    """
    siri = Siri(id='tata', service_url='http://bob.com/', requestor_ref='Stibada')

    mock_requests = MockRequests({'http://bob.com/': (mock_good_response(), 404)})

    route_point = MockRoutePoint(route_id='route_tata', line_id='line_toto', stop_id='stop_tutu')

    with mock.patch('requests.post', mock_requests.post):
        passages = siri.next_passage_for_route_point(route_point, from_dt=_timestamp("12:00"), count=2)

        assert passages is None
Exemplo n.º 12
0
def next_passage_for_route_point_failure_test():
    """
    test the whole next_passage_for_route_point

    the siri's response is in error (status = 404), we should get 'None'
    """
    siri = Siri(id='tata', service_url='http://bob.com/', requestor_ref='Stibada')

    route_point = MockRoutePoint(route_id='route_tata', line_id='line_toto', stop_id='stop_tutu')

    with requests_mock.Mocker() as m:
        m.post('http://bob.com/', text=mock_good_response(), status_code=404)
        passages = siri.next_passage_for_route_point(route_point, from_dt=_timestamp("12:00"), count=2)
        assert m.called

        assert passages is None
Exemplo n.º 13
0
def next_passage_for_route_point_no_data_test():
    """
    test the whole next_passage_for_route_point

    the siri's response contains an error of type NoInfoForTopicError: there is no next departure
    """
    siri = Siri(id='tata', service_url='http://bob.com/', requestor_ref='Stibada')

    route_point = MockRoutePoint(route_id='route_tata', line_id='line_toto', stop_id='stop_tutu')

    with requests_mock.Mocker() as m:
        m.post('http://bob.com/', text=mock_no_data_response(), status_code=200)
        passages = siri.next_passage_for_route_point(route_point, from_dt=_timestamp("12:00"), count=2)
        assert m.called

        assert passages == []
Exemplo n.º 14
0
def next_passage_for_route_point_failure_test():
    """
    test the whole next_passage_for_route_point

    the timeo's response is in error (status = 404), we should get 'None'
    """
    synthese = Synthese(id='tata', timezone='UTC', service_url='http://bob.com/')

    mock_requests = MockRequests({
        'http://bob.com/?SERVICE=tdg&roid=stop_tutu':
        (mock_good_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 = synthese.next_passage_for_route_point(route_point)

        assert passages is None
Exemplo n.º 15
0
def next_passage_for_route_point_test():
    """
    test the whole next_passage_for_route_point
    mock the http call to return a good response, we should get some next_passages
    """
    synthese = Synthese(id='tata',
                        timezone='UTC',
                        service_url='http://bob.com/')

    mock_requests = MockRequests({
        'http://bob.com/?SERVICE=tdg&roid=stop_tutu':
        (mock_good_response(), 200)
    })

    route_point = MockRoutePoint(route_id='route_tata',
                                 line_id='line_toto',
                                 stop_id='stop_tutu')

    with mock.patch('requests.get', mock_requests.get):
        passages = synthese.next_passage_for_route_point(route_point)

        assert len(passages) == 3

        assert passages[0].datetime == datetime.datetime(2016,
                                                         3,
                                                         29,
                                                         13,
                                                         37,
                                                         tzinfo=pytz.UTC)
        assert passages[1].datetime == datetime.datetime(2016,
                                                         3,
                                                         29,
                                                         13,
                                                         47,
                                                         tzinfo=pytz.UTC)
        assert passages[2].datetime == datetime.datetime(2016,
                                                         3,
                                                         29,
                                                         13,
                                                         57,
                                                         tzinfo=pytz.UTC)
Exemplo n.º 16
0
def make_url_test():
    timeo = Timeo(id='tata',
                  timezone='Europe/Paris',
                  service_url='http://bob.com/tata',
                  service_args={
                      'a': 'bobette',
                      'b': '12'
                  })

    url = timeo._make_url(
        MockRoutePoint(route_id='route_tata',
                       line_id='line_toto',
                       stop_id='stop_tutu'))

    # it should be a valid url
    assert validators.url(url)

    assert url.startswith('http://bob.com')
    assert 'a=bobette' in url
    assert 'b=12' in url
    assert 'StopTimeoCode=stop_tutu' in url
    assert 'LineTimeoCode=line_toto' in url
    assert 'Way=route_tata' in url