numpydantic/tests/test_interface/test_interface.py

104 lines
2.8 KiB
Python
Raw Normal View History

2024-05-18 01:14:00 +00:00
import pdb
2024-05-15 03:18:04 +00:00
import pytest
import numpy as np
2024-05-15 03:18:04 +00:00
from numpydantic.interface import Interface
@pytest.fixture(scope="module")
def interfaces():
"""Define test interfaces in this module, and delete afterwards"""
class Interface1(Interface):
input_types = (list,)
return_type = tuple
@classmethod
def check(cls, array):
if isinstance(array, list):
return True
return False
@classmethod
def enabled(cls) -> bool:
return True
Interface2 = type("Interface2", Interface1.__bases__, dict(Interface1.__dict__))
class Interface3(Interface1):
@classmethod
def enabled(cls) -> bool:
return False
class Interfaces:
interface1 = Interface1
interface2 = Interface2
interface3 = Interface3
yield Interfaces
del Interface1
del Interface2
del Interface3
def test_interface_match_error(interfaces):
"""
Test that `match` and `match_output` raises errors when no or multiple matches are found
"""
with pytest.raises(ValueError) as e:
Interface.match([1, 2, 3])
2024-05-18 01:14:00 +00:00
assert "Interface1" in str(e.value)
assert "Interface2" in str(e.value)
2024-05-15 03:18:04 +00:00
with pytest.raises(ValueError) as e:
2024-05-18 01:14:00 +00:00
Interface.match(([1, 2, 3], ["hey"]))
assert "No matching interfaces" in str(e.value)
2024-05-15 03:18:04 +00:00
with pytest.raises(ValueError) as e:
Interface.match_output((1, 2, 3))
2024-05-18 01:14:00 +00:00
assert "Interface1" in str(e.value)
assert "Interface2" in str(e.value)
2024-05-15 03:18:04 +00:00
with pytest.raises(ValueError) as e:
Interface.match_output("hey")
2024-05-18 01:14:00 +00:00
assert "No matching interfaces" in str(e.value)
2024-05-15 03:18:04 +00:00
def test_interface_enabled(interfaces):
"""
An interface shouldn't be included if it's not enabled
"""
2024-05-15 03:19:24 +00:00
assert not interfaces.interface3.enabled()
assert interfaces.interface3 not in Interface.interfaces()
2024-05-15 03:18:04 +00:00
def test_interface_type_lists():
"""
Seems like a silly test, but ensure that our return types and input types
lists have all the class attrs
"""
for interface in Interface.interfaces():
if isinstance(interface.input_types, (list, tuple)):
for atype in interface.input_types:
assert atype in Interface.input_types()
else:
assert interface.input_types in Interface.input_types()
if isinstance(interface.return_type, (list, tuple)):
for atype in interface.return_type:
assert atype in Interface.return_types()
else:
assert interface.return_type in Interface.return_types()
def test_interfaces_sorting():
"""
Interfaces should be returned in descending order of priority
"""
ifaces = Interface.interfaces()
priorities = [i.priority for i in ifaces]
assert (np.diff(priorities) <= 0).all()