-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
247 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import pytest | ||
import dlt | ||
from typing import Any | ||
from tests.load.pipeline.utils import destinations_configs, DestinationTestConfiguration, assert_data_table_counts | ||
from tests.pipeline.utils import assert_load_info | ||
from dlt.pipeline.exceptions import PipelineStepFailed | ||
|
||
def data_with_subtables(offset: int) -> Any: | ||
for _, index in enumerate(range(offset, offset+100), 1): | ||
yield { | ||
"id": index, | ||
"name": f"item {index}", | ||
"sub_items": [{ | ||
"id": index + 1000, | ||
"name": f"sub item {index + 1000}" | ||
}] | ||
} | ||
|
||
@pytest.mark.parametrize("destination_config", destinations_configs(default_sql_configs=True), ids=lambda x: x.name) | ||
def test_switch_from_merge(destination_config: DestinationTestConfiguration): | ||
pipeline = destination_config.setup_pipeline(pipeline_name='test_switch_from_merge', full_refresh=True) | ||
|
||
info = (pipeline.run(data_with_subtables(10), table_name="items", write_disposition="merge")) | ||
assert_data_table_counts(pipeline, { | ||
"items": 100, | ||
"items__sub_items": 100 | ||
}) | ||
assert pipeline.default_schema._normalizers_config["json"]["config"]["propagation"]["tables"]["items"] == {'_dlt_id': '_dlt_root_id'} | ||
|
||
info = (pipeline.run(data_with_subtables(10), table_name="items", write_disposition="merge")) | ||
assert_load_info(info) | ||
assert_data_table_counts(pipeline, { | ||
"items": 100, | ||
"items__sub_items": 100 | ||
}) | ||
assert pipeline.default_schema._normalizers_config["json"]["config"]["propagation"]["tables"]["items"] == {'_dlt_id': '_dlt_root_id'} | ||
|
||
info = (pipeline.run(data_with_subtables(10), table_name="items", write_disposition="append")) | ||
assert_load_info(info) | ||
assert_data_table_counts(pipeline, { | ||
"items": 200, | ||
"items__sub_items": 200 | ||
}) | ||
assert pipeline.default_schema._normalizers_config["json"]["config"]["propagation"]["tables"]["items"] == {'_dlt_id': '_dlt_root_id'} | ||
|
||
info = (pipeline.run(data_with_subtables(10), table_name="items", write_disposition="replace")) | ||
assert_load_info(info) | ||
assert_data_table_counts(pipeline, { | ||
"items": 100, | ||
"items__sub_items": 100 | ||
}) | ||
assert pipeline.default_schema._normalizers_config["json"]["config"]["propagation"]["tables"]["items"] == {'_dlt_id': '_dlt_root_id'} | ||
|
||
|
||
@pytest.mark.parametrize("destination_config", destinations_configs(default_sql_configs=True), ids=lambda x: x.name) | ||
@pytest.mark.parametrize("with_root_key", [True, False]) | ||
def test_switch_to_merge(destination_config: DestinationTestConfiguration, with_root_key: bool): | ||
pipeline = destination_config.setup_pipeline(pipeline_name='test_switch_to_merge', full_refresh=True) | ||
|
||
@dlt.resource() | ||
def resource(): | ||
yield data_with_subtables(10) | ||
|
||
@dlt.source() | ||
def source(): | ||
return resource() | ||
|
||
s = source() | ||
s.root_key = with_root_key | ||
|
||
info = (pipeline.run(s, table_name="items", write_disposition="append")) | ||
assert_data_table_counts(pipeline, { | ||
"items": 100, | ||
"items__sub_items": 100 | ||
}) | ||
|
||
if with_root_key: | ||
assert pipeline.default_schema._normalizers_config["json"]["config"]["propagation"]["root"] == {'_dlt_id': '_dlt_root_id'} | ||
else: | ||
assert "propagation" not in pipeline.default_schema._normalizers_config["json"]["config"] | ||
|
||
# without a root key this will fail, it is expected | ||
if not with_root_key: | ||
with pytest.raises(PipelineStepFailed): | ||
pipeline.run(s, table_name="items", write_disposition="merge") | ||
return | ||
|
||
info = (pipeline.run(s, table_name="items", write_disposition="merge")) | ||
assert_load_info(info) | ||
assert_data_table_counts(pipeline, { | ||
"items": 100, | ||
"items__sub_items": 100 | ||
}) | ||
assert pipeline.default_schema._normalizers_config["json"]["config"]["propagation"]["tables"]["items"] == {'_dlt_id': '_dlt_root_id'} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
|
||
import dlt | ||
|
||
|
||
def test_schema_updates() -> None: | ||
p = dlt.pipeline(pipeline_name="test_schema_updates", full_refresh=True, destination="dummy") | ||
|
||
@dlt.source() | ||
def source(): | ||
@dlt.resource() | ||
def resource(): | ||
yield [1,2,3] | ||
return resource | ||
|
||
# test without normalizer attributes | ||
s = source() | ||
p.run(s, table_name="items", write_disposition="append") | ||
assert p.default_schema._normalizers_config["json"]["config"] == {} | ||
|
||
# add table propagation | ||
s = source() | ||
p.run(s, table_name="items", write_disposition="merge") | ||
assert p.default_schema._normalizers_config["json"]["config"] == { | ||
"propagation": { | ||
"tables": { | ||
"items": {'_dlt_id': '_dlt_root_id'} | ||
} | ||
} | ||
} | ||
|
||
# set root key | ||
s = source() | ||
s.root_key = True | ||
p.run(s, table_name="items", write_disposition="merge") | ||
assert p.default_schema._normalizers_config["json"]["config"] == { | ||
"propagation": { | ||
"tables": { | ||
"items": {'_dlt_id': '_dlt_root_id'} | ||
}, | ||
"root": {'_dlt_id': '_dlt_root_id'} | ||
} | ||
} | ||
|
||
# root key prevails even if not set | ||
s = source() | ||
s.root_key = False | ||
p.run(s, table_name="items", write_disposition="merge") | ||
assert p.default_schema._normalizers_config["json"]["config"] == { | ||
"propagation": { | ||
"tables": { | ||
"items": {'_dlt_id': '_dlt_root_id'} | ||
}, | ||
"root": {'_dlt_id': '_dlt_root_id'} | ||
} | ||
} | ||
|
||
# set max nesting | ||
s = source() | ||
s.max_table_nesting = 5 | ||
p.run(s, table_name="items", write_disposition="merge") | ||
assert p.default_schema._normalizers_config["json"]["config"] == { | ||
"propagation": { | ||
"tables": { | ||
"items": {'_dlt_id': '_dlt_root_id'} | ||
}, | ||
"root": {'_dlt_id': '_dlt_root_id'} | ||
}, | ||
"max_nesting": 5 | ||
} | ||
|
||
# update max nesting and new table | ||
s = source() | ||
s.max_table_nesting = 50 | ||
p.run(s, table_name="items2", write_disposition="merge") | ||
assert p.default_schema._normalizers_config["json"]["config"] == { | ||
"propagation": { | ||
"tables": { | ||
"items": {'_dlt_id': '_dlt_root_id'}, | ||
"items2": {'_dlt_id': '_dlt_root_id'}, | ||
}, | ||
"root": {'_dlt_id': '_dlt_root_id'} | ||
}, | ||
"max_nesting": 50 | ||
} |