Skip to content

Commit 5214ee2

Browse files
committedJun 22, 2023
Introduce PpmsConnection.last_served_from_cache attribute
1 parent 97f0d99 commit 5214ee2

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed
 

‎CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
NOTE: potentially breaking changes are flagged with a 🧨 symbol.
66

7-
## 3.1.1
7+
## 3.2.0
8+
9+
### Added
10+
11+
- `pyppms.booking.PpmsBooking.last_served_from_cache` has been added to indicate
12+
if the last request was served from the cache or on-line.
813

914
### Changed
1015

‎src/pyppms/ppms.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class PpmsConnection:
4040
cache_users_only : bool
4141
Flag indicating that only PPMS user details will be stored in the
4242
on-disk cache, nothing else.
43+
last_served_from_cache
44+
Indicates if the last request was served from the cache or on-line.
4345
users : dict
4446
A dict with usernames as keys, mapping to the related
4547
:py:class:`pyppms.user.PpmsUser` object, serves as a cache during the object's
@@ -108,6 +110,8 @@ def __init__(self, url, api_key, timeout=10, cache="", cache_users_only=False):
108110
}
109111
self.cache_path = cache
110112
self.cache_users_only = cache_users_only
113+
self.last_served_from_cache = False
114+
"""Indicates if the last request was served from the cache or on-line."""
111115

112116
# run in cache-only mode (e.g. for testing or off-line usage) if no API
113117
# key has been specified, skip authentication then:
@@ -208,18 +212,18 @@ def request(self, action, parameters={}, skip_cache=False):
208212
# log.debug("Request parameters: {}", parameters)
209213

210214
response = None
211-
read_from_cache = False
212215
try:
213216
if skip_cache: # pragma: no cover
214217
raise LookupError("Skipping the cache has been requested")
215218
response = self.__intercept_read(req_data)
216-
read_from_cache = True
219+
self.last_served_from_cache = True
217220
except LookupError as err:
218221
log.trace(f"Doing an on-line request: {err}")
219222
response = requests.post(self.url, data=req_data, timeout=self.timeout)
223+
self.last_served_from_cache = False
220224

221225
# store the response if it hasn't been read from the cache before:
222-
if not read_from_cache: # pragma: no cover
226+
if not self.last_served_from_cache: # pragma: no cover
223227
self.__intercept_store(req_data, response)
224228

225229
# NOTE: the HTTP status code returned is always `200` even if

‎tests/test_ppms.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -271,19 +271,19 @@ def test_get_user__skip_cache(caplog, ppms_connection, ppms_user):
271271
assert os.path.exists(cached) is False
272272

273273
ppms_connection.get_user(ppms_user.username)
274-
assert "Doing an on-line request: No cache hit for" in caplog.text
274+
assert ppms_connection.last_served_from_cache is False
275275
assert "Read intercepted response text from" not in caplog.text
276276

277277
assert os.path.exists(cached) is True
278278

279279
caplog.clear()
280280
ppms_connection.get_user(ppms_user.username)
281-
assert "Doing an on-line request: No cache hit for" not in caplog.text
281+
assert ppms_connection.last_served_from_cache is True
282282
assert "Read intercepted response text from" in caplog.text
283283

284284
caplog.clear()
285285
ppms_connection.get_user(ppms_user.username, skip_cache=True)
286-
assert "Doing an on-line request: Skipping the cache" in caplog.text
286+
assert ppms_connection.last_served_from_cache is False
287287
assert "Read intercepted response text from" not in caplog.text
288288

289289
# make sure to clean up the test-specific cache again:
@@ -409,12 +409,10 @@ def test_update_systems(ppms_connection, caplog):
409409
"""Test the get_systems() method."""
410410
caplog.set_level(logging.DEBUG)
411411
switch_cache_mocks(ppms_connection, "update_systems__broken_id")
412-
systems = ppms_connection.get_systems()
413-
412+
assert len(ppms_connection.systems) == 0
413+
ppms_connection.get_systems()
414414
# results should contain exaclty one system:
415-
assert len(systems) == 1
416-
assert "Updated 1 bookable systems from PPMS" in caplog.text
417-
assert "1 systems failed parsing" in caplog.text
415+
assert len(ppms_connection.systems) == 1
418416

419417

420418
def test_get_systems_matching(ppms_connection, system_details_raw):
@@ -819,9 +817,11 @@ def test_flush_cache__keep_users__request_new(ppms_connection, caplog, tmp_path)
819817

820818
log.info(f"Requesting details from PUMAPI for cached user [{old_user_name}]")
821819
ppms_connection.get_user(old_user_name)
822-
assert "No cache hit" not in caplog.text # served from the cache
820+
assert ppms_connection.last_served_from_cache is True
821+
# assert "No cache hit" not in caplog.text # served from the cache
823822

824823
log.info(f"Requesting details from PUMAPI for 'new' user [{new_user_name}]")
825824
ppms_connection.get_user(new_user_name)
826-
assert "No cache hit" in caplog.text # requires an on-line request
825+
assert ppms_connection.last_served_from_cache is False
826+
# assert "No cache hit" in caplog.text # requires an on-line request
827827
assert os.path.exists(new_user_cache)

0 commit comments

Comments
 (0)
Please sign in to comment.