Skip to content

Commit d7af6b8

Browse files
authored
Merge pull request #8 from TaskarCenterAtUW/develop
version 0.0.5
2 parents 22baeea + 3d6342d commit d7af6b8

File tree

7 files changed

+103
-59
lines changed

7 files changed

+103
-59
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,13 @@
88
- Updated README file
99

1010
### 0.0.3
11-
- Added schema file to package
11+
- Added schema file to package
12+
13+
### 0.0.4
14+
- Points are not required for a valid OSW dataset
15+
16+
### 0.0.5
17+
- Support for multi-level geojson file
18+
- Now handles the following two folder structures when unzipped abc.zip
19+
1. abc\{nodes, edges, points}.geojson
20+
2. {nodes, edges, points}.geojson

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ This package validates the OSW geojson file. Package requires a OSW zip file pat
1111
## What this package does?
1212

1313
- It unzip the provided zip files
14-
- Check for nodes, edges and points geojson files inside the unzipped folder
15-
- Validate each file against schema, schema can be found here
14+
- Check for the required nodes and edges geojson files inside the unzipped folder
15+
- Validate each file (nodes, edges and points) against schema, schema can be found here
1616
- Return true or false according to validation
1717
- you can check the error if it returned false.
1818

@@ -73,6 +73,9 @@ test_invalid_geometry_zipfile (test_osw_validation.TestOSWValidation) ... ok
7373
test_invalid_zipfile (test_osw_validation.TestOSWValidation) ... ok
7474
test_invalid_zipfile_with_invalid_schema (test_osw_validation.TestOSWValidation) ... ok
7575
test_invalid_zipfile_with_schema (test_osw_validation.TestOSWValidation) ... ok
76+
test_minimal_zipfile (test_osw_validation.TestOSWValidation) ... ok
77+
test_minimal_zipfile_with_invalid_schema (test_osw_validation.TestOSWValidation) ... ok
78+
test_minimal_zipfile_with_schema (test_osw_validation.TestOSWValidation) ... ok
7679
test_missing_files_inside_zipfile (test_osw_validation.TestOSWValidation) ... ok
7780
test_missing_files_inside_zipfile_with_invalid_schema (test_osw_validation.TestOSWValidation) ... ok
7881
test_missing_files_inside_zipfile_with_schema (test_osw_validation.TestOSWValidation) ... ok
@@ -93,7 +96,7 @@ test_extract_valid_zip (test_zipfile_handler.TestZipFileHandler) ... ok
9396
test_remove_extracted_files (test_zipfile_handler.TestZipFileHandler) ... ok
9497

9598
----------------------------------------------------------------------
96-
Ran 31 tests in 193.358s
99+
Ran 34 tests in 121.220s
97100

98101
OK
99102
```

freeze_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
build_date = date.today().strftime('%Y-%m-%d')
1313

14-
version = '0.0.3'
14+
version = '0.0.5'
1515

1616
with open(version_file_path, 'w+') as version_file:
1717
version_file.write("version = '{}'\n".format(version))

src/python_osw_validation/extracted_data_validator.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,39 @@ def __init__(self, extracted_dir: str):
99
self.error = None
1010

1111
def is_valid(self) -> bool:
12-
if not os.path.exists(self.extracted_dir) or not os.listdir(self.extracted_dir):
13-
self.error = 'Folder is empty or does not exist.'
12+
# Check if the directory exists
13+
if not os.path.exists(self.extracted_dir):
14+
self.error = 'Directory does not exist.'
1415
return False
1516

17+
# Look for required files at the root level
1618
geojson_files = glob.glob(os.path.join(self.extracted_dir, '*.geojson'))
17-
if len(geojson_files) < 3:
18-
self.error = 'There are not enough .geojson files in the folder.'
19+
20+
# If not found at the root, check inside folders
21+
if not geojson_files:
22+
geojson_files = glob.glob(os.path.join(self.extracted_dir, '*', '*.geojson'))
23+
24+
if not geojson_files:
25+
self.error = 'No .geojson files found in the specified directory or its subdirectories.'
1926
return False
2027

28+
required_files = {'nodes', 'edges'}
29+
optional_files = {'points'}
2130
for filename in geojson_files:
2231
base_name = os.path.basename(filename)
23-
if 'edges' in base_name and base_name.endswith('.geojson'):
24-
self.files.append(filename)
25-
elif 'nodes' in base_name and base_name.endswith('.geojson'):
26-
self.files.append(filename)
27-
elif 'points' in base_name and base_name.endswith('.geojson'):
28-
self.files.append(filename)
29-
30-
if len(self.files) != 3:
31-
self.error = 'Missing one or more required .geojson files.'
32+
for required_file in required_files:
33+
if required_file in base_name and base_name.endswith('.geojson'):
34+
self.files.append(filename)
35+
required_files.remove(required_file)
36+
break
37+
for optional_file in optional_files:
38+
if optional_file in base_name and base_name.endswith('.geojson'):
39+
self.files.append(filename)
40+
optional_files.remove(optional_file)
41+
break
42+
43+
if required_files:
44+
self.error = f'Missing required .geojson files: {", ".join(required_files)}.'
3245
return False
3346

3447
return True

tests/assets/minimal.zip

926 Bytes
Binary file not shown.

tests/unit_tests/test_extracted_data_validator.py

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,62 +6,63 @@
66

77

88
class TestExtractedDataValidator(unittest.TestCase):
9-
109
def setUp(self):
11-
self.valid_dir = tempfile.mkdtemp()
12-
self.invalid_empty_dir = tempfile.mkdtemp()
13-
self.invalid_missing_files_dir = tempfile.mkdtemp()
14-
self.invalid_missing_required_files_dir = tempfile.mkdtemp()
10+
# Create a temporary directory for testing
11+
self.test_dir = tempfile.mkdtemp()
1512

1613
def tearDown(self):
17-
shutil.rmtree(self.valid_dir)
18-
shutil.rmtree(self.invalid_empty_dir)
19-
shutil.rmtree(self.invalid_missing_files_dir)
20-
shutil.rmtree(self.invalid_missing_required_files_dir)
14+
# Remove the temporary directory and its contents
15+
shutil.rmtree(self.test_dir)
2116

22-
def create_valid_directory_structure(self):
23-
# Create a valid directory structure with 3 geojson files
24-
valid_files = ['edges.geojson', 'nodes.geojson', 'points.geojson']
25-
for file in valid_files:
26-
open(os.path.join(self.valid_dir, file), 'w').close()
17+
def create_files(self, files):
18+
for file in files:
19+
with open(os.path.join(self.test_dir, file), 'w') as f:
20+
f.write('Test content')
2721

28-
def create_invalid_empty_directory(self):
29-
# Create an empty directory
30-
pass
22+
def test_valid_data_at_root(self):
23+
# Test when required files are at the root level
24+
validator = ExtractedDataValidator(self.test_dir)
25+
self.create_files(['a.nodes.geojson', 'a.edges.geojson', 'a.points.geojson'])
26+
self.assertTrue(validator.is_valid())
3127

32-
def create_invalid_missing_files_directory(self):
33-
# Create a directory with only one geojson file
34-
open(os.path.join(self.invalid_missing_files_dir, 'edges.geojson'), 'w').close()
28+
def test_valid_data_inside_folder(self):
29+
# Test when required files are inside a folder
30+
validator = ExtractedDataValidator(self.test_dir)
31+
os.makedirs(os.path.join(self.test_dir, 'abc'))
32+
self.create_files(['abc/a.nodes.geojson', 'abc/a.edges.geojson', 'abc/a.points.geojson'])
33+
self.assertTrue(validator.is_valid())
3534

36-
def create_invalid_missing_required_files_directory(self):
37-
# Create a directory with three geojson files, but not all required ones
38-
invalid_files = ['edges.geojson', 'invalid.geojson', 'nodes.geojson']
39-
for file in invalid_files:
40-
open(os.path.join(self.invalid_missing_required_files_dir, file), 'w').close()
35+
def test_missing_required_files(self):
36+
# Test when one of the required files is missing
37+
validator = ExtractedDataValidator(self.test_dir)
38+
self.create_files(['a.nodes.geojson', 'a.points.geojson'])
39+
self.assertFalse(validator.is_valid())
40+
self.assertEqual(validator.error, 'Missing required .geojson files: edges.')
4141

42-
def test_valid_directory_structure(self):
43-
self.create_valid_directory_structure()
44-
validator = ExtractedDataValidator(self.valid_dir)
42+
def test_missing_optional_file(self):
43+
# Test when optional file is missing
44+
validator = ExtractedDataValidator(self.test_dir)
45+
self.create_files(['a.nodes.geojson', 'a.edges.geojson'])
4546
self.assertTrue(validator.is_valid())
46-
self.assertEqual(len(validator.files), 3)
4747

48-
def test_invalid_empty_directory(self):
49-
self.create_invalid_empty_directory()
50-
validator = ExtractedDataValidator(self.invalid_empty_dir)
48+
def test_no_geojson_files(self):
49+
# Test when no .geojson files are present
50+
validator = ExtractedDataValidator(self.test_dir)
51+
self.create_files(['some.txt', 'another.txt'])
5152
self.assertFalse(validator.is_valid())
52-
self.assertEqual(validator.error, 'Folder is empty or does not exist.')
53+
self.assertEqual(validator.error, 'No .geojson files found in the specified directory or its subdirectories.')
5354

54-
def test_invalid_missing_files_directory(self):
55-
self.create_invalid_missing_files_directory()
56-
validator = ExtractedDataValidator(self.invalid_missing_files_dir)
55+
def test_invalid_directory(self):
56+
# Test when the specified directory does not exist
57+
validator = ExtractedDataValidator('nonexistent_directory')
5758
self.assertFalse(validator.is_valid())
58-
self.assertEqual(validator.error, 'There are not enough .geojson files in the folder.')
59+
self.assertEqual(validator.error, 'Directory does not exist.')
5960

60-
def test_invalid_missing_required_files_directory(self):
61-
self.create_invalid_missing_required_files_directory()
62-
validator = ExtractedDataValidator(self.invalid_missing_required_files_dir)
61+
def test_empty_directory(self):
62+
# Test when the specified directory is empty
63+
validator = ExtractedDataValidator(self.test_dir)
6364
self.assertFalse(validator.is_valid())
64-
self.assertEqual(validator.error, 'Missing one or more required .geojson files.')
65+
self.assertEqual(validator.error, 'No .geojson files found in the specified directory or its subdirectories.')
6566

6667

6768
if __name__ == '__main__':

tests/unit_tests/test_osw_validation.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class TestOSWValidation(unittest.TestCase):
1414

1515
def setUp(self):
1616
self.valid_zipfile = os.path.join(ASSETS_PATH, 'valid.zip')
17+
self.minimal_zipfile = os.path.join(ASSETS_PATH, 'minimal.zip')
1718
self.invalid_zipfile = os.path.join(ASSETS_PATH, 'invalid.zip')
1819
self.nodes_invalid_zipfile = os.path.join(ASSETS_PATH, 'nodes_invalid.zip')
1920
self.edges_invalid_zipfile = os.path.join(ASSETS_PATH, 'edges_invalid.zip')
@@ -46,6 +47,23 @@ def test_valid_zipfile_with_invalid_schema(self):
4647
result = validation.validate()
4748
self.assertTrue(len(result.errors) > 0)
4849

50+
def test_minimal_zipfile(self):
51+
validation = OSWValidation(zipfile_path=self.minimal_zipfile)
52+
result = validation.validate()
53+
self.assertTrue(result.is_valid)
54+
self.assertIsNone(result.errors)
55+
56+
def test_minimal_zipfile_with_schema(self):
57+
validation = OSWValidation(zipfile_path=self.minimal_zipfile, schema_file_path=self.schema_file_path)
58+
result = validation.validate()
59+
self.assertTrue(result.is_valid)
60+
self.assertIsNone(result.errors)
61+
62+
def test_minimal_zipfile_with_invalid_schema(self):
63+
validation = OSWValidation(zipfile_path=self.minimal_zipfile, schema_file_path=self.invalid_schema_file_path)
64+
result = validation.validate()
65+
self.assertTrue(len(result.errors) > 0)
66+
4967
def test_invalid_zipfile(self):
5068
validation = OSWValidation(zipfile_path=self.invalid_zipfile)
5169
result = validation.validate()

0 commit comments

Comments
 (0)