-
Couldn't load subscription status.
- Fork 4
FakeService: filter: Fix field-existance check #212
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| # License: MIT | ||
| # Copyright © 2025 Frequenz Energy-as-a-Service GmbH | ||
|
|
||
| """Tests for the frequenz.client.dispatch.test._service package.""" | ||
|
|
||
| from datetime import datetime, timedelta, timezone | ||
|
|
||
| from frequenz.api.common.v1alpha8.pagination.pagination_params_pb2 import ( | ||
| PaginationParams, | ||
| ) | ||
|
|
||
| # pylint: disable=no-name-in-module | ||
| from frequenz.api.common.v1alpha8.types.interval_pb2 import Interval as PBInterval | ||
| from frequenz.api.dispatch.v1.dispatch_pb2 import ( | ||
| DispatchFilter, | ||
| ListMicrogridDispatchesRequest, | ||
| ) | ||
|
|
||
| from frequenz.client.base.conversion import to_timestamp | ||
| from frequenz.client.common.microgrid import MicrogridId | ||
| from frequenz.client.dispatch.recurrence import RecurrenceRule | ||
| from frequenz.client.dispatch.test._service import FakeService | ||
| from frequenz.client.dispatch.types import ( | ||
| Dispatch, | ||
| DispatchId, | ||
| TargetIds, | ||
| ) | ||
|
|
||
|
|
||
| async def test_list_dispatches_filter_start_time() -> None: | ||
| """Test listing dispatches with a start time filter.""" | ||
| service = FakeService() | ||
| now = datetime.now(timezone.utc) | ||
| dispatches = [ | ||
| Dispatch( | ||
| id=DispatchId(i), | ||
| start_time=now + timedelta(minutes=i), | ||
| duration=timedelta(minutes=1), | ||
| type="test", | ||
| target=TargetIds(1), | ||
| active=True, | ||
| dry_run=False, | ||
| payload={}, | ||
| recurrence=RecurrenceRule(), | ||
| create_time=now, | ||
| update_time=now, | ||
| ) | ||
| for i in range(10) | ||
| ] | ||
| service.dispatches[MicrogridId(1)] = dispatches | ||
|
|
||
| # Filter for dispatches starting after now + 5 minutes | ||
| req = ListMicrogridDispatchesRequest( | ||
| microgrid_id=1, | ||
| filter=DispatchFilter( | ||
| start_time_interval=PBInterval( | ||
| start_time=to_timestamp(now + timedelta(minutes=5)), | ||
| end_time=None, | ||
| ) | ||
| ), | ||
| ) | ||
| filtered_dispatches = [ | ||
| Dispatch.from_protobuf(dispatch) | ||
| for dispatch in (await service.ListMicrogridDispatches(req)).dispatches | ||
| ] | ||
| assert len(filtered_dispatches) == 5 | ||
| assert all(d.start_time >= now + timedelta(minutes=5) for d in filtered_dispatches) | ||
|
|
||
| # Filter for dispatches starting before now + 5 minutes | ||
| req = ListMicrogridDispatchesRequest( | ||
| microgrid_id=1, | ||
| filter=DispatchFilter( | ||
| start_time_interval=PBInterval( | ||
| start_time=None, | ||
| end_time=to_timestamp(now + timedelta(minutes=5)), | ||
| ) | ||
| ), | ||
| pagination_params=PaginationParams(page_size=100), | ||
| ) | ||
| filtered_dispatches = [ | ||
| Dispatch.from_protobuf(dispatch) | ||
| for dispatch in (await service.ListMicrogridDispatches(req)).dispatches | ||
| ] | ||
| assert len(filtered_dispatches) == 5 | ||
| assert all(d.start_time < now + timedelta(minutes=5) for d in filtered_dispatches) | ||
|
|
||
|
|
||
| async def test_list_dispatches_filter_end_time() -> None: | ||
| """Test listing dispatches with an end time filter.""" | ||
| service = FakeService() | ||
| now = datetime.now(timezone.utc) | ||
| dispatches = [ | ||
| Dispatch( | ||
| id=DispatchId(i), | ||
| start_time=now, | ||
| duration=timedelta(minutes=i), | ||
| type="test", | ||
| target=TargetIds(1), | ||
| active=True, | ||
| dry_run=False, | ||
| payload={}, | ||
| recurrence=RecurrenceRule(), | ||
| create_time=now, | ||
| update_time=now, | ||
| ) | ||
| for i in range(1, 11) | ||
| ] | ||
| service.dispatches[MicrogridId(1)] = dispatches | ||
|
|
||
| # Filter for dispatches ending after now + 5 minutes | ||
| req = ListMicrogridDispatchesRequest( | ||
| microgrid_id=1, | ||
| filter=DispatchFilter( | ||
| end_time_interval=PBInterval( | ||
| start_time=to_timestamp(now + timedelta(minutes=5)), | ||
| end_time=None, | ||
| ) | ||
| ), | ||
| pagination_params=PaginationParams(page_size=100), | ||
| ) | ||
| filtered_dispatches = [ | ||
| Dispatch.from_protobuf(dispatch) | ||
| for dispatch in (await service.ListMicrogridDispatches(req)).dispatches | ||
| ] | ||
| assert len(filtered_dispatches) == 6 | ||
| assert all( | ||
| d.start_time + d.duration >= now + timedelta(minutes=5) # type: ignore[operator] | ||
| for d in filtered_dispatches | ||
| ) | ||
|
|
||
| # Filter for dispatches ending before now + 5 minutes | ||
| req = ListMicrogridDispatchesRequest( | ||
| microgrid_id=1, | ||
| filter=DispatchFilter( | ||
| end_time_interval=PBInterval( | ||
| start_time=None, | ||
| end_time=to_timestamp(now + timedelta(minutes=5)), | ||
| ) | ||
| ), | ||
| ) | ||
| filtered_dispatches = [ | ||
| Dispatch.from_protobuf(dispatch) | ||
| for dispatch in (await service.ListMicrogridDispatches(req)).dispatches | ||
| ] | ||
| assert len(filtered_dispatches) == 4 | ||
| assert all( | ||
| d.start_time + d.duration < now + timedelta(minutes=5) # type: ignore[operator] | ||
| for d in filtered_dispatches | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 condition check order has changed from the original logic. The original code checked
dispatch.durationbefore the time comparison, but the parentheses now group the time comparison separately. Consider movingdispatch.durationcheck outside the parentheses to maintain the original short-circuit behavior:if dispatch.duration and dispatch.start_time + dispatch.duration < _to_dt(end_from):