From eb9aa24bddcf0bae6b23da08997124c99f5b0fdd Mon Sep 17 00:00:00 2001 From: David Graham Date: Sat, 11 May 2024 12:38:31 -0400 Subject: [PATCH] repalce manifest service search usage with EmanifestSearch class usage --- server/apps/manifest/services/emanifest.py | 37 +------------ .../manifest/services/emanifest_search.py | 30 ++++++++-- server/apps/manifest/tasks.py | 4 +- .../manifest/tests/test_search_emanifest.py | 55 +++---------------- server/apps/site/services.py | 9 ++- 5 files changed, 42 insertions(+), 93 deletions(-) diff --git a/server/apps/manifest/services/emanifest.py b/server/apps/manifest/services/emanifest.py index 1f31760e..2f5d79b0 100644 --- a/server/apps/manifest/services/emanifest.py +++ b/server/apps/manifest/services/emanifest.py @@ -1,5 +1,5 @@ import logging -from datetime import UTC, datetime, timedelta, timezone +from datetime import datetime from typing import List, Literal, NotRequired, Optional, TypedDict from django.db import transaction @@ -73,19 +73,6 @@ def is_available(self) -> bool: """Check if e-Manifest is available""" return self.rcrainfo.has_rcrainfo_credentials - def search(self, search_data: SearchManifestData) -> List[str]: - """Search for manifests from e-Manifest""" - data: dict = search_data.copy() - data["end_date"] = self._date_or_now(search_data.get("end_date", None)) - data["start_date"] = self._date_or_three_years_past(search_data.get("start_date", None)) - response = self.rcrainfo.search_mtn(**data) - if response.ok: - print("response OK") - ret = response.json() - print(ret) - return ret - return [] - def pull(self, tracking_numbers: List[str]) -> PullManifestsResult: """Retrieve manifests from e-Manifest and save to database""" results: PullManifestsResult = {"success": [], "error": []} @@ -141,28 +128,6 @@ def save(self, manifest: dict) -> dict: ) raise EManifestError("malformed payload") - def _date_or_three_years_past(self, start_date: Optional[datetime]) -> str: - return ( - start_date.replace(tzinfo=timezone.utc).strftime(self.rcrainfo.datetime_format) - if start_date - else ( - datetime.now(UTC) - - timedelta( - minutes=60 # 60 seconds/1minutes - * 24 # 24 hours/1day - * 30 # 30 days/1month - * 36 # 36 months/3years = 3/years - ) - ).strftime(self.rcrainfo.datetime_format) - ) - - def _date_or_now(self, end_date: Optional[datetime]) -> str: - return ( - end_date.replace(tzinfo=timezone.utc).strftime(self.rcrainfo.datetime_format) - if end_date - else datetime.now(UTC).strftime(self.rcrainfo.datetime_format) - ) - @staticmethod def _filter_mtn( *, mtn: list[str], site_id: str, site_type: Literal["Generator", "Tsdf", "Transporter"] diff --git a/server/apps/manifest/services/emanifest_search.py b/server/apps/manifest/services/emanifest_search.py index 610be6b4..5e44d9df 100644 --- a/server/apps/manifest/services/emanifest_search.py +++ b/server/apps/manifest/services/emanifest_search.py @@ -1,4 +1,4 @@ -from datetime import datetime, timezone +from datetime import UTC, datetime, timedelta, timezone from typing import Literal, Optional, get_args from apps.core.services import RcrainfoService @@ -71,6 +71,28 @@ def _emanifest_date_type(cls, date_type) -> bool: def _emanifest_correction_request_status(cls, correction_request_status) -> bool: return cls.__validate_literal(correction_request_status, CorrectionRequestStatus) + def _date_or_three_years_past(self, start_date: Optional[datetime]) -> str: + return ( + start_date.replace(tzinfo=timezone.utc).strftime(self.rcra_client.datetime_format) + if start_date + else ( + datetime.now(UTC) + - timedelta( + minutes=60 # 60 seconds/1minutes + * 24 # 24 hours/1day + * 30 # 30 days/1month + * 36 # 36 months/3years = 3/years + ) + ).strftime(self.rcra_client.datetime_format) + ) + + def _date_or_now(self, end_date: Optional[datetime]) -> str: + return ( + end_date.replace(tzinfo=timezone.utc).strftime(self.rcra_client.datetime_format) + if end_date + else datetime.now(UTC).strftime(self.rcra_client.datetime_format) + ) + def build_search_args(self): search_params = { "stateCode": self.state_code, @@ -120,13 +142,11 @@ def add_correction_request_status(self, correction_request_status: CorrectionReq return self def add_start_date(self, start_date: datetime = None): - self.start_date = start_date + self.start_date = self._date_or_three_years_past(start_date) return self def add_end_date(self, end_date: datetime = None): - if not end_date: - end_date = datetime.now().replace(tzinfo=timezone.utc) - self.end_date = end_date + self.end_date = self._date_or_now(end_date) return self def execute(self): diff --git a/server/apps/manifest/tasks.py b/server/apps/manifest/tasks.py index f1b0c72f..79734ca7 100644 --- a/server/apps/manifest/tasks.py +++ b/server/apps/manifest/tasks.py @@ -57,10 +57,10 @@ def sign_manifest( @shared_task(name="sync site manifests", bind=True) def sync_site_manifests(self, *, site_id: str, username: str): """asynchronous task to sync an EPA site's manifests""" - from apps.rcrasite.services import HaztrakSiteService + from apps.site.services import TrakSiteService try: - site_service = HaztrakSiteService(username=username) + site_service = TrakSiteService(username=username) results = site_service.sync_manifests(site_id=site_id) return results except Exception as exc: diff --git a/server/apps/manifest/tests/test_search_emanifest.py b/server/apps/manifest/tests/test_search_emanifest.py index c292e986..245e2e5e 100644 --- a/server/apps/manifest/tests/test_search_emanifest.py +++ b/server/apps/manifest/tests/test_search_emanifest.py @@ -1,54 +1,12 @@ import json -from datetime import datetime, timezone -from unittest.mock import Mock, patch +from datetime import datetime import pytest from apps.core.services import RcrainfoService -from apps.manifest.services import EManifest -from apps.manifest.services.emanifest import SearchManifestData from apps.manifest.services.emanifest_search import EmanifestSearch -class TestSearchEManifestService: - @pytest.fixture() - def mock_rcrainfo(self): - rcrainfo_client = Mock() - rcrainfo_client.datetime_format = "%Y-%m-%d" - return rcrainfo_client - - @pytest.fixture() - def mock_response(self): - response = Mock() - response.ok = True - response.json = Mock(return_value=[]) - return response - - def test_end_date_defaults_to_now(self, mock_rcrainfo, mock_response): - emanifest = EManifest(username="test", rcrainfo=mock_rcrainfo) - search_data: SearchManifestData = { - "site_id": "test", - "start_date": datetime.now(), - } - # Act - emanifest.search(search_data) - _, kwargs = mock_rcrainfo.search_mtn.call_args - end_date = kwargs["end_date"] - assert end_date is not None - assert end_date == datetime.now().strftime(mock_rcrainfo.datetime_format) - - def test_uses_a_default_start_date(self, mock_rcrainfo, mock_response): - emanifest = EManifest(username="test", rcrainfo=mock_rcrainfo) - search_data: SearchManifestData = { - "site_id": "test", - } - # Act - emanifest.search(search_data) - _, kwargs = mock_rcrainfo.search_mtn.call_args - start_date = kwargs["start_date"] - assert start_date is not None - - class TestEmanifestSearchClass: def test_defaults_to_unauthenticated_rcra_client(self): search = EmanifestSearch() @@ -143,11 +101,12 @@ def test_add_end_date(self): assert search.end_date is not None def test_add_end_date_defaults_to_now(self): - with patch("apps.manifest.services.emanifest_search.datetime") as mock_datetime: - mock_datetime.now.return_value = datetime(2021, 1, 1).replace(tzinfo=timezone.utc) - search = EmanifestSearch().add_end_date() - assert search.end_date is not None - assert search.end_date == datetime(2021, 1, 1).replace(tzinfo=timezone.utc) + now = datetime.now() + search = EmanifestSearch().add_end_date() + end_date = datetime.strptime(search.end_date, RcrainfoService.datetime_format) + assert end_date.day == now.day + assert end_date.month == now.month + assert end_date.year == now.year class TestBuildSearchWithCorrectionRequestStatus: def test_add_correction_request_status(self): diff --git a/server/apps/site/services.py b/server/apps/site/services.py index bb7d2f43..24ca924d 100644 --- a/server/apps/site/services.py +++ b/server/apps/site/services.py @@ -7,6 +7,7 @@ from apps.core.services import RcrainfoService, get_rcrainfo_client from apps.manifest.services import EManifest, PullManifestsResult, TaskResponse +from apps.manifest.services.emanifest_search import EmanifestSearch from apps.manifest.tasks import sync_site_manifests from apps.site.models import TrakSite @@ -56,8 +57,12 @@ def sync_manifests(self, *, site_id: str) -> PullManifestsResult: def _get_updated_mtn(self, site_id: str, last_sync_date: datetime) -> list[str]: logger.info(f"retrieving updated MTN for site {site_id}") - return EManifest(username=self.username, rcrainfo=self.rcrainfo).search( - {"site_id": site_id, "start_date": last_sync_date} + return ( + EmanifestSearch(self.rcrainfo) + .add_site_id(site_id) + .add_start_date(last_sync_date) + .add_end_date() + .execute() )