Files
repo-pattern/app/lib/helpers/util_test.go
Eden Kirin 21dcabe180 Initial
2024-06-18 22:01:31 +02:00

71 lines
1.5 KiB
Go

package helpers
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_StructToMap(t *testing.T) {
t.Run("Convert simple struct to map", func(t *testing.T) {
type User struct {
Name string `json:"name"`
Age int `json:"age"`
Height float32 `json:"height"`
}
user := &User{
Name: "Pero",
Age: 66,
Height: 187.6,
}
result, _ := StructToMap(user)
expected := make(map[string]interface{})
expected["name"] = "Pero"
expected["age"] = 66
expected["height"] = 187.6
assert.Equal(t, fmt.Sprintf("%v", result), fmt.Sprintf("%v", expected))
})
t.Run("Convert nested struct to map", func(t *testing.T) {
type Address struct {
City string `json:"city"`
PostalCode int `json:"postalCode"`
}
type User struct {
Name string `json:"name"`
Age int `json:"age"`
Height float32 `json:"height"`
Address Address `json:"address"`
}
user := &User{
Name: "Pero",
Age: 66,
Height: 187.6,
Address: Address{
City: "Zagreb",
PostalCode: 10020,
},
}
result, _ := StructToMap(user)
address := make(map[string]interface{})
address["city"] = "Zagreb"
address["postalCode"] = 10020
expected := make(map[string]interface{})
expected["name"] = "Pero"
expected["age"] = 66
expected["height"] = 187.6
expected["address"] = make(map[string]interface{})
expected["address"] = address
assert.Equal(t, fmt.Sprintf("%v", result), fmt.Sprintf("%v", expected))
})
}