|
1 | 1 | import json |
2 | | -from typing import Literal |
| 2 | +from typing import Generator, Literal |
3 | 3 | from unittest.mock import Mock |
4 | 4 |
|
5 | 5 | import pandas as pd |
6 | 6 | import pytest |
7 | 7 | from great_expectations import Checkpoint, ExpectationSuite, ValidationDefinition |
8 | | -from great_expectations.data_context import AbstractDataContext |
| 8 | +from great_expectations.data_context import AbstractDataContext, FileDataContext |
9 | 9 | from great_expectations.expectations import ExpectColumnValuesToBeInSet |
10 | 10 | from pytest_mock import MockerFixture |
11 | 11 |
|
@@ -191,3 +191,118 @@ def test_batch_parameters(self) -> None: |
191 | 191 |
|
192 | 192 | # assert |
193 | 193 | mock_checkpoint.run.assert_called_once_with(batch_parameters=batch_parameters) |
| 194 | + |
| 195 | + def test_configure_file_data_context_with_without_generator(self) -> None: |
| 196 | + """Expect that configure_file_data_context can just return a DataContext""" |
| 197 | + # arrange |
| 198 | + mock_context = Mock(spec=AbstractDataContext) |
| 199 | + setup = Mock() |
| 200 | + teardown = Mock() |
| 201 | + configure_checkpoint = Mock() |
| 202 | + |
| 203 | + def configure_file_data_context() -> FileDataContext: |
| 204 | + setup() |
| 205 | + return mock_context |
| 206 | + |
| 207 | + validate_checkpoint = GXValidateCheckpointOperator( |
| 208 | + task_id="validate_checkpoint_success", |
| 209 | + configure_checkpoint=configure_checkpoint, |
| 210 | + context_type="file", |
| 211 | + configure_file_data_context=configure_file_data_context, |
| 212 | + ) |
| 213 | + |
| 214 | + # act |
| 215 | + validate_checkpoint.execute(context={}) |
| 216 | + |
| 217 | + # assert |
| 218 | + setup.assert_called_once() |
| 219 | + configure_checkpoint.assert_called_once_with(mock_context) |
| 220 | + teardown.assert_not_called() |
| 221 | + |
| 222 | + def test_configure_file_data_context_with_generator(self) -> None: |
| 223 | + """Expect that configure_file_data_context can return a generator that yeidls a DataContext.""" |
| 224 | + # arrange |
| 225 | + mock_context = Mock(spec=AbstractDataContext) |
| 226 | + setup = Mock() |
| 227 | + teardown = Mock() |
| 228 | + configure_checkpoint = Mock() |
| 229 | + |
| 230 | + def configure_file_data_context() -> Generator[FileDataContext, None, None]: |
| 231 | + setup() |
| 232 | + yield mock_context |
| 233 | + teardown() |
| 234 | + |
| 235 | + validate_checkpoint = GXValidateCheckpointOperator( |
| 236 | + task_id="validate_checkpoint_success", |
| 237 | + configure_checkpoint=configure_checkpoint, |
| 238 | + context_type="file", |
| 239 | + configure_file_data_context=configure_file_data_context, |
| 240 | + ) |
| 241 | + |
| 242 | + # act |
| 243 | + validate_checkpoint.execute(context={}) |
| 244 | + |
| 245 | + # assert |
| 246 | + setup.assert_called_once() |
| 247 | + configure_checkpoint.assert_called_once_with(mock_context) |
| 248 | + teardown.assert_called_once() |
| 249 | + |
| 250 | + def test_configure_file_data_context_with_generator_no_yield(self) -> None: |
| 251 | + """Expect that configure_file_data_context errors if it does not yield a DataContext.""" |
| 252 | + # arrange |
| 253 | + mock_context = Mock(spec=AbstractDataContext) |
| 254 | + setup = Mock() |
| 255 | + teardown = Mock() |
| 256 | + configure_checkpoint = Mock() |
| 257 | + |
| 258 | + def configure_file_data_context() -> Generator[FileDataContext, None, None]: |
| 259 | + setup() |
| 260 | + if False: |
| 261 | + # Force this to be a generator for the test |
| 262 | + yield mock_context |
| 263 | + teardown() |
| 264 | + |
| 265 | + validate_checkpoint = GXValidateCheckpointOperator( |
| 266 | + task_id="validate_checkpoint_success", |
| 267 | + configure_checkpoint=configure_checkpoint, |
| 268 | + context_type="file", |
| 269 | + configure_file_data_context=configure_file_data_context, |
| 270 | + ) |
| 271 | + |
| 272 | + # act |
| 273 | + with pytest.raises(RuntimeError, match="did not yield"): |
| 274 | + validate_checkpoint.execute(context={}) |
| 275 | + |
| 276 | + # assert |
| 277 | + setup.assert_called_once() |
| 278 | + configure_checkpoint.assert_not_called() |
| 279 | + teardown.assert_called_once() |
| 280 | + |
| 281 | + def test_configure_file_data_context_with_generator_multiple_yields(self) -> None: |
| 282 | + """Expect that configure_file_data_context errors if it yields multiple times.""" |
| 283 | + # arrange |
| 284 | + mock_context = Mock(spec=AbstractDataContext) |
| 285 | + setup = Mock() |
| 286 | + teardown = Mock() |
| 287 | + configure_checkpoint = Mock() |
| 288 | + |
| 289 | + def configure_file_data_context() -> Generator[FileDataContext, None, None]: |
| 290 | + setup() |
| 291 | + yield mock_context |
| 292 | + yield mock_context |
| 293 | + teardown() |
| 294 | + |
| 295 | + validate_checkpoint = GXValidateCheckpointOperator( |
| 296 | + task_id="validate_checkpoint_success", |
| 297 | + configure_checkpoint=configure_checkpoint, |
| 298 | + context_type="file", |
| 299 | + configure_file_data_context=configure_file_data_context, |
| 300 | + ) |
| 301 | + |
| 302 | + # act |
| 303 | + with pytest.raises(RuntimeError, match="yielded more than once"): |
| 304 | + validate_checkpoint.execute(context={}) |
| 305 | + |
| 306 | + # assert |
| 307 | + setup.assert_called_once() |
| 308 | + teardown.assert_not_called() |
0 commit comments