Skip to content
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
27 changes: 25 additions & 2 deletions digital_land/phase/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,17 @@ def process(self, stream):

class FactLookupPhase(LookupPhase):
def __init__(
self, lookups={}, redirect_lookups={}, issue_log=None, odp_collections=[]
self,
lookups={},
redirect_lookups={},
issue_log=None,
odp_collections=None,
package_prefixes=None,
):
super().__init__(lookups, redirect_lookups, issue_log)
self.entity_field = "reference-entity"
self.odp_collections = odp_collections
self.package_prefixes = package_prefixes or {}

def process(self, stream):
for block in stream:
Expand Down Expand Up @@ -211,11 +217,28 @@ def process(self, stream):
# as it is organisation specific.
find_entity = self.check_associated_organisation(find_entity)

for package_prefix in self.package_prefixes.get(prefix, []):
if find_entity:
break

find_entity = self.lookup(
prefix=package_prefix,
organisation=organisation,
reference=reference,
)
if not find_entity:
find_entity = self.lookup(
prefix=package_prefix, reference=reference
)
find_entity = self.check_associated_organisation(find_entity)

if not find_entity or (
str(find_entity) in self.redirect_lookups
and int(self.redirect_lookups[str(find_entity)].get("status", 0)) == 410
):
if self.odp_collections and prefix in self.odp_collections:
if (self.odp_collections and prefix in self.odp_collections) or (
prefix in self.package_prefixes
):
self.issues.log_issue(
prefix,
"missing associated entity",
Expand Down
1 change: 1 addition & 0 deletions digital_land/pipeline/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ def transform(
redirect_lookups=redirect_lookups,
issue_log=self.issue_log,
odp_collections=self.specification.get_odp_collections(),
package_prefixes=self.specification.get_package_prefixes(),
),
FactPrunePhase(),
SavePhase(
Expand Down
10 changes: 10 additions & 0 deletions digital_land/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,16 @@ def get_field_typology_map(self):
def get_field_prefix_map(self):
return {key: self.field_prefix(key) for key in self.field.keys()}

def get_package_prefixes(self):
return {
"plan": [
"local-plan",
"minerals-plan",
"waste-plan",
"supplementary-plan",
],
}

def get_dataset_typology(self, dataset):
if dataset:
return self.dataset[dataset]["typology"]
Expand Down
66 changes: 66 additions & 0 deletions tests/unit/phase/test_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,69 @@ def test_missing_associated_entity_issue_no_organisation(

assert output[0]["row"]["reference-entity"] == "1"
assert len(issues.rows) == 0

def test_package_prefix_lookup_assigns_reference_entity(self):
input_stream = [
{
"organisation": "local-authorityabc",
"row": {
"fact": "abc",
"entity": "10",
"field": "plan",
"value": "local-plan-1",
"prefix": "plan",
"reference": "local-plan-1",
"entry-number": 1,
"line-number": 2,
},
}
]
lookups = {
",local-plan,local-plan-1,local-authorityabc": "1",
",plan-timetable,1,local-authorityabc": "10",
}
issues = IssueLog()

phase = FactLookupPhase(
lookups=lookups,
issue_log=issues,
package_prefixes={"plan": ["local-plan"]},
)
output = [block for block in phase.process(input_stream)]

assert output[0]["row"]["reference-entity"] == "1"
assert len(issues.rows) == 0

def test_package_prefix_lookup_raises_missing_associated_entity(self):
input_stream = [
{
"organisation": "local-authorityabc",
"row": {
"fact": "abc",
"entity": "10",
"field": "plan",
"value": "missing-plan",
"prefix": "plan",
"reference": "missing-plan",
"entry-number": 1,
"line-number": 2,
},
}
]
lookups = {
",local-plan,other-plan,local-authorityabc": "1",
",plan-timetable,1,local-authorityabc": "10",
}
issues = IssueLog()

phase = FactLookupPhase(
lookups=lookups,
issue_log=issues,
package_prefixes={"plan": ["local-plan"]},
)
output = [block for block in phase.process(input_stream)]

assert "reference-entity" not in output[0]["row"]
assert issues.rows[0]["field"] == "plan"
assert issues.rows[0]["issue-type"] == "missing associated entity"
assert issues.rows[0]["value"] == "missing-plan"
12 changes: 12 additions & 0 deletions tests/unit/test_specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,15 @@ def test_get_field_prefix_map_returns_correct_values(input, expected):
spec = Specification()
spec.field = input
assert expected == spec.get_field_prefix_map()


def test_get_package_prefixes_returns_plan_package_prefixes():
spec = Specification()
assert spec.get_package_prefixes() == {
"plan": [
"local-plan",
"minerals-plan",
"waste-plan",
"supplementary-plan",
],
}