Skip to content

Commit 0809c4a

Browse files
authored
Merge pull request #15 from TaskarCenterAtUW/develop
Develop to Main for version 0.2.2
2 parents cf610ff + 8c5c8be commit 0809c4a

File tree

6 files changed

+67
-10
lines changed

6 files changed

+67
-10
lines changed

.github/workflows/unit_tests.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
name: Tests
3+
on:
4+
pull_request:
5+
branches: [develop]
6+
7+
jobs:
8+
Tests:
9+
name: Unit tests
10+
# Set the agent to run on
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v3
15+
16+
- name: Set up Python
17+
uses: actions/setup-python@v4
18+
with:
19+
python-version: "3.10"
20+
21+
- name: Install dependencies
22+
run: |
23+
pip install -r requirements.txt
24+
25+
- name: Run unit tests
26+
run: |
27+
python -m unittest discover -v tests/unit_tests

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Change log
22

3+
### 0.2.2
4+
- Added functionality to get the specific number of errors
5+
```
6+
validator = OSWValidation(zipfile_path=<ZIP_FILE_PATH>)
7+
result = validator.validate() // will return only first 20 errors by default
8+
result = validator.validate(max_errors=10) // will return only first 10 errors
9+
```
10+
311
### 0.2.1
412
- Updated zipfile_handler
513
- Fixed "No .geojson files found in the specified directory or its subdirectories." issue

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ This package validates the OSW geojson file. Package requires a OSW zip file pat
2828
from python_osw_validation import OSWValidation
2929

3030
validator = OSWValidation(zipfile_path='<Zip file path>')
31-
result = validator.validate()
31+
result = validator.validate()
3232
print(result.is_valid)
33-
print(result.errors)
33+
print(result.errors) # will return first 20 errors by default if there are errors
34+
35+
result = validator.validate(max_errors=10)
36+
print(result.is_valid)
37+
print(result.errors) # will return first 10 errors depending on the max_errors parameter
3438

3539
```
3640

src/python_osw_validation/__init__.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def are_ids_unique(self, gdf):
4545

4646
return is_valid, list(duplicates)
4747

48-
def validate(self) -> ValidationResult:
48+
def validate(self, max_errors=20) -> ValidationResult:
4949
try:
5050
# Extract the zipfile
5151
zip_handler = ZipFileHandler(self.zipfile_path)
@@ -62,7 +62,8 @@ def validate(self) -> ValidationResult:
6262
return ValidationResult(False, self.errors)
6363
for file in validator.files:
6464
file_path = os.path.join(file)
65-
is_valid = self.validate_osw_errors(file_path)
65+
if not self.validate_osw_errors(file_path, max_errors):
66+
break
6667

6768
if self.errors:
6869
zip_handler.remove_extracted_files()
@@ -119,7 +120,7 @@ def validate(self) -> ValidationResult:
119120

120121
# Geometry validation: check geometry type in each file and test if coordinates make a shape that is reasonable geometric shape according to the Simple Feature Access standard
121122
for osw_file in OSW_dataset:
122-
invalid_geojson = OSW_dataset[osw_file][(OSW_dataset[osw_file].geometry.type != OSW_dataset_files[osw_file]['geometry']) | (OSW_dataset[osw_file].is_valid==False)]
123+
invalid_geojson = OSW_dataset[osw_file][(OSW_dataset[osw_file].geometry.type != OSW_dataset_files[osw_file]['geometry']) | (OSW_dataset[osw_file].is_valid == False)]
123124
is_valid = len(invalid_geojson) == 0
124125
if not is_valid:
125126
self.errors.append(f"Invalid {osw_file} geometries found, id's of invalid geometries: {set(invalid_geojson['_id'])}")
@@ -128,7 +129,7 @@ def validate(self) -> ValidationResult:
128129
for file in validator.externalExtensions:
129130
file_path = os.path.join(file)
130131
extensionFile = gpd.read_file(file_path)
131-
invalid_geojson = extensionFile[extensionFile.is_valid==False]
132+
invalid_geojson = extensionFile[extensionFile.is_valid == False]
132133
is_valid = len(invalid_geojson) == 0
133134
if not is_valid:
134135
self.errors.append(f"Invalid geometries found in extension file {file}, list of invalid geometries: {invalid_geojson.to_json()}")
@@ -147,14 +148,17 @@ def load_osw_file(self, graph_geojson_path: str) -> Dict[str, Any]:
147148
with open(graph_geojson_path, 'r') as file:
148149
return json.load(file)
149150

150-
def validate_osw_errors(self, file_path: str) -> bool:
151+
def validate_osw_errors(self, file_path: str, max_errors: int) -> bool:
151152
'''Validate OSW Data against the schema and process all errors'''
152153
geojson_data = self.load_osw_file(file_path)
153154
validator = jsonschema.Draft7Validator(self.load_osw_schema(self.schema_file_path))
154155
errors = list(validator.iter_errors(geojson_data))
155156

156157
if errors:
157-
for error in errors:
158-
self.errors.append(f'Validation error: {error.message}')
158+
for index, error in enumerate(errors):
159+
if index < max_errors:
160+
self.errors.append(f'Validation error: {error.message}')
161+
if len(self.errors) == max_errors:
162+
break
159163
return False
160164
return True
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.2.1'
1+
__version__ = '0.2.2'

tests/unit_tests/test_osw_validation.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ def test_invalid_zipfile_with_schema(self):
7676
self.assertFalse(result.is_valid)
7777
self.assertIsNotNone(result.errors)
7878

79+
def test_invalid_zipfile_default_error_count(self):
80+
validation = OSWValidation(zipfile_path=self.invalid_zipfile)
81+
result = validation.validate()
82+
self.assertFalse(result.is_valid)
83+
self.assertIsNotNone(result.errors)
84+
self.assertLessEqual(len(result.errors), 20)
85+
86+
def test_invalid_zipfile_should_specific_errors_counts(self):
87+
validation = OSWValidation(zipfile_path=self.invalid_zipfile)
88+
result = validation.validate(max_errors=10)
89+
self.assertFalse(result.is_valid)
90+
self.assertIsNotNone(result.errors)
91+
self.assertLessEqual(len(result.errors), 10)
92+
7993
def test_invalid_zipfile_with_invalid_schema(self):
8094
validation = OSWValidation(zipfile_path=self.invalid_zipfile,
8195
schema_file_path=self.invalid_schema_file_path)

0 commit comments

Comments
 (0)