|
1 | 1 | """Tests for the concurrency module.""" |
2 | 2 |
|
| 3 | +import json |
3 | 4 | import random |
4 | 5 | import threading |
5 | 6 | import time |
@@ -102,28 +103,6 @@ def test_batch_item_from_dict(): |
102 | 103 | assert item.error is None |
103 | 104 |
|
104 | 105 |
|
105 | | -def test_batch_item_from_dict_with_error(): |
106 | | - """Test BatchItem from_dict with error object.""" |
107 | | - error_data = { |
108 | | - "message": "Test error", |
109 | | - "type": "TestError", |
110 | | - "data": None, |
111 | | - "stackTrace": None, |
112 | | - } |
113 | | - data = { |
114 | | - "index": 1, |
115 | | - "status": "FAILED", |
116 | | - "result": None, |
117 | | - "error": error_data, |
118 | | - } |
119 | | - |
120 | | - item = BatchItem.from_dict(data) |
121 | | - assert item.index == 1 |
122 | | - assert item.status == BatchItemStatus.FAILED |
123 | | - assert item.result is None |
124 | | - assert item.error is not None |
125 | | - |
126 | | - |
127 | 106 | def test_batch_result_creation(): |
128 | 107 | """Test BatchResult creation.""" |
129 | 108 | items = [ |
@@ -2676,3 +2655,79 @@ def mock_get_checkpoint_result(operation_id): |
2676 | 2655 | assert len(result.all) == 1 |
2677 | 2656 | assert result.all[0].status == BatchItemStatus.SUCCEEDED |
2678 | 2657 | assert result.all[0].result == "re_executed_result" |
| 2658 | + |
| 2659 | + |
| 2660 | +def test_batch_item_from_dict_with_error(): |
| 2661 | + """Test BatchItem.from_dict() with error.""" |
| 2662 | + data = { |
| 2663 | + "index": 3, |
| 2664 | + "status": "FAILED", |
| 2665 | + "result": None, |
| 2666 | + "error": { |
| 2667 | + "ErrorType": "ValueError", |
| 2668 | + "ErrorMessage": "bad value", |
| 2669 | + "StackTrace": [], |
| 2670 | + }, |
| 2671 | + } |
| 2672 | + |
| 2673 | + item = BatchItem.from_dict(data) |
| 2674 | + |
| 2675 | + assert item.index == 3 |
| 2676 | + assert item.status == BatchItemStatus.FAILED |
| 2677 | + assert item.error.type == "ValueError" |
| 2678 | + assert item.error.message == "bad value" |
| 2679 | + |
| 2680 | + |
| 2681 | +def test_batch_result_with_mixed_statuses(): |
| 2682 | + """Test BatchResult serialization with mixed item statuses.""" |
| 2683 | + result = BatchResult( |
| 2684 | + all=[ |
| 2685 | + BatchItem(0, BatchItemStatus.SUCCEEDED, result="success"), |
| 2686 | + BatchItem( |
| 2687 | + 1, |
| 2688 | + BatchItemStatus.FAILED, |
| 2689 | + error=ErrorObject(message="msg", type="E", data=None, stack_trace=[]), |
| 2690 | + ), |
| 2691 | + BatchItem(2, BatchItemStatus.STARTED), |
| 2692 | + ], |
| 2693 | + completion_reason=CompletionReason.FAILURE_TOLERANCE_EXCEEDED, |
| 2694 | + ) |
| 2695 | + |
| 2696 | + serialized = json.dumps(result.to_dict()) |
| 2697 | + deserialized = BatchResult.from_dict(json.loads(serialized)) |
| 2698 | + |
| 2699 | + assert len(deserialized.all) == 3 |
| 2700 | + assert deserialized.all[0].status == BatchItemStatus.SUCCEEDED |
| 2701 | + assert deserialized.all[1].status == BatchItemStatus.FAILED |
| 2702 | + assert deserialized.all[2].status == BatchItemStatus.STARTED |
| 2703 | + assert deserialized.completion_reason == CompletionReason.FAILURE_TOLERANCE_EXCEEDED |
| 2704 | + |
| 2705 | + |
| 2706 | +def test_batch_result_empty_list(): |
| 2707 | + """Test BatchResult serialization with empty items list.""" |
| 2708 | + result = BatchResult(all=[], completion_reason=CompletionReason.ALL_COMPLETED) |
| 2709 | + |
| 2710 | + serialized = json.dumps(result.to_dict()) |
| 2711 | + deserialized = BatchResult.from_dict(json.loads(serialized)) |
| 2712 | + |
| 2713 | + assert len(deserialized.all) == 0 |
| 2714 | + assert deserialized.completion_reason == CompletionReason.ALL_COMPLETED |
| 2715 | + |
| 2716 | + |
| 2717 | +def test_batch_result_complex_nested_data(): |
| 2718 | + """Test BatchResult with complex nested data structures.""" |
| 2719 | + complex_result = { |
| 2720 | + "users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}], |
| 2721 | + "metadata": {"count": 2, "timestamp": "2025-10-31"}, |
| 2722 | + } |
| 2723 | + |
| 2724 | + result = BatchResult( |
| 2725 | + all=[BatchItem(0, BatchItemStatus.SUCCEEDED, result=complex_result)], |
| 2726 | + completion_reason=CompletionReason.ALL_COMPLETED, |
| 2727 | + ) |
| 2728 | + |
| 2729 | + serialized = json.dumps(result.to_dict()) |
| 2730 | + deserialized = BatchResult.from_dict(json.loads(serialized)) |
| 2731 | + |
| 2732 | + assert deserialized.all[0].result == complex_result |
| 2733 | + assert deserialized.all[0].result["users"][0]["name"] == "Alice" |
0 commit comments