Skip to content

Commit

Permalink
Add test to ensure OPDS2Version1Serializer has not changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbernstein committed Oct 29, 2024
1 parent fc3b07d commit 8ff807c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/palace/manager/feed/serializer/opds2.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,19 @@ def _serialize_feed_links(self, feed: FeedData) -> dict[str, Any]:

return link_data

def _serialize_link(self, link: Link) -> dict[str, Any]:
serialized = super()._serialize_link(link)

if link.get("activeFacet", False):
serialized["rel"] = "self"

if link.get("defaultFacet", False):
properties: dict[str, Any] = dict()
properties.update({PALACE_PROPERTIES_DEFAULT: "true"})
serialized["properties"] = properties

return serialized

@classmethod
def _serialize_sort_link(cls, link: Link) -> dict[str, Any]:
sort_link: dict[str, Any] = {
Expand Down
60 changes: 57 additions & 3 deletions tests/manager/feed/test_opds2_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,51 @@ def test_serialize_opds_message(self):
OPDSMessage("URN", 200, "Description")
) == dict(urn="URN", description="Description")

def test_serialize_v1_feed_links(self):
feed_data = FeedData()

# specify a sort link
link = Link(href="test1", rel="test_rel1", title="text1")
link.add_attributes(
dict(facetGroup="Sort by", activeFacet="true", defaultFacet="true")
)

# include a non-sort facet
link2 = Link(href="test2", title="text2", rel="test_rel2")
link2.add_attributes(
dict(facetGroup="test_group", activeFacet="true", defaultFacet="true")
)

feed_data.facet_links.append(link)
feed_data.facet_links.append(link2)
links = OPDS2Version1Serializer()._serialize_feed_links(feed=feed_data)

assert links == {
"links": [],
"facets": [
{
"metadata": {"title": "Sort by"},
"links": [
{
"href": "test1",
"rel": "test_rel1",
"title": "text1",
}
],
},
{
"metadata": {"title": "test_group"},
"links": [
{
"href": "test2",
"rel": "test_rel2",
"title": "text2",
}
],
},
],
}

def test_serialize_v2_feed_links(self):
feed_data = FeedData()

Expand All @@ -244,9 +289,9 @@ def test_serialize_v2_feed_links(self):
)

# include a non-sort facet
link2 = Link(href="test2", rel="test_rel", title="text1")
link2 = Link(href="test2", title="text2", rel="test_2_rel")
link2.add_attributes(
dict(facetGroup="test_group", activeFacet="false", defaultFacet="false")
dict(facetGroup="test_group", activeFacet="true", defaultFacet="true")
)

feed_data.facet_links.append(link)
Expand All @@ -268,7 +313,16 @@ def test_serialize_v2_feed_links(self):
"facets": [
{
"metadata": {"title": "test_group"},
"links": [{"href": "test2", "rel": "test_rel", "title": "text1"}],
"links": [
{
"href": "test2",
"rel": "self",
"title": "text2",
"properties": {
PALACE_PROPERTIES_DEFAULT: "true",
},
}
],
}
],
}

0 comments on commit 8ff807c

Please sign in to comment.