Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oneof refactor 2 #126

Merged
merged 18 commits into from
Apr 3, 2024
57 changes: 27 additions & 30 deletions src/arcaflow_plugin_sdk/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
SchemaBuildException,
)

# default discriminator field name
discriminator_default = "_type"
# default discriminator field name used by the OneOfType
# when no discriminator field name is declared
default_discriminator = "_type"

# The string "type_" is the discriminator identifier that will
# be embedded in StrInline. It must match the OneOfType's
Expand All @@ -31,7 +32,7 @@ class StrBasic:

@dataclasses.dataclass
class StrBasic2:
msg2: int
msg2: str


@dataclasses.dataclass
Expand Down Expand Up @@ -524,17 +525,6 @@ def setUp(self):
"a",
)

def test_unserialize_error_discriminator_type(self):
s_type = schema.OneOfStringType(
{
"a": schema.RefType("a", self.scope_basic),
"b": schema.RefType("b", self.scope_basic),
},
scope=self.scope_basic,
)
with self.assertRaises(ConstraintException):
s_type.unserialize({discriminator_default: "1", 1: "Hello world!"})

def test_unserialize(self):
s_type = schema.OneOfStringType(
{
Expand All @@ -550,28 +540,35 @@ def test_unserialize(self):
with self.assertRaises(ConstraintException):
s_type.unserialize({"b": 42})

# invalid type for 'data' argument
# Invalid type for discriminator value
with self.assertRaises(ConstraintException):
discriminator_value = "O"
s_type.unserialize(
{default_discriminator: discriminator_value,
1: "Hello world!"})
webbnh marked this conversation as resolved.
Show resolved Hide resolved

# Invalid type for 'data' argument
with self.assertRaises(ConstraintException):
s_type.unserialize([])
# Mismatching key value
with self.assertRaises(ConstraintException):
s_type.unserialize(
{discriminator_default: "a", "b": "Hello world!"}
{default_discriminator: "a", "b": "Hello world!"}
)
# Invalid key value
with self.assertRaises(ConstraintException):
s_type.unserialize({discriminator_default: 1, "a": "Hello world!"})
s_type.unserialize({default_discriminator: 1, "a": "Hello world!"})
# Invalid discriminator
with self.assertRaises(ConstraintException):
s_type.unserialize({discriminator_default: 1, "b": "Hello world!"})
s_type.unserialize({default_discriminator: 1, "b": "Hello world!"})

unserialized_data: StrBasic = s_type.unserialize(
{discriminator_default: "a", "msg": "Hello world!"}
{default_discriminator: "a", "msg": "Hello world!"}
)
self.assertIsInstance(unserialized_data, StrBasic)
self.assertEqual(unserialized_data.msg, "Hello world!")
unserialized_data2: IntBasic = s_type.unserialize(
{discriminator_default: "b", "b": 42}
{default_discriminator: "b", "b": 42}
)
self.assertIsInstance(unserialized_data2, IntBasic)
self.assertEqual(unserialized_data2.b, 42)
Expand All @@ -584,7 +581,7 @@ def test_unserialize(self):
scope=self.scope_basic,
)
unserialized_data3: StrBasic = s_type_int.unserialize(
{discriminator_default: 1, "msg": "Hello world!"}
{default_discriminator: 1, "msg": "Hello world!"}
)
self.assertIsInstance(unserialized_data3, StrBasic)
self.assertEqual(unserialized_data3.msg, "Hello world!")
Expand Down Expand Up @@ -1496,7 +1493,7 @@ def test_one_of(self):
"b": schema.RefType(StrBasic2.__name__, scope),
},
scope,
discriminator_default,
default_discriminator,
)
)
},
Expand Down Expand Up @@ -1543,51 +1540,51 @@ def test_one_of(self):
"type": "object",
"properties": {
"msg": {"type": "string"},
discriminator_default: {
default_discriminator: {
"type": "string",
"const": "a",
},
},
"required": [discriminator_default, "msg"],
"required": [default_discriminator, "msg"],
"additionalProperties": False,
"dependentRequired": {},
},
f"{StrBasic.__name__}_discriminated_string_a": {
"type": "object",
"properties": {
"msg": {"type": "string"},
discriminator_default: {
default_discriminator: {
"type": "string",
"const": "a",
},
},
"required": [discriminator_default, "msg"],
"required": [default_discriminator, "msg"],
"additionalProperties": False,
"dependentRequired": {},
},
StrBasic2.__name__: {
"type": "object",
"properties": {
"msg2": {"type": "string"},
discriminator_default: {
default_discriminator: {
"type": "string",
"const": "b",
},
},
"required": [discriminator_default, "msg2"],
"required": [default_discriminator, "msg2"],
"additionalProperties": False,
"dependentRequired": {},
},
f"{StrBasic2.__name__}_discriminated_string_b": {
"type": "object",
"properties": {
"msg2": {"type": "string"},
discriminator_default: {
default_discriminator: {
"type": "string",
"const": "b",
},
},
"required": [discriminator_default, "msg2"],
"required": [default_discriminator, "msg2"],
"additionalProperties": False,
"dependentRequired": {},
},
Expand Down