Skip to content

Commit

Permalink
Add SerpMetadata fields to Metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallaecio committed Nov 13, 2024
1 parent 6264eb4 commit 3c6a55f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/test_components.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import datetime

import attrs
from web_poet import RequestUrl

from zyte_common_items import (
Address,
AggregateRating,
Amenity,
BaseMetadata,
BaseSalary,
Breadcrumb,
BusinessPlaceMetadata,
Expand Down Expand Up @@ -72,6 +74,25 @@ def test_metadata_get_date_downloaded():
)


def get_all_subclasses(cls):
subclasses = set()
for subclass in cls.__subclasses__():
subclasses.add(subclass)
subclasses.update(get_all_subclasses(subclass))
return subclasses


def test_metadata_fields():
"""Metadata must contain a superset of the fields of all metadata
classes."""
superset = set(attrs.fields_dict(Metadata))
for cls in get_all_subclasses(BaseMetadata):
subset = set(attrs.fields_dict(cls))
assert subset.issubset(
superset
), f"Metadata is missing some fields from {cls.__name__}: {subset - superset}"


def test_named_link_optional_fields():
NamedLink(name="foo")
NamedLink(url="https://example.com")
Expand Down
9 changes: 9 additions & 0 deletions zyte_common_items/components/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,14 @@ class Metadata(DetailsMetadata):
later converted to the corresponding, more specific metadata class.
"""

#: Search query as seen in the webpage.
displayedQuery: Optional[str] = None

#: Search query as specified in the input URL.
searchedQuery: Optional[str] = None

#: The search text used to find the item.
searchText: Optional[str] = None

#: Total number of organic results reported by the search engine.
totalOrganicResults: Optional[int] = None

0 comments on commit 3c6a55f

Please sign in to comment.