コード例 #1
0
    def test_http_error(self) -> None:
        """Tests the case when we keep getting HTTP errors."""
        mock_overpass_sleep_called = False

        def mock_overpass_sleep() -> None:
            nonlocal mock_overpass_sleep_called
            mock_overpass_sleep_called = True

        with unittest.mock.patch("cron.overpass_sleep", mock_overpass_sleep):
            with unittest.mock.patch('urllib.request.urlopen',
                                     mock_urlopen_raise_error):
                relations = get_relations()
                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(relations, update=True)
                self.assertTrue(mock_overpass_sleep_called)
                # 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)
コード例 #2
0
ファイル: test_cron.py プロジェクト: urbalazs/osm-gimmisn
 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)
コード例 #3
0
    def test_happy(self) -> None:
        """Tests the happy path."""
        mock_overpass_sleep_called = False

        def mock_overpass_sleep() -> None:
            nonlocal mock_overpass_sleep_called
            mock_overpass_sleep_called = True

        result_from_overpass = "******"

        def mock_urlopen(_url: str, _data: Optional[bytes] = None) -> BinaryIO:
            buf = io.BytesIO()
            buf.write(result_from_overpass.encode('utf-8'))
            buf.seek(0)
            return buf

        with unittest.mock.patch("cron.overpass_sleep", mock_overpass_sleep):
            with unittest.mock.patch('urllib.request.urlopen', mock_urlopen):
                relations = get_relations()
                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(relations, update=True)
                mtime = os.path.getmtime(path)
                cron.update_osm_streets(relations, update=False)
                self.assertEqual(os.path.getmtime(path), mtime)
                self.assertTrue(mock_overpass_sleep_called)
                actual = util.get_content(relations.get_workdir(), "streets-gazdagret.csv")
                self.assertEqual(actual, expected)
コード例 #4
0
ファイル: test_cron.py プロジェクト: urbalazs/osm-gimmisn
 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)