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

Add unit tests for buildOneOfExpressions #208

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
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
21 changes: 15 additions & 6 deletions workflow/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,31 @@ const YamlOneOfTag = "!oneof"

func buildOneOfExpressions(data yaml.Node, path []string) (any, error) {
if data.Type() != yaml.TypeIDMap {
return nil, fmt.Errorf("!oneof found on non-map node at %s; expected a map with a list of options and the discriminator ", strings.Join(path, " -> "))
return nil, fmt.Errorf(
"!oneof found on non-map node at %s; expected a map with a list of options and the discriminator ",
strings.Join(path, " -> "))
}
discriminatorNode, found := data.MapKey(YamlDiscriminatorKey)
if !found {
return nil, fmt.Errorf("key %q not present within !oneof at %q", YamlDiscriminatorKey, strings.Join(path, " -> "))
return nil, fmt.Errorf("key %q not present within %s at %q",
YamlDiscriminatorKey, YamlOneOfTag, strings.Join(path, " -> "))
}
if discriminatorNode.Type() != yaml.TypeIDString {
return nil, fmt.Errorf("%q within !oneof should be a string; got %s", discriminatorNode.Type(), YamlDiscriminatorKey)
return nil, fmt.Errorf("%q within %s should be a string; got %s",
YamlDiscriminatorKey, YamlOneOfTag, discriminatorNode.Type())
}
discriminator := discriminatorNode.Value()
if len(discriminator) == 0 {
return nil, fmt.Errorf("%q within %s is empty", YamlDiscriminatorKey, YamlOneOfTag)
}
oneOfOptionsNode, found := data.MapKey(YamlOneOfKey)
if !found {
return nil, fmt.Errorf("key %q not present within !oneof at %q", YamlOneOfKey, strings.Join(path, " -> "))
return nil, fmt.Errorf("key %q not present within %s at %q",
YamlOneOfKey, YamlOneOfTag, strings.Join(path, " -> "))
}
if oneOfOptionsNode.Type() != yaml.TypeIDMap {
return nil, fmt.Errorf("%q within !oneof should be a map; got %s", YamlOneOfKey, discriminatorNode.Type())
return nil, fmt.Errorf("%q within %q should be a map; got %s",
YamlOneOfKey, YamlOneOfTag, discriminatorNode.Type())
}
options := map[string]any{}
for _, optionNodeKey := range oneOfOptionsNode.MapKeys() {
Expand All @@ -87,7 +97,6 @@ func buildOneOfExpressions(data yaml.Node, path []string) (any, error) {
}
}

discriminator := discriminatorNode.Value()
return &infer.OneOfExpression{
Discriminator: discriminator,
Options: options,
Expand Down
105 changes: 105 additions & 0 deletions workflow/yaml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package workflow //nolint:testpackage // Tests internal functions for unit testing.

import (
"go.arcalot.io/assert"
"go.arcalot.io/lang"
"go.flow.arcalot.io/engine/internal/infer"
"go.flow.arcalot.io/engine/internal/yaml"
"go.flow.arcalot.io/expressions"
"testing"
)

func TestBuildOneOfExpression_Simple(t *testing.T) {
yamlInput := []byte(`
!oneof
discriminator: d
one_of:
a: !expr some_expr
b: !expr some_other_expr
`)
input := assert.NoErrorR[yaml.Node](t)(yaml.New().Parse(yamlInput))
result, err := buildOneOfExpressions(input, make([]string, 0))
assert.NoError(t, err)
assert.InstanceOf[*infer.OneOfExpression](t, result)
oneofResult := result.(*infer.OneOfExpression)
assert.Equals(t, oneofResult.Discriminator, "d")
assert.Equals(t, oneofResult.Options, map[string]any{
"a": lang.Must2(expressions.New("some_expr")),
"b": lang.Must2(expressions.New("some_other_expr")),
})
}

func TestBuildOneOfExpression_InputValidation(t *testing.T) {
// Not a map
yamlInput := []byte(`
!oneof "thisisastring"`)
input := assert.NoErrorR[yaml.Node](t)(yaml.New().Parse(yamlInput))
_, err := buildOneOfExpressions(input, make([]string, 0))
assert.Error(t, err)
assert.Contains(t, err.Error(), "expected a map")

// Wrong discriminator key
yamlInput = []byte(`
!oneof
wrong_key: ""
one_of: {}`)
input = assert.NoErrorR[yaml.Node](t)(yaml.New().Parse(yamlInput))
_, err = buildOneOfExpressions(input, make([]string, 0))
assert.Error(t, err)
assert.Contains(t, err.Error(), "key \""+YamlDiscriminatorKey+"\" not present")

// Discriminator not a string
yamlInput = []byte(`
!oneof
discriminator: {}
one_of: {}`)
input = assert.NoErrorR[yaml.Node](t)(yaml.New().Parse(yamlInput))
_, err = buildOneOfExpressions(input, make([]string, 0))
assert.Error(t, err)
assert.Contains(t, err.Error(), "should be a string")

// Empty discriminator
yamlInput = []byte(`
!oneof
discriminator: ""
one_of: {}`)
input = assert.NoErrorR[yaml.Node](t)(yaml.New().Parse(yamlInput))
_, err = buildOneOfExpressions(input, make([]string, 0))
assert.Error(t, err)
assert.Contains(t, err.Error(), "is empty")

// Missing or wrong oneof key
yamlInput = []byte(`
!oneof
discriminator: "valid"
one_of_wrong: {}`)
input = assert.NoErrorR[yaml.Node](t)(yaml.New().Parse(yamlInput))
_, err = buildOneOfExpressions(input, make([]string, 0))
assert.Error(t, err)
assert.Contains(t, err.Error(), "key \"one_of\" not present")

// Wrong type for oneof options node
yamlInput = []byte(`
!oneof
discriminator: "valid"
one_of: wrong`)
input = assert.NoErrorR[yaml.Node](t)(yaml.New().Parse(yamlInput))
_, err = buildOneOfExpressions(input, make([]string, 0))
assert.Error(t, err)
assert.Contains(t, err.Error(), "should be a map")

// Non-object type as option in one_of section.
yamlInput = []byte(`
!oneof
discriminator: "valid"
one_of:
a: test`)
input = assert.NoErrorR[yaml.Node](t)(yaml.New().Parse(yamlInput))
oneofResult, err := buildOneOfExpressions(input, make([]string, 0))
assert.NoError(t, err)
assert.InstanceOf[*infer.OneOfExpression](t, oneofResult)
_, err = oneofResult.(*infer.OneOfExpression).Type(nil, nil, nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "not an object")

}
Loading