Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: address_resolver did not use new datasource name #697

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/common/providers/address_resolver/address_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ def _resolve_path_items(
data_source: DataSource,
path_items: list[AttributeItem | QueryItem | IdItem],
get_data_source: Callable,
) -> tuple[list | dict, list[str]]:
) -> tuple[list | dict, list[str], str]:
if len(path_items) == 0 or isinstance(path_items[0], AttributeItem):
raise NotFoundException(f"Invalid path_items {path_items}.")
entity, id = path_items[0].get_entry_point(data_source)
path = [id]
data_source_name = data_source.name
for index, ref_item in enumerate(path_items[1:]):
if isinstance(ref_item, IdItem):
raise NotFoundException(f"Invalid path_items {path_items}.")
Expand All @@ -33,11 +34,12 @@ def _resolve_path_items(
address = Address.from_relative(entity["address"], path[0], data_source.name)
resolved_reference = resolve_address(address, get_data_source)
path = [resolved_reference.document_id, *resolved_reference.attribute_path]
data_source_name = resolved_reference.data_source_id
else:
path.append(attribute)
if not isinstance(entity, list | dict):
raise NotFoundException(f"Path {path} leads to a primitive value.")
return entity, path
return entity, path, data_source_name


@dataclass(frozen=True)
Expand All @@ -61,11 +63,11 @@ def resolve_address(address: Address, get_data_source: Callable) -> ResolvedAddr

# The first reference item should always be a DataSourceItem
data_source = get_data_source(address.data_source)
document, path = _resolve_path_items(data_source, path_items, get_data_source)
document, path, data_source = _resolve_path_items(data_source, path_items, get_data_source)

return ResolvedAddress(
entity=document,
data_source_id=address.data_source,
data_source_id=data_source,
document_id=str(path[0]),
attribute_path=path[1:],
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def setUp(self):
"name": "Volvo 240",
"plateNumber": "123",
"engine": {
"address": "$engine_id",
"address": "dmss://datasource2/$engine_id",
"type": SIMOS.REFERENCE.value,
"referenceType": REFERENCE_TYPES.LINK.value,
},
Expand Down Expand Up @@ -62,7 +62,7 @@ def setUp(self):
"name": "myEngine",
"description": "",
"fuelPump": {
"address": "$fuel_pump_id",
"address": "dmss://datasource2/$fuel_pump_id",
"type": SIMOS.REFERENCE.value,
"referenceType": REFERENCE_TYPES.LINK.value,
},
Expand All @@ -78,20 +78,31 @@ def setUp(self):
self.document_repository.name = "datasource"
self.document_repository.get = self.mock_get
self.document_repository.find = self.mock_find

self.document_repository2 = mock.Mock()
self.document_repository2.name = "datasource2"
self.document_repository2.get = self.mock_get2
# self.document_repository2.find = self.mock_find2

self.document_service = get_mock_document_service(
repository_provider=lambda x, y: self.document_repository,
repository_provider=lambda x, y: next(
dr for dr in (self.document_repository, self.document_repository2) if dr.name == x
),
blueprint_provider=None,
)

def mock_get(self, document_id: str):
if document_id == "car_rental_company_id":
return {**self.car_rental_company}
if document_id == "customer_id":
return {**self.customer}
return None

def mock_get2(self, document_id: str):
if document_id == "engine_id":
return {**self.engine}
if document_id == "fuel_pump_id":
return {**self.fuel_pump}
if document_id == "customer_id":
return {**self.customer}
return None

def mock_find(self, query: dict) -> list[dict]:
Expand Down Expand Up @@ -140,7 +151,7 @@ def test_with_attributes_to_uncontained_child(self):
Address.from_absolute("datasource/$car_rental_company_id.cars[0].engine.fuelPump"),
self.document_service.get_data_source,
)
assert ref.data_source_id == "datasource"
assert ref.data_source_id == "datasource2"
assert ref.document_id == "engine_id"
assert ref.attribute_path == ["fuelPump"]
assert ref.entity == self.engine["fuelPump"]
Expand Down Expand Up @@ -231,3 +242,13 @@ def test_invalid_reference_to_primitive(self):
Address.from_absolute("datasource/$car_rental_company_id.cars[0].plateNumber"),
self.document_service.get_data_source,
)

def test_correct_nested_reference_resolved(self):
ref = resolve_address(
Address.from_absolute("datasource/$car_rental_company_id.cars[0].engine.fuelPump"),
self.document_service.get_data_source,
)
assert ref.data_source_id == "datasource2"
assert ref.document_id == "engine_id"
assert ref.attribute_path == ["fuelPump"]
assert ref.entity == self.engine["fuelPump"]
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def mock_update(entity: dict, *args, **kwargs):

document_repository = mock.Mock()
document_repository.get = mock_get
document_repository.name = "testing"
document_repository.update = mock_update

def repository_provider(data_source_id, user: User):
Expand Down