Skip to content

Commit

Permalink
mocks: Add mock search provider for developers to use (#2546)
Browse files Browse the repository at this point in the history
* mocks: Add mock search provider for developers to use

* fix code formatting
  • Loading branch information
MarkLark86 committed Mar 26, 2024
1 parent d042d49 commit 1714cd2
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
File renamed without changes.
90 changes: 90 additions & 0 deletions superdesk/tests/mocks/search_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from superdesk import SearchProvider, register_search_provider
from superdesk.utils import ListCursor
from superdesk.errors import SuperdeskApiError
from apps.search_providers import allowed_search_providers


FIXTURES = [
{
"type": "text",
"mimetype": "application/superdesk.item.text",
"pubstatus": "usable",
"_id": "urn:localhost:sofab:english-abcd123",
"guid": "urn:localhost:sofab:english-abcd123",
"headline": "Test English Headline",
"slugline": "test-slugline",
"body_html": "<p>Test English body</p>",
"versioncreated": "2024-03-20T15:27:15Z",
"firstcreated": "2024-03-20T15:27:15Z",
"source": "sofab",
"language": "en",
"word_count": 3,
"_fetchable": False,
},
{
"type": "text",
"mimetype": "application/superdesk.item.text",
"pubstatus": "usable",
"_id": "urn:localhost:sofab:deutsch-abcd123",
"guid": "urn:localhost:sofab:deutsch-abcd123",
"headline": "Test Deutsch Headline",
"slugline": "test-slugline",
"body_html": "<p>Test Deutsch body</p>",
"versioncreated": "2024-03-20T15:27:15Z",
"firstcreated": "2024-03-20T15:27:15Z",
"source": "sofab",
"language": "de",
"word_count": 3,
"_fetchable": False,
},
{
"type": "text",
"mimetype": "application/superdesk.item.text",
"pubstatus": "usable",
"_id": "urn:localhost:sofab:deutsch-abcd123-2",
"guid": "urn:localhost:sofab:deutsch-abcd123-2",
"headline": "Test Deutsch Headline 2",
"slugline": "test-slugline",
"body_html": "<p>Test Deutsch body 2</p>",
"versioncreated": "2024-03-20T15:27:15Z",
"firstcreated": "2024-03-20T15:27:15Z",
"source": "sofab",
"language": "de",
"word_count": 4,
"_fetchable": False,
},
{
"type": "text",
"mimetype": "application/superdesk.item.text",
"pubstatus": "usable",
"_id": "urn:localhost:sofab:french-abcd123",
"guid": "urn:localhost:sofab:french-abcd123",
"headline": "Test French Headline",
"slugline": "test-slugline",
"body_html": "<p>Test French body</p>",
"versioncreated": "2024-03-20T15:27:15Z",
"firstcreated": "2024-03-20T15:27:15Z",
"source": "sofab",
"language": "fr",
"word_count": 3,
"_fetchable": False,
},
]


class TestSearchProvider(SearchProvider):
label = "Test Search Provider"

def find(self, query):
return ListCursor(FIXTURES)

def fetch(self, guid):
item = next((item for item in FIXTURES if item.get("_id") == guid), None)
if item is None:
raise SuperdeskApiError.notFoundError("Search item not found")
return item


def init_app(_app):
if "TestSearchProvider" not in allowed_search_providers:
register_search_provider("TestSearchProvider", provider_class=TestSearchProvider)

0 comments on commit 1714cd2

Please sign in to comment.