-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mocks: Add mock search provider for developers to use (#2546)
* mocks: Add mock search provider for developers to use * fix code formatting
- Loading branch information
1 parent
d042d49
commit 1714cd2
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |