|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import datetime |
| 6 | +import json |
6 | 7 |
|
7 | 8 | import pytest |
8 | | - |
9 | 9 | from aws_durable_execution_sdk_python.lambda_service import ( |
10 | 10 | OperationStatus, |
11 | 11 | OperationType, |
12 | 12 | ) |
| 13 | + |
13 | 14 | from aws_durable_execution_sdk_python_testing.exceptions import ( |
14 | 15 | InvalidParameterValueException, |
15 | 16 | ) |
|
46 | 47 | GetDurableExecutionResponse, |
47 | 48 | GetDurableExecutionStateRequest, |
48 | 49 | GetDurableExecutionStateResponse, |
| 50 | + InvocationCompletedDetails, |
49 | 51 | ListDurableExecutionsByFunctionRequest, |
50 | 52 | ListDurableExecutionsByFunctionResponse, |
51 | 53 | ListDurableExecutionsRequest, |
@@ -3564,3 +3566,102 @@ def test_events_to_operations_invalid_sub_type(): |
3564 | 3566 | match=f"'{invalid_sub_type}' is not a valid OperationSubType", |
3565 | 3567 | ): |
3566 | 3568 | events_to_operations([event]) |
| 3569 | + |
| 3570 | + |
| 3571 | +def test_invocation_completed_details_to_json_dict(): |
| 3572 | + """Test InvocationCompletedDetails.to_json_dict() converts datetime to Unix milliseconds.""" |
| 3573 | + start_time = datetime.datetime(2023, 1, 1, 0, 0, 0, 123456, tzinfo=datetime.UTC) |
| 3574 | + end_time = datetime.datetime(2023, 1, 1, 0, 1, 0, 456789, tzinfo=datetime.UTC) |
| 3575 | + |
| 3576 | + details = InvocationCompletedDetails( |
| 3577 | + start_timestamp=start_time, end_timestamp=end_time, request_id="req-123" |
| 3578 | + ) |
| 3579 | + |
| 3580 | + json_dict = details.to_json_dict() |
| 3581 | + |
| 3582 | + # Verify timestamps are converted to Unix milliseconds (integers) |
| 3583 | + assert json_dict["StartTimestamp"] == 1672531200123 |
| 3584 | + assert json_dict["EndTimestamp"] == 1672531260456 |
| 3585 | + assert json_dict["RequestId"] == "req-123" |
| 3586 | + |
| 3587 | + # Verify all values are JSON-serializable |
| 3588 | + json_str = json.dumps(json_dict) |
| 3589 | + assert json_str is not None |
| 3590 | + |
| 3591 | + |
| 3592 | +def test_invocation_completed_details_from_json_dict(): |
| 3593 | + """Test InvocationCompletedDetails.from_json_dict() converts Unix milliseconds to datetime.""" |
| 3594 | + json_dict = { |
| 3595 | + "StartTimestamp": 1672531200123, |
| 3596 | + "EndTimestamp": 1672531260456, |
| 3597 | + "RequestId": "req-456", |
| 3598 | + } |
| 3599 | + |
| 3600 | + details = InvocationCompletedDetails.from_json_dict(json_dict) |
| 3601 | + |
| 3602 | + # Verify timestamps are converted to datetime objects |
| 3603 | + assert details.start_timestamp == datetime.datetime( |
| 3604 | + 2023, 1, 1, 0, 0, 0, 123000, tzinfo=datetime.UTC |
| 3605 | + ) |
| 3606 | + assert details.end_timestamp == datetime.datetime( |
| 3607 | + 2023, 1, 1, 0, 1, 0, 456000, tzinfo=datetime.UTC |
| 3608 | + ) |
| 3609 | + assert details.request_id == "req-456" |
| 3610 | + |
| 3611 | + |
| 3612 | +def test_invocation_completed_details_json_round_trip(): |
| 3613 | + """Test InvocationCompletedDetails to_json_dict/from_json_dict round-trip.""" |
| 3614 | + original = InvocationCompletedDetails( |
| 3615 | + start_timestamp=datetime.datetime( |
| 3616 | + 2023, 6, 15, 12, 30, 45, 678000, tzinfo=datetime.UTC |
| 3617 | + ), |
| 3618 | + end_timestamp=datetime.datetime( |
| 3619 | + 2023, 6, 15, 12, 31, 50, 123000, tzinfo=datetime.UTC |
| 3620 | + ), |
| 3621 | + request_id="round-trip-test", |
| 3622 | + ) |
| 3623 | + |
| 3624 | + # Serialize to JSON dict |
| 3625 | + json_dict = original.to_json_dict() |
| 3626 | + |
| 3627 | + # Deserialize back |
| 3628 | + restored = InvocationCompletedDetails.from_json_dict(json_dict) |
| 3629 | + |
| 3630 | + # Verify round-trip preserves data |
| 3631 | + assert restored.start_timestamp == original.start_timestamp |
| 3632 | + assert restored.end_timestamp == original.end_timestamp |
| 3633 | + assert restored.request_id == original.request_id |
| 3634 | + |
| 3635 | + |
| 3636 | +def test_invocation_completed_details_to_dict_preserves_datetime(): |
| 3637 | + """Test InvocationCompletedDetails.to_dict() preserves datetime objects (not converted).""" |
| 3638 | + start_time = datetime.datetime(2023, 1, 1, 0, 0, 0, tzinfo=datetime.UTC) |
| 3639 | + end_time = datetime.datetime(2023, 1, 1, 0, 1, 0, tzinfo=datetime.UTC) |
| 3640 | + |
| 3641 | + details = InvocationCompletedDetails( |
| 3642 | + start_timestamp=start_time, end_timestamp=end_time, request_id="req-789" |
| 3643 | + ) |
| 3644 | + |
| 3645 | + regular_dict = details.to_dict() |
| 3646 | + |
| 3647 | + # Verify to_dict() preserves datetime objects (not converted to Unix milliseconds) |
| 3648 | + assert regular_dict["StartTimestamp"] == start_time |
| 3649 | + assert regular_dict["EndTimestamp"] == end_time |
| 3650 | + assert isinstance(regular_dict["StartTimestamp"], datetime.datetime) |
| 3651 | + assert isinstance(regular_dict["EndTimestamp"], datetime.datetime) |
| 3652 | + |
| 3653 | + |
| 3654 | +def test_invocation_completed_details_from_json_dict_invalid_timestamp(): |
| 3655 | + """Test InvocationCompletedDetails.from_json_dict() raises error for invalid timestamps.""" |
| 3656 | + # Test with invalid timestamp that would return None |
| 3657 | + json_dict = { |
| 3658 | + "StartTimestamp": None, |
| 3659 | + "EndTimestamp": 1672531260456, |
| 3660 | + "RequestId": "req-error", |
| 3661 | + } |
| 3662 | + |
| 3663 | + with pytest.raises( |
| 3664 | + InvalidParameterValueException, |
| 3665 | + match="StartTimestamp and EndTimestamp cannot be null", |
| 3666 | + ): |
| 3667 | + InvocationCompletedDetails.from_json_dict(json_dict) |
0 commit comments