|
11 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
| 14 | +import logging |
14 | 15 | from collections.abc import Callable |
15 | 16 | from dataclasses import dataclass, field |
16 | | -from unittest.mock import patch |
| 17 | +from textwrap import shorten |
| 18 | +from unittest.mock import Mock, patch |
17 | 19 |
|
18 | 20 | import pytest |
19 | 21 |
|
20 | 22 | from cognite.client import CogniteClient |
| 23 | +from cognite.client.data_classes import ExtractionPipeline, ExtractionPipelineRun |
21 | 24 | from cognite.extractorutils import Extractor |
22 | 25 | from cognite.extractorutils.configtools import BaseConfig, StateStoreConfig |
23 | 26 | from cognite.extractorutils.statestore import LocalStateStore, NoStateStore |
@@ -126,3 +129,151 @@ def test_state_store_getter() -> None: |
126 | 129 | e5._load_state_store() |
127 | 130 |
|
128 | 131 | assert isinstance(Extractor.get_current_statestore(), LocalStateStore) |
| 132 | + |
| 133 | + |
| 134 | +@patch("cognite.client.CogniteClient") |
| 135 | +def test_report_success( |
| 136 | + get_client_mock: Callable[[], CogniteClient], |
| 137 | +) -> None: |
| 138 | + print("Report success test") |
| 139 | + |
| 140 | + EXTRACTION_PIPELINE = "test_extraction_pipeline" |
| 141 | + MESSAGE = "test message" |
| 142 | + |
| 143 | + def validate_message(run: ExtractionPipelineRun): |
| 144 | + print(f"Validating message: {run.message}") |
| 145 | + assert run.extpipe_external_id == EXTRACTION_PIPELINE |
| 146 | + assert run.status == "success" |
| 147 | + assert run.message == MESSAGE, "Message does not match expected value" |
| 148 | + |
| 149 | + extractor = Extractor( |
| 150 | + name="extractor_test_report_success", |
| 151 | + description="description", |
| 152 | + config_class=ConfigWithoutStates, |
| 153 | + ) |
| 154 | + extractor._initial_load_config("tests/tests_unit/dummyconfig.yaml") |
| 155 | + extractor.cognite_client = get_client_mock() |
| 156 | + extractor.logger = logging.getLogger("test_logger") |
| 157 | + |
| 158 | + extractor.extraction_pipeline = ExtractionPipeline(external_id=EXTRACTION_PIPELINE, name=EXTRACTION_PIPELINE) |
| 159 | + extractor.cognite_client.extraction_pipelines.runs.create = Mock(side_effect=validate_message) |
| 160 | + |
| 161 | + extractor._report_success(message=MESSAGE) |
| 162 | + extractor.extraction_pipeline = None |
| 163 | + |
| 164 | + |
| 165 | +@patch("cognite.client.CogniteClient") |
| 166 | +def test_report_failure( |
| 167 | + get_client_mock: Callable[[], CogniteClient], |
| 168 | +) -> None: |
| 169 | + print("Report success test") |
| 170 | + |
| 171 | + EXTRACTION_PIPELINE = "test_extraction_pipeline" |
| 172 | + MESSAGE = "test message" |
| 173 | + |
| 174 | + def validate_message(run: ExtractionPipelineRun): |
| 175 | + print(f"Validating message: {run.message}") |
| 176 | + assert run.extpipe_external_id == EXTRACTION_PIPELINE |
| 177 | + assert run.status == "failure" |
| 178 | + assert run.message == MESSAGE, "Message does not match expected value" |
| 179 | + |
| 180 | + extractor = Extractor( |
| 181 | + name="extractor_test_report_failure", |
| 182 | + description="description", |
| 183 | + config_class=ConfigWithoutStates, |
| 184 | + ) |
| 185 | + extractor._initial_load_config("tests/tests_unit/dummyconfig.yaml") |
| 186 | + extractor.cognite_client = get_client_mock() |
| 187 | + extractor.logger = logging.getLogger("test_logger") |
| 188 | + |
| 189 | + extractor.extraction_pipeline = ExtractionPipeline(external_id=EXTRACTION_PIPELINE, name=EXTRACTION_PIPELINE) |
| 190 | + extractor.cognite_client.extraction_pipelines.runs.create = Mock(side_effect=validate_message) |
| 191 | + |
| 192 | + extractor._report_failure(message=MESSAGE) |
| 193 | + extractor.extraction_pipeline = None |
| 194 | + |
| 195 | + |
| 196 | +@patch("cognite.client.CogniteClient") |
| 197 | +def test_report_error( |
| 198 | + get_client_mock: Callable[[], CogniteClient], |
| 199 | +) -> None: |
| 200 | + print("Report error test") |
| 201 | + |
| 202 | + EXTRACTION_PIPELINE = "test_extraction_pipeline" |
| 203 | + MESSAGE = "test exception" |
| 204 | + expected_message = f"Exception: {MESSAGE}" |
| 205 | + |
| 206 | + def validate_message(run: ExtractionPipelineRun): |
| 207 | + print(f"Validating message: {run.message}") |
| 208 | + assert run.extpipe_external_id == EXTRACTION_PIPELINE |
| 209 | + assert run.status == "failure" |
| 210 | + assert run.message == expected_message, "Message does not match expected value" |
| 211 | + |
| 212 | + extractor = Extractor( |
| 213 | + name="extractor_test_report_error", |
| 214 | + description="description", |
| 215 | + config_class=ConfigWithoutStates, |
| 216 | + ) |
| 217 | + extractor._initial_load_config("tests/tests_unit/dummyconfig.yaml") |
| 218 | + extractor.cognite_client = get_client_mock() |
| 219 | + extractor.logger = logging.getLogger("test_logger") |
| 220 | + |
| 221 | + extractor.extraction_pipeline = ExtractionPipeline(external_id=EXTRACTION_PIPELINE, name=EXTRACTION_PIPELINE) |
| 222 | + extractor.cognite_client.extraction_pipelines.runs.create = Mock(side_effect=validate_message) |
| 223 | + |
| 224 | + exception = Exception(MESSAGE) |
| 225 | + |
| 226 | + extractor._report_error(exception=exception) |
| 227 | + extractor.extraction_pipeline = None |
| 228 | + |
| 229 | + |
| 230 | +@patch("cognite.client.CogniteClient") |
| 231 | +def test_report_run(get_client_mock: Callable[[], CogniteClient]): |
| 232 | + print("Report run test") |
| 233 | + |
| 234 | + MAX_MESSAGE_LENGTH_FOR_EXTRACTION_PIPELINE_RUN = 1000 |
| 235 | + |
| 236 | + EXTRACTION_PIPELINE = "test_extraction_pipeline" |
| 237 | + SHORT_MESSAGE = "hello world" |
| 238 | + LONG_MESSAGE = "x " * 1500 |
| 239 | + |
| 240 | + expected_long_message = shorten( |
| 241 | + text=LONG_MESSAGE, |
| 242 | + width=MAX_MESSAGE_LENGTH_FOR_EXTRACTION_PIPELINE_RUN, |
| 243 | + placeholder="...", |
| 244 | + ) |
| 245 | + |
| 246 | + # Mock method for reporting run |
| 247 | + def validate_short_message(run: ExtractionPipelineRun): |
| 248 | + print(f"Validating short message: {run.message}") |
| 249 | + assert run.extpipe_external_id == EXTRACTION_PIPELINE |
| 250 | + assert len(run.message) <= MAX_MESSAGE_LENGTH_FOR_EXTRACTION_PIPELINE_RUN, ( |
| 251 | + f"Short message length exceeds maximum allowed length: {MAX_MESSAGE_LENGTH_FOR_EXTRACTION_PIPELINE_RUN}" |
| 252 | + ) |
| 253 | + assert run.message == SHORT_MESSAGE, "Short message does not match expected value" |
| 254 | + |
| 255 | + def validate_long_message(run: ExtractionPipelineRun): |
| 256 | + print(f"Validating long message: {run.message}") |
| 257 | + assert len(run.message) <= MAX_MESSAGE_LENGTH_FOR_EXTRACTION_PIPELINE_RUN, ( |
| 258 | + f"Long message length exceeds maximum allowed length: {MAX_MESSAGE_LENGTH_FOR_EXTRACTION_PIPELINE_RUN}" |
| 259 | + ) |
| 260 | + assert run.message == expected_long_message, "Long message does not match expected value" |
| 261 | + |
| 262 | + extractor = Extractor( |
| 263 | + name="extractor_test_report_run", |
| 264 | + description="description", |
| 265 | + config_class=ConfigWithoutStates, |
| 266 | + ) |
| 267 | + extractor._initial_load_config("tests/tests_unit/dummyconfig.yaml") |
| 268 | + extractor.cognite_client = get_client_mock() |
| 269 | + extractor.logger = logging.getLogger("test_logger") |
| 270 | + |
| 271 | + extractor.extraction_pipeline = ExtractionPipeline(external_id=EXTRACTION_PIPELINE, name=EXTRACTION_PIPELINE) |
| 272 | + extractor.cognite_client.extraction_pipelines.runs.create = Mock(side_effect=validate_short_message) |
| 273 | + extractor._report_run(status="success", message=SHORT_MESSAGE) |
| 274 | + |
| 275 | + extractor.cognite_client.extraction_pipelines.runs.create = Mock(side_effect=validate_long_message) |
| 276 | + extractor._report_run(status="success", message=LONG_MESSAGE) |
| 277 | + |
| 278 | + extractor.extraction_pipeline = None |
| 279 | + # assert False |
0 commit comments