Commit 062c8ae4 authored by Vilem Ded's avatar Vilem Ded
Browse files

use several xlsx file for testing and validate result of parser

parent b11266af
import json
import os
from pathlib import Path
......@@ -7,20 +6,45 @@ from unittest import TestCase
from metadata_tools.importxls.export_utils import save_exported_datasets_to_file
from metadata_tools.importxls.dish_xls_exporter import DishXlsExporter
from jsonschema import validate
from jsonschema.exceptions import SchemaError, ValidationError
from json import loads
class TestDishXlsPartnerExtractor(TestCase):
def test_dish_export(self):
def process_excel_file(fname):
try:
full_file_path = os.path.join(dirName, fname)
dataset_dict = exporter.export_submission(full_file_path)
with open(Path(full_file_path).stem + ".json", 'w') as outfile:
save_exported_datasets_to_file(dataset_dict, outfile)
except ValueError:
print("Could not import {}".format(full_file_path))
return dataset_dict
def validate_dataset_dict(dataset_dict):
schemaFile = open("metadata_tools/resources/json-schemas/schemas/elu-dataset.json", encoding='utf-8')
schema = loads(schemaFile.read())
try:
validate(dataset_dict, schema)
except ValidationError:
self.fail(f"Validation of '{fname}' failed using schema 'json-schemas/schemas/elu-dataset.json'")
finally:
schemaFile.close()
return
exporter = DishXlsExporter()
#full_file_path = os.path.join(os.path.dirname(__file__), 'resources', 'UL_Extended_Data_Information_Sheet-8.xlsx')
for dirName, subdirList, fileList in os.walk(os.path.join(os.path.dirname(__file__), 'resources')):
for fname in fileList:
for dirName, _, fileList in os.walk(os.path.join(os.path.dirname(__file__), 'resources')):
for fname in fileList:
if fname.lower().endswith('xls') or fname.lower().endswith('xlsx'):
try:
full_file_path = os.path.join(dirName, fname)
dataset_dict = exporter.export_submission(full_file_path)
with open(Path(full_file_path).stem + ".json", 'w') as outfile:
save_exported_datasets_to_file(dataset_dict, outfile)
except ValueError:
print("Could not import {}".format(full_file_path))
# Test export from Excel to JSON
dataset_dict = process_excel_file(fname)
# Validate resulting JSON file
validate_dataset_dict(dataset_dict)
return
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment