135 lines
4.9 KiB
Python
135 lines
4.9 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 pydantic import StrictInt, model_validator
|
|
from pydantic.fields import Field
|
|
|
|
print(__name__)
|
|
print(__package__)
|
|
|
|
from src import dabmodel
|
|
from typing import Annotated, Any
|
|
from uuid import uuid4
|
|
|
|
testdir_path = Path(__file__).parent.resolve()
|
|
chdir(testdir_path.parent.resolve())
|
|
|
|
|
|
class MyAppliance(dabmodel.BaseAppliance):
|
|
app_specifi_integer_arg: Annotated[StrictInt | None, dabmodel.DABField(42)]
|
|
|
|
class MyFeature(dabmodel.BaseFeature):
|
|
@dabmodel.default_values_override
|
|
@classmethod
|
|
def __override_config__(cls, values):
|
|
print("!!! CONFIG Feature 1")
|
|
values["template_id"] = "421d61cb-e364-46d8-9b77-ec439f1fa666"
|
|
values["template_short_name"] = "my-feature-1"
|
|
values["template_long_name"] = "My feature template 1 !!"
|
|
values["template_description"] = """A very nice FEature 1"""
|
|
|
|
class MyFeature2(dabmodel.BaseFeature):
|
|
@dabmodel.default_values_override
|
|
@classmethod
|
|
def __override_config__(cls, values):
|
|
print("!!! CONFIG Feature 2")
|
|
values["template_id"] = "421d61cb-e364-46d8-9b77-ec439f1fa666"
|
|
values["template_short_name"] = "my-feature-2"
|
|
values["template_long_name"] = "My feature template 2 !!"
|
|
values["template_description"] = """A very nice FEature 2"""
|
|
|
|
@dabmodel.default_values_override
|
|
@classmethod
|
|
def __override_config__(cls, values):
|
|
print("!!! CONFIG Appliance 1")
|
|
print(f"!!!! {values['rootfs_size']}")
|
|
values["template_id"] = "421d61cb-e364-46d8-9b64-ec439f1faae8"
|
|
values["template_short_name"] = "my-app- tem 1"
|
|
values["template_long_name"] = "My appliance template 1 !!"
|
|
values["template_description"] = """A very nice Appliance 1"""
|
|
values["ram_size"] = 1024
|
|
cls.add_feature(cls.MyFeature())
|
|
cls.add_feature(cls.MyFeature2())
|
|
|
|
|
|
class MyAppliance2(MyAppliance):
|
|
@dabmodel.default_values_override
|
|
@classmethod
|
|
def __override_config__(cls, values):
|
|
print("!!! CONFIG Appliance 2")
|
|
print(f"!!!! {values['template_id']}")
|
|
values["template_id"] = "421d61cb-e664-46d8-9b64-ec439f1fafff"
|
|
values["template_short_name"] = "my-app- tem 2"
|
|
values["template_long_name"] = "My appliance template 2 !!"
|
|
values["template_description"] = """A very nice Appliance 2"""
|
|
cls.del_feature(MyAppliance.MyFeature)
|
|
|
|
|
|
class MyAppliance3(dabmodel.BaseAppliance):
|
|
|
|
class MyFeature(MyAppliance.MyFeature):
|
|
# testtt: Annotated[MyAppliance.MyFeature, Field(MyAppliance.MyFeature())] # error case
|
|
|
|
@dabmodel.default_values_override
|
|
@classmethod
|
|
def __override_config__(cls, values):
|
|
print("!!! CONFIG Feature 1 (modified)")
|
|
values["template_id"] = "421d61cb-e364-46d8-9b77-ec439f1fa777"
|
|
values["template_short_name"] = "my-feature-1-bis"
|
|
|
|
# testtt: Annotated[MyAppliance.MyFeature, Field(MyAppliance.MyFeature())] # error case
|
|
|
|
@dabmodel.default_values_override
|
|
@classmethod
|
|
def __override_config__(cls, values):
|
|
print("!!! CONFIG Appliance 3")
|
|
values["template_id"] = "421d61cb-e364-46d8-9b64-ec439f1faaaa"
|
|
values["template_short_name"] = "my-app- tem 3"
|
|
values["template_long_name"] = "My appliance template 3 !!"
|
|
values["template_description"] = """A very nice Appliance 3"""
|
|
values["ram_size"] = 3076
|
|
cls.add_feature(cls.MyFeature())
|
|
|
|
|
|
class TestModel(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
chdir(testdir_path.parent.resolve())
|
|
|
|
def test_version(self):
|
|
self.assertNotEqual(dabmodel.__version__, "?.?.?")
|
|
|
|
def test_model(self):
|
|
|
|
feature1 = MyAppliance.MyFeature()
|
|
print(feature1)
|
|
print(MyAppliance.MyFeature)
|
|
print(MyAppliance.MyFeature.__name__)
|
|
print(MyAppliance.MyFeature.__class__)
|
|
print("==")
|
|
print(feature1.model_dump_json(indent=1))
|
|
|
|
app = MyAppliance(dabinst_short_name="my-app-1", app_specifi_integer_arg=123)
|
|
app2 = MyAppliance2(dabinst_short_name="my-app-2", app_specifi_integer_arg=654)
|
|
app3 = MyAppliance3(dabinst_short_name="my-app-3", template_description="FORCED")
|
|
|
|
print(app.model_dump_json(indent=1))
|
|
print(app2.model_dump_json(indent=1))
|
|
print(app3.model_dump_json(indent=1))
|
|
|
|
print("aaa")
|
|
tmp_json = app3.model_dump_json(indent=1)
|
|
print(tmp_json)
|
|
recreated_obj = MyAppliance3.parse_raw(tmp_json)
|
|
print(recreated_obj)
|
|
|
|
# app3.add_feature(MyAppliance.MyFeature()) # error case (add_feature not callable from instance)
|