|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Visit https://aboutcode.org and https://github.com/aboutcode-org/univers for support and download. |
| 6 | + |
| 7 | +from typing import NamedTuple |
| 8 | +from typing import Union |
| 9 | + |
| 10 | + |
| 11 | +class SchemaDrivenVersTest(NamedTuple): |
| 12 | + test_type: str |
| 13 | + test_group: str |
| 14 | + input: dict |
| 15 | + expected_output: Union[list, bool] |
| 16 | + |
| 17 | + @classmethod |
| 18 | + def from_data(cls, data: dict): |
| 19 | + return cls(**data) |
| 20 | + |
| 21 | + def assert_result(self): |
| 22 | + assert self.result == self.expected_output |
| 23 | + |
| 24 | + @property |
| 25 | + def result(self): |
| 26 | + if self.test_type == "equality": |
| 27 | + return self.equality() |
| 28 | + elif self.test_type == "comparison": |
| 29 | + return self.comparison() |
| 30 | + elif self.test_type == "containment": |
| 31 | + return self.containment() |
| 32 | + elif self.test_type == "roundtrip": |
| 33 | + return self.roundtrip() |
| 34 | + elif self.test_type == "from_native": |
| 35 | + return self.from_native() |
| 36 | + |
| 37 | + raise Exception(f"Unknown test_type: {self.test_type}") |
| 38 | + |
| 39 | + def equality(self): |
| 40 | + raise NotImplementedError |
| 41 | + |
| 42 | + def comparison(self): |
| 43 | + raise NotImplementedError |
| 44 | + |
| 45 | + def containment(self): |
| 46 | + raise NotImplementedError |
| 47 | + |
| 48 | + def roundtrip(self): |
| 49 | + raise NotImplementedError |
| 50 | + |
| 51 | + def from_native(self): |
| 52 | + raise NotImplementedError |
| 53 | + |
| 54 | + @property |
| 55 | + def input_scheme(self): |
| 56 | + return self.input["input_scheme"] |
| 57 | + |
| 58 | + @property |
| 59 | + def input_native_range(self): |
| 60 | + return self.input["native_range"] |
| 61 | + |
| 62 | + @property |
| 63 | + def input_version1(self): |
| 64 | + return self.input["versions"][0] |
| 65 | + |
| 66 | + @property |
| 67 | + def input_version2(self): |
| 68 | + return self.input["versions"][1] |
| 69 | + |
| 70 | + |
| 71 | +def get_test_data_comp(scheme, versions, output): |
| 72 | + return { |
| 73 | + "test_group": "advanced", |
| 74 | + "test_type": "comparison", |
| 75 | + "input": { |
| 76 | + "input_scheme": scheme, |
| 77 | + "versions": versions, |
| 78 | + }, |
| 79 | + "expected_output": output, |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +def get_test_data_eq(scheme, versions, output): |
| 84 | + return { |
| 85 | + "test_group": "advanced", |
| 86 | + "test_type": "equality", |
| 87 | + "input": { |
| 88 | + "input_scheme": scheme, |
| 89 | + "versions": versions, |
| 90 | + }, |
| 91 | + "expected_output": output, |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | +def get_test_data_containment(vers, version, output): |
| 96 | + return { |
| 97 | + "test_group": "advanced", |
| 98 | + "test_type": "containment", |
| 99 | + "input": { |
| 100 | + "vers": vers, |
| 101 | + "version": version, |
| 102 | + }, |
| 103 | + "expected_output": output, |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | +def get_test_data_roundtrip(vers, output): |
| 108 | + return { |
| 109 | + "test_group": "advanced", |
| 110 | + "test_type": "roundtrip", |
| 111 | + "input": { |
| 112 | + "vers": vers, |
| 113 | + }, |
| 114 | + "expected_output": output, |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | +def get_test_data_from_native(native_range, schema, output): |
| 119 | + return { |
| 120 | + "test_group": "advanced", |
| 121 | + "test_type": "from_native", |
| 122 | + "input": { |
| 123 | + "native_range": native_range, |
| 124 | + "scheme": schema, |
| 125 | + }, |
| 126 | + "expected_output": output, |
| 127 | + } |
0 commit comments