1
+ import contextlib
1
2
import datetime
2
- import pathlib
3
+ from pathlib import Path
4
+ from typing import Any
3
5
4
6
import cacholote
5
7
import pytest
6
8
7
9
from cads_worker import entry_points
8
10
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
12
12
13
13
14
14
@cacholote .cacheable
@@ -17,31 +17,37 @@ def cached_now() -> datetime.datetime:
17
17
18
18
19
19
@pytest .mark .parametrize (
20
- "collection_id,before,after " ,
20
+ "collection_id,all_collections,raises " ,
21
21
[
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 ) ),
26
26
],
27
27
)
28
28
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 ] ,
33
33
) -> 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
+
34
38
with cacholote .config .set (
35
39
cache_db_urlpath = f"sqlite:///{ tmp_path / 'cacholote.db' } " ,
36
40
tag = "foo" ,
37
41
):
38
42
now = cached_now ()
39
43
assert now == cached_now ()
40
44
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