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