From 741e38493dff026f9960f6f7f4c7146589bbfbae Mon Sep 17 00:00:00 2001 From: JohnPetros Date: Thu, 4 Jul 2024 18:03:19 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=A1=20mocks:=20fix=20get=20sensors=20r?= =?UTF-8?q?ecords=20count=20params=20of=20sensors=20records=20repository?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sensors_record_repository_interface.py | 4 ++- .../create_checklist_record_by_form_test.py | 6 ++--- .../create_sensors_records_by_form_test.py | 4 +-- .../tests/update_sensors_record_test.py | 25 ++++++++++++++++--- .../sensors_records_repository_mock.py | 4 ++- 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/app/core/interfaces/repositories/sensors_record_repository_interface.py b/src/app/core/interfaces/repositories/sensors_record_repository_interface.py index cc8a11a..838adb0 100644 --- a/src/app/core/interfaces/repositories/sensors_record_repository_interface.py +++ b/src/app/core/interfaces/repositories/sensors_record_repository_interface.py @@ -25,7 +25,9 @@ def get_last_sensors_records(self, count) -> list[SensorsRecord]: ... def get_sensors_record_by_id(self, id: str) -> SensorsRecord | None: ... @abstractmethod - def get_sensors_records_count(self) -> int: ... + def get_sensors_records_count( + self, plant_id: str, start_date: date, end_date: date, page_number: int = 1 + ) -> int: ... @abstractmethod def update_sensors_record_by_id(self, sensors_record: SensorsRecord) -> None: ... diff --git a/src/app/core/use_cases/checklist_records/tests/create_checklist_record_by_form_test.py b/src/app/core/use_cases/checklist_records/tests/create_checklist_record_by_form_test.py index bbaf8b2..1a5dd63 100644 --- a/src/app/core/use_cases/checklist_records/tests/create_checklist_record_by_form_test.py +++ b/src/app/core/use_cases/checklist_records/tests/create_checklist_record_by_form_test.py @@ -1,4 +1,4 @@ -from datetime import datetime, date +from datetime import date from pytest import fixture, raises @@ -16,9 +16,7 @@ def fake_request(base_fake_request: dict): return { - "fertilizer_expiration_date": datetime( - year=2024, month=3, day=16, hour=12, minute=0 - ), + "fertilizer_expiration_date": date(year=2024, month=3, day=16), "date": date(year=2024, month=12, day=12), "hour": 12, "illuminance": 12, diff --git a/src/app/core/use_cases/sensors_records/tests/create_sensors_records_by_form_test.py b/src/app/core/use_cases/sensors_records/tests/create_sensors_records_by_form_test.py index dbb29b9..82b5d3c 100644 --- a/src/app/core/use_cases/sensors_records/tests/create_sensors_records_by_form_test.py +++ b/src/app/core/use_cases/sensors_records/tests/create_sensors_records_by_form_test.py @@ -1,4 +1,4 @@ -from datetime import datetime, date +from datetime import date, time from pytest import fixture, raises @@ -16,7 +16,7 @@ def fake_request(base_fake_request: dict): return { - "time": datetime(year=2024, month=12, day=12, hour=12, minute=52), + "time": time(hour=12, minute=52), "date": date(year=2024, month=12, day=12), "soil_humidity": 32, "ambient_humidity": 55, diff --git a/src/app/core/use_cases/sensors_records/tests/update_sensors_record_test.py b/src/app/core/use_cases/sensors_records/tests/update_sensors_record_test.py index aff9d98..ee2b163 100644 --- a/src/app/core/use_cases/sensors_records/tests/update_sensors_record_test.py +++ b/src/app/core/use_cases/sensors_records/tests/update_sensors_record_test.py @@ -1,4 +1,4 @@ -from datetime import datetime, date +from datetime import time, date from pytest import fixture, raises @@ -7,7 +7,11 @@ SensorRecordsRepositoryMock, ) from core.entities.tests.fakers import SensorsRecordsFaker, PlantsFaker -from core.errors.validation import SensorsRecordNotValidError, DatetimeNotValidError +from core.errors.validation import ( + SensorsRecordNotValidError, + DatetimeNotValidError, + DateNotValidError, +) from core.errors.sensors_records import SensorsRecordNotFoundError from core.errors.plants import PlantNotFoundError @@ -16,7 +20,7 @@ def fake_request(base_fake_request: dict): return { - "time": datetime(year=2024, month=12, day=12, hour=12, minute=52), + "time": time(hour=12, minute=52), "date": date(year=2024, month=12, day=12), "soil_humidity": 32, "ambient_humidity": 55, @@ -64,14 +68,27 @@ def it_should_throw_error_if_no_sensors_record_is_found_in_repository( request=fake_request({"sensors_record_id": fake_record.id}) ) - def it_should_throw_error_if_date_from_request_is_not_valid( + def it_should_throw_error_if_time_from_request_is_not_valid( sensors_records_repository: SensorRecordsRepositoryMock, use_case: UpdateSensorsRecord, ): fake_record = SensorsRecordsFaker.fake() sensors_records_repository.create_sensors_record(fake_record) + request = fake_request( + {"time": "not valid time", "sensors_record_id": fake_record.id} + ) with raises(DatetimeNotValidError): + use_case.execute(request) + + def it_should_throw_error_if_date_from_request_is_not_valid( + sensors_records_repository: SensorRecordsRepositoryMock, + use_case: UpdateSensorsRecord, + ): + fake_record = SensorsRecordsFaker.fake() + sensors_records_repository.create_sensors_record(fake_record) + + with raises(DateNotValidError): use_case.execute( request=fake_request( {"sensors_record_id": fake_record.id, "date": None} diff --git a/src/app/core/use_cases/tests/mocks/repositories/sensors_records_repository_mock.py b/src/app/core/use_cases/tests/mocks/repositories/sensors_records_repository_mock.py index a92d167..516791d 100644 --- a/src/app/core/use_cases/tests/mocks/repositories/sensors_records_repository_mock.py +++ b/src/app/core/use_cases/tests/mocks/repositories/sensors_records_repository_mock.py @@ -40,7 +40,9 @@ def get_sensors_record_by_id(self, id: str) -> SensorsRecord | None: return None - def get_sensors_records_count(self) -> int: + def get_sensors_records_count( + self, plant_id: str, start_date: date, end_date: date, page_number: int = 1 + ) -> int: return len(self._sensors_records) def update_sensors_record_by_id(self, sensors_record: SensorsRecord):