Files
Gumble-Backend/internal/test/helper_mappers_test.go
Zane Walker 7e940c83a7
Some checks failed
Build & Test / build-test (push) Has been cancelled
Build & Test / swagger-codegen-cli (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
(Feat): Initial Commit
2026-07-03 19:41:31 +05:30

82 lines
1.6 KiB
Go

package test_test
import (
"testing"
"allaboutapps.dev/aw/go-starter/internal/test"
"github.com/go-openapi/swag"
"github.com/stretchr/testify/assert"
)
func TestGetMapFromStruct(t *testing.T) {
type tmp struct {
A string
B int
C interface{}
D float32
E bool
F *string
G *int
}
data := tmp{
A: "string",
B: 1,
C: tmp{
A: "string2",
},
D: 2.3,
E: true,
F: swag.String("stringPtr"),
G: swag.Int(5),
}
xMap := test.GetMapFromStruct(data)
assert.Len(t, xMap, 7)
assert.Equal(t, "string", xMap["A"])
assert.Equal(t, "1", xMap["B"])
assert.Contains(t, xMap["C"], "string2")
assert.Equal(t, "2.3", xMap["D"])
assert.Equal(t, "true", xMap["E"])
assert.Equal(t, "stringPtr", xMap["F"])
assert.Equal(t, "5", xMap["G"])
}
func TestGetMapFromStructByTag(t *testing.T) {
type tmp struct {
A string `x:"1,omitempty" y:"2"`
B int `x:"3"`
C interface{} `x:"12"`
D float32 `x:"2"`
E bool `x:"5"`
F *string `x:"4"`
G *int `x:"6"`
}
data := tmp{
A: "string",
B: 1,
C: tmp{
A: "string2",
},
D: 2.3,
E: true,
F: swag.String("stringPtr"),
G: swag.Int(5),
}
xMap := test.GetMapFromStructByTag("x", data)
assert.Len(t, xMap, 7)
assert.Equal(t, "string", xMap["1"])
assert.Equal(t, "1", xMap["3"])
assert.Contains(t, xMap["12"], "string2")
assert.Equal(t, "2.3", xMap["2"])
assert.Equal(t, "true", xMap["5"])
assert.Equal(t, "stringPtr", xMap["4"])
assert.Equal(t, "5", xMap["6"])
yMap := test.GetMapFromStructByTag("y", data)
assert.Len(t, yMap, 1)
assert.Equal(t, "string", yMap["2"])
}