|
| 1 | +# Copyright (c) 2001-2024, Hove and/or its affiliates. All rights reserved. |
| 2 | +# |
| 3 | +# This file is part of Navitia, |
| 4 | +# the software to build cool stuff with public transport. |
| 5 | +# |
| 6 | +# Hope you'll enjoy and contribute to this project, |
| 7 | +# powered by Hove (www.hove.com). |
| 8 | +# Help us simplify mobility and open public transport: |
| 9 | +# a non ending quest to the responsive locomotion way of traveling! |
| 10 | +# |
| 11 | +# LICENCE: This program is free software; you can redistribute it and/or modify |
| 12 | +# it under the terms of the GNU Affero General Public License as published by |
| 13 | +# the Free Software Foundation, either version 3 of the License, or |
| 14 | +# (at your option) any later version. |
| 15 | +# |
| 16 | +# This program is distributed in the hope that it will be useful, |
| 17 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +# GNU Affero General Public License for more details. |
| 20 | +# |
| 21 | +# You should have received a copy of the GNU Affero General Public License |
| 22 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | +# |
| 24 | +# Stay tuned using |
| 25 | +# twitter @navitia |
| 26 | +# channel `#navitia` on riot https://riot.im/app/#/room/#navitia:matrix.org |
| 27 | +# https://groups.google.com/d/forum/navitia |
| 28 | +# www.navitia.io |
| 29 | +import pytest |
| 30 | +import shapely |
| 31 | +from collections import namedtuple |
| 32 | +from datetime import date |
| 33 | + |
| 34 | +from jormungandr.excluded_zones_manager import ExcludedZonesManager |
| 35 | + |
| 36 | +shape_str = "POLYGON ((2.3759783199572553 48.8446081592198, 2.3739259200592358 48.84555123669577, 2.372796492896299 48.84471205845662, 2.372590038468587 48.84448827521729, 2.3768770039353626 48.842122505530256, 2.3793180239319156 48.84329741187722, 2.3759783199572553 48.8446081592198))" |
| 37 | + |
| 38 | + |
| 39 | +def mock_get_all_excluded_zones(): |
| 40 | + |
| 41 | + return [ |
| 42 | + { |
| 43 | + "id": "Gare de lyon 1", |
| 44 | + "instance": "toto", |
| 45 | + "poi": "poi:gare_de_lyon", |
| 46 | + "activation_periods": [{"from": "20240810", "to": "20240812"}], |
| 47 | + "modes": ["walking", "bike"], |
| 48 | + "shape": shape_str, |
| 49 | + }, |
| 50 | + { |
| 51 | + "id": "Gare de lyon 2", |
| 52 | + "instance": "toto", |
| 53 | + "poi": "poi:gare_de_lyon", |
| 54 | + "activation_periods": [ |
| 55 | + {"from": "20240801", "to": "20240802"}, |
| 56 | + {"from": "20240810", "to": "20240812"}, |
| 57 | + ], |
| 58 | + "modes": ["bike"], |
| 59 | + "shape": shape_str, |
| 60 | + }, |
| 61 | + ] |
| 62 | + |
| 63 | + |
| 64 | +@pytest.fixture(scope="function", autouse=True) |
| 65 | +def mock_http_karos(monkeypatch): |
| 66 | + monkeypatch.setattr( |
| 67 | + 'jormungandr.excluded_zones_manager.ExcludedZonesManager.get_all_excluded_zones', |
| 68 | + mock_get_all_excluded_zones, |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +def get_excluded_zones_test(): |
| 73 | + zones = ExcludedZonesManager.get_excluded_zones() |
| 74 | + assert len(zones) == 2 |
| 75 | + |
| 76 | + zones = ExcludedZonesManager.get_excluded_zones(mode='walking') |
| 77 | + assert len(zones) == 1 |
| 78 | + assert zones[0]["id"] == "Gare de lyon 1" |
| 79 | + |
| 80 | + request_date = date.fromisoformat('2024-08-10') |
| 81 | + zones = ExcludedZonesManager.get_excluded_zones(mode='bike', date=request_date) |
| 82 | + assert len(zones) == 2 |
| 83 | + |
| 84 | + request_date = date.fromisoformat('2024-08-01') |
| 85 | + zones = ExcludedZonesManager.get_excluded_zones(mode='bike', date=request_date) |
| 86 | + assert len(zones) == 1 |
| 87 | + assert zones[0]["id"] == "Gare de lyon 2" |
| 88 | + |
| 89 | + request_date = date.fromisoformat('2024-08-01') |
| 90 | + zones = ExcludedZonesManager.get_excluded_zones(mode='walking', date=request_date) |
| 91 | + assert len(zones) == 0 |
| 92 | + |
| 93 | + |
| 94 | +def excluded_manager_is_excluded_test(): |
| 95 | + Coord = namedtuple('Coord', ['lon', 'lat']) |
| 96 | + |
| 97 | + ExcludedZonesManager.excluded_shapes["poi:gare_de_lyon"] = shapely.wkt.loads(shape_str) |
| 98 | + |
| 99 | + res = ExcludedZonesManager.is_excluded(Coord(1, 1), mode='walking', timestamp=1723280205) |
| 100 | + assert not res |
| 101 | + |
| 102 | + res = ExcludedZonesManager.is_excluded(Coord(2.376365, 48.843402), mode='walking', timestamp=1723280205) |
| 103 | + assert res |
| 104 | + |
| 105 | + res = ExcludedZonesManager.is_excluded(Coord(2.376365, 48.843402), mode='walking', timestamp=1722502605) |
| 106 | + assert not res |
0 commit comments