-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add changes #6
add changes #6
Changes from all commits
bec84ef
1fd4279
6e839c9
aca0f9c
c416dd0
efd8ced
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,14 @@ | |
from ted_sws.domain.model.manifestation import XMLManifestation | ||
from ted_sws.domain.model.metadata import TEDMetadata | ||
from ted_sws.domain.model.notice import Notice | ||
from ted_sws.notice_fetcher.adapters.ted_api import TedDocumentSearch | ||
from ted_sws.notice_fetcher.adapters.ted_api_abc import DocumentSearchABC | ||
|
||
|
||
class NoticeFetcherABC(abc.ABC): | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we decide to have only functions in the service layer of the application, then the class NoticeFetcher class can be merged into DocumetnSearch class. This is to be discussed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We decide to keep adapter logic separate of notice fetcher service logic. |
||
|
||
""" | ||
|
||
@abc.abstractmethod | ||
def get_notice_by_id(self, document_id: str) -> Notice: | ||
""" | ||
|
@@ -48,6 +52,13 @@ class NoticeFetcher(NoticeFetcherABC): | |
|
||
""" | ||
|
||
def __init__(self, document_search : DocumentSearchABC): | ||
""" | ||
|
||
:param document_search: | ||
""" | ||
self.document_search = document_search | ||
|
||
def __create_notice(self, notice_data: dict) -> Notice: | ||
""" | ||
|
||
|
@@ -68,7 +79,7 @@ def get_notice_by_id(self, document_id): | |
:param document_id: | ||
:return: | ||
""" | ||
document_result = TedDocumentSearch().get_by_id(document_id=document_id) | ||
document_result = self.document_search.get_by_id(document_id=document_id) | ||
|
||
return self.__create_notice(notice_data=document_result) | ||
|
||
|
@@ -78,7 +89,7 @@ def get_notices_by_query(self, query: dict) -> List[Notice]: | |
:param query: | ||
:return: | ||
""" | ||
documents = TedDocumentSearch().get_by_query(query=query) | ||
documents = self.document_search.get_by_query(query=query) | ||
return [self.__create_notice(notice_data=document) for document in documents] | ||
|
||
def get_notices_by_date_range(self, start_date: date, end_date: date) -> List[Notice]: | ||
|
@@ -88,7 +99,7 @@ def get_notices_by_date_range(self, start_date: date, end_date: date) -> List[No | |
:param end_date: | ||
:return: | ||
""" | ||
documents = TedDocumentSearch().get_by_range_date(start_date=start_date, end_date=end_date) | ||
documents = self.document_search.get_by_range_date(start_date=start_date, end_date=end_date) | ||
return [self.__create_notice(notice_data=document) for document in documents] | ||
|
||
def get_notices_by_date_wild_card(self, wildcard_date: str) -> List[Notice]: | ||
|
@@ -97,5 +108,5 @@ def get_notices_by_date_wild_card(self, wildcard_date: str) -> List[Notice]: | |
:param wildcard_date: | ||
:return: | ||
""" | ||
documents = TedDocumentSearch().get_by_wildcard_date(wildcard_date=wildcard_date) | ||
documents = self.document_search.get_by_wildcard_date(wildcard_date=wildcard_date) | ||
return [self.__create_notice(notice_data=document) for document in documents] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import datetime | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks grteat! |
||
|
||
from ted_sws.domain.model.notice import Notice, NoticeStatus | ||
from ted_sws.notice_fetcher.adapters.ted_api import TedDocumentSearch, TedRequestAPI | ||
from ted_sws.notice_fetcher.services.notice_fetcher import NoticeFetcher | ||
|
||
|
||
def test_notice_fetcher_by_identifier(): | ||
document_id = "067623-2022" | ||
notice = NoticeFetcher(document_search=TedDocumentSearch(request_api=TedRequestAPI())).get_notice_by_id( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use a fixture here for TedDocumentSearch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
document_id=document_id) | ||
|
||
assert isinstance(notice, Notice) | ||
assert notice | ||
assert notice.original_metadata | ||
assert notice.ted_id | ||
assert notice.ted_id == document_id | ||
assert notice.xml_manifestation | ||
assert notice.status == NoticeStatus.RAW | ||
|
||
|
||
def test_notice_fetcher_by_search_query(): | ||
query = {"q": "ND=[67623-2022]"} | ||
|
||
notices = NoticeFetcher(document_search=TedDocumentSearch(request_api=TedRequestAPI())).get_notices_by_query( | ||
query=query) | ||
|
||
assert isinstance(notices, list) | ||
assert len(notices) == 1 | ||
assert isinstance(notices[0], Notice) | ||
|
||
|
||
def test_notice_fetcher_by_date_range(): | ||
notices = NoticeFetcher(document_search=TedDocumentSearch(request_api=TedRequestAPI())).get_notices_by_date_range( | ||
start_date=datetime.date(2022, 2, 3), | ||
end_date=datetime.date(2022, 2, 3)) | ||
xml_text = "<NOTICE_DATA>" | ||
|
||
assert isinstance(notices, list) | ||
assert len(notices) == 95 | ||
assert isinstance(notices[0], Notice) | ||
assert xml_text in notices[0].xml_manifestation.object_data |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import datetime | ||
|
||
import pytest | ||
|
||
from ted_sws.notice_fetcher.adapters.ted_api import TedDocumentSearch, TedRequestAPI | ||
|
||
|
||
def test_ted_api(): | ||
ted = TedDocumentSearch(request_api=TedRequestAPI()) | ||
xml_text = "<NOTICE_DATA>" | ||
|
||
notice_by_id = ted.get_by_id(document_id="67623-2022") | ||
notice_by_date = ted.get_by_range_date(start_date=datetime.date(2022, 2, 3), end_date=datetime.date(2022, 2, 3)) | ||
notice_by_date_wildcard = ted.get_by_wildcard_date(wildcard_date="20220203*") | ||
notice_by_query = ted.get_by_query(query={"q": "ND=[67623-2022]"}) | ||
|
||
assert xml_text in notice_by_id["content"] | ||
assert isinstance(notice_by_id, dict) | ||
assert len(notice_by_date) == 95 | ||
assert len(notice_by_date_wildcard) == 95 | ||
assert isinstance(notice_by_date, list) | ||
assert isinstance(notice_by_date_wildcard, list) | ||
assert isinstance(notice_by_query, list) | ||
assert isinstance(notice_by_date[0], dict) | ||
assert isinstance(notice_by_date_wildcard[0], dict) | ||
assert isinstance(notice_by_query[0], dict) | ||
assert len(notice_by_query) == 1 | ||
|
||
|
||
def test_ted_api_error(): | ||
ted = TedDocumentSearch(request_api=TedRequestAPI()) | ||
with pytest.raises(Exception) as e: | ||
ted.get_by_query(query={"q": "NDE=67623-2022"}) | ||
assert str(e.value) == "The API call failed with: <Response [500]>" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import copy | ||
import json | ||
import pathlib | ||
from datetime import date | ||
from typing import List | ||
from ted_sws.notice_fetcher.adapters.ted_api_abc import DocumentSearchABC, RequestAPI | ||
|
||
|
||
def get_fake_api_response() -> dict: | ||
path = pathlib.Path(__file__).parent.parent / "test_data" / "notices" / "2021-OJS237-623049.json" | ||
return json.loads(path.read_text()) | ||
|
||
|
||
class FakeRequestAPI(RequestAPI): | ||
""" | ||
|
||
""" | ||
|
||
def __call__(self, api_url: str, api_query: dict) -> dict: | ||
""" | ||
|
||
:param args: | ||
:param kwargs: | ||
:return: | ||
""" | ||
return copy.deepcopy(get_fake_api_response()) | ||
|
||
|
||
class FakeTedDocumentSearch(DocumentSearchABC): | ||
""" | ||
|
||
""" | ||
|
||
def get_by_wildcard_date(self, wildcard_date: str) -> List[dict]: | ||
""" | ||
|
||
:param wildcard_date: | ||
:return: | ||
""" | ||
return [get_fake_api_response()] | ||
|
||
def get_by_id(self, document_id: str) -> dict: | ||
""" | ||
|
||
:param document_id: | ||
:return: | ||
""" | ||
return get_fake_api_response() | ||
|
||
def get_by_range_date(self, start_date: date, end_date: date) -> List[dict]: | ||
""" | ||
|
||
:param start_date: | ||
:param end_date: | ||
:return: | ||
""" | ||
return [get_fake_api_response()] | ||
|
||
def get_by_query(self, query: dict) -> List[dict]: | ||
""" | ||
|
||
:param query: | ||
:return: | ||
""" | ||
return [get_fake_api_response()] |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
import base64 | ||
import json | ||
import pathlib | ||
|
||
import pytest | ||
|
||
def get_api_response(): | ||
path = pathlib.Path(__file__).parent.parent.parent / "test_data" / "notices" / "2021-OJS237-623049.json" | ||
return json.loads(path.read_text()) | ||
from ted_sws.notice_fetcher.adapters.ted_api import TedDocumentSearch | ||
from tests.fakes.fake_ted_api import FakeRequestAPI | ||
|
||
|
||
|
||
@pytest.fixture | ||
def ted_document_search(): | ||
return TedDocumentSearch(request_api=FakeRequestAPI()) |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The value of this abstract class is to define the call methods.
I would recommend defining those here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done