|
1 | 1 | import re |
2 | 2 | import typing |
| 3 | +from unittest import mock |
3 | 4 |
|
4 | 5 | import pytest |
5 | 6 | import time_machine |
@@ -278,16 +279,33 @@ def test_get_package_versions_na(): |
278 | 279 | } |
279 | 280 |
|
280 | 281 |
|
281 | | -def test_generate_unique_id(): |
| 282 | +@pytest.fixture |
| 283 | +def generate_unique_id_mock() -> str: |
| 284 | + """Fixture to fix the UUID used in `generate_unique_id`""" |
| 285 | + # TODO: make this more reusable |
| 286 | + with mock.patch("openeo_driver.utils.uuid") as uuid: |
| 287 | + fake_uuid = "0123456789abcdef0123456789abcdef" |
| 288 | + uuid.uuid4.return_value.hex = fake_uuid |
| 289 | + yield fake_uuid |
| 290 | + |
| 291 | + |
| 292 | +def test_generate_unique_id_basics(): |
282 | 293 | assert re.match("^[0-9a-f]{32}$", generate_unique_id()) |
283 | 294 | assert re.match("^j-[0-9a-f]{32}$", generate_unique_id("j")) |
284 | 295 |
|
285 | 296 |
|
286 | | -def test_generate_unique_id_date_prefix(): |
287 | | - with time_machine.travel("2022-12-14T12:34:56Z"): |
288 | | - job_id = generate_unique_id("j") |
289 | | - assert re.match("^j-[0-9a-f]{32}$", generate_unique_id("j")) |
290 | | - assert job_id.startswith("j-221214") |
| 297 | +@pytest.mark.parametrize( |
| 298 | + ["date_prefix", "expected"], |
| 299 | + [ |
| 300 | + (False, "j-0123456789abcdef0123456789abcdef"), |
| 301 | + (True, "j-191227070809cdef0123456789abcdef"), |
| 302 | + ("%Y%m", "j-2019126789abcdef0123456789abcdef"), |
| 303 | + ("d%y%m%d-T%H%M-", "j-d191227-T0708-ef0123456789abcdef"), |
| 304 | + ], |
| 305 | +) |
| 306 | +def test_generate_unique_id_date_prefix(generate_unique_id_mock, date_prefix, expected): |
| 307 | + with time_machine.travel("2019-12-27T07:08:09Z"): |
| 308 | + assert generate_unique_id("j", date_prefix=date_prefix) == expected |
291 | 309 |
|
292 | 310 |
|
293 | 311 | def test_filter_supported_kwargs_basic(): |
|
0 commit comments