245 lines
8.2 KiB
Python
245 lines
8.2 KiB
Python
"""Tests for schema modules."""
|
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from bibapi.schemas.api_types import (
|
|
ALMASchema,
|
|
DNBSchema,
|
|
DublinCoreSchema,
|
|
HBZSchema,
|
|
HebisSchema,
|
|
KOBVSchema,
|
|
OEVKSchema,
|
|
PicaSchema,
|
|
SWBSchema,
|
|
)
|
|
from bibapi.schemas.bookdata import BookData
|
|
from bibapi.schemas.errors import BibAPIError, CatalogueError, NetworkError
|
|
from bibapi.sru import QueryTransformer
|
|
|
|
# --- QueryTransformer tests with different schemas ---
|
|
|
|
arguments = [
|
|
"TITLE=Java ist auch eine Insel",
|
|
"AUTHOR=Ullenboom, Christian",
|
|
"YEAR=2020",
|
|
"PPN=1693321114",
|
|
]
|
|
|
|
|
|
def test_pica_schema():
|
|
transformer = QueryTransformer(PicaSchema, arguments)
|
|
transformed = transformer.transform()
|
|
assert len(transformed) == 4
|
|
assert transformed[0].startswith(PicaSchema.TITLE.value)
|
|
assert transformed[1].startswith(PicaSchema.AUTHOR.value)
|
|
assert transformed[2].startswith(PicaSchema.YEAR.value)
|
|
assert transformed[3].startswith(PicaSchema.PPN.value)
|
|
|
|
|
|
def test_alma_schema():
|
|
transformer = QueryTransformer(ALMASchema, arguments)
|
|
transformed = transformer.transform()
|
|
assert len(transformed) == 3 # PPN is not supported
|
|
assert transformed[0].startswith(ALMASchema.TITLE.value)
|
|
assert transformed[1].startswith(ALMASchema.AUTHOR.value)
|
|
assert transformed[2].startswith(ALMASchema.YEAR.value)
|
|
|
|
|
|
def test_dublin_core_schema():
|
|
transformer = QueryTransformer(DublinCoreSchema, arguments)
|
|
transformed = transformer.transform()
|
|
assert len(transformed) == 3 # YEAR is supported, PPN is not
|
|
assert transformed[0].startswith(DublinCoreSchema.TITLE.value)
|
|
assert transformed[1].startswith(DublinCoreSchema.AUTHOR.value)
|
|
assert transformed[2].startswith(DublinCoreSchema.YEAR.value)
|
|
|
|
|
|
# --- API Schema configuration tests ---
|
|
|
|
|
|
class TestApiSchemas:
|
|
"""Tests for API schema configurations."""
|
|
|
|
def test_swb_schema_config(self):
|
|
"""Test SWB schema configuration."""
|
|
assert SWBSchema.NAME.value == "SWB"
|
|
assert "sru.k10plus.de" in SWBSchema.URL.value
|
|
assert SWBSchema.ARGSCHEMA.value == PicaSchema
|
|
assert SWBSchema.LIBRARY_NAME_LOCATION_FIELD.value == "924$b"
|
|
|
|
def test_dnb_schema_config(self):
|
|
"""Test DNB schema configuration."""
|
|
assert DNBSchema.NAME.value == "DNB"
|
|
assert "services.dnb.de" in DNBSchema.URL.value
|
|
assert DNBSchema.ARGSCHEMA.value == DublinCoreSchema
|
|
|
|
def test_kobv_schema_config(self):
|
|
"""Test KOBV schema configuration."""
|
|
assert KOBVSchema.NAME.value == "KOBV"
|
|
assert "sru.kobv.de" in KOBVSchema.URL.value
|
|
assert KOBVSchema.ARGSCHEMA.value == DublinCoreSchema
|
|
|
|
def test_hebis_schema_config(self):
|
|
"""Test HEBIS schema configuration."""
|
|
assert HebisSchema.NAME.value == "HEBIS"
|
|
assert "sru.hebis.de" in HebisSchema.URL.value
|
|
assert HebisSchema.ARGSCHEMA.value == PicaSchema
|
|
# HEBIS has specific character replacements
|
|
assert " " in HebisSchema.REPLACE.value
|
|
|
|
def test_oevk_schema_config(self):
|
|
"""Test OEVK schema configuration."""
|
|
assert OEVKSchema.NAME.value == "OEVK"
|
|
assert OEVKSchema.ARGSCHEMA.value == PicaSchema
|
|
|
|
def test_hbz_schema_config(self):
|
|
"""Test HBZ schema configuration."""
|
|
assert HBZSchema.NAME.value == "HBZ"
|
|
assert HBZSchema.ARGSCHEMA.value == ALMASchema
|
|
assert HBZSchema.LIBRARY_NAME_LOCATION_FIELD.value == "852$a"
|
|
# HBZ doesn't support PPN
|
|
assert "PPN" in HBZSchema.NOTSUPPORTEDARGS.value
|
|
|
|
|
|
# --- BookData tests ---
|
|
|
|
|
|
class TestBookData:
|
|
"""Tests for the BookData class."""
|
|
|
|
def test_bookdata_creation_defaults(self):
|
|
"""Test BookData creation with defaults."""
|
|
book = BookData()
|
|
assert book.ppn is None
|
|
assert book.title is None
|
|
assert book.in_apparat is False
|
|
assert book.in_library is False
|
|
|
|
def test_bookdata_creation_with_values(self):
|
|
"""Test BookData creation with values."""
|
|
book = BookData(
|
|
ppn="123456",
|
|
title="Test Book",
|
|
signature="ABC 123",
|
|
year=2023,
|
|
isbn=["9783123456789"],
|
|
)
|
|
assert book.ppn == "123456"
|
|
assert book.title == "Test Book"
|
|
assert book.signature == "ABC 123"
|
|
assert book.year == "2023" # Converted to string without non-digits
|
|
assert book.in_library is True # Because signature exists
|
|
|
|
def test_bookdata_post_init_year_cleaning(self):
|
|
"""Test that year is cleaned of non-digits."""
|
|
book = BookData(year="2023 [erschienen]")
|
|
assert book.year == "2023"
|
|
|
|
def test_bookdata_post_init_language_normalization(self):
|
|
"""Test language list normalization."""
|
|
book = BookData(language=["ger", "eng", " fra "])
|
|
assert book.language == "ger,eng,fra"
|
|
|
|
def test_bookdata_post_init_library_location(self):
|
|
"""Test library_location is converted to string."""
|
|
book = BookData(library_location=123)
|
|
assert book.library_location == "123"
|
|
|
|
def test_bookdata_from_dict(self):
|
|
"""Test BookData.from_dict method."""
|
|
book = BookData()
|
|
data = {"ppn": "123", "title": "Test", "year": "2023"}
|
|
book.from_dict(data)
|
|
assert book.ppn == "123"
|
|
assert book.title == "Test"
|
|
|
|
def test_bookdata_merge(self):
|
|
"""Test BookData.merge method."""
|
|
book1 = BookData(ppn="123", title="Book 1")
|
|
book2 = BookData(title="Book 2", author="Author", isbn=["978123"])
|
|
|
|
book1.merge(book2)
|
|
assert book1.ppn == "123" # Original value preserved
|
|
assert book1.title == "Book 1" # Original value preserved (not None)
|
|
assert book1.author == "Author" # Merged from book2
|
|
assert "978123" in book1.isbn # Merged list
|
|
|
|
def test_bookdata_merge_lists(self):
|
|
"""Test BookData.merge with list merging."""
|
|
book1 = BookData(isbn=["978123"])
|
|
book2 = BookData(isbn=["978456", "978123"]) # Has duplicate
|
|
|
|
book1.merge(book2)
|
|
# Should have both ISBNs but no duplicates
|
|
assert len(book1.isbn) == 2
|
|
assert "978123" in book1.isbn
|
|
assert "978456" in book1.isbn
|
|
|
|
def test_bookdata_to_dict(self):
|
|
"""Test BookData.to_dict property."""
|
|
book = BookData(ppn="123", title="Test Book")
|
|
json_str = book.to_dict
|
|
data = json.loads(json_str)
|
|
assert data["ppn"] == "123"
|
|
assert data["title"] == "Test Book"
|
|
assert "old_book" not in data # Should be removed
|
|
|
|
def test_bookdata_from_string(self):
|
|
"""Test BookData.from_string method."""
|
|
json_str = '{"ppn": "123", "title": "Test"}'
|
|
book = BookData().from_string(json_str)
|
|
assert book.ppn == "123"
|
|
assert book.title == "Test"
|
|
|
|
def test_bookdata_edition_number(self):
|
|
"""Test BookData.edition_number property."""
|
|
book = BookData(edition="3rd edition")
|
|
assert book.edition_number == 3
|
|
|
|
book2 = BookData(edition="First edition")
|
|
assert book2.edition_number == 0 # No digit found
|
|
|
|
book3 = BookData(edition=None)
|
|
assert book3.edition_number == 0
|
|
|
|
def test_bookdata_get_book_type(self):
|
|
"""Test BookData.get_book_type method."""
|
|
book = BookData(media_type="print", pages="Online Resource")
|
|
assert book.get_book_type() == "eBook"
|
|
|
|
book2 = BookData(media_type="print", pages="300 pages")
|
|
assert book2.get_book_type() == "Druckausgabe"
|
|
|
|
|
|
# --- Error classes tests ---
|
|
|
|
|
|
class TestErrors:
|
|
"""Tests for error classes."""
|
|
|
|
def test_bibapi_error(self):
|
|
"""Test BibAPIError exception."""
|
|
with pytest.raises(BibAPIError):
|
|
raise BibAPIError("Test error")
|
|
|
|
def test_catalogue_error(self):
|
|
"""Test CatalogueError exception."""
|
|
with pytest.raises(CatalogueError):
|
|
raise CatalogueError("Catalogue error")
|
|
|
|
# Should also be a BibAPIError
|
|
with pytest.raises(BibAPIError):
|
|
raise CatalogueError("Catalogue error")
|
|
|
|
def test_network_error(self):
|
|
"""Test NetworkError exception."""
|
|
with pytest.raises(NetworkError):
|
|
raise NetworkError("Network error")
|
|
|
|
# Should also be a BibAPIError
|
|
with pytest.raises(BibAPIError):
|
|
raise NetworkError("Network error")
|