88from collections import Counter , defaultdict
99from functools import reduce
1010from logging import Logger
11- from typing import Any , Dict , List , Mapping , MutableMapping , Set
11+ from typing import Any , Dict , List , Mapping , MutableMapping , Optional , Set
12+ from xmlrpc .client import Boolean
1213
1314import dpath .util
1415import jsonschema
2728from docker .errors import ContainerError
2829from jsonschema ._utils import flatten
2930from source_acceptance_test .base import BaseTest
30- from source_acceptance_test .config import BasicReadTestConfig , ConnectionTestConfig , DiscoveryTestConfig , SpecTestConfig
31+ from source_acceptance_test .config import (
32+ BasicReadTestConfig ,
33+ Config ,
34+ ConnectionTestConfig ,
35+ DiscoveryTestConfig ,
36+ EmptyStreamConfiguration ,
37+ ExpectedRecordsConfig ,
38+ SpecTestConfig ,
39+ )
3140from source_acceptance_test .utils import ConnectorRunner , SecretDict , filter_output , make_hashable , verify_records_schema
3241from source_acceptance_test .utils .backward_compatibility import CatalogDiffChecker , SpecDiffChecker , validate_previous_configs
33- from source_acceptance_test .utils .common import find_all_values_for_key_in_schema , find_keyword_schema
42+ from source_acceptance_test .utils .common import (
43+ build_configured_catalog_from_custom_catalog ,
44+ build_configured_catalog_from_discovered_catalog_and_empty_streams ,
45+ find_all_values_for_key_in_schema ,
46+ find_keyword_schema ,
47+ )
3448from source_acceptance_test .utils .json_schema_helper import JsonSchemaHelper , get_expected_schema_structure , get_object_structure
3549
3650
@@ -462,11 +476,58 @@ def _validate_expected_records(
462476 detailed_logger = detailed_logger ,
463477 )
464478
479+ @pytest .fixture (name = "should_validate_schema" )
480+ def should_validate_schema_fixture (self , inputs : BasicReadTestConfig , test_strictness_level : Config .TestStrictnessLevel ):
481+ if not inputs .validate_schema and test_strictness_level is Config .TestStrictnessLevel .high :
482+ pytest .fail ("High strictness level error: validate_schema must be set to true in the basic read test configuration." )
483+ else :
484+ return inputs .validate_schema
485+
486+ @pytest .fixture (name = "should_validate_data_points" )
487+ def should_validate_data_points_fixture (self , inputs : BasicReadTestConfig ) -> Boolean :
488+ # TODO: we might want to enforce this when Config.TestStrictnessLevel.high
489+ return inputs .validate_data_points
490+
491+ @pytest .fixture (name = "configured_catalog" )
492+ def configured_catalog_fixture (
493+ self ,
494+ test_strictness_level : Config .TestStrictnessLevel ,
495+ configured_catalog_path : Optional [str ],
496+ discovered_catalog : MutableMapping [str , AirbyteStream ],
497+ empty_streams : Set [EmptyStreamConfiguration ],
498+ ) -> ConfiguredAirbyteCatalog :
499+ """Build a configured catalog for basic read only.
500+ We discard the use of custom configured catalog if:
501+ - No custom configured catalog is declared with configured_catalog_path.
502+ - We are in high test strictness level.
503+ When a custom configured catalog is discarded we use the discovered catalog from which we remove the declared empty streams.
504+ We use a custom configured catalog if a configured_catalog_path is declared and we are not in high test strictness level.
505+ Args:
506+ test_strictness_level (Config.TestStrictnessLevel): The current test strictness level according to the global test configuration.
507+ configured_catalog_path (Optional[str]): Path to a JSON file containing a custom configured catalog.
508+ discovered_catalog (MutableMapping[str, AirbyteStream]): The discovered catalog.
509+ empty_streams (Set[EmptyStreamConfiguration]): The empty streams declared in the test configuration.
510+
511+ Returns:
512+ ConfiguredAirbyteCatalog: the configured Airbyte catalog.
513+ """
514+ if test_strictness_level is Config .TestStrictnessLevel .high or not configured_catalog_path :
515+ if configured_catalog_path :
516+ pytest .fail (
517+ "High strictness level error: you can't set a custom configured catalog on the basic read test when strictness level is high."
518+ )
519+ return build_configured_catalog_from_discovered_catalog_and_empty_streams (discovered_catalog , empty_streams )
520+ else :
521+ return build_configured_catalog_from_custom_catalog (configured_catalog_path , discovered_catalog )
522+
465523 def test_read (
466524 self ,
467525 connector_config ,
468526 configured_catalog ,
469- inputs : BasicReadTestConfig ,
527+ expect_records_config : ExpectedRecordsConfig ,
528+ should_validate_schema : Boolean ,
529+ should_validate_data_points : Boolean ,
530+ empty_streams : Set [EmptyStreamConfiguration ],
470531 expected_records_by_stream : MutableMapping [str , List [MutableMapping ]],
471532 docker_runner : ConnectorRunner ,
472533 detailed_logger ,
@@ -476,25 +537,25 @@ def test_read(
476537
477538 assert records , "At least one record should be read using provided catalog"
478539
479- if inputs . validate_schema :
540+ if should_validate_schema :
480541 self ._validate_schema (records = records , configured_catalog = configured_catalog )
481542
482- self ._validate_empty_streams (records = records , configured_catalog = configured_catalog , allowed_empty_streams = inputs . empty_streams )
543+ self ._validate_empty_streams (records = records , configured_catalog = configured_catalog , allowed_empty_streams = empty_streams )
483544 for pks , record in primary_keys_for_records (streams = configured_catalog .streams , records = records ):
484545 for pk_path , pk_value in pks .items ():
485546 assert (
486547 pk_value is not None
487548 ), f"Primary key subkeys { repr (pk_path )} have null values or not present in { record .stream } stream records."
488549
489550 # TODO: remove this condition after https://github.com/airbytehq/airbyte/issues/8312 is done
490- if inputs . validate_data_points :
551+ if should_validate_data_points :
491552 self ._validate_field_appears_at_least_once (records = records , configured_catalog = configured_catalog )
492553
493554 if expected_records_by_stream :
494555 self ._validate_expected_records (
495556 records = records ,
496557 expected_records_by_stream = expected_records_by_stream ,
497- flags = inputs . expect_records ,
558+ flags = expect_records_config ,
498559 detailed_logger = detailed_logger ,
499560 )
500561
0 commit comments