Example #1
0
def next_passage_for_route_point_with_direction_type_test(
        mock_direction_type_is_forward_response):
    """
    test the whole next_passage_for_route_point with direction_type parameter
    mock the http call to return a good response, we should get some next_passages
    """
    sytral = Sytral(id='tata', service_url='http://bob.com/')

    mock_requests = MockRequests({
        'http://bob.com/?direction_type=forward&stop_id=42':
        (mock_direction_type_is_forward_response, 200)
    })

    route_point = MockRoutePoint(line_code='05',
                                 stop_id='42',
                                 direction_type='forward')

    with mock.patch('requests.get', mock_requests.get):
        with mock.patch(
                'jormungandr.realtime_schedule.sytral.Sytral._get_direction',
                lambda Sytral, **kwargs: Direction("3341", "Piscine Chambéry"),
        ):
            passages = sytral.next_passage_for_route_point(route_point)

            assert len(passages) == 1

            assert passages[0].datetime == datetime.datetime(2016,
                                                             4,
                                                             11,
                                                             12,
                                                             37,
                                                             15,
                                                             tzinfo=pytz.UTC)
            assert passages[0].is_real_time
Example #2
0
def next_passage_for_route_point_test(mock_good_response):
    """
    test the whole next_passage_for_route_point
    mock the http call to return a good response, we should get some next_passages
    """
    sytral = Sytral(id='tata', service_url='http://bob.com/')

    mock_requests = MockRequests(
        {'http://bob.com/?stop_id=42': (mock_good_response, 200)})

    route_point = MockRoutePoint(line_code='05', stop_id='42')

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

        assert len(passages) == 2

        assert passages[0].datetime == datetime.datetime(2016,
                                                         4,
                                                         11,
                                                         12,
                                                         37,
                                                         15,
                                                         tzinfo=pytz.UTC)
        assert passages[0].is_real_time
        assert passages[1].datetime == datetime.datetime(2016,
                                                         4,
                                                         11,
                                                         12,
                                                         45,
                                                         35,
                                                         tzinfo=pytz.UTC)
        assert passages[1].is_real_time
Example #3
0
def next_passage_for_route_point_multiline_test(mock_multiline_response):
    """
    test the whole next_passage_for_route_point with a routepoint having multiple SAE lines
    """
    sytral = Sytral(id='tata', service_url='http://bob.com/')

    mock_requests = MockRequests(
        {'http://bob.com/?stop_id=42': (mock_multiline_response, 200)})

    route_point = MockRoutePoint(line_code=['05A', '05B'], stop_id='42')

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

        assert len(passages) == 2

        assert passages[0].datetime == datetime.datetime(2016,
                                                         4,
                                                         11,
                                                         12,
                                                         37,
                                                         15,
                                                         tzinfo=pytz.UTC)
        assert passages[0].is_real_time
        assert passages[1].datetime == datetime.datetime(2016,
                                                         4,
                                                         11,
                                                         12,
                                                         45,
                                                         35,
                                                         tzinfo=pytz.UTC)
        assert passages[1].is_real_time
Example #4
0
def next_passage_for_route_point_multi_stop_point_id_test(
        mock_multi_stop_point_id_response):
    """
    test next_passage for route point with multi stop point ID
    """
    sytral = Sytral(id='tata',
                    service_url='http://bob.com/',
                    instance=MockInstance())

    mock_requests = MockRequests({
        'http://bob.com/?stop_id=42&stop_id=43':
        (mock_multi_stop_point_id_response, 200)
    })

    route_point = MockRoutePoint(line_code='05', stop_id=['42', '43'])

    with mock.patch('requests.get', mock_requests.get):
        with mock.patch(
                'jormungandr.realtime_schedule.sytral.Sytral._get_direction',
                lambda Sytral, **kwargs: Direction("3341", "Piscine Chambéry"),
        ):
            passages = sytral.next_passage_for_route_point(route_point)

            assert len(passages) == 4

            # Stop id 42
            assert passages[0].datetime == datetime.datetime(2016,
                                                             4,
                                                             11,
                                                             13,
                                                             37,
                                                             15,
                                                             tzinfo=pytz.UTC)
            assert not passages[0].is_real_time
            assert passages[1].datetime == datetime.datetime(2016,
                                                             4,
                                                             11,
                                                             13,
                                                             45,
                                                             35,
                                                             tzinfo=pytz.UTC)
            assert passages[1].is_real_time
            # Stop id 43
            assert passages[2].datetime == datetime.datetime(2016,
                                                             4,
                                                             11,
                                                             13,
                                                             38,
                                                             15,
                                                             tzinfo=pytz.UTC)
            assert not passages[2].is_real_time
            assert passages[3].datetime == datetime.datetime(2016,
                                                             4,
                                                             11,
                                                             13,
                                                             47,
                                                             35,
                                                             tzinfo=pytz.UTC)
            assert passages[3].is_real_time
Example #5
0
def status_test():
    sytral = Sytral(id=u'tata-é$~#@"*!\'`§èû',
                    service_url='http://bob.com/',
                    service_args={
                        'a': 'bobette',
                        'b': '12'
                    })
    status = sytral.status()
    assert status['id'] == u"tata-é$~#@\"*!'`§èû"
Example #6
0
def make_url_test():
    sytral = Sytral(id='tata', service_url='http://bob.com/')

    url = sytral._make_url(
        MockRoutePoint(line_code='line_toto', stop_id='stop_tutu'))

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

    assert url == 'http://bob.com/?stop_id=stop_tutu'
Example #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
    """
    sytral = Sytral(id='tata', service_url='http://bob.com/')

    url = sytral._make_url(MockRoutePoint(line_code='line_toto', stop_id=None))

    assert url is None
Example #8
0
def next_passage_for_route_point_multi_stop_point_id_test(
        mock_multi_stop_point_id_response):
    """
    test next_passage for route point with multi stop point ID
    """
    sytral = Sytral(id='tata', service_url='http://bob.com/')

    mock_requests = MockRequests({
        'http://bob.com/?stop_id=42&stop_id=43':
        (mock_multi_stop_point_id_response, 200)
    })

    route_point = MockRoutePoint(line_code='05', stop_id=['42', '43'])

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

        assert len(passages) == 4

        # Stop id 42
        assert passages[0].datetime == datetime.datetime(2016,
                                                         4,
                                                         11,
                                                         13,
                                                         37,
                                                         15,
                                                         tzinfo=pytz.UTC)
        assert not passages[0].is_real_time
        assert passages[1].datetime == datetime.datetime(2016,
                                                         4,
                                                         11,
                                                         13,
                                                         45,
                                                         35,
                                                         tzinfo=pytz.UTC)
        assert passages[1].is_real_time
        # Stop id 43
        assert passages[2].datetime == datetime.datetime(2016,
                                                         4,
                                                         11,
                                                         13,
                                                         38,
                                                         15,
                                                         tzinfo=pytz.UTC)
        assert not passages[2].is_real_time
        assert passages[3].datetime == datetime.datetime(2016,
                                                         4,
                                                         11,
                                                         13,
                                                         47,
                                                         35,
                                                         tzinfo=pytz.UTC)
        assert passages[3].is_real_time
Example #9
0
def make_url_params_with_invalid_code_test():
    """
    test make_url when RoutePoint does not have a mandatory code

    we should not get any url
    """
    sytral = Sytral(id='tata', service_url='http://bob.com/')

    # The return is like [(stop_id, val1), (stop_id, val2), (direction_type, val), ...]
    params = sytral._make_params(
        MockRoutePoint(line_code='line_toto', stop_id=[]))

    assert params == None
Example #10
0
def next_passage_for_no_departures_response_test(mock_no_departure_response):
    """
    test the whole next_passage_for_route_point
    mock the http call to return a response without any departures, we should get no departure
    """
    sytral = Sytral(id='tata', service_url='http://bob.com/')

    mock_requests = MockRequests(
        {'http://bob.com/?stop_id=42': (mock_no_departure_response, 200)})

    route_point = MockRoutePoint(line_code='05', stop_id='42')

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

        assert passages == []
Example #11
0
def next_passage_with_theoric_time_response_test(mock_theoric_response):
    """
    test the whole next_passage_for_route_point
    mock the http call to return a response with a theoric time we should get one departure
    """
    sytral = Sytral(
        id='tata',
        service_url='http://bob.com/',
        service_args={
            'a': 'bobette',
            'b': '12'
        },
        instance=MockInstance(),
    )

    mock_requests = MockRequests(
        {'http://bob.com/?stop_id=42': (mock_theoric_response, 200)})

    route_point = MockRoutePoint(line_code='05', stop_id='42')

    with mock.patch('requests.get', mock_requests.get):
        with mock.patch(
                'jormungandr.realtime_schedule.sytral.Sytral._get_direction',
                lambda Sytral, **kwargs: Direction("3341", "Piscine Chambéry"),
        ):
            passages = sytral.next_passage_for_route_point(route_point)

            assert len(passages) == 2

            assert passages[0].datetime == datetime.datetime(2016,
                                                             4,
                                                             11,
                                                             13,
                                                             37,
                                                             15,
                                                             tzinfo=pytz.UTC)
            assert not passages[0].is_real_time
            assert passages[1].datetime == datetime.datetime(2016,
                                                             4,
                                                             11,
                                                             13,
                                                             45,
                                                             35,
                                                             tzinfo=pytz.UTC)
            assert passages[1].is_real_time
Example #12
0
def next_passage_for_empty_response_test(mock_empty_response):
    """
    test the whole next_passage_for_route_point
    mock the http call to return a empty response, we should get None
    """
    sytral = Sytral(id='tata',
                    service_url='http://bob.com/',
                    instance=MockInstance())

    mock_requests = MockRequests(
        {'http://bob.com/?stop_id=42': (mock_empty_response, 500)})

    route_point = MockRoutePoint(line_code='05', stop_id='42')

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

        assert passages is None
Example #13
0
def next_passage_for_missing_line_response_test(mock_missing_line_response):
    """
    test the whole next_passage_for_route_point
    mock the http call to return a response without wanted line  we should get no departure
    """
    sytral = Sytral(id='tata',
                    service_url='http://bob.com/',
                    service_args={
                        'a': 'bobette',
                        'b': '12'
                    })

    mock_requests = MockRequests(
        {'http://bob.com/?stop_id=42': (mock_missing_line_response, 200)})

    route_point = MockRoutePoint(line_code='05', stop_id='42')

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

        assert passages == []
Example #14
0
def make_url_params_test():
    sytral = Sytral(id='tata', service_url='http://bob.com/')

    # The return is like [(stop_id, val1), (stop_id, val2), (direction_type, val), ...]
    params = sytral._make_params(
        MockRoutePoint(line_code='line_toto', stop_id='stop_tutu'))

    assert params == [('stop_id', 'stop_tutu')]

    params = sytral._make_params(
        MockRoutePoint(line_code='line_toto',
                       stop_id=['stop_tutu', 'stop_toto']))

    assert params == [('stop_id', 'stop_tutu'), ('stop_id', 'stop_toto')]

    params = sytral._make_params(
        MockRoutePoint(line_code='line_toto',
                       stop_id='stop_tutu',
                       direction_type='forward'))

    assert params == [('stop_id', 'stop_tutu'), ('direction_type', 'forward')]
Example #15
0
def next_passage_for_route_point_multiline_test(mock_multiline_response):
    """
    test the whole next_passage_for_route_point with a routepoint having multiple SAE lines
    """
    sytral = Sytral(id='tata',
                    service_url='http://bob.com/',
                    instance=MockInstance())

    mock_requests = MockRequests(
        {'http://bob.com/?stop_id=42': (mock_multiline_response, 200)})

    route_point = MockRoutePoint(line_code=['05A', '05B'], stop_id='42')

    with mock.patch('requests.get', mock_requests.get):
        with mock.patch(
                'jormungandr.realtime_schedule.sytral.Sytral._get_direction',
                lambda Sytral, **kwargs: Direction("3341", "Piscine Chambéry"),
        ):
            passages = sytral.next_passage_for_route_point(route_point)

            assert len(passages) == 2

            assert passages[0].datetime == datetime.datetime(2016,
                                                             4,
                                                             11,
                                                             12,
                                                             37,
                                                             15,
                                                             tzinfo=pytz.UTC)
            assert passages[0].is_real_time
            assert passages[1].datetime == datetime.datetime(2016,
                                                             4,
                                                             11,
                                                             12,
                                                             45,
                                                             35,
                                                             tzinfo=pytz.UTC)
            assert passages[1].is_real_time
Example #16
0
def next_passage_with_theoric_time_response_test(mock_theoric_response):
    """
    test the whole next_passage_for_route_point
    mock the http call to return a response with a theoric time we should get one departure
    """
    sytral = Sytral(id='tata',
                    service_url='http://bob.com/',
                    service_args={
                        'a': 'bobette',
                        'b': '12'
                    })

    mock_requests = MockRequests(
        {'http://bob.com/?stop_id=42': (mock_theoric_response, 200)})

    route_point = MockRoutePoint(line_code='05', stop_id='42')

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

        assert len(passages) == 2

        assert passages[0].datetime == datetime.datetime(2016,
                                                         4,
                                                         11,
                                                         13,
                                                         37,
                                                         15,
                                                         tzinfo=pytz.UTC)
        assert not passages[0].is_real_time
        assert passages[1].datetime == datetime.datetime(2016,
                                                         4,
                                                         11,
                                                         13,
                                                         45,
                                                         35,
                                                         tzinfo=pytz.UTC)
        assert passages[1].is_real_time