Exemple #1
0
    def test_http_error(self) -> None:
        """Tests the case when we keep getting HTTP errors."""
        actual_args: List[str] = []
        actual_check = False

        def mock_subprocess_run(args: List[str], check: bool) -> None:
            nonlocal actual_args
            nonlocal actual_check
            actual_args = args
            actual_check = check

        mock_overpass_sleep_called = False

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

        with unittest.mock.patch('config.get_abspath', get_abspath):
            with unittest.mock.patch("cron.overpass_sleep", mock_overpass_sleep):
                with unittest.mock.patch('urllib.request.urlopen', mock_urlopen_raise_error):
                    with unittest.mock.patch('subprocess.run', mock_subprocess_run):
                        cron.update_stats()
        self.assertTrue(mock_overpass_sleep_called)
        self.assertTrue(actual_args[0].endswith("stats-daily.sh"))
        self.assertTrue(actual_check)
Exemple #2
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)
Exemple #3
0
    def test_no_overpass(self) -> None:
        """Tests the case when we don't call overpass."""
        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('datetime.date', MockDate):
                cron.update_stats(overpass=False)
        self.assertFalse(mock_overpass_sleep_called)
Exemple #4
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):
                with unittest.mock.patch('datetime.date', MockDate):
                    cron.update_stats(overpass=True)
        self.assertTrue(mock_overpass_sleep_called)
Exemple #5
0
 def test_http_error(self) -> None:
     """Tests the case when we keep getting HTTP errors."""
     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"),
     ]
     network = test_context.TestNetwork(routes)
     ctx.set_network(network)
     old_mtime = ctx.get_file_system().getmtime(
         ctx.get_abspath("workdir/stats/stats.json"))
     cron.update_stats(ctx, overpass=True)
     new_mtime = ctx.get_file_system().getmtime(
         ctx.get_abspath("workdir/stats/stats.json"))
     self.assertGreater(new_mtime, old_mtime)
Exemple #6
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 = "******"
        result_from_overpass += "7677\tOrfű\tDollár utca\t1\tvasony\n"
        result_from_overpass += "7677\tOrfű\tDollár utca\t2\tvasony\n"

        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

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

        today = time.strftime("%Y-%m-%d")
        path = config.get_abspath("workdir/stats/%s.csv" % today)
        with unittest.mock.patch("cron.overpass_sleep", mock_overpass_sleep):
            with unittest.mock.patch('urllib.request.urlopen', mock_urlopen):
                with unittest.mock.patch('datetime.date', MockDate):
                    cron.update_stats(overpass=True)
        actual = util.get_content(path)
        self.assertEqual(actual, result_from_overpass)

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

        self.assertTrue(mock_overpass_sleep_called)

        with open(config.get_abspath("workdir/stats/ref.count"),
                  "r") as stream:
            num_ref = int(stream.read().strip())
        self.assertEqual(num_ref, 300)
Exemple #7
0
    def test_no_overpass(self) -> None:
        """Tests the case when we don't call overpass."""
        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-wait.txt"),
            test_context.URLRoute(
                url="https://overpass-api.de/api/status",
                data_path="",
                result_path="tests/network/overpass-status-happy.txt")
        ]
        network = test_context.TestNetwork(routes)
        ctx.set_network(network)

        cron.update_stats(ctx, overpass=False)

        test_time = cast(test_context.TestTime, ctx.get_time())
        self.assertEqual(test_time.get_sleep(), 0)
Exemple #8
0
    def test_happy(self) -> None:
        """Tests the happy path."""
        actual_args: List[str] = []
        actual_check = False

        def mock_subprocess_run(args: List[str], check: bool) -> None:
            nonlocal actual_args
            nonlocal actual_check
            actual_args = args
            actual_check = check

        mock_overpass_sleep_called = False

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

        result_from_overpass = "******"
        result_from_overpass += "7677\tOrfű\tDollár utca\t1\tvasony\n"

        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('config.get_abspath', get_abspath):
            today = time.strftime("%Y-%m-%d")
            path = config.get_abspath("workdir/stats/%s.csv" % today)
            with unittest.mock.patch("cron.overpass_sleep", mock_overpass_sleep):
                with unittest.mock.patch('urllib.request.urlopen', mock_urlopen):
                    with unittest.mock.patch('subprocess.run', mock_subprocess_run):
                        cron.update_stats()
            actual = util.get_content(path)
            self.assertEqual(actual, result_from_overpass)

        self.assertTrue(mock_overpass_sleep_called)
        self.assertTrue(actual_args[0].endswith("stats-daily.sh"))
        self.assertTrue(actual_check)