Skip to content

Commit

Permalink
Merge pull request #12 from meaningfy-ws/feature/TED-39
Browse files Browse the repository at this point in the history
Feature/ted 39
  • Loading branch information
CaptainOfHacks committed Feb 17, 2022
2 parents e94717c + 6232576 commit 8923b03
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 69 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,5 @@ dmypy.json
.pytype/


junit_report.xml
junit_report.xml
venv_activation.sh
33 changes: 19 additions & 14 deletions ted_sws/notice_fetcher/services/notice_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import date
from typing import List

from ted_sws.domain.adapters.repository_abc import NoticeRepositoryABC
from ted_sws.domain.model.manifestation import XMLManifestation
from ted_sws.domain.model.metadata import TEDMetadata
from ted_sws.domain.model.notice import Notice
Expand All @@ -14,23 +15,23 @@ class NoticeFetcherABC(abc.ABC):
"""

@abc.abstractmethod
def get_notice_by_id(self, document_id: str) -> Notice:
def fetch_notice_by_id(self, document_id: str):
"""
This method will fetch a notice by id
:param document_id:
:return:
"""

@abc.abstractmethod
def get_notices_by_query(self, query: dict) -> List[Notice]:
def fetch_notices_by_query(self, query: dict):
"""
This method will fetch a list of notices by using a search query
:param query:
:return:
"""

@abc.abstractmethod
def get_notices_by_date_range(self, start_date: date, end_date: date) -> List[Notice]:
def fetch_notices_by_date_range(self, start_date: date, end_date: date):
"""
This method will fetch a list of notices by using a date range
:param start_date:
Expand All @@ -39,7 +40,7 @@ def get_notices_by_date_range(self, start_date: date, end_date: date) -> List[No
"""

@abc.abstractmethod
def get_notices_by_date_wild_card(self, wildcard_date: str) -> List[Notice]:
def fetch_notices_by_date_wild_card(self, wildcard_date: str):
"""
This method will fetch a list of notices by using a wildcard date
:param wildcard_date:
Expand All @@ -52,12 +53,14 @@ class NoticeFetcher(NoticeFetcherABC):
This class will fetch notices
"""

def __init__(self, ted_api_adapter: TedAPIAdapterABC):
def __init__(self, notice_repository: NoticeRepositoryABC, ted_api_adapter: TedAPIAdapterABC):
"""
:type notice_repository
:param ted_api_adapter:
"""
self.ted_api_adapter = ted_api_adapter
self.notice_repository = notice_repository

def _create_notice(self, notice_data: dict) -> Notice:
"""
Expand All @@ -73,40 +76,42 @@ def _create_notice(self, notice_data: dict) -> Notice:

return Notice(ted_id=ted_id, xml_manifestation=xml_manifestation, original_metadata=original_metadata)

def get_notice_by_id(self, document_id):
def fetch_notice_by_id(self, document_id):
"""
This method will fetch a notice by id
:param document_id:
:return:
"""
document_result = self.ted_api_adapter.get_by_id(document_id=document_id)
self.notice_repository.add(notice=self._create_notice(notice_data=document_result))

return self._create_notice(notice_data=document_result)

def get_notices_by_query(self, query: dict) -> List[Notice]:
def fetch_notices_by_query(self, query: dict):
"""
This method will fetch a list of notices by using a search query
:param query:
:return:
"""
documents = self.ted_api_adapter.get_by_query(query=query)
return [self._create_notice(notice_data=document) for document in documents]
for document in documents:
self.notice_repository.add(notice=self._create_notice(notice_data=document))

def get_notices_by_date_range(self, start_date: date, end_date: date) -> List[Notice]:
def fetch_notices_by_date_range(self, start_date: date, end_date: date):
"""
This method will fetch a list of notices by using a date range
:param start_date:
:param end_date:
:return:
"""
documents = self.ted_api_adapter.get_by_range_date(start_date=start_date, end_date=end_date)
return [self._create_notice(notice_data=document) for document in documents]
for document in documents:
self.notice_repository.add(notice=self._create_notice(notice_data=document))

def get_notices_by_date_wild_card(self, wildcard_date: str) -> List[Notice]:
def fetch_notices_by_date_wild_card(self, wildcard_date: str):
"""
This method will fetch a list of notices by using a wildcard date
:param wildcard_date:
:return:
"""
documents = self.ted_api_adapter.get_by_wildcard_date(wildcard_date=wildcard_date)
return [self._create_notice(notice_data=document) for document in documents]
for document in documents:
self.notice_repository.add(notice=self._create_notice(notice_data=document))
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import pytest

from ted_sws.notice_fetcher.adapters.ted_api import TedAPIAdapter
from tests.fakes.fake_repository import FakeNoticeRepository
from tests.fakes.fake_ted_api import FakeRequestAPI



@pytest.fixture
def notice_repository():
return FakeNoticeRepository()

@pytest.fixture
def ted_document_search():
return TedAPIAdapter(request_api=FakeRequestAPI())
8 changes: 8 additions & 0 deletions tests/e2e/notice_fetcher/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest

from ted_sws.notice_fetcher.adapters.ted_api import TedAPIAdapter, TedRequestAPI


@pytest.fixture
def ted_document_search():
return TedAPIAdapter(request_api=TedRequestAPI())
31 changes: 14 additions & 17 deletions tests/e2e/notice_fetcher/test_notice_fetcher.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import datetime

from ted_sws.domain.model.notice import Notice, NoticeStatus
from ted_sws.notice_fetcher.adapters.ted_api import TedAPIAdapter, TedRequestAPI
from ted_sws.notice_fetcher.services.notice_fetcher import NoticeFetcher


def test_notice_fetcher_by_identifier():
def test_notice_fetcher_by_identifier(notice_repository, ted_document_search):
document_id = "067623-2022"
notice = NoticeFetcher(ted_api_adapter=TedAPIAdapter(request_api=TedRequestAPI())).get_notice_by_id(
NoticeFetcher(notice_repository=notice_repository, ted_api_adapter=ted_document_search).fetch_notice_by_id(
document_id=document_id)

notice = notice_repository.get(reference=document_id)
assert isinstance(notice, Notice)
assert notice
assert notice.original_metadata
Expand All @@ -19,35 +17,34 @@ def test_notice_fetcher_by_identifier():
assert notice.status == NoticeStatus.RAW


def test_notice_fetcher_by_search_query():
def test_notice_fetcher_by_search_query(notice_repository, ted_document_search):
query = {"q": "ND=[67623-2022]"}

notices = NoticeFetcher(ted_api_adapter=TedAPIAdapter(request_api=TedRequestAPI())).get_notices_by_query(
NoticeFetcher(notice_repository=notice_repository, ted_api_adapter=ted_document_search).fetch_notices_by_query(
query=query)

notices = [notice_repository.get(reference=reference) for reference in notice_repository.list()]
assert isinstance(notices, list)
assert len(notices) == 1
assert isinstance(notices[0], Notice)


def test_notice_fetcher_by_date_range():
notices = NoticeFetcher(ted_api_adapter=TedAPIAdapter(request_api=TedRequestAPI())).get_notices_by_date_range(
start_date=datetime.date(2022, 2, 3),
end_date=datetime.date(2022, 2, 3))
def test_notice_fetcher_by_date_range(notice_repository, ted_document_search):
NoticeFetcher(notice_repository=notice_repository, ted_api_adapter=ted_document_search).fetch_notices_by_date_range(
start_date=datetime.date(2022, 2, 3), end_date=datetime.date(2022, 2, 3))
xml_text = "<NOTICE_DATA>"

notices = [notice_repository.get(reference=reference) for reference in notice_repository.list()]
assert isinstance(notices, list)
assert len(notices) == 95
assert isinstance(notices[0], Notice)
assert xml_text in notices[0].xml_manifestation.object_data


def test_notice_fetcher_by_date_wild_card():
notices = NoticeFetcher(
ted_api_adapter=TedAPIAdapter(request_api=TedRequestAPI())).get_notices_by_date_wild_card(
def test_notice_fetcher_by_date_wild_card(notice_repository, ted_document_search):
NoticeFetcher(notice_repository=notice_repository,
ted_api_adapter=ted_document_search).fetch_notices_by_date_wild_card(
wildcard_date="20220203*")
xml_text = "<NOTICE_DATA>"

notices = [notice_repository.get(reference=reference) for reference in notice_repository.list()]
assert isinstance(notices, list)
assert len(notices) == 95
assert isinstance(notices[0], Notice)
Expand Down
2 changes: 1 addition & 1 deletion tests/fakes/fake_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ def list(self) -> List[str]:
This method allows all records to be retrieved from the repository.
:return:
"""
return list(self.repository.keys())
return list(self.repository.keys())
14 changes: 8 additions & 6 deletions tests/features/notice_fetcher/test_fetching_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ def step_impl(notice_identifier):


@when("the call to the API is made", target_fixture="api_call")
def step_impl(identifier, api_url):
return NoticeFetcher(
ted_api_adapter=TedAPIAdapter(request_api=TedRequestAPI(), ted_api_url=api_url)).get_notice_by_id(
def step_impl(identifier, api_url, fake_notice_storage):
NoticeFetcher(notice_repository=fake_notice_storage,
ted_api_adapter=TedAPIAdapter(request_api=TedRequestAPI(), ted_api_url=api_url)).fetch_notice_by_id(
document_id=identifier)
return fake_notice_storage.get(reference=identifier)


@then("a notice with that identifier and the notice metadata are available", target_fixture="fake_notice_storage")
Expand All @@ -59,10 +60,11 @@ def step_impl(notice_search_query):


@when("the call to the search API is made", target_fixture="api_call")
def step_impl(notice_search_query, api_end_point):
return NoticeFetcher(
ted_api_adapter=TedAPIAdapter(request_api=TedRequestAPI(), ted_api_url=api_end_point)).get_notices_by_query(
def step_impl(notice_search_query, api_end_point, fake_notice_storage):
NoticeFetcher(notice_repository=fake_notice_storage,
ted_api_adapter=TedAPIAdapter(request_api=TedRequestAPI(), ted_api_url=api_end_point)).fetch_notices_by_query(
query=notice_search_query)
return [fake_notice_storage.get(reference=reference) for reference in fake_notice_storage.list()]


@then("notices that match the search query result and their metadata are available",
Expand Down
13 changes: 7 additions & 6 deletions tests/features/notice_fetcher/test_notice_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ def step_impl(notice_search_query):


@when("call to the API is made", target_fixture="api_call")
def step_impl(notice_search_query, api_end_point):
return NoticeFetcher(
ted_api_adapter=TedAPIAdapter(request_api=TedRequestAPI(), ted_api_url=api_end_point)).get_notices_by_query(
def step_impl(notice_search_query, api_end_point, fake_notice_storage):
NoticeFetcher(notice_repository=fake_notice_storage,
ted_api_adapter=TedAPIAdapter(request_api=TedRequestAPI(), ted_api_url=api_end_point)).fetch_notices_by_query(
query=notice_search_query)
return [fake_notice_storage.get(reference=reference) for reference in fake_notice_storage.list()]


@then("a notice and notice metadata is received from the API", target_fixture="fake_notice_storage")
Expand Down Expand Up @@ -62,10 +63,10 @@ def step_impl(notice_incorrect_search_query):


@when("the call to the API is made", target_fixture="api_call_message")
def step_impl(notice_incorrect_search_query, api_end_point):
def step_impl(notice_incorrect_search_query, api_end_point,fake_notice_storage):
with pytest.raises(Exception) as e:
NoticeFetcher(
ted_api_adapter=TedAPIAdapter(request_api=TedRequestAPI(), ted_api_url=api_end_point)).get_notices_by_query(
NoticeFetcher(notice_repository=fake_notice_storage,
ted_api_adapter=TedAPIAdapter(request_api=TedRequestAPI(), ted_api_url=api_end_point)).fetch_notices_by_query(
query=notice_incorrect_search_query)
return e

Expand Down
9 changes: 5 additions & 4 deletions tests/features/notice_fetcher/test_search_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ def step_impl(start, end):


@when("the call to the API is executed", target_fixture="api_call")
def step_impl(dates):
def step_impl(dates, fake_notice_storage):
start_date, end_date = dates
return NoticeFetcher(ted_api_adapter=TedAPIAdapter(request_api=TedRequestAPI())).get_notices_by_date_range(
start_date=start_date,
end_date=end_date)
NoticeFetcher(notice_repository=fake_notice_storage,
ted_api_adapter=TedAPIAdapter(request_api=TedRequestAPI())).fetch_notices_by_date_range(
start_date=start_date, end_date=end_date)
return [fake_notice_storage.get(reference=reference) for reference in fake_notice_storage.list()]


@then("search result set is returned", target_fixture="search_result")
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/domain/test_fake_notice_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

def test_fake_notice_repository(ted_document_search):
fake_notice_repository = FakeNoticeRepository()
notices = NoticeFetcher(ted_api_adapter=ted_document_search).get_notices_by_date_range(
start_date=datetime.date(2022, 2, 3),
end_date=datetime.date(2022, 2, 3))
NoticeFetcher(notice_repository=fake_notice_repository,
ted_api_adapter=ted_document_search).fetch_notices_by_date_range(
start_date=datetime.date(2022, 2, 3), end_date=datetime.date(2022, 2, 3))
notices = [fake_notice_repository.get(reference=reference) for reference in fake_notice_repository.list()]
for notice in notices:
fake_notice_repository.add(notice)
for notice in notices:
Expand Down
33 changes: 16 additions & 17 deletions tests/unit/notice_fetcher/test_notice_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
from ted_sws.notice_fetcher.services.notice_fetcher import NoticeFetcher


def test_notice_fetcher_by_identifier(ted_document_search):
def test_notice_fetcher_by_identifier(notice_repository, ted_document_search):
document_id = "067623-2022"
notice = NoticeFetcher(ted_api_adapter=ted_document_search).get_notice_by_id(
document_id=document_id)

NoticeFetcher(notice_repository=notice_repository, ted_api_adapter=ted_document_search).fetch_notice_by_id(document_id=document_id)
notice = notice_repository.get(reference=document_id)
assert isinstance(notice, Notice)
assert notice
assert notice.original_metadata
Expand All @@ -17,35 +16,35 @@ def test_notice_fetcher_by_identifier(ted_document_search):
assert notice.status == NoticeStatus.RAW


def test_notice_fetcher_by_search_query(ted_document_search):
def test_notice_fetcher_by_search_query(notice_repository, ted_document_search):
query = {"q": "ND=[67623-2022]"}

notices = NoticeFetcher(ted_api_adapter=ted_document_search).get_notices_by_query(
NoticeFetcher(notice_repository=notice_repository, ted_api_adapter=ted_document_search).fetch_notices_by_query(
query=query)

notices = [notice_repository.get(reference=reference) for reference in notice_repository.list()]
assert isinstance(notices, list)
assert len(notices) == 2
assert len(notices) == 1
assert isinstance(notices[0], Notice)


def test_notice_fetcher_by_date_range(ted_document_search):
notices = NoticeFetcher(ted_api_adapter=ted_document_search).get_notices_by_date_range(
start_date=datetime.date(2022, 2, 3),
end_date=datetime.date(2022, 2, 3))
def test_notice_fetcher_by_date_range(notice_repository, ted_document_search):
NoticeFetcher(notice_repository=notice_repository, ted_api_adapter=ted_document_search).fetch_notices_by_date_range(
start_date=datetime.date(2022, 2, 3), end_date=datetime.date(2022, 2, 3))
notices = [notice_repository.get(reference=reference) for reference in notice_repository.list()]
xml_text = "<NOTICE_DATA>"

assert isinstance(notices, list)
assert len(notices) == 2
assert len(notices) == 1
assert isinstance(notices[0], Notice)
assert xml_text in notices[0].xml_manifestation.object_data


def test_notice_fetcher_by_date_wild_card(ted_document_search):
notices = NoticeFetcher(ted_api_adapter=ted_document_search).get_notices_by_date_wild_card(
def test_notice_fetcher_by_date_wild_card(notice_repository, ted_document_search):
NoticeFetcher(notice_repository=notice_repository,ted_api_adapter=ted_document_search).fetch_notices_by_date_wild_card(
wildcard_date="20220203*")
notices = [notice_repository.get(reference=reference) for reference in notice_repository.list()]
xml_text = "<NOTICE_DATA>"

assert isinstance(notices, list)
assert len(notices) == 2
assert len(notices) == 1
assert isinstance(notices[0], Notice)
assert xml_text in notices[0].xml_manifestation.object_data

0 comments on commit 8923b03

Please sign in to comment.