|
| 1 | +# encoding: utf-8 |
| 2 | + |
| 3 | +# This file is part of CycloneDX Python Lib |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +# SPDX-License-Identifier: Apache-2.0 |
| 18 | +# Copyright (c) OWASP Foundation. All Rights Reserved. |
| 19 | + |
| 20 | + |
| 21 | +from itertools import product |
| 22 | +from typing import Tuple |
| 23 | +from unittest import TestCase |
| 24 | + |
| 25 | +from ddt import data, ddt, named_data, unpack |
| 26 | + |
| 27 | +from cyclonedx.schema import OutputFormat, SchemaVersion |
| 28 | +from cyclonedx.validation import get_instance as get_validator |
| 29 | + |
| 30 | +UndefinedFormatVersion = {(OutputFormat.JSON, SchemaVersion.V1_1), (OutputFormat.JSON, SchemaVersion.V1_0), } |
| 31 | + |
| 32 | + |
| 33 | +@ddt |
| 34 | +class TestGetInstance(TestCase): |
| 35 | + |
| 36 | + @named_data(*([f'{f.name} {v.name}', f, v] |
| 37 | + for f, v |
| 38 | + in product(OutputFormat, SchemaVersion) |
| 39 | + if (f, v) not in UndefinedFormatVersion)) |
| 40 | + @unpack |
| 41 | + def test_as_expected(self, of: OutputFormat, sv: SchemaVersion) -> None: |
| 42 | + validator = get_validator(of, sv) |
| 43 | + self.assertIs(validator.output_format, of) |
| 44 | + self.assertIs(validator.schema_version, sv) |
| 45 | + |
| 46 | + @data( |
| 47 | + *(('foo', sv, (TypeError, "unexpected output_format: 'foo'")) for sv in SchemaVersion), |
| 48 | + *((f, v, (ValueError, f'unsupported schema_version: {v}')) for f, v in UndefinedFormatVersion) |
| 49 | + ) |
| 50 | + @unpack |
| 51 | + def test_fails_on_wrong_args(self, of: OutputFormat, sv: SchemaVersion, raisesRegex: Tuple) -> None: |
| 52 | + with self.assertRaisesRegexp(*raisesRegex): |
| 53 | + get_validator(of, sv) |
0 commit comments