common/protocol/unmarshal_test.go

515 lines
11 KiB
Go

package protocol_test
import (
"testing"
commonv1 "code.icb4dc0.de/buildr/api/generated/common/v1"
"code.icb4dc0.de/buildr/common/protocol"
)
func TestUnmarshal_Bool_Success(t *testing.T) {
target := struct {
Delete bool
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"delete": {
Type: commonv1.ModuleSpec_ValueTypeSingle,
SingleValue: &commonv1.ModuleSpec_Value_BoolValue{
BoolValue: true,
},
},
},
}
if err := protocol.Unmarshal(spec, &target); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
if !target.Delete {
t.Errorf("Expected Delete to be true")
}
}
func TestUnmarshal_Bool_Err(t *testing.T) {
target := struct {
Delete string
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"delete": {
Type: commonv1.ModuleSpec_ValueTypeSingle,
SingleValue: &commonv1.ModuleSpec_Value_BoolValue{
BoolValue: true,
},
},
},
}
if err := protocol.Unmarshal(spec, &target); err == nil {
t.Errorf("Expected error")
} else {
t.Log(err.Error())
}
}
func TestUnmarshal_Bool_Slice_Success(t *testing.T) {
target := struct {
Delete []bool
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"delete": {
Type: commonv1.ModuleSpec_ValueTypeBoolSlice,
BoolValues: []bool{true},
},
},
}
if err := protocol.Unmarshal(spec, &target); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
if len(target.Delete) < 1 {
t.Errorf("Expected Delete to have at least one element")
} else if !target.Delete[0] {
t.Errorf("Expected Delete[0] to be true")
}
}
func TestUnmarshal_Bool_Slice_Err(t *testing.T) {
target := struct {
Delete []string
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"delete": {
Type: commonv1.ModuleSpec_ValueTypeBoolSlice,
BoolValues: []bool{true},
},
},
}
if err := protocol.Unmarshal(spec, &target); err == nil {
t.Errorf("Expected error")
} else {
t.Log(err.Error())
}
}
func TestUnmarshal_String_Success(t *testing.T) {
target := struct {
Name string
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"name": {
Type: commonv1.ModuleSpec_ValueTypeSingle,
SingleValue: &commonv1.ModuleSpec_Value_StringValue{
StringValue: "Ted",
},
},
},
}
if err := protocol.Unmarshal(spec, &target); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
if target.Name != "Ted" {
t.Errorf("Expected Name to be 'Ted'")
}
}
func TestUnmarshal_String_Err(t *testing.T) {
target := struct {
Name int
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"name": {
Type: commonv1.ModuleSpec_ValueTypeSingle,
SingleValue: &commonv1.ModuleSpec_Value_StringValue{
StringValue: "Ted",
},
},
},
}
if err := protocol.Unmarshal(spec, &target); err == nil {
t.Errorf("Expected error")
} else {
t.Log(err.Error())
}
}
func TestUnmarshal_String_Slice_Success(t *testing.T) {
target := struct {
Names []string
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"names": {
Type: commonv1.ModuleSpec_ValueTypeStringSlice,
StringValues: []string{"Ted"},
},
},
}
if err := protocol.Unmarshal(spec, &target); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
if len(target.Names) < 1 {
t.Errorf("Expected Names to have at least one element")
} else if target.Names[0] != "Ted" {
t.Errorf("Expected Names[0] to be 'Ted'")
}
}
func TestUnmarshal_String_Slice_Err(t *testing.T) {
target := struct {
Names []int
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"names": {
Type: commonv1.ModuleSpec_ValueTypeStringSlice,
StringValues: []string{"Ted"},
},
},
}
if err := protocol.Unmarshal(spec, &target); err == nil {
t.Errorf("Expected error")
} else {
t.Log(err.Error())
}
}
func TestUnmarshal_Int_Success(t *testing.T) {
target := struct {
Age int
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"age": {
Type: commonv1.ModuleSpec_ValueTypeSingle,
SingleValue: &commonv1.ModuleSpec_Value_IntValue{
IntValue: 42,
},
},
},
}
if err := protocol.Unmarshal(spec, &target); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
if target.Age != 42 {
t.Errorf("Expected Age to be 42")
}
}
func TestUnmarshal_Int_Err(t *testing.T) {
target := struct {
Age string
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"age": {
Type: commonv1.ModuleSpec_ValueTypeSingle,
SingleValue: &commonv1.ModuleSpec_Value_IntValue{
IntValue: 42,
},
},
},
}
if err := protocol.Unmarshal(spec, &target); err == nil {
t.Errorf("Expected error")
} else {
t.Log(err.Error())
}
}
func TestUnmarshal_Int_Slice_Success(t *testing.T) {
target := struct {
Ages []int
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"ages": {
Type: commonv1.ModuleSpec_ValueTypeIntSlice,
IntValues: []int64{42},
},
},
}
if err := protocol.Unmarshal(spec, &target); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
if len(target.Ages) < 1 {
t.Errorf("Expected Ages to have at least one element")
} else if target.Ages[0] != 42 {
t.Errorf("Expected Ages[0] to be 42")
}
}
func TestUnmarshal_Int_Slice_Err(t *testing.T) {
target := struct {
Ages []string
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"ages": {
Type: commonv1.ModuleSpec_ValueTypeIntSlice,
IntValues: []int64{42},
},
},
}
if err := protocol.Unmarshal(spec, &target); err == nil {
t.Errorf("Expected error")
} else {
t.Log(err.Error())
}
}
func TestUnmarshal_Double_Success(t *testing.T) {
target := struct {
Pi float64
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"Pi": {
Type: commonv1.ModuleSpec_ValueTypeSingle,
SingleValue: &commonv1.ModuleSpec_Value_DoubleValue{
DoubleValue: 3.14,
},
},
},
}
if err := protocol.Unmarshal(spec, &target); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
if target.Pi-3.14 > 0.0000001 {
t.Errorf("Expected Pi to be 3.14")
}
}
func TestUnmarshal_Double_Err(t *testing.T) {
target := struct {
Pi string
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"pi": {
Type: commonv1.ModuleSpec_ValueTypeSingle,
SingleValue: &commonv1.ModuleSpec_Value_DoubleValue{
DoubleValue: 3.14,
},
},
},
}
if err := protocol.Unmarshal(spec, &target); err == nil {
t.Errorf("Expected error")
} else {
t.Log(err.Error())
}
}
func TestUnmarshal_Double_Slice_Success(t *testing.T) {
target := struct {
Pis []float64
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"pis": {
Type: commonv1.ModuleSpec_ValueTypeDoubleSlice,
DoubleValues: []float64{3.14},
},
},
}
if err := protocol.Unmarshal(spec, &target); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
if len(target.Pis) < 1 {
t.Errorf("Expected Pis to have at least one element")
} else if target.Pis[0]-3.14 > 0.0000001 {
t.Errorf("Expected Pis[0] to be 3.14")
}
}
func TestUnmarshal_Double_Slice_Err(t *testing.T) {
target := struct {
Pis []string
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"pis": {
Type: commonv1.ModuleSpec_ValueTypeDoubleSlice,
DoubleValues: []float64{3.14},
},
},
}
if err := protocol.Unmarshal(spec, &target); err == nil {
t.Errorf("Expected error")
} else {
t.Log(err.Error())
}
}
func TestUnmarshal_NestedStruct_Success(t *testing.T) {
target := struct {
Address struct {
City string
}
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"address": {
Type: commonv1.ModuleSpec_ValueTypeObject,
ComplexValue: map[string]*commonv1.ModuleSpec_Value{
"city": {
Type: commonv1.ModuleSpec_ValueTypeSingle,
SingleValue: &commonv1.ModuleSpec_Value_StringValue{
StringValue: "New York",
},
},
},
},
},
}
if err := protocol.Unmarshal(spec, &target); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
if target.Address.City != "New York" {
t.Errorf("Expected City to be 'New York'")
}
}
func TestUnmarshal_NestedStructPointer_Success(t *testing.T) {
target := struct {
Address *struct {
City string
}
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"address": {
Type: commonv1.ModuleSpec_ValueTypeObject,
ComplexValue: map[string]*commonv1.ModuleSpec_Value{
"city": {
Type: commonv1.ModuleSpec_ValueTypeSingle,
SingleValue: &commonv1.ModuleSpec_Value_StringValue{
StringValue: "New York",
},
},
},
},
},
}
if err := protocol.Unmarshal(spec, &target); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
if target.Address.City != "New York" {
t.Errorf("Expected City to be 'New York'")
}
}
func TestUnmarshal_Map_Success(t *testing.T) {
target := struct {
Values map[string]string
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"values": {
Type: commonv1.ModuleSpec_ValueTypeMap,
ComplexValue: map[string]*commonv1.ModuleSpec_Value{
"City": {
Type: commonv1.ModuleSpec_ValueTypeSingle,
SingleValue: &commonv1.ModuleSpec_Value_StringValue{
StringValue: "New York",
},
},
},
},
},
}
if err := protocol.Unmarshal(spec, &target); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
if target.Values["City"] != "New York" {
t.Errorf("Expected City to be 'New York'")
}
}
func TestUnmarshal_NestedMap_Success(t *testing.T) {
target := struct {
City struct {
Labels map[string]string
}
}{}
spec := &commonv1.ModuleSpec{
Values: map[string]*commonv1.ModuleSpec_Value{
"city": {
Type: commonv1.ModuleSpec_ValueTypeObject,
ComplexValue: map[string]*commonv1.ModuleSpec_Value{
"labels": {
Type: commonv1.ModuleSpec_ValueTypeMap,
ComplexValue: map[string]*commonv1.ModuleSpec_Value{
"Region": {
Type: commonv1.ModuleSpec_ValueTypeSingle,
SingleValue: &commonv1.ModuleSpec_Value_StringValue{
StringValue: "west",
},
},
},
},
},
},
},
}
if err := protocol.Unmarshal(spec, &target); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
if target.City.Labels["Region"] != "west" {
t.Errorf("Expected 'Region' to be 'west'")
}
}