Jeremiah Lowin commited on
Commit
2627940
·
1 Parent(s): 3c8e7d8

Fix typing and static issues

Browse files
Files changed (1) hide show
  1. tests/utilities/test_cache.py +41 -38
tests/utilities/test_cache.py CHANGED
@@ -22,13 +22,13 @@ class TestTimedCache:
22
  """Test that values can be set in the cache."""
23
  cache = TimedCache(datetime.timedelta(seconds=10))
24
  key, value = "test_key", "test_value"
25
-
26
  with patch("datetime.datetime") as mock_datetime:
27
- now = datetime.datetime(2023, 1, 1, tzinfo=datetime.UTC)
28
  mock_datetime.now.return_value = now
29
-
30
  cache.set(key, value)
31
-
32
  # Check that the value is stored with the correct expiration
33
  assert key in cache.cache
34
  stored_value, expiration = cache.cache[key]
@@ -39,11 +39,13 @@ class TestTimedCache:
39
  """Test retrieving a value that exists and has not expired."""
40
  cache = TimedCache(datetime.timedelta(seconds=10))
41
  key, value = "test_key", "test_value"
42
-
43
  # Set a future expiration time
44
- future = datetime.datetime.now(datetime.UTC) + datetime.timedelta(seconds=30)
 
 
45
  cache.cache[key] = (value, future)
46
-
47
  # The value should be returned
48
  assert cache.get(key) == value
49
 
@@ -51,30 +53,32 @@ class TestTimedCache:
51
  """Test retrieving a value that exists but has expired."""
52
  cache = TimedCache(datetime.timedelta(seconds=10))
53
  key, value = "test_key", "test_value"
54
-
55
  # Set a past expiration time
56
- past = datetime.datetime.now(datetime.UTC) - datetime.timedelta(seconds=1)
 
 
57
  cache.cache[key] = (value, past)
58
-
59
  # Should return NOT_FOUND
60
  assert cache.get(key) is TimedCache.NOT_FOUND
61
 
62
  def test_get_not_found(self):
63
  """Test retrieving a value that doesn't exist in the cache."""
64
  cache = TimedCache(datetime.timedelta(seconds=10))
65
-
66
  # Key doesn't exist
67
  assert cache.get("nonexistent_key") is TimedCache.NOT_FOUND
68
 
69
  def test_clear(self):
70
  """Test that the cache can be cleared."""
71
  cache = TimedCache(datetime.timedelta(seconds=10))
72
-
73
  # Add some items
74
  cache.set("key1", "value1")
75
  cache.set("key2", "value2")
76
  assert len(cache.cache) == 2
77
-
78
  # Clear the cache
79
  cache.clear()
80
  assert len(cache.cache) == 0
@@ -84,14 +88,14 @@ class TestTimedCache:
84
  # Use a very short expiration for the test
85
  cache = TimedCache(datetime.timedelta(milliseconds=50))
86
  key, value = "test_key", "test_value"
87
-
88
  cache.set(key, value)
89
  # Value should be available immediately
90
  assert cache.get(key) == value
91
-
92
  # Wait for expiration
93
  time.sleep(0.06) # 60 milliseconds, slightly longer than expiration
94
-
95
  # Value should now be expired
96
  assert cache.get(key) is TimedCache.NOT_FOUND
97
 
@@ -99,11 +103,11 @@ class TestTimedCache:
99
  """Test that setting a key that already exists overwrites the old value."""
100
  cache = TimedCache(datetime.timedelta(seconds=10))
101
  key = "test_key"
102
-
103
  # Set initial value
104
  cache.set(key, "initial_value")
105
  assert cache.get(key) == "initial_value"
106
-
107
  # Overwrite with new value
108
  cache.set(key, "new_value")
109
  assert cache.get(key) == "new_value"
@@ -112,21 +116,21 @@ class TestTimedCache:
112
  """Test that overwriting a key extends its expiration time."""
113
  cache = TimedCache(datetime.timedelta(seconds=10))
114
  key = "test_key"
115
-
116
  with patch("datetime.datetime") as mock_datetime:
117
  # Set initial value at t=0
118
- initial_time = datetime.datetime(2023, 1, 1, tzinfo=datetime.UTC)
119
  mock_datetime.now.return_value = initial_time
120
  cache.set(key, "initial_value")
121
-
122
  initial_expiration = cache.cache[key][1]
123
  assert initial_expiration == initial_time + datetime.timedelta(seconds=10)
124
-
125
  # Overwrite at t=5
126
  later_time = initial_time + datetime.timedelta(seconds=5)
127
  mock_datetime.now.return_value = later_time
128
  cache.set(key, "new_value")
129
-
130
  # Expiration should be extended
131
  new_expiration = cache.cache[key][1]
132
  assert new_expiration == later_time + datetime.timedelta(seconds=10)
@@ -134,7 +138,7 @@ class TestTimedCache:
134
  def test_different_key_types(self):
135
  """Test that different types of keys can be used."""
136
  cache = TimedCache(datetime.timedelta(seconds=10))
137
-
138
  # Test various key types
139
  keys_and_values = [
140
  (42, "int_value"),
@@ -142,7 +146,7 @@ class TestTimedCache:
142
  ((1, 2), "tuple_value"),
143
  (frozenset({1, 2, 3}), "frozenset_value"),
144
  ]
145
-
146
  for key, value in keys_and_values:
147
  cache.set(key, value)
148
  assert cache.get(key) == value
@@ -151,7 +155,7 @@ class TestTimedCache:
151
  """Test that None can be stored as a value."""
152
  cache = TimedCache(datetime.timedelta(seconds=10))
153
  key = "none_key"
154
-
155
  cache.set(key, None)
156
  # The stored value is None, but get() should return None, not NOT_FOUND
157
  assert cache.get(key) is None
@@ -160,7 +164,7 @@ class TestTimedCache:
160
  """Test with a zero expiration time."""
161
  cache = TimedCache(datetime.timedelta(seconds=0))
162
  key, value = "test_key", "test_value"
163
-
164
  cache.set(key, value)
165
  # The value might already be expired by the time we call get()
166
  # We can't make strong assertions here due to timing variability
@@ -171,7 +175,7 @@ class TestTimedCache:
171
  """Test with a negative expiration time."""
172
  cache = TimedCache(datetime.timedelta(seconds=-1))
173
  key, value = "test_key", "test_value"
174
-
175
  cache.set(key, value)
176
  # Value should be immediately expired
177
  assert cache.get(key) is TimedCache.NOT_FOUND
@@ -179,52 +183,51 @@ class TestTimedCache:
179
  def test_cache_consistency(self):
180
  """Test cache consistency with multiple operations."""
181
  cache = TimedCache(datetime.timedelta(seconds=10))
182
-
183
  # Add multiple items
184
  cache.set("key1", "value1")
185
  cache.set("key2", "value2")
186
  cache.set("key3", "value3")
187
-
188
  # Check all items
189
  assert cache.get("key1") == "value1"
190
  assert cache.get("key2") == "value2"
191
  assert cache.get("key3") == "value3"
192
-
193
  # Overwrite one item
194
  cache.set("key2", "updated_value")
195
-
196
  # Check again
197
  assert cache.get("key1") == "value1"
198
  assert cache.get("key2") == "updated_value"
199
  assert cache.get("key3") == "value3"
200
-
201
  # Clear and verify all items are gone
202
  cache.clear()
203
  assert cache.get("key1") is TimedCache.NOT_FOUND
204
  assert cache.get("key2") is TimedCache.NOT_FOUND
205
  assert cache.get("key3") is TimedCache.NOT_FOUND
206
 
207
-
208
  def test_large_expiration(self):
209
  """Test with a very large expiration time."""
210
  # One year expiration
211
  cache = TimedCache(datetime.timedelta(days=365))
212
  key, value = "test_key", "test_value"
213
-
214
  cache.set(key, value)
215
  assert cache.get(key) == value
216
 
217
  def test_many_items(self):
218
  """Test cache with many items."""
219
  cache = TimedCache(datetime.timedelta(seconds=10))
220
-
221
  # Add 1000 items
222
  for i in range(1000):
223
  cache.set(f"key{i}", f"value{i}")
224
-
225
  # Check size
226
  assert len(cache.cache) == 1000
227
-
228
  # Check some random items
229
  for i in [0, 123, 456, 789, 999]:
230
  assert cache.get(f"key{i}") == f"value{i}"
 
22
  """Test that values can be set in the cache."""
23
  cache = TimedCache(datetime.timedelta(seconds=10))
24
  key, value = "test_key", "test_value"
25
+
26
  with patch("datetime.datetime") as mock_datetime:
27
+ now = datetime.datetime(2023, 1, 1, tzinfo=datetime.timezone.utc)
28
  mock_datetime.now.return_value = now
29
+
30
  cache.set(key, value)
31
+
32
  # Check that the value is stored with the correct expiration
33
  assert key in cache.cache
34
  stored_value, expiration = cache.cache[key]
 
39
  """Test retrieving a value that exists and has not expired."""
40
  cache = TimedCache(datetime.timedelta(seconds=10))
41
  key, value = "test_key", "test_value"
42
+
43
  # Set a future expiration time
44
+ future = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(
45
+ seconds=30
46
+ )
47
  cache.cache[key] = (value, future)
48
+
49
  # The value should be returned
50
  assert cache.get(key) == value
51
 
 
53
  """Test retrieving a value that exists but has expired."""
54
  cache = TimedCache(datetime.timedelta(seconds=10))
55
  key, value = "test_key", "test_value"
56
+
57
  # Set a past expiration time
58
+ past = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(
59
+ seconds=1
60
+ )
61
  cache.cache[key] = (value, past)
62
+
63
  # Should return NOT_FOUND
64
  assert cache.get(key) is TimedCache.NOT_FOUND
65
 
66
  def test_get_not_found(self):
67
  """Test retrieving a value that doesn't exist in the cache."""
68
  cache = TimedCache(datetime.timedelta(seconds=10))
69
+
70
  # Key doesn't exist
71
  assert cache.get("nonexistent_key") is TimedCache.NOT_FOUND
72
 
73
  def test_clear(self):
74
  """Test that the cache can be cleared."""
75
  cache = TimedCache(datetime.timedelta(seconds=10))
76
+
77
  # Add some items
78
  cache.set("key1", "value1")
79
  cache.set("key2", "value2")
80
  assert len(cache.cache) == 2
81
+
82
  # Clear the cache
83
  cache.clear()
84
  assert len(cache.cache) == 0
 
88
  # Use a very short expiration for the test
89
  cache = TimedCache(datetime.timedelta(milliseconds=50))
90
  key, value = "test_key", "test_value"
91
+
92
  cache.set(key, value)
93
  # Value should be available immediately
94
  assert cache.get(key) == value
95
+
96
  # Wait for expiration
97
  time.sleep(0.06) # 60 milliseconds, slightly longer than expiration
98
+
99
  # Value should now be expired
100
  assert cache.get(key) is TimedCache.NOT_FOUND
101
 
 
103
  """Test that setting a key that already exists overwrites the old value."""
104
  cache = TimedCache(datetime.timedelta(seconds=10))
105
  key = "test_key"
106
+
107
  # Set initial value
108
  cache.set(key, "initial_value")
109
  assert cache.get(key) == "initial_value"
110
+
111
  # Overwrite with new value
112
  cache.set(key, "new_value")
113
  assert cache.get(key) == "new_value"
 
116
  """Test that overwriting a key extends its expiration time."""
117
  cache = TimedCache(datetime.timedelta(seconds=10))
118
  key = "test_key"
119
+
120
  with patch("datetime.datetime") as mock_datetime:
121
  # Set initial value at t=0
122
+ initial_time = datetime.datetime(2023, 1, 1, tzinfo=datetime.timezone.utc)
123
  mock_datetime.now.return_value = initial_time
124
  cache.set(key, "initial_value")
125
+
126
  initial_expiration = cache.cache[key][1]
127
  assert initial_expiration == initial_time + datetime.timedelta(seconds=10)
128
+
129
  # Overwrite at t=5
130
  later_time = initial_time + datetime.timedelta(seconds=5)
131
  mock_datetime.now.return_value = later_time
132
  cache.set(key, "new_value")
133
+
134
  # Expiration should be extended
135
  new_expiration = cache.cache[key][1]
136
  assert new_expiration == later_time + datetime.timedelta(seconds=10)
 
138
  def test_different_key_types(self):
139
  """Test that different types of keys can be used."""
140
  cache = TimedCache(datetime.timedelta(seconds=10))
141
+
142
  # Test various key types
143
  keys_and_values = [
144
  (42, "int_value"),
 
146
  ((1, 2), "tuple_value"),
147
  (frozenset({1, 2, 3}), "frozenset_value"),
148
  ]
149
+
150
  for key, value in keys_and_values:
151
  cache.set(key, value)
152
  assert cache.get(key) == value
 
155
  """Test that None can be stored as a value."""
156
  cache = TimedCache(datetime.timedelta(seconds=10))
157
  key = "none_key"
158
+
159
  cache.set(key, None)
160
  # The stored value is None, but get() should return None, not NOT_FOUND
161
  assert cache.get(key) is None
 
164
  """Test with a zero expiration time."""
165
  cache = TimedCache(datetime.timedelta(seconds=0))
166
  key, value = "test_key", "test_value"
167
+
168
  cache.set(key, value)
169
  # The value might already be expired by the time we call get()
170
  # We can't make strong assertions here due to timing variability
 
175
  """Test with a negative expiration time."""
176
  cache = TimedCache(datetime.timedelta(seconds=-1))
177
  key, value = "test_key", "test_value"
178
+
179
  cache.set(key, value)
180
  # Value should be immediately expired
181
  assert cache.get(key) is TimedCache.NOT_FOUND
 
183
  def test_cache_consistency(self):
184
  """Test cache consistency with multiple operations."""
185
  cache = TimedCache(datetime.timedelta(seconds=10))
186
+
187
  # Add multiple items
188
  cache.set("key1", "value1")
189
  cache.set("key2", "value2")
190
  cache.set("key3", "value3")
191
+
192
  # Check all items
193
  assert cache.get("key1") == "value1"
194
  assert cache.get("key2") == "value2"
195
  assert cache.get("key3") == "value3"
196
+
197
  # Overwrite one item
198
  cache.set("key2", "updated_value")
199
+
200
  # Check again
201
  assert cache.get("key1") == "value1"
202
  assert cache.get("key2") == "updated_value"
203
  assert cache.get("key3") == "value3"
204
+
205
  # Clear and verify all items are gone
206
  cache.clear()
207
  assert cache.get("key1") is TimedCache.NOT_FOUND
208
  assert cache.get("key2") is TimedCache.NOT_FOUND
209
  assert cache.get("key3") is TimedCache.NOT_FOUND
210
 
 
211
  def test_large_expiration(self):
212
  """Test with a very large expiration time."""
213
  # One year expiration
214
  cache = TimedCache(datetime.timedelta(days=365))
215
  key, value = "test_key", "test_value"
216
+
217
  cache.set(key, value)
218
  assert cache.get(key) == value
219
 
220
  def test_many_items(self):
221
  """Test cache with many items."""
222
  cache = TimedCache(datetime.timedelta(seconds=10))
223
+
224
  # Add 1000 items
225
  for i in range(1000):
226
  cache.set(f"key{i}", f"value{i}")
227
+
228
  # Check size
229
  assert len(cache.cache) == 1000
230
+
231
  # Check some random items
232
  for i in [0, 123, 456, 789, 999]:
233
  assert cache.get(f"key{i}") == f"value{i}"