Skip to content

Commit 8c95ffc

Browse files
Merge pull request #138 from NatLibFi/EKIR-299-Set-removed-license-as-unavailable
Ekir 299 set removed license as unavailable
2 parents 7cef13e + c172b7c commit 8c95ffc

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

core/metadata_layer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,8 +1034,10 @@ def apply(
10341034
]
10351035
for license in old_licenses:
10361036
if license not in new_licenses:
1037+
# In case a license is removed from the feed we need to set it to be unavailable so that it's not used for loans or statistics.
1038+
license.status = LicenseStatus.unavailable
10371039
self.log.warning(
1038-
f"License {license.identifier} has been removed from feed."
1040+
f"License {license.identifier} has been removed from feed and set to be unavailable"
10391041
)
10401042
changed_availability = pool.update_availability_from_licenses(
10411043
as_of=self.last_checked,

tests/core/test_circulation_data.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,51 @@ def test_apply_updates_existing_licenses(self, db: DatabaseTransactionFixture):
298298
assert new_license.id == old_license.id
299299
assert old_license.status == LicenseStatus.unavailable
300300

301+
def test_apply_updates_existing_license_when_removed_from_feed(
302+
self, db: DatabaseTransactionFixture, caplog
303+
):
304+
edition, pool = db.edition(with_license_pool=True)
305+
306+
# Start with one license for this pool.
307+
existing_license = db.license(
308+
pool,
309+
expires=None,
310+
checkouts_left=2,
311+
checkouts_available=3,
312+
status=LicenseStatus.available,
313+
)
314+
315+
assert isinstance(existing_license.identifier, str)
316+
assert existing_license.checkouts_left == 2
317+
assert existing_license.checkouts_available == 3
318+
assert existing_license.status == LicenseStatus.available
319+
320+
# The feed does not include the license
321+
circulation_data = CirculationData(
322+
licenses=[], # No licenses in the updated feed
323+
data_source=edition.data_source,
324+
primary_identifier=edition.primary_identifier,
325+
)
326+
with caplog.at_level("WARNING"):
327+
circulation_data.apply(db.session, pool.collection)
328+
db.session.commit()
329+
330+
updated_license = pool.licenses[0]
331+
assert updated_license.id == existing_license.id
332+
assert (
333+
updated_license.status == LicenseStatus.unavailable
334+
) # Status should have changed
335+
assert (
336+
updated_license.checkouts_left == 2
337+
) # Checkouts left should remain unchanged.
338+
assert (
339+
updated_license.checkouts_available == 3
340+
) # Checkouts available should remain unchanged.
341+
assert (
342+
f"License {existing_license.identifier} has been removed from feed"
343+
in caplog.text
344+
)
345+
301346
def test_apply_with_licenses_overrides_availability(
302347
self, db: DatabaseTransactionFixture
303348
):

0 commit comments

Comments
 (0)