Skip to content

Commit ef803ca

Browse files
Copilotmekarpeles
andcommitted
Add tests for bulk availability functionality and update test mocks
Co-authored-by: mekarpeles <978325+mekarpeles@users.noreply.github.com>
1 parent abe2785 commit ef803ca

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

openlibrary/plugins/books/tests/test_dynlinks.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,13 @@ def monkeypatch_ol(monkeypatch):
233233
monkeypatch.setattr(dynlinks, "ol_get_many", mock)
234234

235235
monkeypatch.setattr(ia, "get_metadata", lambda itemid: web.storage())
236+
237+
# Mock lending.get_availability to return empty dict (simulates API failure/unavailable)
238+
# This ensures tests fall back to the individual get_ia_availability calls
239+
mock_lending = type('MockLending', (), {
240+
'get_availability': lambda id_type, ids: {}
241+
})()
242+
monkeypatch.setattr(dynlinks, "lending", mock_lending)
236243

237244

238245
def test_query_keys(monkeypatch):
@@ -354,6 +361,89 @@ def test_dynlinks(monkeypatch):
354361
assert json.loads(js) == expected_result
355362

356363

364+
def test_bulk_availability_processing(monkeypatch):
365+
"""Test that bulk availability processing works correctly."""
366+
monkeypatch_ol(monkeypatch)
367+
368+
# Mock lending.get_availability to return test data
369+
def mock_get_availability(id_type, ids):
370+
return {
371+
'test-borrow': {
372+
'is_lendable': True,
373+
'is_printdisabled': False,
374+
'status': 'borrow_available'
375+
},
376+
'test-restricted': {
377+
'is_lendable': False,
378+
'is_printdisabled': True,
379+
'status': 'open'
380+
},
381+
'test-full': {
382+
'is_lendable': False,
383+
'is_printdisabled': False,
384+
'status': 'open'
385+
}
386+
}
387+
388+
mock_lending = type('MockLending', (), {
389+
'get_availability': mock_get_availability
390+
})()
391+
monkeypatch.setattr(dynlinks, "lending", mock_lending)
392+
393+
# Test data with multiple items having different ocaids
394+
test_result = {
395+
"isbn:1111111111": {
396+
"key": "/books/OL1M",
397+
"title": "Test Book 1",
398+
"ocaid": "test-borrow"
399+
},
400+
"isbn:2222222222": {
401+
"key": "/books/OL2M",
402+
"title": "Test Book 2",
403+
"ocaid": "test-restricted"
404+
},
405+
"isbn:3333333333": {
406+
"key": "/books/OL3M",
407+
"title": "Test Book 3",
408+
"ocaid": "test-full"
409+
},
410+
"isbn:4444444444": {
411+
"key": "/books/OL4M",
412+
"title": "Test Book 4"
413+
# No ocaid
414+
}
415+
}
416+
417+
# Test the bulk processing function
418+
result = dynlinks.process_result_for_viewapi(test_result)
419+
420+
# Verify correct availability mapping
421+
assert result["isbn:1111111111"]["preview"] == "borrow"
422+
assert result["isbn:2222222222"]["preview"] == "restricted"
423+
assert result["isbn:3333333333"]["preview"] == "full"
424+
assert result["isbn:4444444444"]["preview"] == "noview"
425+
426+
427+
def test_availability_mapping():
428+
"""Test the availability mapping function."""
429+
430+
# Test lendable item
431+
borrow_data = {'is_lendable': True, 'is_printdisabled': False}
432+
assert dynlinks.map_availability_to_legacy_contract(borrow_data) == 'borrow'
433+
434+
# Test printdisabled item
435+
restricted_data = {'is_lendable': False, 'is_printdisabled': True}
436+
assert dynlinks.map_availability_to_legacy_contract(restricted_data) == 'restricted'
437+
438+
# Test open item
439+
full_data = {'is_lendable': False, 'is_printdisabled': False}
440+
assert dynlinks.map_availability_to_legacy_contract(full_data) == 'full'
441+
442+
# Test missing data
443+
assert dynlinks.map_availability_to_legacy_contract(None) == 'noview'
444+
assert dynlinks.map_availability_to_legacy_contract({}) == 'noview'
445+
446+
357447
def test_isbnx(monkeypatch):
358448
site = mock_infobase.MockSite()
359449
site.save(

0 commit comments

Comments
 (0)