121 lines
3.1 KiB
Go
121 lines
3.1 KiB
Go
package apiconfig
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func TestUnmarshalActionYAML(t *testing.T) {
|
|
yamlData := `
|
|
type: some_type
|
|
config:
|
|
key1: value1
|
|
key2: value2
|
|
next: next_action
|
|
fail: fail_action
|
|
`
|
|
var action Action
|
|
err := yaml.Unmarshal([]byte(yamlData), &action)
|
|
if err != nil {
|
|
t.Fatalf("Failed to unmarshal Action: %v", err)
|
|
}
|
|
|
|
if action.Type != "some_type" {
|
|
t.Errorf("Expected Type to be 'some_type', but got '%s'", action.Type)
|
|
}
|
|
|
|
expectedConfig := map[string]interface{}{
|
|
"key1": "value1",
|
|
"key2": "value2",
|
|
}
|
|
|
|
var config map[string]interface{}
|
|
if err := json.Unmarshal(action.Config, &config); err != nil {
|
|
t.Fatalf("Failed to unmarshal action.Config: %v", err)
|
|
}
|
|
|
|
assert.Equal(t, expectedConfig, config)
|
|
}
|
|
|
|
func TestUnmarshalDatasourceConfigYAML(t *testing.T) {
|
|
yamlData := `
|
|
id: some_id
|
|
type: some_type
|
|
config:
|
|
key3: value3
|
|
key4: value4
|
|
`
|
|
var datasourceConfig DatasourceConfig
|
|
err := yaml.Unmarshal([]byte(yamlData), &datasourceConfig)
|
|
if err != nil {
|
|
t.Fatalf("Failed to unmarshal DatasourceConfig: %v", err)
|
|
}
|
|
|
|
if datasourceConfig.ID != "some_id" {
|
|
t.Errorf("Expected ID to be 'some_id', but got '%s'", datasourceConfig.ID)
|
|
}
|
|
|
|
if datasourceConfig.Type != "some_type" {
|
|
t.Errorf("Expected Type to be 'some_type', but got '%s'", datasourceConfig.Type)
|
|
}
|
|
|
|
expectedConfig := map[string]interface{}{
|
|
"key3": "value3",
|
|
"key4": "value4",
|
|
}
|
|
|
|
var config map[string]interface{}
|
|
if err := json.Unmarshal(datasourceConfig.Config, &config); err != nil {
|
|
t.Fatalf("Failed to unmarshal datasourceConfig.Config: %v", err)
|
|
}
|
|
|
|
assert.Equal(t, expectedConfig, config)
|
|
}
|
|
|
|
func TestResponseObjectToProtoAndBack(t *testing.T) {
|
|
original := ResponseObject{
|
|
Value: "root_value",
|
|
Fields: map[string]ResponseObject{
|
|
"field1": {
|
|
Value: "field1_value",
|
|
Fields: map[string]ResponseObject{
|
|
"nested1": {
|
|
Value: "nested1_value",
|
|
Fields: map[string]ResponseObject{},
|
|
},
|
|
},
|
|
},
|
|
"field2": {
|
|
Value: "field2_value",
|
|
Fields: map[string]ResponseObject{},
|
|
},
|
|
},
|
|
}
|
|
|
|
// Convert to proto
|
|
protoObj := original.ToProto()
|
|
|
|
// Convert back from proto
|
|
result := ResponseObjectFromProto(protoObj)
|
|
|
|
// Verify the result matches the original
|
|
assert.Equal(t, original.Value, result.Value, "Root value should match")
|
|
assert.Equal(t, len(original.Fields), len(result.Fields), "Should have same number of fields")
|
|
|
|
// Check field1
|
|
assert.Equal(t, original.Fields["field1"].Value, result.Fields["field1"].Value, "field1 value should match")
|
|
assert.Equal(t, len(original.Fields["field1"].Fields), len(result.Fields["field1"].Fields), "field1 should have same number of nested fields")
|
|
|
|
// Check nested field
|
|
assert.Equal(t, original.Fields["field1"].Fields["nested1"].Value, result.Fields["field1"].Fields["nested1"].Value, "nested1 value should match")
|
|
|
|
// Check field2
|
|
assert.Equal(t, original.Fields["field2"].Value, result.Fields["field2"].Value, "field2 value should match")
|
|
|
|
// Complete equality check
|
|
assert.Equal(t, original, result, "The converted object should be identical to the original")
|
|
}
|