test_dish_xls_exporter.py 1.85 KB
Newer Older
1
2
import os

3
from pathlib import Path
4
from unittest import TestCase
5

6
7
from metadata_tools.importxls.export_utils import save_exported_datasets_to_file
from metadata_tools.importxls.dish_xls_exporter import DishXlsExporter
8

9
10
11
12
from jsonschema import validate
from jsonschema.exceptions import SchemaError, ValidationError
from json import loads

13

14
15
class TestDishXlsPartnerExtractor(TestCase):
    def test_dish_export(self):
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

        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

39
40
        exporter = DishXlsExporter()

41
42
        for dirName, _, fileList in os.walk(os.path.join(os.path.dirname(__file__), 'resources')):
            for fname in fileList:
43
                if fname.lower().endswith('xls') or fname.lower().endswith('xlsx'):
44
45
46
47
48
49
                    # Test export from Excel to JSON
                    dataset_dict = process_excel_file(fname)

                    # Validate resulting JSON file
                    validate_dataset_dict(dataset_dict)

50
        return