Ejemplo n.º 1
0
 def test_happy(self) -> None:
     """Tests the happy path."""
     relations = areas.Relations(test_context.make_test_context())
     relation = relations.get_relation("gazdagret")
     filters = relation.get_street_ranges()
     expected_filters = {
         "Budaörsi út":
         ranges.Ranges([make_range(137, 165)]),
         "Csiki-hegyek utca":
         ranges.Ranges([make_range(1, 15),
                        make_range(2, 26)]),
         'Hamzsabégi út':
         ranges.Ranges([ranges.Range(start=1, end=12, interpolation="all")])
     }
     self.assertEqual(filters, expected_filters)
     expected_streets = {
         'OSM Name 1': 'Ref Name 1',
         'OSM Name 2': 'Ref Name 2',
         'Misspelled OSM Name 1': 'OSM Name 1',
     }
     relations = areas.Relations(test_context.make_test_context())
     self.assertEqual(
         relations.get_relation("gazdagret").get_config().get_refstreets(),
         expected_streets)
     street_blacklist = relations.get_relation(
         "gazdagret").get_config().get_street_filters()
     self.assertEqual(street_blacklist, ['Only In Ref Nonsense utca'])
Ejemplo n.º 2
0
 def test_no_path(self) -> None:
     """Tests the case when PATH_INFO is empty (should give the main page)."""
     environ = {
         "PATH_INFO": ""
     }
     ctx = test_context.make_test_context()
     relations = areas.Relations(test_context.make_test_context())
     ret = webframe.get_request_uri(environ, ctx, relations)
     self.assertEqual(ret, "")
Ejemplo n.º 3
0
 def test_happy(self) -> None:
     """Tests the happy path."""
     ctx = test_context.make_test_context()
     relations = areas.Relations(ctx)
     # Expect an alias -> canonicalname map.
     expected = {"budapest_22": "budafok"}
     self.assertEqual(relations.get_aliases(), expected)
Ejemplo n.º 4
0
 def test_view_result_update_result_link(self) -> None:
     """Tests view result: the update-result link."""
     ctx = test_context.make_test_context()
     root = self.get_dom_for_path("/street-housenumbers/gazdagret/view-result")
     uri = ctx.get_ini().get_uri_prefix() + "/missing-housenumbers/gazdagret/view-result"
     results = root.findall("body/div[@id='toolbar']/a[@href='" + uri + "']")
     self.assertTrue(results)
Ejemplo n.º 5
0
 def test_happy(self) -> None:
     """Tests the happy path."""
     relations = areas.Relations(test_context.make_test_context())
     relation = relations.get_relation("test")
     actual = [i.get_osm_name() for i in relation.get_osm_streets()]
     expected = ['B1', 'B2', 'HB1', 'HB2']
     self.assertEqual(actual, expected)
Ejemplo n.º 6
0
    def test_happy(self) -> None:
        """Tests tha happy path."""
        ctx = test_context.make_test_context()
        file_system = test_context.TestFileSystem()

        today_csv = util.to_bytes(
            """addr:postcode	addr:city	addr:street	addr:housenumber	@user
7677	Orfű	Dollár utca	1	mgpx
""")
        today_csv_value = io.BytesIO(today_csv)
        today_csv_value.__setattr__("close", lambda: None)
        today_topusers_value = io.BytesIO()
        today_topusers_value.__setattr__("close", lambda: None)
        today_usercount_value = io.BytesIO()
        today_usercount_value.__setattr__("close", lambda: None)
        files = {
            ctx.get_abspath("workdir/stats/2020-05-10.csv"):
            today_csv_value,
            ctx.get_abspath("workdir/stats/2020-05-10.topusers"):
            today_topusers_value,
            ctx.get_abspath("workdir/stats/2020-05-10.usercount"):
            today_usercount_value,
        }
        file_system.set_files(files)
        ctx.set_file_system(file_system)

        cron.update_stats_topusers(ctx, "2020-05-10")

        self.assertTrue(today_topusers_value.tell())
        self.assertTrue(today_usercount_value.tell())
Ejemplo n.º 7
0
    def test_happy(self) -> None:
        """Tests the happy path."""
        argv = ["", "tests/mock/access_log"]
        buf = io.StringIO()
        ctx = test_context.make_test_context()
        ctx.set_time(test_context.make_test_time())
        ctx.set_time(test_context.make_test_time())
        relations_path = ctx.get_abspath("data/relations.yaml")
        # 2020-05-09, so this will be recent
        outputs = {
            "git blame --line-porcelain " + relations_path: b"""
author-time 1588975200
\tujbuda:
"""
        }
        subprocess = test_context.TestSubprocess(outputs)
        ctx.set_subprocess(subprocess)
        parse_access_log.main(argv, buf, ctx)

        buf.seek(0)
        actual = buf.read()
        self.assertIn("data/relation-inactiverelation.yaml: set inactive: false\n", actual)
        self.assertIn("data/relation-gazdagret.yaml: set inactive: true\n", actual)
        self.assertNotIn("data/relation-nosuchrelation.yaml: set inactive: ", actual)

        # This is not in the output because it's considered as a recent relation.
        self.assertNotIn("data/relation-ujbuda.yaml: set inactive: ", actual)

        # This is not in the output as it's not a valid relation name.
        self.assertNotIn("budafokxxx", actual)

        # This is not in the output as it's a search bot, so such visits don't count.
        # Also, if this would be not ignored, it would push 'inactiverelation' out of the active
        # relation list.
        self.assertNotIn("gyomaendrod", actual)
Ejemplo n.º 8
0
 def test_happy(self) -> None:
     """Tests the happy path."""
     relations = areas.Relations(test_context.make_test_context())
     relation_name = "gazdagret"
     relation = relations.get_relation(relation_name)
     ret = relation.get_osm_housenumbers_query()
     self.assertEqual(ret, 'housenr aaa 2713748 bbb 3602713748 ccc\n')
Ejemplo n.º 9
0
    def test_happy(self) -> None:
        """Tests the happy path."""
        ctx = test_context.make_test_context()
        ctx.set_time(test_context.make_test_time())
        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)

        # Create a CSV that is definitely old enough to be removed.
        old_path = ctx.get_abspath("workdir/stats/old.csv")
        create_old_file(old_path)

        today = time.strftime("%Y-%m-%d")
        path = ctx.get_abspath("workdir/stats/%s.csv" % today)
        cron.update_stats(ctx, overpass=True)
        actual = util.get_content(path)
        self.assertEqual(actual,
                         util.get_content("tests/network/overpass-stats.csv"))

        # Make sure that the old CSV is removed.
        self.assertFalse(os.path.exists(old_path))

        with open(ctx.get_abspath("workdir/stats/ref.count"), "r") as stream:
            num_ref = int(stream.read().strip())
        self.assertEqual(num_ref, 300)
Ejemplo n.º 10
0
 def test_street_is_node(self) -> None:
     """Tests the case when the street name is coming from a house number (node)."""
     relations = areas.Relations(test_context.make_test_context())
     relation = relations.get_relation("gh830")
     actual = relation.get_osm_streets()
     self.assertEqual(len(actual), 1)
     self.assertEqual(actual[0].get_osm_type(), "node")
Ejemplo n.º 11
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())
Ejemplo n.º 12
0
 def test_happy(self) -> None:
     """Tests the happy 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-streets-gazdagret.csv"),
     ]
     network = test_context.TestNetwork(routes)
     ctx.set_network(network)
     relations = areas.Relations(ctx)
     for relation_name in relations.get_active_names():
         if relation_name != "gazdagret":
             relations.get_relation(relation_name).get_config().set_active(
                 False)
     expected = util.get_content(relations.get_workdir(),
                                 "streets-gazdagret.csv")
     path = os.path.join(relations.get_workdir(), "streets-gazdagret.csv")
     os.unlink(path)
     cron.update_osm_streets(ctx, relations, update=True)
     mtime = os.path.getmtime(path)
     cron.update_osm_streets(ctx, relations, update=False)
     self.assertEqual(os.path.getmtime(path), mtime)
     actual = util.get_content(relations.get_workdir(),
                               "streets-gazdagret.csv")
     self.assertEqual(actual, expected)
Ejemplo n.º 13
0
 def test_happy(self) -> None:
     """Tests the happy path."""
     relation_name = "ujbuda"
     relations = areas.Relations(test_context.make_test_context())
     relation = relations.get_relation(relation_name)
     ret = relation.get_config().should_check_missing_streets()
     self.assertEqual(ret, "only")
Ejemplo n.º 14
0
 def test_happy(self) -> None:
     """Tests the happy path."""
     ctx = test_context.make_test_context()
     relations = areas.Relations(ctx)
     relation = relations.get_relation("budafok")
     actual = wsgi.handle_main_housenr_additional_count(ctx, relation)
     self.assertIn("42 house numbers", actual.getvalue())
Ejemplo n.º 15
0
 def test_happy(self) -> None:
     """Tests the happy path."""
     ctx = test_context.make_test_context()
     relations = areas.Relations(ctx)
     for relation_name in relations.get_active_names():
         # gellerthegy is streets=no
         if relation_name not in ("gazdagret", "gellerthegy"):
             relations.get_relation(relation_name).get_config().set_active(
                 False)
     path = os.path.join(relations.get_workdir(),
                         "gazdagret-additional-streets.count")
     expected = "1"
     if os.path.exists(path):
         util.get_content(path)
         os.unlink(path)
     cron.update_additional_streets(ctx, relations, update=True)
     mtime = os.path.getmtime(path)
     cron.update_additional_streets(ctx, relations, update=False)
     self.assertEqual(os.path.getmtime(path), mtime)
     actual = util.get_content(path).decode("utf-8")
     self.assertEqual(actual, expected)
     # Make sure street stat is not created for the streets=no case.
     self.assertFalse(
         os.path.exists(
             os.path.join(relations.get_workdir(),
                          "gellerthegy-additional-streets.count")))
Ejemplo n.º 16
0
 def test_happy(self) -> None:
     """Tests the happy path."""
     ctx = test_context.make_test_context()
     relations = areas.Relations(ctx)
     self.assertEqual(relations.refcounty_get_refsettlement_ids("01"),
                      ["011", "012"])
     self.assertEqual(relations.refcounty_get_refsettlement_ids("99"), [])
Ejemplo n.º 17
0
 def test_http_error(self) -> None:
     """Tests the case when we keep getting HTTP errors."""
     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=""),
     ]
     network = test_context.TestNetwork(routes)
     ctx.set_network(network)
     relations = areas.Relations(ctx)
     for relation_name in relations.get_active_names():
         if relation_name != "gazdagret":
             relations.get_relation(relation_name).get_config().set_active(
                 False)
     expected = util.get_content(relations.get_workdir(),
                                 "streets-gazdagret.csv")
     cron.update_osm_streets(ctx, relations, update=True)
     # Make sure that in case we keep getting errors we give up at some stage and
     # leave the last state unchanged.
     actual = util.get_content(relations.get_workdir(),
                               "streets-gazdagret.csv")
     self.assertEqual(actual, expected)
Ejemplo n.º 18
0
 def test_nosuchrelation(self) -> None:
     """Tests a relation without a filter file."""
     relation_name = "nosuchrelation"
     relations = areas.Relations(test_context.make_test_context())
     relation = relations.get_relation(relation_name)
     ret = relation.get_config().should_check_missing_streets()
     self.assertEqual(ret, "yes")
Ejemplo n.º 19
0
    def test_invalid_simplify(self) -> None:
        """Tests how 'invalid' interacts with housenumber-letters: true or false."""
        relations = areas.Relations(test_context.make_test_context())
        relation_name = "gh385"
        relation = relations.get_relation(relation_name)

        # Default case: housenumber-letters=false.
        filters: Dict[str, Any] = {"Kővirág sor": {"invalid": ["37b"]}}
        relation.get_config().set_filters(filters)
        ongoing_streets, _done_streets = relation.get_missing_housenumbers()
        # Note how 37b from invalid is simplified to 37; and how 37/B from ref is simplified to
        # 37 as well, so we find the match.
        self.assertFalse(len(ongoing_streets))

        # Opt-in case: housenumber-letters=true.
        relation.get_config().set_housenumber_letters(True)
        filters = {"Kővirág sor": {"invalid": ["37b"]}}
        relation.get_config().set_filters(filters)
        ongoing_streets, _done_streets = relation.get_missing_housenumbers()
        # In this case 37b from invalid matches 37/B from ref.
        self.assertFalse(len(ongoing_streets))

        # Make sure out-of-range invalid elements are just ignored and no exception is raised.
        relation.get_config().set_housenumber_letters(False)
        filters = {
            "Kővirág sor": {
                "invalid": ["5"],
                "ranges": [{
                    "start": "1",
                    "end": "3"
                }],
            }
        }
        relation.get_config().set_filters(filters)
        relation.get_missing_housenumbers()
Ejemplo n.º 20
0
 def test_update_result_link(self) -> None:
     """Tests if the update-result output links back to the correct page."""
     root = self.get_dom_for_path("/missing-housenumbers/gazdagret/update-result")
     ctx = test_context.make_test_context()
     prefix = ctx.get_ini().get_uri_prefix()
     results = root.findall("body/a[@href='" + prefix + "/missing-housenumbers/gazdagret/view-result']")
     self.assertEqual(len(results), 1)
Ejemplo n.º 21
0
 def test_no_house_number(self) -> None:
     """Tests the case when we have streets, but no house numbers."""
     relations = areas.Relations(test_context.make_test_context())
     relation = relations.get_relation("ujbuda")
     actual = [i.get_osm_name() for i in relation.get_osm_streets()]
     expected = ['OSM Name 1', 'Törökugrató utca', 'Tűzkő utca']
     self.assertEqual(actual, expected)
Ejemplo n.º 22
0
 def test_happy(self) -> None:
     """Tests the happy path."""
     ctx = test_context.make_test_context()
     relations = areas.Relations(ctx)
     self.assertEqual(relations.refsettlement_get_name("01", "011"),
                      "Újbuda")
     self.assertEqual(relations.refsettlement_get_name("99", ""), "")
     self.assertEqual(relations.refsettlement_get_name("01", "99"), "")
Ejemplo n.º 23
0
 def test_empty(self) -> None:
     """Tests the default value."""
     relation_name = "empty"
     relations = areas.Relations(test_context.make_test_context())
     relation = relations.get_relation(relation_name)
     self.assertEqual(relation.get_name(), "empty")
     ret = relation.get_config().should_check_missing_streets()
     self.assertEqual(ret, "yes")
Ejemplo n.º 24
0
 def test_separator_semicolon(self) -> None:
     """Tests the case when ';' is a separator."""
     relations = areas.Relations(test_context.make_test_context())
     relation = relations.get_relation("gazdagret")
     normalizers = relation.get_street_ranges()
     house_numbers = areas.normalize(relation, "1;2", "Budaörs út",
                                     normalizers)
     self.assertEqual([i.get_number() for i in house_numbers], ["1", "2"])
Ejemplo n.º 25
0
 def test_happy(self) -> None:
     """Tests the happy path."""
     relation_name = "gazdagret"
     street_name = "Törökugrató utca"
     relations = areas.Relations(test_context.make_test_context())
     relation = relations.get_relation(relation_name)
     house_numbers = relation.get_osm_housenumbers(street_name)
     self.assertEqual([i.get_number() for i in house_numbers], ["1", "2"])
Ejemplo n.º 26
0
 def test_separator_interval_parity(self) -> None:
     """Tests the 5-8 case: means just 5 and 8 as the parity doesn't match."""
     relations = areas.Relations(test_context.make_test_context())
     relation = relations.get_relation("gazdagret")
     normalizers = relation.get_street_ranges()
     house_numbers = areas.normalize(relation, "5-8", "Budaörs út",
                                     normalizers)
     self.assertEqual([i.get_number() for i in house_numbers], ["5", "8"])
Ejemplo n.º 27
0
 def test_not_a_number(self) -> None:
     """Tests the case when the house number is not a number."""
     relations = areas.Relations(test_context.make_test_context())
     relation = relations.get_relation("gazdagret")
     normalizers = relation.get_street_ranges()
     house_numbers = areas.normalize(relation, "x", "Budaörsi út",
                                     normalizers)
     self.assertEqual(house_numbers, [])
Ejemplo n.º 28
0
 def test_nofilter(self) -> None:
     """Tests the case when there is no filter for this street."""
     relations = areas.Relations(test_context.make_test_context())
     relation = relations.get_relation("gazdagret")
     normalizers = relation.get_street_ranges()
     house_numbers = areas.normalize(relation, "1", "Budaörs út",
                                     normalizers)
     self.assertEqual([i.get_number() for i in house_numbers], ["1"])
Ejemplo n.º 29
0
 def test_addr_place(self) -> None:
     """Tests the case when addr:place is used instead of addr:street."""
     relation_name = "gh964"
     relations = areas.Relations(test_context.make_test_context())
     relation = relations.get_relation(relation_name)
     street_name = "Tolvajos tanya"
     house_numbers = relation.get_osm_housenumbers(street_name)
     self.assertEqual([i.get_number() for i in house_numbers], ["52"])
Ejemplo n.º 30
0
 def test_happy(self) -> None:
     """Tests the happy path."""
     relations = areas.Relations(test_context.make_test_context())
     relation = relations.get_relation("gazdagret")
     normalizers = relation.get_street_ranges()
     house_numbers = areas.normalize(relation, "139", "Budaörsi út",
                                     normalizers)
     self.assertEqual([i.get_number() for i in house_numbers], ["139"])