Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainOfHacks committed Feb 13, 2024
1 parent 0d5939a commit 800f480
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
10 changes: 6 additions & 4 deletions ted_sws/notice_fetcher/adapters/ted_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
from ted_sws.event_manager.services.log import log_warning
from ted_sws.notice_fetcher.adapters.ted_api_abc import TedAPIAdapterABC, RequestAPI

DEFAULT_TED_API_QUERY_RESULT_SIZE = {"limit": 100,

DOCUMENTS_PER_PAGE = 100

DEFAULT_TED_API_QUERY_RESULT_SIZE = {"limit": DOCUMENTS_PER_PAGE,
"page": 1,
"scope": "ALL",
}
Expand Down Expand Up @@ -43,7 +46,7 @@ def __call__(self, api_url: str, api_query: dict) -> dict:
response_content = json.loads(response.text)
return response_content
else:
raise Exception(f"The TED-API call failed with: {response}, {response.content}, {api_url}")
raise Exception(f"The TED-API call failed with: {response}")


class TedAPIAdapter(TedAPIAdapterABC):
Expand Down Expand Up @@ -123,7 +126,7 @@ def get_generator_by_query(self, query: dict, result_fields: dict = None) -> Gen
query.update(result_fields or DEFAULT_TED_API_QUERY_RESULT_FIELDS)
response_body = self.request_api(api_url=self.ted_api_url, api_query=query)
documents_number = response_body[TOTAL_DOCUMENTS_NUMBER]
result_pages = 1 + int(documents_number) // 100
result_pages = 1 + int(documents_number) // DOCUMENTS_PER_PAGE
documents_content = response_body[RESPONSE_RESULTS]
if result_pages > 1:
for page_number in range(2, result_pages + 1):
Expand All @@ -136,7 +139,6 @@ def get_generator_by_query(self, query: dict, result_fields: dict = None) -> Gen
# document_id = "0" * (11 - len(document_id)) + document_id
# document_content[DOCUMENT_NOTICE_ID_KEY] = document_id
del document_content[LINKS_TO_CONTENT_KEY]
print("content exist")
yield document_content
else:
for document_content in documents_content:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def validate_and_update_daily_supra_notice(ted_publication_date: day_type, mongo
fetched_notice_ids = set(fetched_notice_ids_list)

ted_api_adapter: TedAPIAdapter = TedAPIAdapter(request_api=request_api)
query = {"q": f"PD=[{ted_publication_date.strftime('%Y%m%d*')}]"}
query = {"query": f"PD={ted_publication_date.strftime('%Y%m%d*')}"}
documents = ted_api_adapter.get_by_query(query=query, result_fields={"fields": ["ND"]})
api_notice_ids_list = [document["ND"] for document in documents] if documents and len(documents) else []
api_notice_ids = set(api_notice_ids_list)
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/data_manager/test_mongodb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def test_mongo_db_query_2():

def test_create_matview_for_notices(fake_mongodb_client):
notice_id = "696661-2022"
ted_api_query = {"q": f"ND=[{notice_id}]"}
ted_api_query = {"query": f"ND={notice_id}"}
mongodb_client = fake_mongodb_client
notice_repository = NoticeRepository(mongodb_client=mongodb_client)
NoticeFetcher(notice_repository=notice_repository,
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/data_manager/test_notice_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ def test_notice_repository_create(mongodb_client):
notice_repository = NoticeRepository(mongodb_client=mongodb_client, database_name=TEST_DATABASE_NAME)
notice = Notice(ted_id=NOTICE_TED_ID)
notice.set_xml_manifestation(XMLManifestation(object_data="HELLO"))
notice.set_original_metadata(TEDMetadata(**{"AA": ["Metadata"]}))
notice.set_original_metadata(TEDMetadata(**{"RN": ["Metadata"]}))
notice_repository.add(notice)
result_notice = notice_repository.get(reference=NOTICE_TED_ID)
assert result_notice
assert result_notice.ted_id == NOTICE_TED_ID
assert result_notice.original_metadata.AA == ["Metadata"]
assert result_notice.original_metadata.RN == ["Metadata"]
result_notices = list(notice_repository.list())
assert result_notices
assert len(result_notices) == 1
notice_repository.add(notice)
notice.set_original_metadata(ted_metadata=TEDMetadata(**{"AA": ["Updated metadata"]}))
notice.set_original_metadata(ted_metadata=TEDMetadata(**{"RN": ["Updated metadata"]}))
notice_repository.update(notice)
result_notice = notice_repository.get(reference=NOTICE_TED_ID)
assert result_notice
assert result_notice.ted_id == NOTICE_TED_ID
assert result_notice.original_metadata.AA == ["Updated metadata"]
assert result_notice.original_metadata.RN == ["Updated metadata"]
mongodb_client.drop_database(TEST_DATABASE_NAME)


Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/notice_fetcher/test_ted_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_ted_api():
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]"})
notice_by_query = ted.get_by_query(query={"query": "ND=67623-2022"})
assert xml_text in notice_by_id["content"]
assert isinstance(notice_by_id, dict)
assert len(notice_by_date) == 95
Expand All @@ -28,5 +28,5 @@ def test_ted_api():
def test_ted_api_error():
ted = TedAPIAdapter(request_api=TedRequestAPI())
with pytest.raises(Exception) as e:
ted.get_by_query(query={"q": "NDE=67623-2022"})
assert str(e.value) == "The TED-API call failed with: <Response [500]>"
ted.get_by_query(query={"query": "NDE=67623-2022"})
assert str(e.value) == "The TED-API call failed with: <Response [400]>"
13 changes: 9 additions & 4 deletions tests/e2e/notice_fetcher/test_ted_request_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@

def test_ted_request_api():
ted_api_request = TedRequestAPI()
notice_by_query = ted_api_request(api_url=config.TED_API_URL, api_query={"query": "ND=[67623-2022]"})
api_query = {"limit": 100,
"page": 1,
"scope": "ALL",
"query": "ND=67623-2022",
"fields": ["ND", "PD", "RN"]
}
notice_by_query = ted_api_request(api_url=config.TED_API_URL, api_query=api_query)
assert notice_by_query
assert isinstance(notice_by_query, dict)
with pytest.raises(Exception) as e:
ted_api_request(api_url=config.TED_API_URL, api_query={"q": "INCORRECT PARAMS"})
assert str(e.value) == "The TED-API call failed with: <Response [500]>"

ted_api_request(api_url=config.TED_API_URL, api_query={"query": "INCORRECT PARAMS"})
assert str(e.value) == "The TED-API call failed with: <Response [400]>"

0 comments on commit 800f480

Please sign in to comment.