-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate functions and correct tests
- Loading branch information
1 parent
24594c2
commit adddaa4
Showing
3 changed files
with
71 additions
and
77 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
56 changes: 56 additions & 0 deletions
56
digital_land/expectations/expectation_functions/resource_validations.py
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,56 @@ | ||
import csv | ||
|
||
|
||
def check_for_duplicate_references(csv_path): | ||
duplicates = {} | ||
issues = [] | ||
|
||
with csv_path.open(newline="") as csvfile: | ||
reader = csv.DictReader(csvfile) | ||
for row_number, row in enumerate(reader, start=1): | ||
ref = row.get("reference") | ||
if ref in duplicates: | ||
duplicates[ref].append(row_number) | ||
else: | ||
duplicates[ref] = [row_number] | ||
|
||
for ref, rows in duplicates.items(): | ||
if len(rows) > 1: | ||
issues.append( | ||
{ | ||
"scope": "row-group", | ||
"message": f"Duplicate reference '{ref}' found on rows: {', '.join(map(str, rows))}", | ||
"dataset": "dataset", | ||
"table_name": "resource", | ||
"rows": rows, | ||
"row_id": str(rows[0]), | ||
"organisation": "organisation", | ||
} | ||
) | ||
|
||
return issues | ||
|
||
|
||
def validate_references(csv_path): | ||
issues = [] | ||
|
||
with csv_path.open(newline="") as csvfile: | ||
reader = csv.DictReader(csvfile) | ||
for row_number, row in enumerate(reader, start=1): | ||
ref = row.get("reference") | ||
|
||
if not ref: # This will be True for both None and empty strings | ||
issues.append( | ||
{ | ||
"scope": "value", | ||
"message": f"Reference is missing on row {row_number}.", | ||
"dataset": "dataset", | ||
"table_name": "resource", | ||
"field_name": "reference", | ||
"row_id": str(row_number), | ||
"value": "Missing", | ||
"organisation": "organisation", | ||
} | ||
) | ||
|
||
return issues |
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