167 lines
4.4 KiB
Python
167 lines
4.4 KiB
Python
# dabmodel (c) by chacha
|
|
#
|
|
# dabmodel is licensed under a
|
|
# Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Unported License.
|
|
#
|
|
# You should have received a copy of the license along with this
|
|
# work. If not, see <https://creativecommons.org/licenses/by-nc-sa/4.0/>.
|
|
|
|
import unittest
|
|
|
|
from os import chdir
|
|
from pathlib import Path
|
|
|
|
from typing import (
|
|
Any,
|
|
Annotated,
|
|
)
|
|
from dabmodel.appliance import Appliance
|
|
|
|
|
|
print(__name__)
|
|
print(__package__)
|
|
|
|
from src import dabmodel as dm
|
|
|
|
|
|
testdir_path = Path(__file__).parent.resolve()
|
|
chdir(testdir_path.parent.resolve())
|
|
|
|
|
|
def test_initializer_safe_testfc():
|
|
eval("print('hi')")
|
|
|
|
|
|
class FeatureBindTest(unittest.TestCase):
|
|
def setUp(self):
|
|
print("\n->", unittest.TestCase.id(self))
|
|
|
|
def test_simple(self):
|
|
"""Testing first appliance feature, and Field types (simple)"""
|
|
|
|
# class can be created
|
|
class Appliance1(dm.Appliance):
|
|
VarStrOuter: str = "testvalue APPLIANCE"
|
|
|
|
class Feature1(dm.Feature):
|
|
VarStrInner: str = "testvalue FEATURE"
|
|
|
|
app1 = Appliance1()
|
|
|
|
self.assertIn("Feature1", app1.__lam_schema__["features"])
|
|
self.assertTrue(hasattr(app1, "Feature1"))
|
|
|
|
def test_outside_bind(self):
|
|
"""Testing first appliance feature, and Field types (simple)"""
|
|
|
|
# class can be created
|
|
class Appliance1(dm.Appliance):
|
|
pass
|
|
|
|
class Feature1(dm.Feature, appliance=Appliance1):
|
|
VarStrInner: str = "testvalue FEATURE1"
|
|
|
|
app = Appliance1(feat1=Feature1)
|
|
self.assertEqual(app.feat1.VarStrInner, "testvalue FEATURE1")
|
|
|
|
# check it does not leak accross instances
|
|
|
|
app = Appliance1(feat2=Feature1)
|
|
self.assertEqual(app.feat2.VarStrInner, "testvalue FEATURE1")
|
|
|
|
with self.assertRaises(AttributeError):
|
|
app.feat1
|
|
|
|
def test_outside_bind2(self):
|
|
"""Testing first appliance feature, and Field types (simple)"""
|
|
|
|
# class can be created
|
|
class Appliance1(dm.Appliance):
|
|
pass
|
|
|
|
class Feature1(dm.Feature):
|
|
VarStrInner: str = "testvalue FEATURE1"
|
|
|
|
Feature1.bind_appliance(Appliance1)
|
|
|
|
app = Appliance1(feat1=Feature1)
|
|
self.assertEqual(app.feat1.VarStrInner, "testvalue FEATURE1")
|
|
|
|
# check it does not leak accross instances
|
|
|
|
app = Appliance1(feat2=Feature1)
|
|
self.assertEqual(app.feat2.VarStrInner, "testvalue FEATURE1")
|
|
|
|
with self.assertRaises(AttributeError):
|
|
app.feat1
|
|
|
|
def test_bind_inheritance_no_leak(self):
|
|
"""Testing first appliance feature, and Field types (simple)"""
|
|
|
|
# class can be created
|
|
class Appliance1(dm.Appliance):
|
|
pass
|
|
|
|
class Feature1(dm.Feature):
|
|
VarStrInner: str = "testvalue FEATURE1"
|
|
|
|
class Feature2(Feature1, appliance=Appliance1):
|
|
VarStrInner = "testvalue FEATURE2"
|
|
|
|
app = Appliance1(feat=Feature2)
|
|
self.assertEqual(app.feat.VarStrInner, "testvalue FEATURE2")
|
|
|
|
with self.assertRaises(dm.FeatureNotBound):
|
|
app = Appliance1(feat=Feature1)
|
|
|
|
def test_bind_notbound(self):
|
|
"""Testing first appliance feature, and Field types (simple)"""
|
|
|
|
# class can be created
|
|
class Appliance1(dm.Appliance):
|
|
pass
|
|
|
|
class Feature1(dm.Feature):
|
|
VarStrInner: str = "testvalue FEATURE1"
|
|
|
|
with self.assertRaises(dm.FeatureNotBound):
|
|
Appliance1(feat1=Feature1)
|
|
|
|
def test_bind_defect(self):
|
|
|
|
class Feature1(dm.Feature):
|
|
pass
|
|
|
|
with self.assertRaises(dm.FeatureNotBound):
|
|
Feature1()
|
|
|
|
def test_not_bound_runtime_attach_fails(self):
|
|
class App(dm.Appliance):
|
|
pass
|
|
|
|
class UnboundFeature(dm.Feature):
|
|
x: int = 1
|
|
|
|
# attaching an unbound feature should raise
|
|
with self.assertRaises(dm.FeatureNotBound):
|
|
App(Unbound=UnboundFeature)
|
|
|
|
def test_runtime_attach_bound_success(self):
|
|
class App(dm.Appliance):
|
|
class F1(dm.Feature):
|
|
val: int = 1
|
|
|
|
class Extra(App.F1): # stays bound to App
|
|
val = 7
|
|
|
|
app = App(Extra=Extra)
|
|
self.assertTrue(hasattr(app, "Extra"))
|
|
self.assertIsInstance(app.Extra, Extra)
|
|
self.assertEqual(app.Extra.val, 7)
|
|
|
|
|
|
# ---------- main ----------
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|