|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Copyright 2023 The Foundry Visionmongers Ltd |
| 3 | +""" |
| 4 | +Tests Specifications include required traits. |
| 5 | +""" |
| 6 | + |
| 7 | +# pylint: disable=no-self-use |
| 8 | +# pylint: disable=invalid-name |
| 9 | +# pylint: disable=missing-class-docstring |
| 10 | + |
| 11 | +import inspect |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +from openassetio_mediacreation.traits.usage import EntityTrait, RelationshipTrait |
| 16 | +from openassetio_mediacreation import specifications |
| 17 | + |
| 18 | + |
| 19 | +class Test_specification_traits: |
| 20 | + def test_contain_usage_trait(self): |
| 21 | + """ |
| 22 | + All specifications should contain one trait from the usage namespace |
| 23 | + so that their purpose can be determined at runtime. |
| 24 | + """ |
| 25 | + for cls in self.__specifications(): |
| 26 | + # Ensure exactly one is present |
| 27 | + assert (EntityTrait.kId in cls.kTraitSet) != (RelationshipTrait.kId in cls.kTraitSet) |
| 28 | + |
| 29 | + def test_traits_constructible(self): |
| 30 | + """ |
| 31 | + Asserts that specifications reference valid traits, without |
| 32 | + relying on private knowledge that the trait set already uses |
| 33 | + their Id attribute. |
| 34 | + """ |
| 35 | + for cls in self.__specifications(): |
| 36 | + instance = cls.create() |
| 37 | + functions = inspect.getmembers(instance, inspect.ismethod) |
| 38 | + for _, func in functions: |
| 39 | + if not func.__name__.endswith("Trait"): |
| 40 | + continue |
| 41 | + func() |
| 42 | + |
| 43 | + def test_relationship_names_suffixed(self): |
| 44 | + """ |
| 45 | + Relationship specifications should be suffixed with "Relationship" |
| 46 | + """ |
| 47 | + for cls in self.__specifications(): |
| 48 | + if RelationshipTrait.kId in cls.kTraitSet: |
| 49 | + assert cls.__name__.endswith("RelationshipSpecification") |
| 50 | + |
| 51 | + def __specifications(self): |
| 52 | + """ |
| 53 | + Retrieves all defined Specification classes |
| 54 | + """ |
| 55 | + specs = [] |
| 56 | + namespaces = inspect.getmembers(specifications, inspect.ismodule) |
| 57 | + for _, namespace in namespaces: |
| 58 | + classes = inspect.getmembers(namespace, inspect.isclass) |
| 59 | + for _, cls in classes: |
| 60 | + if cls.__name__.endswith("Specification"): |
| 61 | + specs.append(cls) |
| 62 | + assert specs |
| 63 | + return specs |
0 commit comments