Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion core/metadata_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1034,8 +1034,10 @@ def apply(
]
for license in old_licenses:
if license not in new_licenses:
# 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.
license.status = LicenseStatus.unavailable
self.log.warning(
f"License {license.identifier} has been removed from feed."
f"License {license.identifier} has been removed from feed and set to be unavailable"
)
changed_availability = pool.update_availability_from_licenses(
as_of=self.last_checked,
Expand Down
45 changes: 45 additions & 0 deletions tests/core/test_circulation_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,51 @@ def test_apply_updates_existing_licenses(self, db: DatabaseTransactionFixture):
assert new_license.id == old_license.id
assert old_license.status == LicenseStatus.unavailable

def test_apply_updates_existing_license_when_removed_from_feed(
self, db: DatabaseTransactionFixture, caplog
):
edition, pool = db.edition(with_license_pool=True)

# Start with one license for this pool.
existing_license = db.license(
pool,
expires=None,
checkouts_left=2,
checkouts_available=3,
status=LicenseStatus.available,
)

assert isinstance(existing_license.identifier, str)
assert existing_license.checkouts_left == 2
assert existing_license.checkouts_available == 3
assert existing_license.status == LicenseStatus.available

# The feed does not include the license
circulation_data = CirculationData(
licenses=[], # No licenses in the updated feed
data_source=edition.data_source,
primary_identifier=edition.primary_identifier,
)
with caplog.at_level("WARNING"):
circulation_data.apply(db.session, pool.collection)
db.session.commit()

updated_license = pool.licenses[0]
assert updated_license.id == existing_license.id
assert (
updated_license.status == LicenseStatus.unavailable
) # Status should have changed
assert (
updated_license.checkouts_left == 2
) # Checkouts left should remain unchanged.
assert (
updated_license.checkouts_available == 3
) # Checkouts available should remain unchanged.
assert (
f"License {existing_license.identifier} has been removed from feed"
in caplog.text
)

def test_apply_with_licenses_overrides_availability(
self, db: DatabaseTransactionFixture
):
Expand Down