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

Added test cases that explicitly test Object unserialization #111

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion schema/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var testStructSchemaPtr = schema.NewTypedObject[*testStructPtr]("testStruct", ma
),
})

func TestObjectUnserialization(t *testing.T) {
func TestObjectUnserialization_Success(t *testing.T) {
data := map[string]any{
"Field1": 42,
"field3": "Hello world!",
Expand All @@ -94,6 +94,43 @@ func TestObjectUnserialization(t *testing.T) {
})
}

func TestObjectUnserialization_MissingFields(t *testing.T) {
dataMissing1 := map[string]any{
"field3": "Hello world!",
}
_, err := testStructSchema.Unserialize(dataMissing1)
assert.Error(t, err)
assert.Contains(t, err.Error(), "field is required")

dataMissing3 := map[string]any{
"Field1": 42,
}
_, err = testStructSchema.Unserialize(dataMissing3)
assert.Error(t, err)
assert.Contains(t, err.Error(), "field is required")
}

func TestObjectUnserialization_IncorrectType(t *testing.T) {
dataMissing1 := map[string]any{
"Field1": "this cannot be represented as an integer",
"field3": "Hello world!",
}
_, err := testStructSchema.Unserialize(dataMissing1)
assert.Error(t, err)
assert.Contains(t, err.Error(), "parsing")
}

func TestObjectUnserialization_ExtraField(t *testing.T) {
dataMissing1 := map[string]any{
"Field1": 42,
"wrong": "wrong",
"field3": "Hello world!",
}
_, err := testStructSchema.Unserialize(dataMissing1)
assert.Error(t, err)
assert.Contains(t, err.Error(), "Invalid parameter 'wrong'")
}

type embeddedTestStruct struct {
Field1 int64
}
Expand Down
Loading