Skip to content

Commit ccd6d08

Browse files
committed
[Tests] Add Specification tests
Asserts they contain a single usage trait. This is required for runtime introspection of the puropse of the spec. Asserts Specification traits constructible, as we were implicitly testing this as part of checking the trait set contained a usage trait (as the traitgen uses their attr to build the set), but that was relying on an implementation details. This requires that MediaCreation doesn't reference any other packages (which it doesn't at present). Asserts relationship specication names follow guidelines. Signed-off-by: Tom Cowland <[email protected]>
1 parent 7244f61 commit ccd6d08

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

Comments
 (0)