Skip to content

Commit

Permalink
Merge pull request #50 from christoph-blessing/fix_issue49
Browse files Browse the repository at this point in the history
Fix tier prefix not being ignored when matching parts
  • Loading branch information
christoph-blessing authored Oct 9, 2023
2 parents 758a26c + 14c60cd commit 3164276
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
18 changes: 12 additions & 6 deletions link/infrastructure/facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,19 @@ def add_to_local(self, primary_keys: Iterable[PrimaryKey]) -> None:
def is_part_table(parent: Table, child: Table) -> bool:
return child.table_name.startswith(parent.table_name + "__")

def remove_parent_prefix_from_part_name(parent: Table, part: Table) -> str:
assert is_part_table(parent, part)
return part.table_name[len(parent.table_name) :]

def get_parts(parent: Table) -> dict[str, Table]:
parts = (child for child in parent.children(as_objects=True) if is_part_table(parent, child))
return {remove_parent_prefix_from_part_name(parent, part): part for part in parts}

def add_parts_to_local(download_path: str) -> None:
local_children = {child.table_name: child for child in self.local().children(as_objects=True)}
for source_child in self.source().children(as_objects=True):
if not is_part_table(self.source(), source_child):
continue
local_children[source_child.table_name].insert(
(source_child & primary_keys).fetch(as_dict=True, download_path=download_path)
local_parts = get_parts(self.local())
for source_name, source_part in get_parts(self.source()).items():
local_parts[source_name].insert(
(source_part & primary_keys).fetch(as_dict=True, download_path=download_path)
)

primary_keys = list(primary_keys)
Expand Down
21 changes: 21 additions & 0 deletions tests/functional/test_pulling.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,24 @@ def create_random_table_name():
assert len(actual) == len(part_table_expected)
assert all(entry in part_table_expected for entry in actual)
assert local_table_cls().source.proj().fetch(as_dict=True) == [{"foo": 3}]


def test_tier_prefix_is_ignored_when_matching_parts(prepare_link, act_as, create_table, prepare_table):
schema_names, actors = prepare_link()
source_table_name = "Foo"
data = [{"foo": 1}]
data_parts = {"Bar": [{"foo": 1}]}
with act_as(actors["source"]):
source_table_cls = create_table(
source_table_name, dj.Computed, "foo: int", parts=[create_table("Bar", dj.Part, "-> master")]
)
prepare_table(schema_names["source"], source_table_cls, data=data, parts=data_parts)
with act_as(actors["local"]):
local_table_cls = link(
actors["source"].credentials.host,
schema_names["source"],
schema_names["outbound"],
"Outbound",
schema_names["local"],
)(type(source_table_name, tuple(), {}))
local_table_cls().source.pull()

0 comments on commit 3164276

Please sign in to comment.