示例#1
0
    def test_happy(self) -> None:
        """Tests the happy path."""
        calls = 0

        def count_calls(_relations: areas.Relation, _update: bool) -> None:
            nonlocal calls
            calls += 1

        relations = get_relations()
        with unittest.mock.patch("cron.update_osm_streets", count_calls):
            with unittest.mock.patch("cron.update_osm_housenumbers", count_calls):
                with unittest.mock.patch("cron.update_ref_streets", count_calls):
                    with unittest.mock.patch("cron.update_ref_housenumbers", count_calls):
                        with unittest.mock.patch("cron.update_missing_streets", count_calls):
                            with unittest.mock.patch("cron.update_missing_housenumbers", count_calls):
                                cron.our_main(relations, mode="relations", update=True, overpass=True)

        expected = 0
        # Consider what to update automatically: the 2 sources and the diff between them.
        for _ in ("osm", "ref", "missing"):
            # What object types we have.
            for _ in ("streets", "housenumbers"):
                expected += 1

        self.assertEqual(calls, expected)
示例#2
0
    def test_stats(self) -> None:
        """Tests the stats path."""
        ctx = test_context.make_test_context()
        routes: List[test_context.URLRoute] = [
            test_context.URLRoute(
                url="https://overpass-api.de/api/status",
                data_path="",
                result_path="tests/network/overpass-status-happy.txt"),
            test_context.URLRoute(
                url="https://overpass-api.de/api/interpreter",
                data_path="",
                result_path="tests/network/overpass-stats.csv"),
        ]
        network = test_context.TestNetwork(routes)
        ctx.set_network(network)
        file_system = test_context.TestFileSystem()
        stats_value = io.BytesIO()
        stats_value.__setattr__("close", lambda: None)
        files = {
            ctx.get_abspath("workdir/stats/stats.json"): stats_value,
        }
        file_system.set_files(files)
        ctx.set_file_system(file_system)

        relations = areas.Relations(ctx)
        cron.our_main(ctx,
                      relations,
                      mode="stats",
                      update=False,
                      overpass=True)

        self.assertTrue(stats_value.tell())
示例#3
0
    def test_stats(self) -> None:
        """Tests the stats path."""
        calls = 0

        def count_calls(_overpass: bool) -> None:
            nonlocal calls
            calls += 1

        relations = get_relations()
        with unittest.mock.patch("cron.update_stats", count_calls):
            cron.our_main(relations, mode="stats", update=False, overpass=True)

        self.assertEqual(calls, 1)
示例#4
0
    def test_stats(self) -> None:
        """Tests the stats path."""
        calls = 0

        def count_calls() -> None:
            nonlocal calls
            calls += 1

        with unittest.mock.patch('config.get_abspath', get_abspath):
            relations = get_relations()
            with unittest.mock.patch("cron.update_stats", count_calls):
                cron.our_main(relations, mode="stats", update=False)

        self.assertEqual(calls, 1)
示例#5
0
    def test_happy(self) -> None:
        """Tests the happy path."""
        calls = 0

        def count_calls(
                _relations: areas.Relation,
                _config: Optional[configparser.ConfigParser] = None) -> None:
            nonlocal calls
            calls += 1

        with unittest.mock.patch('util.get_abspath', get_abspath):
            relations = get_relations()
            config = webframe.get_config()
            with unittest.mock.patch("cron.update_osm_streets", count_calls):
                with unittest.mock.patch("cron.update_osm_housenumbers",
                                         count_calls):
                    with unittest.mock.patch("cron.update_ref_streets",
                                             count_calls):
                        with unittest.mock.patch(
                                "cron.update_ref_housenumbers", count_calls):
                            with unittest.mock.patch(
                                    "cron.update_missing_streets",
                                    count_calls):
                                with unittest.mock.patch(
                                        "cron.update_missing_housenumbers",
                                        count_calls):
                                    cron.our_main(relations, config)

        expected = 0
        # Consider what to update automatically: the 2 sources and the diff between them.
        for _ in ("osm", "ref", "missing"):
            # What object types we have.
            for _ in ("streets", "housenumbers"):
                expected += 1

        self.assertEqual(calls, expected)
示例#6
0
    def test_happy(self) -> None:
        """Tests the happy path."""
        ctx = test_context.make_test_context()
        routes: List[test_context.URLRoute] = [
            # For update_osm_streets().
            test_context.URLRoute(
                url="https://overpass-api.de/api/status",
                data_path="",
                result_path="tests/network/overpass-status-happy.txt"),
            test_context.URLRoute(
                url="https://overpass-api.de/api/interpreter",
                data_path="",
                result_path="tests/network/overpass-streets-gazdagret.csv"),
            # For update_osm_housenumbers().
            test_context.URLRoute(
                url="https://overpass-api.de/api/interpreter",
                data_path="",
                result_path="tests/network/overpass-housenumbers-gazdagret.csv"
            ),
        ]
        network = test_context.TestNetwork(routes)
        ctx.set_network(network)
        file_system = test_context.TestFileSystem()
        yamls_cache = {
            "relations.yaml": {
                "gazdagret": {
                    "osmrelation": 2713748,
                    "refcounty": "01",
                    "refsettlement": "011",
                },
            },
            "refcounty-names.yaml": {},
            "refsettlement-names.yaml": {},
        }
        yamls_cache_value = io.BytesIO()
        yamls_cache_value.__setattr__("close", lambda: None)
        yamls_cache_value.write(util.to_bytes(json.dumps(yamls_cache)))
        yamls_cache_value.seek(0)
        osm_streets_value = io.BytesIO()
        osm_streets_value.__setattr__("close", lambda: None)
        osm_housenumbers_value = io.BytesIO()
        osm_housenumbers_value.__setattr__("close", lambda: None)
        ref_streets_value = io.BytesIO()
        ref_streets_value.__setattr__("close", lambda: None)
        ref_housenumbers_value = io.BytesIO()
        ref_housenumbers_value.__setattr__("close", lambda: None)
        missing_streets_value = io.BytesIO()
        missing_streets_value.__setattr__("close", lambda: None)
        missing_housenumbers_value = io.BytesIO()
        missing_housenumbers_value.__setattr__("close", lambda: None)
        additional_streets_value = io.BytesIO()
        additional_streets_value.__setattr__("close", lambda: None)
        files = {
            ctx.get_abspath("data/yamls.cache"):
            yamls_cache_value,
            ctx.get_abspath("workdir/streets-gazdagret.csv"):
            osm_streets_value,
            ctx.get_abspath("workdir/street-housenumbers-gazdagret.csv"):
            osm_housenumbers_value,
            ctx.get_abspath("workdir/streets-reference-gazdagret.lst"):
            ref_streets_value,
            ctx.get_abspath("workdir/street-housenumbers-reference-gazdagret.lst"):
            ref_housenumbers_value,
            ctx.get_abspath("workdir/gazdagret-streets.percent"):
            missing_streets_value,
            ctx.get_abspath("workdir/gazdagret.percent"):
            missing_housenumbers_value,
            ctx.get_abspath("workdir/gazdagret-additional-streets.count"):
            additional_streets_value,
        }
        file_system.set_files(files)
        ctx.set_file_system(file_system)
        relations = areas.Relations(ctx)

        cron.our_main(ctx,
                      relations,
                      mode="relations",
                      update=True,
                      overpass=True)

        # update_osm_streets() is called.
        self.assertTrue(osm_streets_value.tell())
        # update_osm_housenumbers() is called.
        self.assertTrue(osm_housenumbers_value.tell())
        # update_ref_streets() is called.
        self.assertTrue(ref_streets_value.tell())
        # update_ref_housenumbers() is called.
        self.assertTrue(ref_housenumbers_value.tell())
        # update_missing_streets() is called.
        self.assertTrue(missing_streets_value.tell())
        # update_missing_housenumbers() is called.
        self.assertTrue(missing_housenumbers_value.tell())
        # update_additional_streets() is called.
        self.assertTrue(additional_streets_value.tell())