Files
b2b/app/utils/mapper/mapper_test.go
2026-03-10 09:02:57 +01:00

79 lines
1.6 KiB
Go

package mapper_test
import (
"testing"
"git.ma-al.com/goc_marek/timetracker/app/utils/mapper"
)
// --- example structs ---
type UserInput struct {
Name string
Email string
Age int
}
type UserRecord struct {
Name string
Email string
Age int
CreatedAt string // not in src → stays ""
Active bool // not in src → stays false
}
// --- tests ---
func TestMap_MatchingFields(t *testing.T) {
src := &UserInput{Name: "Alice", Email: "alice@example.com", Age: 30}
dst := &UserRecord{}
if err := mapper.Map(dst, src); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if dst.Name != "Alice" {
t.Errorf("Name: want Alice, got %s", dst.Name)
}
if dst.Email != "alice@example.com" {
t.Errorf("Email: want alice@example.com, got %s", dst.Email)
}
if dst.Age != 30 {
t.Errorf("Age: want 30, got %d", dst.Age)
}
}
func TestMap_UnmatchedFieldsAreZero(t *testing.T) {
src := &UserInput{Name: "Bob"}
dst := &UserRecord{}
mapper.MustMap(dst, src)
if dst.CreatedAt != "" {
t.Errorf("CreatedAt: expected empty string, got %q", dst.CreatedAt)
}
if dst.Active != false {
t.Errorf("Active: expected false, got %v", dst.Active)
}
}
func TestMap_TypeConversion(t *testing.T) {
type Src struct{ Score int32 }
type Dst struct{ Score int64 }
src := &Src{Score: 99}
dst := &Dst{}
mapper.MustMap(dst, src)
if dst.Score != 99 {
t.Errorf("Score: want 99, got %d", dst.Score)
}
}
func TestMap_InvalidInput(t *testing.T) {
if err := mapper.Map("not a struct", 42); err == nil {
t.Error("expected error for non-struct inputs")
}
}