376 lines
11 KiB
Python
376 lines
11 KiB
Python
"""Tests for the _transformers module."""
|
|
|
|
from src.bibapi._transformers import (
|
|
RDS_AVAIL_DATA,
|
|
RDS_DATA,
|
|
RDS_GENERIC_DATA,
|
|
ARRAYData,
|
|
BibTeXData,
|
|
COinSData,
|
|
DictToTable,
|
|
Item,
|
|
RISData,
|
|
)
|
|
from src.bibapi.schemas.bookdata import BookData
|
|
|
|
# --- Item dataclass tests ---
|
|
|
|
|
|
class TestItem:
|
|
"""Tests for the Item dataclass."""
|
|
|
|
def test_item_creation_defaults(self):
|
|
"""Test Item creation with defaults."""
|
|
item = Item()
|
|
assert item.superlocation == ""
|
|
assert item.status == ""
|
|
assert item.availability == ""
|
|
|
|
def test_item_creation_with_values(self):
|
|
"""Test Item creation with values."""
|
|
item = Item(
|
|
superlocation="Main Library",
|
|
status="available",
|
|
callnumber="ABC 123",
|
|
)
|
|
assert item.superlocation == "Main Library"
|
|
assert item.status == "available"
|
|
assert item.callnumber == "ABC 123"
|
|
|
|
def test_item_from_dict(self):
|
|
"""Test Item.from_dict method."""
|
|
item = Item()
|
|
data = {
|
|
"items": [
|
|
{
|
|
"status": "available",
|
|
"callnumber": "ABC 123",
|
|
"location": "Floor 1",
|
|
},
|
|
],
|
|
}
|
|
result = item.from_dict(data)
|
|
assert result.status == "available"
|
|
assert result.callnumber == "ABC 123"
|
|
assert result.location == "Floor 1"
|
|
|
|
|
|
# --- RDS_DATA dataclass tests ---
|
|
|
|
|
|
class TestRDSData:
|
|
"""Tests for the RDS_DATA dataclass."""
|
|
|
|
def test_rds_data_creation_defaults(self):
|
|
"""Test RDS_DATA creation with defaults."""
|
|
rds = RDS_DATA()
|
|
assert rds.RDS_SIGNATURE == ""
|
|
assert rds.RDS_STATUS == ""
|
|
assert rds.RDS_LOCATION == ""
|
|
|
|
def test_rds_data_import_from_dict(self):
|
|
"""Test RDS_DATA.import_from_dict method."""
|
|
rds = RDS_DATA()
|
|
data = {
|
|
"RDS_SIGNATURE": "ABC 123",
|
|
"RDS_STATUS": "available",
|
|
"RDS_LOCATION": "Floor 1",
|
|
}
|
|
result = rds.import_from_dict(data)
|
|
assert result.RDS_SIGNATURE == "ABC 123"
|
|
assert result.RDS_STATUS == "available"
|
|
assert result.RDS_LOCATION == "Floor 1"
|
|
|
|
|
|
# --- RDS_AVAIL_DATA dataclass tests ---
|
|
|
|
|
|
class TestRDSAvailData:
|
|
"""Tests for the RDS_AVAIL_DATA dataclass."""
|
|
|
|
def test_rds_avail_data_creation_defaults(self):
|
|
"""Test RDS_AVAIL_DATA creation with defaults."""
|
|
rds = RDS_AVAIL_DATA()
|
|
assert rds.library_sigil == ""
|
|
assert rds.items == []
|
|
|
|
def test_rds_avail_data_import_from_dict(self):
|
|
"""Test RDS_AVAIL_DATA.import_from_dict method."""
|
|
rds = RDS_AVAIL_DATA()
|
|
json_data = (
|
|
'{"DE-Frei129": {"Location1": {"items": [{"status": "available"}]}}}'
|
|
)
|
|
result = rds.import_from_dict(json_data)
|
|
assert result.library_sigil == "DE-Frei129"
|
|
assert len(result.items) == 1
|
|
|
|
|
|
# --- RDS_GENERIC_DATA dataclass tests ---
|
|
|
|
|
|
class TestRDSGenericData:
|
|
"""Tests for the RDS_GENERIC_DATA dataclass."""
|
|
|
|
def test_rds_generic_data_creation_defaults(self):
|
|
"""Test RDS_GENERIC_DATA creation with defaults."""
|
|
rds = RDS_GENERIC_DATA()
|
|
assert rds.LibrarySigil == ""
|
|
assert rds.RDS_DATA == []
|
|
|
|
def test_rds_generic_data_import_from_dict(self):
|
|
"""Test RDS_GENERIC_DATA.import_from_dict method."""
|
|
rds = RDS_GENERIC_DATA()
|
|
json_data = '{"DE-Frei129": [{"RDS_SIGNATURE": "ABC 123"}]}'
|
|
result = rds.import_from_dict(json_data)
|
|
assert result.LibrarySigil == "DE-Frei129"
|
|
assert len(result.RDS_DATA) == 1
|
|
|
|
|
|
# --- ARRAYData tests ---
|
|
|
|
|
|
class TestARRAYData:
|
|
"""Tests for the ARRAYData transformer."""
|
|
|
|
def test_array_data_transform(self):
|
|
"""Test ARRAYData transform method."""
|
|
sample_data = """
|
|
[kid] => 123456789
|
|
[ti_long] => Array
|
|
(
|
|
[0] => Test Book Title
|
|
)
|
|
[isbn] => Array
|
|
(
|
|
[0] => 9783123456789
|
|
)
|
|
[la_facet] => Array
|
|
(
|
|
[0] => German
|
|
)
|
|
[pu] => Array
|
|
(
|
|
[0] => Test Publisher
|
|
)
|
|
[py_display] => Array
|
|
(
|
|
[0] => 2023
|
|
)
|
|
[umfang] => Array
|
|
(
|
|
[0] => 300 pages
|
|
)
|
|
"""
|
|
transformer = ARRAYData()
|
|
result = transformer.transform(sample_data)
|
|
|
|
assert isinstance(result, BookData)
|
|
assert result.ppn == "123456789"
|
|
|
|
def test_array_data_with_signature(self):
|
|
"""Test ARRAYData with predefined signature."""
|
|
sample_data = "[kid] => 123456789"
|
|
transformer = ARRAYData(signature="ABC 123")
|
|
result = transformer.transform(sample_data)
|
|
|
|
assert isinstance(result, BookData)
|
|
|
|
|
|
# --- COinSData tests ---
|
|
|
|
|
|
class TestCOinSData:
|
|
"""Tests for the COinSData transformer."""
|
|
|
|
def test_coins_data_transform(self):
|
|
"""Test COinSData transform method."""
|
|
# Note: COinS format uses & separators, last field shouldn't have trailing &
|
|
sample_data = (
|
|
"ctx_ver=Z39.88-2004&"
|
|
"rft_id=info:sid/test?kid=123456&"
|
|
"rft.btitle=Test Bookrft&" # btitle ends parsing at next 'rft'
|
|
"rft.aulast=Smithrft&"
|
|
"rft.aufirst=Johnrft&"
|
|
"rft.edition=2ndrft&"
|
|
"rft.isbn=9783123456789rft&"
|
|
"rft.pub=Publisherrft&"
|
|
"rft.date=2023rft&"
|
|
"rft.tpages=300"
|
|
)
|
|
transformer = COinSData()
|
|
result = transformer.transform(sample_data)
|
|
|
|
assert isinstance(result, BookData)
|
|
# The transformer splits on 'rft' after the field value
|
|
assert "Test Book" in result.title
|
|
assert "Smith" in result.author
|
|
|
|
|
|
# --- RISData tests ---
|
|
|
|
|
|
class TestRISData:
|
|
"""Tests for the RISData transformer."""
|
|
|
|
def test_ris_data_transform(self):
|
|
"""Test RISData transform method."""
|
|
sample_data = """TY - BOOK
|
|
TI - Test Book Title
|
|
AU - Smith, John
|
|
ET - 2nd edition
|
|
CN - ABC 123
|
|
SN - 9783123456789
|
|
LA - English
|
|
PB - Test Publisher
|
|
PY - 2023
|
|
SP - 300
|
|
DP - https://example.com/book?kid=123456
|
|
ER -"""
|
|
transformer = RISData()
|
|
result = transformer.transform(sample_data)
|
|
|
|
assert isinstance(result, BookData)
|
|
assert result.title == "Test Book Title"
|
|
assert result.signature == "ABC 123"
|
|
assert result.edition == "2nd edition"
|
|
assert result.year == "2023"
|
|
|
|
|
|
# --- BibTeXData tests ---
|
|
|
|
|
|
class TestBibTeXData:
|
|
"""Tests for the BibTeXData transformer."""
|
|
|
|
def test_bibtex_data_transform(self):
|
|
"""Test BibTeXData transform method."""
|
|
sample_data = """@book{test2023,
|
|
title = {Test Book Title},
|
|
author = {Smith, John and Doe, Jane},
|
|
edition = {2nd},
|
|
isbn = {9783123456789},
|
|
language = {English},
|
|
publisher = {Test Publisher},
|
|
year = {2023},
|
|
pages = {300},
|
|
bestand = {ABC 123}
|
|
}"""
|
|
transformer = BibTeXData()
|
|
result = transformer.transform(sample_data)
|
|
|
|
assert isinstance(result, BookData)
|
|
assert result.title == "Test Book Title"
|
|
# BibTeX transformer joins with ; and removes commas
|
|
assert "Smith John" in result.author
|
|
assert "Doe Jane" in result.author
|
|
assert result.signature == "ABC 123"
|
|
|
|
|
|
# --- DictToTable tests ---
|
|
|
|
|
|
class TestDictToTable:
|
|
"""Tests for the DictToTable transformer."""
|
|
|
|
def test_dict_to_table_book_mode(self):
|
|
"""Test DictToTable with book mode."""
|
|
data = {
|
|
"mode": "book",
|
|
"book_author": "Smith, John",
|
|
"book_signature": "ABC 123",
|
|
"book_place": "Berlin",
|
|
"book_year": "2023",
|
|
"book_title": "Test Book",
|
|
"book_edition": "2nd",
|
|
"book_pages": "300",
|
|
"book_publisher": "Publisher",
|
|
"book_isbn": "9783123456789",
|
|
}
|
|
transformer = DictToTable()
|
|
result = transformer.transform(data)
|
|
|
|
assert result["type"] == "book"
|
|
assert result["work_author"] == "Smith, John"
|
|
assert result["signature"] == "ABC 123"
|
|
assert result["year"] == "2023"
|
|
|
|
def test_dict_to_table_hg_mode(self):
|
|
"""Test DictToTable with hg (editor) mode."""
|
|
data = {
|
|
"mode": "hg",
|
|
"hg_author": "Chapter Author",
|
|
"hg_editor": "Editor Name",
|
|
"hg_year": "2023",
|
|
"hg_title": "Collection Title",
|
|
"hg_publisher": "Publisher",
|
|
"hg_place": "Berlin",
|
|
"hg_edition": "1st",
|
|
"hg_chaptertitle": "Chapter Title",
|
|
"hg_pages": "50-75",
|
|
"hg_signature": "ABC 123",
|
|
"hg_isbn": "9783123456789",
|
|
}
|
|
transformer = DictToTable()
|
|
result = transformer.transform(data)
|
|
|
|
assert result["type"] == "hg"
|
|
assert result["section_author"] == "Chapter Author"
|
|
assert result["work_author"] == "Editor Name"
|
|
assert result["chapter_title"] == "Chapter Title"
|
|
|
|
def test_dict_to_table_zs_mode(self):
|
|
"""Test DictToTable with zs (journal) mode."""
|
|
data = {
|
|
"mode": "zs",
|
|
"zs_author": "Article Author",
|
|
"zs_chapter_title": "Article Title",
|
|
"zs_place": "Berlin",
|
|
"zs_issue": "Vol. 5, No. 2",
|
|
"zs_pages": "100-120",
|
|
"zs_publisher": "Publisher",
|
|
"zs_isbn": "1234-5678",
|
|
"zs_year": "2023",
|
|
"zs_signature": "PER 123",
|
|
"zs_title": "Journal Name",
|
|
}
|
|
transformer = DictToTable()
|
|
result = transformer.transform(data)
|
|
|
|
assert result["type"] == "zs"
|
|
assert result["section_author"] == "Article Author"
|
|
assert result["chapter_title"] == "Article Title"
|
|
assert result["issue"] == "Vol. 5, No. 2"
|
|
|
|
def test_dict_to_table_reset(self):
|
|
"""Test DictToTable reset method."""
|
|
transformer = DictToTable()
|
|
transformer.work_author = "Test"
|
|
transformer.year = "2023"
|
|
|
|
transformer.reset()
|
|
|
|
assert transformer.work_author is None
|
|
assert transformer.year is None
|
|
|
|
def test_dict_to_table_make_result_excludes_none(self):
|
|
"""Test that makeResult excludes None values."""
|
|
transformer = DictToTable()
|
|
transformer.work_author = "Test Author"
|
|
transformer.year = "2023"
|
|
# Leave others as None
|
|
|
|
result = transformer.makeResult()
|
|
|
|
assert "work_author" in result
|
|
assert "year" in result
|
|
assert "section_author" not in result # Should be excluded
|
|
assert "pages" not in result # Should be excluded
|
|
|
|
def test_dict_to_table_invalid_mode(self):
|
|
"""Test DictToTable with invalid mode returns None."""
|
|
data = {"mode": "invalid"}
|
|
transformer = DictToTable()
|
|
result = transformer.transform(data)
|
|
|
|
assert result is None
|