Jeremy Johnson | 77fc614 | 2023-09-04 17:04:21 +0100 | [diff] [blame^] | 1 | """Tests for schemavalidation.py.""" |
| 2 | # Copyright (c) 2023, ARM Limited. |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | import pytest |
| 5 | import schemavalidation.schemavalidation as sch |
| 6 | from jsonschema.exceptions import ValidationError |
| 7 | |
| 8 | |
| 9 | def test_schemavalidation_full_fail(): |
| 10 | json = {} |
| 11 | |
| 12 | sv = sch.TestDescSchemaValidator() |
| 13 | with pytest.raises(ValidationError) as excinfo: |
| 14 | sv.validate_config(json) |
| 15 | info = str(excinfo.value).split("\n") |
| 16 | assert info[0] == "'tosa_file' is a required property" |
| 17 | |
| 18 | |
| 19 | def test_schemavalidation_compliance_fail(): |
| 20 | json = {"version": "v"} |
| 21 | |
| 22 | sv = sch.TestDescSchemaValidator() |
| 23 | with pytest.raises(ValidationError) as excinfo: |
| 24 | sv.validate_config(json, sch.TD_SCHEMA_COMPLIANCE) |
| 25 | info = str(excinfo.value).split("\n") |
| 26 | assert info[0] == "'tensors' is a required property" |
| 27 | |
| 28 | |
| 29 | def test_schemavalidation_data_gen_fail(): |
| 30 | json = {"version": "v", "tensors": {"input": {}}} |
| 31 | |
| 32 | sv = sch.TestDescSchemaValidator() |
| 33 | with pytest.raises(ValidationError) as excinfo: |
| 34 | sv.validate_config(json, sch.TD_SCHEMA_DATA_GEN) |
| 35 | info = str(excinfo.value).split("\n") |
| 36 | assert info[0] == "'generator' is a required property" |
| 37 | |
| 38 | |
| 39 | def test_schemavalidation_full_minimal(): |
| 40 | json = { |
| 41 | "tosa_file": "file", |
| 42 | "ifm_name": ["name1", "name2"], |
| 43 | "ifm_file": ["file1", "file2"], |
| 44 | "ofm_name": ["name1", "name2"], |
| 45 | "ofm_file": ["file1", "file2"], |
| 46 | } |
| 47 | |
| 48 | sv = sch.TestDescSchemaValidator() |
| 49 | sv.validate_config(json) |
| 50 | |
| 51 | |
| 52 | def test_schemavalidation_full_unexpected(): |
| 53 | json = { |
| 54 | "tosa_file": "file", |
| 55 | "ifm_name": ["name1", "name2"], |
| 56 | "ifm_file": ["file1", "file2"], |
| 57 | "ofm_name": ["name1", "name2"], |
| 58 | "ofm_file": ["file1", "file2"], |
| 59 | "unexpected_property": 1, |
| 60 | } |
| 61 | |
| 62 | sv = sch.TestDescSchemaValidator() |
| 63 | with pytest.raises(ValidationError) as excinfo: |
| 64 | sv.validate_config(json) |
| 65 | info = str(excinfo.value).split("\n") |
| 66 | assert ( |
| 67 | info[0] |
| 68 | == "Additional properties are not allowed ('unexpected_property' was unexpected)" |
| 69 | ) |
| 70 | |
| 71 | |
| 72 | def test_schemavalidation_compliance_minimal(): |
| 73 | json = { |
| 74 | "version": "v", |
| 75 | "tensors": { |
| 76 | "output": { |
| 77 | "mode": "mode", |
| 78 | } |
| 79 | }, |
| 80 | } |
| 81 | |
| 82 | sv = sch.TestDescSchemaValidator() |
| 83 | sv.validate_config(json, sch.TD_SCHEMA_COMPLIANCE) |
| 84 | |
| 85 | |
| 86 | def test_schemavalidation_data_gen_minimal(): |
| 87 | json = { |
| 88 | "version": "v", |
| 89 | "tensors": { |
| 90 | "input": { |
| 91 | "generator": "generator", |
| 92 | "data_type": "type", |
| 93 | "input_type": "constant", |
| 94 | "shape": [], |
| 95 | "op": "name", |
| 96 | "input_pos": 0, |
| 97 | } |
| 98 | }, |
| 99 | } |
| 100 | |
| 101 | sv = sch.TestDescSchemaValidator() |
| 102 | sv.validate_config(json, sch.TD_SCHEMA_DATA_GEN) |