2023-08-22 15:15:04 +02:00
|
|
|
import unittest
|
|
|
|
from typing import Set
|
|
|
|
|
|
|
|
from osc.util.models import *
|
|
|
|
from osc.util.models import get_origin
|
|
|
|
|
|
|
|
|
|
|
|
class TestTyping(unittest.TestCase):
|
|
|
|
def test_get_origin_list(self):
|
|
|
|
typ = get_origin(list)
|
|
|
|
self.assertEqual(typ, None)
|
|
|
|
|
|
|
|
def test_get_origin_list_str(self):
|
|
|
|
typ = get_origin(List[str])
|
|
|
|
self.assertEqual(typ, list)
|
|
|
|
|
|
|
|
|
|
|
|
class TestNotSet(unittest.TestCase):
|
|
|
|
def test_repr(self):
|
|
|
|
self.assertEqual(repr(NotSet), "NotSet")
|
|
|
|
|
|
|
|
def test_bool(self):
|
|
|
|
self.assertEqual(bool(NotSet), False)
|
|
|
|
|
|
|
|
|
|
|
|
class Test(unittest.TestCase):
|
2024-01-04 08:39:06 +01:00
|
|
|
def test_dict(self):
|
|
|
|
class TestSubmodel(BaseModel):
|
|
|
|
text: str = Field(default="default")
|
|
|
|
|
2023-08-22 15:15:04 +02:00
|
|
|
class TestModel(BaseModel):
|
|
|
|
a: str = Field(default="default")
|
|
|
|
b: Optional[str] = Field(default=None)
|
2024-01-04 08:39:06 +01:00
|
|
|
sub: Optional[List[TestSubmodel]] = Field(default=None)
|
2023-08-22 15:15:04 +02:00
|
|
|
|
|
|
|
m = TestModel()
|
2024-01-04 08:39:06 +01:00
|
|
|
self.assertEqual(m.dict(), {"a": "default", "b": None, "sub": None})
|
2023-08-22 15:15:04 +02:00
|
|
|
|
2024-01-04 08:39:06 +01:00
|
|
|
m.b = "B"
|
|
|
|
m.sub = [{"text": "one"}, {"text": "two"}]
|
|
|
|
self.assertEqual(m.dict(), {"a": "default", "b": "B", "sub": [{"text": "one"}, {"text": "two"}]})
|
2023-08-22 15:15:04 +02:00
|
|
|
|
|
|
|
def test_unknown_fields(self):
|
|
|
|
class TestModel(BaseModel):
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, TestModel, does_not_exist=None)
|
|
|
|
|
|
|
|
def test_uninitialized(self):
|
|
|
|
class TestModel(BaseModel):
|
|
|
|
field: str = Field()
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, TestModel)
|
|
|
|
|
|
|
|
def test_invalid_type(self):
|
|
|
|
class TestModel(BaseModel):
|
|
|
|
field: Optional[str] = Field()
|
|
|
|
|
|
|
|
m = TestModel()
|
|
|
|
self.assertRaises(TypeError, setattr, m.field, [])
|
|
|
|
|
|
|
|
def test_unsupported_type(self):
|
|
|
|
class TestModel(BaseModel):
|
|
|
|
field: Set[str] = Field(default=None)
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, TestModel)
|
|
|
|
|
2023-12-01 16:20:55 +01:00
|
|
|
def test_lazy_default(self):
|
|
|
|
class TestModel(BaseModel):
|
|
|
|
field: List[str] = Field(default=lambda: ["string"])
|
|
|
|
|
|
|
|
m = TestModel()
|
|
|
|
self.assertEqual(m.field, ["string"])
|
|
|
|
|
|
|
|
def test_lazy_default_invalid_type(self):
|
|
|
|
class TestModel(BaseModel):
|
|
|
|
field: List[str] = Field(default=lambda: None)
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, TestModel)
|
|
|
|
|
2023-08-22 15:15:04 +02:00
|
|
|
def test_is_set(self):
|
|
|
|
class TestModel(BaseModel):
|
|
|
|
field: Optional[str] = Field()
|
|
|
|
|
|
|
|
m = TestModel()
|
|
|
|
|
2024-01-03 21:21:40 +01:00
|
|
|
self.assertNotIn("field", m._values)
|
2023-08-22 15:15:04 +02:00
|
|
|
self.assertEqual(m.field, None)
|
2024-01-03 21:21:40 +01:00
|
|
|
|
2023-08-22 15:15:04 +02:00
|
|
|
m.field = "text"
|
2024-01-03 21:21:40 +01:00
|
|
|
|
|
|
|
self.assertIn("field", m._values)
|
2023-08-22 15:15:04 +02:00
|
|
|
self.assertEqual(m.field, "text")
|
|
|
|
|
|
|
|
def test_str(self):
|
|
|
|
class TestModel(BaseModel):
|
|
|
|
field: str = Field(default="default")
|
|
|
|
|
|
|
|
m = TestModel()
|
|
|
|
|
|
|
|
field = m.__fields__["field"]
|
|
|
|
self.assertEqual(field.is_model, False)
|
|
|
|
self.assertEqual(field.is_optional, False)
|
|
|
|
self.assertEqual(field.origin_type, str)
|
|
|
|
|
|
|
|
self.assertEqual(m.field, "default")
|
|
|
|
m.field = "text"
|
|
|
|
self.assertEqual(m.field, "text")
|
|
|
|
|
|
|
|
def test_optional_str(self):
|
|
|
|
class TestModel(BaseModel):
|
|
|
|
field: Optional[str] = Field()
|
|
|
|
|
|
|
|
m = TestModel()
|
|
|
|
|
|
|
|
field = m.__fields__["field"]
|
|
|
|
self.assertEqual(field.is_model, False)
|
|
|
|
self.assertEqual(field.is_optional, True)
|
|
|
|
self.assertEqual(field.origin_type, str)
|
|
|
|
|
|
|
|
self.assertEqual(m.field, None)
|
|
|
|
m.field = "text"
|
|
|
|
self.assertEqual(m.field, "text")
|
|
|
|
|
|
|
|
def test_int(self):
|
|
|
|
class TestModel(BaseModel):
|
|
|
|
field: int = Field(default=0)
|
|
|
|
|
|
|
|
m = TestModel()
|
|
|
|
|
|
|
|
field = m.__fields__["field"]
|
|
|
|
self.assertEqual(field.is_model, False)
|
|
|
|
self.assertEqual(field.is_optional, False)
|
|
|
|
self.assertEqual(field.origin_type, int)
|
|
|
|
|
|
|
|
self.assertEqual(m.field, 0)
|
|
|
|
m.field = 1
|
|
|
|
self.assertEqual(m.field, 1)
|
|
|
|
|
|
|
|
def test_optional_int(self):
|
|
|
|
class TestModel(BaseModel):
|
|
|
|
field: Optional[int] = Field()
|
|
|
|
|
|
|
|
m = TestModel()
|
|
|
|
|
|
|
|
field = m.__fields__["field"]
|
|
|
|
self.assertEqual(field.is_model, False)
|
|
|
|
self.assertEqual(field.is_optional, True)
|
|
|
|
self.assertEqual(field.origin_type, int)
|
|
|
|
|
|
|
|
self.assertEqual(m.field, None)
|
|
|
|
m.field = 1
|
|
|
|
self.assertEqual(m.field, 1)
|
|
|
|
|
|
|
|
def test_submodel(self):
|
|
|
|
class TestSubmodel(BaseModel):
|
|
|
|
text: str = Field(default="default")
|
|
|
|
|
|
|
|
class TestModel(BaseModel):
|
|
|
|
field: TestSubmodel = Field(default={})
|
|
|
|
|
|
|
|
m = TestModel()
|
|
|
|
|
|
|
|
field = m.__fields__["field"]
|
|
|
|
self.assertEqual(field.is_model, True)
|
|
|
|
self.assertEqual(field.is_optional, False)
|
|
|
|
self.assertEqual(field.origin_type, TestSubmodel)
|
|
|
|
|
|
|
|
m = TestModel(field=TestSubmodel())
|
|
|
|
self.assertEqual(m.field.text, "default")
|
|
|
|
|
|
|
|
m = TestModel(field={"text": "text"})
|
|
|
|
self.assertEqual(m.field.text, "text")
|
|
|
|
|
|
|
|
def test_optional_submodel(self):
|
|
|
|
class TestSubmodel(BaseModel):
|
|
|
|
text: str = Field(default="default")
|
|
|
|
|
|
|
|
class TestModel(BaseModel):
|
|
|
|
field: Optional[TestSubmodel] = Field(default=None)
|
|
|
|
|
|
|
|
m = TestModel()
|
|
|
|
|
|
|
|
field = m.__fields__["field"]
|
|
|
|
self.assertEqual(field.is_model, True)
|
|
|
|
self.assertEqual(field.is_optional, True)
|
|
|
|
self.assertEqual(field.origin_type, TestSubmodel)
|
|
|
|
self.assertEqual(m.field, None)
|
2024-01-23 20:05:28 +01:00
|
|
|
m.dict()
|
2023-08-22 15:15:04 +02:00
|
|
|
|
|
|
|
m = TestModel(field=TestSubmodel())
|
|
|
|
self.assertIsInstance(m.field, TestSubmodel)
|
|
|
|
self.assertEqual(m.field.text, "default")
|
2024-01-23 20:05:28 +01:00
|
|
|
m.dict()
|
2023-08-22 15:15:04 +02:00
|
|
|
|
|
|
|
m = TestModel(field={"text": "text"})
|
|
|
|
self.assertNotEqual(m.field, None)
|
|
|
|
self.assertEqual(m.field.text, "text")
|
2024-01-23 20:05:28 +01:00
|
|
|
m.dict()
|
2023-08-22 15:15:04 +02:00
|
|
|
|
2024-01-23 20:05:17 +01:00
|
|
|
def test_enum(self):
|
|
|
|
class Numbers(Enum):
|
|
|
|
one = "one"
|
|
|
|
two = "two"
|
|
|
|
|
|
|
|
class TestModel(BaseModel):
|
|
|
|
field: Optional[Numbers] = Field(default=None)
|
|
|
|
|
|
|
|
m = TestModel()
|
|
|
|
field = m.__fields__["field"]
|
|
|
|
self.assertEqual(field.is_model, False)
|
|
|
|
self.assertEqual(field.is_optional, True)
|
|
|
|
self.assertEqual(field.origin_type, Numbers)
|
|
|
|
self.assertEqual(m.field, None)
|
|
|
|
|
|
|
|
m.field = "one"
|
|
|
|
self.assertEqual(m.field, "one")
|
|
|
|
|
|
|
|
self.assertRaises(ValueError, setattr, m, "field", "does-not-exist")
|
|
|
|
|
2023-08-22 15:15:04 +02:00
|
|
|
def test_parent(self):
|
|
|
|
class ParentModel(BaseModel):
|
|
|
|
field: str = Field(default="text")
|
|
|
|
|
|
|
|
class ChildModel(BaseModel):
|
|
|
|
field: str = Field(default=FromParent("field"))
|
|
|
|
field2: str = Field(default=FromParent("field"))
|
|
|
|
|
|
|
|
p = ParentModel()
|
|
|
|
c = ChildModel(_parent=p)
|
|
|
|
self.assertEqual(p.field, "text")
|
|
|
|
self.assertEqual(c.field, "text")
|
|
|
|
self.assertEqual(c.field2, "text")
|
|
|
|
|
|
|
|
c.field = "new-text"
|
|
|
|
self.assertEqual(p.field, "text")
|
|
|
|
self.assertEqual(c.field, "new-text")
|
|
|
|
self.assertEqual(c.field2, "text")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|