23
23
import pytest
24
24
import unittest
25
25
import unittest .mock
26
+ import pathlib
26
27
27
28
from ..policy import utils
28
29
29
30
30
31
class TestPolicyCache :
32
+ @pytest .fixture
33
+ def tmp_paths (self , tmp_path : pathlib .Path ) -> list [pathlib .Path ]:
34
+ path1 = tmp_path / "path1"
35
+ path2 = tmp_path / "path2"
36
+ path1 .mkdir ()
37
+ path2 .mkdir ()
38
+ return [path1 , path2 ]
39
+
31
40
@pytest .fixture
32
41
def mock_parser (self , monkeypatch ):
33
42
mock_parser = unittest .mock .Mock ()
@@ -37,58 +46,60 @@ def mock_parser(self, monkeypatch):
37
46
return mock_parser
38
47
39
48
def test_00_policy_init (self , tmp_path , mock_parser ):
40
- cache = utils .PolicyCache (tmp_path )
41
- mock_parser .assert_called_once_with (policy_path = tmp_path )
49
+ cache = utils .PolicyCache ([ tmp_path ] )
50
+ mock_parser .assert_called_once_with (policy_path = [ tmp_path ] )
42
51
43
52
@pytest .mark .asyncio
44
- async def test_10_file_created (self , tmp_path , mock_parser ):
45
- cache = utils .PolicyCache (tmp_path )
46
- cache .initialize_watcher ()
53
+ async def test_10_file_created (self , tmp_paths , mock_parser ):
54
+ for i in tmp_paths :
55
+ cache = utils .PolicyCache (tmp_paths )
56
+ cache .initialize_watcher ()
47
57
48
- assert not cache .outdated
58
+ assert not cache .outdated
49
59
50
- file = tmp_path / "test"
51
- file .write_text ("test" )
60
+ (i / "file" ).write_text ("test" )
52
61
53
- await asyncio .sleep (1 )
62
+ await asyncio .sleep (1 )
54
63
55
- assert cache .outdated
64
+ assert cache .outdated
56
65
57
66
@pytest .mark .asyncio
58
- async def test_11_file_changed (self , tmp_path , mock_parser ):
59
- file = tmp_path / "test"
60
- file .write_text ("test" )
67
+ async def test_11_file_changed (self , tmp_paths , mock_parser ):
68
+ for i in tmp_paths :
69
+ file = i / "test"
70
+ file .write_text ("test" )
61
71
62
- cache = utils .PolicyCache (tmp_path )
63
- cache .initialize_watcher ()
72
+ cache = utils .PolicyCache (tmp_paths )
73
+ cache .initialize_watcher ()
64
74
65
- assert not cache .outdated
75
+ assert not cache .outdated
66
76
67
- file .write_text ("new_content" )
77
+ file .write_text ("new_content" )
68
78
69
- await asyncio .sleep (1 )
79
+ await asyncio .sleep (1 )
70
80
71
- assert cache .outdated
81
+ assert cache .outdated
72
82
73
83
@pytest .mark .asyncio
74
- async def test_12_file_deleted (self , tmp_path , mock_parser ):
75
- file = tmp_path / "test"
76
- file .write_text ("test" )
84
+ async def test_12_file_deleted (self , tmp_paths , mock_parser ):
85
+ for i in tmp_paths :
86
+ file = i / "test"
87
+ file .write_text ("test" )
77
88
78
- cache = utils .PolicyCache (tmp_path )
79
- cache .initialize_watcher ()
89
+ cache = utils .PolicyCache (tmp_paths )
90
+ cache .initialize_watcher ()
80
91
81
- assert not cache .outdated
92
+ assert not cache .outdated
82
93
83
- os .remove (file )
94
+ os .remove (file )
84
95
85
- await asyncio .sleep (1 )
96
+ await asyncio .sleep (1 )
86
97
87
- assert cache .outdated
98
+ assert cache .outdated
88
99
89
100
@pytest .mark .asyncio
90
- async def test_13_no_change (self , tmp_path , mock_parser ):
91
- cache = utils .PolicyCache (tmp_path )
101
+ async def test_13_no_change (self , tmp_paths , mock_parser ):
102
+ cache = utils .PolicyCache (tmp_paths )
92
103
cache .initialize_watcher ()
93
104
94
105
assert not cache .outdated
@@ -101,10 +112,10 @@ async def test_13_no_change(self, tmp_path, mock_parser):
101
112
async def test_14_policy_move (self , tmp_path , mock_parser ):
102
113
policy_path = tmp_path / "policy"
103
114
policy_path .mkdir ()
104
- cache = utils .PolicyCache (policy_path )
115
+ cache = utils .PolicyCache ([ policy_path ] )
105
116
cache .initialize_watcher ()
106
117
107
- mock_parser .assert_called_once_with (policy_path = policy_path )
118
+ mock_parser .assert_called_once_with (policy_path = [ policy_path ] )
108
119
109
120
assert not cache .outdated
110
121
@@ -135,27 +146,35 @@ async def test_14_policy_move(self, tmp_path, mock_parser):
135
146
136
147
cache .get_policy ()
137
148
138
- call = unittest .mock .call (policy_path = policy_path )
149
+ call = unittest .mock .call (policy_path = [ policy_path ] )
139
150
assert mock_parser .mock_calls == [call , call , call ]
140
151
141
152
@pytest .mark .asyncio
142
- async def test_20_policy_updates (self , tmp_path , mock_parser ):
143
- cache = utils .PolicyCache (tmp_path )
153
+ async def test_20_policy_updates (self , tmp_paths , mock_parser ):
154
+ cache = utils .PolicyCache (tmp_paths )
144
155
cache .initialize_watcher ()
156
+ count = 0
145
157
146
- mock_parser .assert_called_once_with (policy_path = tmp_path )
158
+ for i in tmp_paths :
159
+ call = unittest .mock .call (policy_path = tmp_paths )
147
160
148
- assert not cache .outdated
161
+ count += 2
162
+ assert mock_parser .mock_calls == [call ] * (count - 1 )
163
+ cache = utils .PolicyCache (tmp_paths )
164
+ cache .initialize_watcher ()
149
165
150
- file = tmp_path / "test"
151
- file . write_text ( "test" )
166
+ l = len ( mock_parser . mock_calls )
167
+ assert mock_parser . mock_calls == [ call ] * l
152
168
153
- await asyncio . sleep ( 1 )
169
+ assert not cache . outdated
154
170
155
- assert cache .outdated
171
+ file = i / "test"
172
+ file .write_text ("test" )
156
173
157
- cache .get_policy ()
174
+ await asyncio .sleep (1 )
175
+
176
+ assert cache .outdated
158
177
159
- call = unittest . mock . call ( policy_path = tmp_path )
178
+ cache . get_policy ( )
160
179
161
- assert mock_parser .mock_calls == [call , call ]
180
+ assert mock_parser .mock_calls == [call ] * ( count + 1 )
0 commit comments