<commit_before># coding=utf-8
# Copyright (c) 2001-2016, Canal TP and/or its affiliates. All rights reserved.
#
# This file is part of Navitia,
# the software to build cool stuff with public transport.
#
# Hope you'll enjoy and contribute to this project,
#     powered by Canal TP (www.canaltp.fr).
# Help us simplify mobility and open public transport:
#     a non ending quest to the responsive locomotion way of traveling!
#
# LICENCE: This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Stay tuned using
# twitter @navitia
# IRC #navitia on freenode
# https://groups.google.com/d/forum/navitia
# www.navitia.io
from __future__ import absolute_import, print_function, division
import mock
from jormungandr.realtime_schedule.sytral import Sytral
import validators
import datetime
import pytz
import pytest


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')]


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


class MockResponse(object):
    def __init__(self, data, status_code, url, *args, **kwargs):
        self.data = data
        self.status_code = status_code
        self.url = url

    def json(self):
        return self.data


class MockRequests(object):
    def __init__(self, responses):
        self.responses = responses

    def get(self, url, *args, **kwargs):
        params = kwargs.get('params')
        if params:
            from six.moves.urllib.parse import urlencode

            url += "?{}".format(urlencode(params, doseq=True))

        r = self.responses.get(url)
        if r:
            return MockResponse(r[0], r[1], url)
        else:
            raise Exception("impossible to find mock response for url {}".format(url))


@pytest.fixture(scope="module")
def mock_multiline_response():
    return {
        "departures": [
            {
                "direction_id": "3341",
                "direction_name": "Piscine Chambéry",
                "direction_type": "both",
                "datetime": "2016-04-11T14:37:15+02:00",
                "type": "E",
                "line": "05A",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:38:15+02:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:45:35+02:00",
                "type": "E",
                "line": "05B",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:49:35+02:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
        ]
    }


@pytest.fixture(scope="module")
def mock_good_response():
    return {
        "departures": [
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:37:15+02:00",
                "type": "E",
                "line": "05",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:38:15+02:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:45:35+02:00",
                "type": "E",
                "line": "05",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:49:35+02:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
        ]
    }


@pytest.fixture(scope="module")
def mock_direction_type_is_forward_response():
    return {
        "departures": [
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "forward",
                "datetime": "2016-04-11T14:37:15+02:00",
                "type": "E",
                "line": "05",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "forward",
                "datetime": "2016-04-11T14:38:15+02:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
        ]
    }


@pytest.fixture(scope="module")
def mock_empty_response():
    return {}


@pytest.fixture(scope="module")
def mock_no_departure_response():
    return {"departures": []}


@pytest.fixture(scope="module")
def mock_missing_line_response():
    return {
        "departures": [
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:38:15+02:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:49:35+02:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
        ]
    }


@pytest.fixture(scope="module")
def mock_theoric_response():
    return {
        "departures": [
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:37:15+01:00",
                "type": "T",
                "line": "05",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:38:15+01:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:45:35+01:00",
                "type": "E",
                "line": "05",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:49:35+01:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
        ]
    }


@pytest.fixture(scope="module")
def mock_multi_stop_point_id_response():
    return {
        "departures": [
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:37:15+01:00",
                "type": "T",
                "line": "05",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:38:15+01:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:45:35+01:00",
                "type": "E",
                "line": "05",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:49:35+01:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:38:15+01:00",
                "type": "T",
                "line": "05",
                "stop": "43",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:38:15+01:00",
                "type": "E",
                "line": "04",
                "stop": "43",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:47:35+01:00",
                "type": "E",
                "line": "05",
                "stop": "43",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:49:35+01:00",
                "type": "E",
                "line": "04",
                "stop": "43",
            },
        ]
    }


class MockRoutePoint(object):
    def __init__(self, *args, **kwargs):
        l = kwargs['line_code']
        if isinstance(l, list):
            self._hardcoded_line_ids = l
        else:
            self._hardcoded_line_ids = [l]

        l = kwargs['stop_id']
        if isinstance(l, list):
            self._hardcoded_stop_ids = l
        else:
            self._hardcoded_stop_ids = [l]

        if 'direction_type' in kwargs:
            self._hardcoded_direction_type = kwargs['direction_type']
        else:
            self._hardcoded_direction_type = None

    def fetch_all_stop_id(self, object_id_tag):
        return self._hardcoded_stop_ids

    def fetch_all_line_id(self, object_id_tag):
        return self._hardcoded_line_ids

    def fetch_direction_type(self):
        return self._hardcoded_direction_type


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


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/?stop_id=42&direction_type=forward': (mock_direction_type_is_forward_response, 200)}
    )
<commit_msg>adapt tests with common mock<commit_after># coding=utf-8
# Copyright (c) 2001-2016, Canal TP and/or its affiliates. All rights reserved.
#
# This file is part of Navitia,
# the software to build cool stuff with public transport.
#
# Hope you'll enjoy and contribute to this project,
#     powered by Canal TP (www.canaltp.fr).
# Help us simplify mobility and open public transport:
#     a non ending quest to the responsive locomotion way of traveling!
#
# LICENCE: This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Stay tuned using
# twitter @navitia
# IRC #navitia on freenode
# https://groups.google.com/d/forum/navitia
# www.navitia.io
from __future__ import absolute_import, print_function, division
import mock
from jormungandr.realtime_schedule.sytral import Sytral
from jormungandr.tests.utils_test import MockRequests
import validators
import datetime
import pytz
import pytest


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')]


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


class MockResponse(object):
    def __init__(self, data, status_code, url, *args, **kwargs):
        self.data = data
        self.status_code = status_code
        self.url = url

    def json(self):
        return self.data


@pytest.fixture(scope="module")
def mock_multiline_response():
    return {
        "departures": [
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:37:15+02:00",
                "type": "E",
                "line": "05A",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:38:15+02:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:45:35+02:00",
                "type": "E",
                "line": "05B",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:49:35+02:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
        ]
    }


@pytest.fixture(scope="module")
def mock_good_response():
    return {
        "departures": [
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:37:15+02:00",
                "type": "E",
                "line": "05",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:38:15+02:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:45:35+02:00",
                "type": "E",
                "line": "05",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:49:35+02:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
        ]
    }


@pytest.fixture(scope="module")
def mock_direction_type_is_forward_response():
    return {
        "departures": [
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "forward",
                "datetime": "2016-04-11T14:37:15+02:00",
                "type": "E",
                "line": "05",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "forward",
                "datetime": "2016-04-11T14:38:15+02:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
        ]
    }


@pytest.fixture(scope="module")
def mock_empty_response():
    return {}


@pytest.fixture(scope="module")
def mock_no_departure_response():
    return {"departures": []}


@pytest.fixture(scope="module")
def mock_missing_line_response():
    return {
        "departures": [
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:38:15+02:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:49:35+02:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
        ]
    }


@pytest.fixture(scope="module")
def mock_theoric_response():
    return {
        "departures": [
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:37:15+01:00",
                "type": "T",
                "line": "05",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:38:15+01:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:45:35+01:00",
                "type": "E",
                "line": "05",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:49:35+01:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
        ]
    }


@pytest.fixture(scope="module")
def mock_multi_stop_point_id_response():
    return {
        "departures": [
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:37:15+01:00",
                "type": "T",
                "line": "05",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:38:15+01:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:45:35+01:00",
                "type": "E",
                "line": "05",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:49:35+01:00",
                "type": "E",
                "line": "04",
                "stop": "42",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:38:15+01:00",
                "type": "T",
                "line": "05",
                "stop": "43",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:38:15+01:00",
                "type": "E",
                "line": "04",
                "stop": "43",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:47:35+01:00",
                "type": "E",
                "line": "05",
                "stop": "43",
            },
            {
                "direction_id": "3341",
                "direction_name": "<NAME>",
                "direction_type": "both",
                "datetime": "2016-04-11T14:49:35+01:00",
                "type": "E",
                "line": "04",
                "stop": "43",
            },
        ]
    }


class MockRoutePoint(object):
    def __init__(self, *args, **kwargs):
        l = kwargs['line_code']
        if isinstance(l, list):
            self._hardcoded_line_ids = l
        else:
            self._hardcoded_line_ids = [l]

        l = kwargs['stop_id']
        if isinstance(l, list):
            self._hardcoded_stop_ids = l
        else:
            self._hardcoded_stop_ids = [l]

        if 'direction_type' in kwargs:
            self._hardcoded_direction_type = kwargs['direction_type']
        else:
            self._hardcoded_direction_type = None

    def fetch_all_stop_id(self, object_id_tag):
        return self._hardcoded_stop_ids

    def fetch_all_line_id(self, object_id_tag):
        return self._hardcoded_line_ids

    def fetch_direction_type(self):
        return self._hardcoded_direction_type


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


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)}
    )

{'fraction_non_alphanumeric': 0.1463596393173858, 'fraction_numerical': 0.05097250167672703, 'mean_word_length': 1.6481499753330044, 'pattern_counts': {'":': 350, '<': 52, '<?xml version=': 0, '>': 52, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6036525', 'n_tokens_mistral': 9117, 'n_tokens_neox': 7908, 'n_words': 1666}