Skip to content

Commit b7150e4

Browse files
authored
improve expiration args (#46)
1 parent 11f9dd8 commit b7150e4

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

cads_worker/entry_points.py

+19-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import datetime
22
import os
3-
from typing import Annotated, Optional
3+
from typing import Annotated
44

55
import cacholote
66
import structlog
@@ -32,31 +32,34 @@ def _cache_cleaner() -> None:
3232
raise
3333

3434

35-
def _add_tzinfo(timestamp: datetime.datetime | None) -> datetime.datetime | None:
36-
if timestamp is not None and timestamp.tzinfo is None:
35+
def _add_tzinfo(timestamp: datetime.datetime) -> datetime.datetime:
36+
if timestamp.tzinfo is None:
3737
timestamp = timestamp.replace(tzinfo=datetime.timezone.utc)
3838
return timestamp
3939

4040

4141
def _expire_cache_entries(
42-
collection_id: Annotated[
43-
Optional[list[str]], Option(help="Collection ID to expire", show_default="all")
44-
] = None,
4542
before: Annotated[
46-
Optional[datetime.datetime],
47-
Option(
48-
help="Expire entries created before this date",
49-
show_default="no bound",
50-
),
51-
] = None,
43+
datetime.datetime,
44+
Option(help="Expire entries created before this date"),
45+
],
5246
after: Annotated[
53-
Optional[datetime.datetime],
54-
Option(help="Expire entries created after this date", show_default="no bound"),
55-
] = None,
47+
datetime.datetime,
48+
Option(help="Expire entries created after this date"),
49+
],
50+
collection_id: Annotated[list[str], Option(help="Collection ID to expire")] = [],
51+
all_collections: Annotated[
52+
bool, Option("--all-collections", help="Expire all collections")
53+
] = False,
5654
) -> int:
5755
"""Expire cache entries."""
56+
if (all_collections and collection_id) or not (all_collections or collection_id):
57+
raise ValueError(
58+
"Either '--collection-id' or '--all-collections' must be chosen, but not both."
59+
)
60+
5861
count = cacholote.expire_cache_entries(
59-
tags=collection_id,
62+
tags=None if all_collections else collection_id,
6063
before=_add_tzinfo(before),
6164
after=_add_tzinfo(after),
6265
)

tests/test_20_expire_cache_entries.py

+26-20
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
import contextlib
12
import datetime
2-
import pathlib
3+
from pathlib import Path
4+
from typing import Any
35

46
import cacholote
57
import pytest
68

79
from cads_worker import entry_points
810

9-
TODAY = datetime.datetime.now(tz=datetime.timezone.utc)
10-
TOMORROW = TODAY + datetime.timedelta(days=1)
11-
YESTERDAY = TODAY - datetime.timedelta(days=1)
11+
does_not_raise = contextlib.nullcontext
1212

1313

1414
@cacholote.cacheable
@@ -17,31 +17,37 @@ def cached_now() -> datetime.datetime:
1717

1818

1919
@pytest.mark.parametrize(
20-
"collection_id,before,after",
20+
"collection_id,all_collections,raises",
2121
[
22-
(["foo"], None, None),
23-
(None, TOMORROW, None),
24-
(None, None, YESTERDAY),
25-
(["foo"], TOMORROW, YESTERDAY),
22+
(["foo"], False, does_not_raise()),
23+
([], True, does_not_raise()),
24+
([], False, pytest.raises(ValueError)),
25+
(["foo"], True, pytest.raises(ValueError)),
2626
],
2727
)
2828
def test_cache_entries(
29-
tmp_path: pathlib.Path,
30-
collection_id: list[str] | None,
31-
before: datetime.datetime | None,
32-
after: datetime.datetime | None,
29+
tmp_path: Path,
30+
collection_id: list[str],
31+
all_collections: bool,
32+
raises: contextlib.nullcontext[Any],
3333
) -> None:
34+
today = datetime.datetime.now(tz=datetime.timezone.utc)
35+
tomorrow = today + datetime.timedelta(days=1)
36+
yeasterday = today - datetime.timedelta(days=1)
37+
3438
with cacholote.config.set(
3539
cache_db_urlpath=f"sqlite:///{tmp_path / 'cacholote.db'}",
3640
tag="foo",
3741
):
3842
now = cached_now()
3943
assert now == cached_now()
4044

41-
count = entry_points._expire_cache_entries(
42-
collection_id=collection_id,
43-
before=before,
44-
after=after,
45-
)
46-
assert count == 1
47-
assert now != cached_now()
45+
with raises:
46+
count = entry_points._expire_cache_entries(
47+
before=tomorrow,
48+
after=yeasterday,
49+
collection_id=collection_id,
50+
all_collections=all_collections,
51+
)
52+
assert count == 1
53+
assert now != cached_now()

0 commit comments

Comments
 (0)