|
| 1 | +# (C) 2026 GoodData Corporation |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | +from gooddata_sdk import ExecutionResultLimitBreak, GoodDataSdk, ObjId, SimpleMetric, TableDimension |
| 8 | +from gooddata_sdk.compute.model.execution import ExecutionDefinition, ExecutionResult |
| 9 | +from tests_support.vcrpy_utils import get_vcr |
| 10 | + |
| 11 | +gd_vcr = get_vcr() |
| 12 | + |
| 13 | +_current_dir = Path(__file__).parent.absolute() |
| 14 | +_fixtures_dir = _current_dir / "fixtures" |
| 15 | + |
| 16 | + |
| 17 | +# --------------------------------------------------------------------------- |
| 18 | +# Unit tests — ExecutionResultLimitBreak.from_api |
| 19 | +# --------------------------------------------------------------------------- |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.parametrize( |
| 23 | + "scenario, data, expected_limit, expected_limit_type, expected_value", |
| 24 | + [ |
| 25 | + ( |
| 26 | + "all_fields", |
| 27 | + {"limit": 1000, "limitType": "rowCount", "value": 1500}, |
| 28 | + 1000, |
| 29 | + "rowCount", |
| 30 | + 1500, |
| 31 | + ), |
| 32 | + ( |
| 33 | + "absent_value", |
| 34 | + {"limit": 500, "limitType": "cellCount"}, |
| 35 | + 500, |
| 36 | + "cellCount", |
| 37 | + None, |
| 38 | + ), |
| 39 | + ( |
| 40 | + "null_value", |
| 41 | + {"limit": 200, "limitType": "rowCount", "value": None}, |
| 42 | + 200, |
| 43 | + "rowCount", |
| 44 | + None, |
| 45 | + ), |
| 46 | + ], |
| 47 | +) |
| 48 | +def test_execution_result_limit_break_from_api( |
| 49 | + scenario: str, |
| 50 | + data: dict, |
| 51 | + expected_limit: int, |
| 52 | + expected_limit_type: str, |
| 53 | + expected_value: int | None, |
| 54 | +) -> None: |
| 55 | + lb = ExecutionResultLimitBreak.from_api(data) |
| 56 | + assert lb.limit == expected_limit |
| 57 | + assert lb.limit_type == expected_limit_type |
| 58 | + assert lb.value == expected_value |
| 59 | + |
| 60 | + |
| 61 | +# --------------------------------------------------------------------------- |
| 62 | +# Unit tests — ExecutionResult.limit_breaks property |
| 63 | +# --------------------------------------------------------------------------- |
| 64 | + |
| 65 | + |
| 66 | +def _make_execution_result(metadata: dict) -> ExecutionResult: |
| 67 | + """Build an ExecutionResult from a plain-dict mock result.""" |
| 68 | + result = { |
| 69 | + "data": [], |
| 70 | + "dimension_headers": [], |
| 71 | + "grand_totals": [], |
| 72 | + "paging": {"total": [0], "count": [0], "offset": [0]}, |
| 73 | + "metadata": metadata, |
| 74 | + } |
| 75 | + return ExecutionResult(result) |
| 76 | + |
| 77 | + |
| 78 | +def test_limit_breaks_absent_returns_empty_list() -> None: |
| 79 | + """When limitBreaks is not in the metadata, limit_breaks returns [].""" |
| 80 | + er = _make_execution_result({"dataSourceMessages": []}) |
| 81 | + assert er.limit_breaks == [] |
| 82 | + |
| 83 | + |
| 84 | +def test_limit_breaks_present_returns_parsed_objects() -> None: |
| 85 | + """When limitBreaks is present, limit_breaks returns a list of ExecutionResultLimitBreak.""" |
| 86 | + metadata = { |
| 87 | + "dataSourceMessages": [], |
| 88 | + "limitBreaks": [ |
| 89 | + {"limit": 1000, "limitType": "rowCount", "value": 1234}, |
| 90 | + {"limit": 500, "limitType": "cellCount"}, |
| 91 | + ], |
| 92 | + } |
| 93 | + er = _make_execution_result(metadata) |
| 94 | + breaks = er.limit_breaks |
| 95 | + assert len(breaks) == 2 |
| 96 | + |
| 97 | + assert breaks[0].limit == 1000 |
| 98 | + assert breaks[0].limit_type == "rowCount" |
| 99 | + assert breaks[0].value == 1234 |
| 100 | + |
| 101 | + assert breaks[1].limit == 500 |
| 102 | + assert breaks[1].limit_type == "cellCount" |
| 103 | + assert breaks[1].value is None |
| 104 | + |
| 105 | + |
| 106 | +# --------------------------------------------------------------------------- |
| 107 | +# Integration test — limit_breaks accessible after real execution |
| 108 | +# --------------------------------------------------------------------------- |
| 109 | + |
| 110 | + |
| 111 | +@gd_vcr.use_cassette(str(_fixtures_dir / "test_execution_limit_breaks.yaml")) |
| 112 | +def test_execution_limit_breaks_integration(test_config): |
| 113 | + """Integration test: execute a computation and verify limit_breaks is accessible. |
| 114 | +
|
| 115 | + In normal operation (no limits exceeded) limit_breaks returns an empty list. |
| 116 | + This test verifies the SDK correctly handles the absent limitBreaks field. |
| 117 | + """ |
| 118 | + sdk = GoodDataSdk.create(host_=test_config["host"], token_=test_config["token"]) |
| 119 | + |
| 120 | + exec_def = ExecutionDefinition( |
| 121 | + attributes=None, |
| 122 | + metrics=[SimpleMetric(local_id="m1", item=ObjId(type="metric", id="order_amount"))], |
| 123 | + filters=None, |
| 124 | + dimensions=[TableDimension(item_ids=["measureGroup"])], |
| 125 | + ) |
| 126 | + |
| 127 | + execution = sdk.compute.for_exec_def(test_config["workspace"], exec_def) |
| 128 | + result = execution.read_result(limit=1) |
| 129 | + |
| 130 | + assert isinstance(result.limit_breaks, list) |
0 commit comments