Core

/app/util/parse_test.go (4.7 KB)

  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package util_test

import (
"testing"
"time"

"github.com/google/uuid"

"{{{ .Package }}}/app/util"
)

type parseTest[T any] struct {
name string
input any
path string
allowEmpty bool
want T
wantErr bool
}

func TestParseBool(t *testing.T) {
t.Parallel()
tests := []parseTest[bool]{
{"valid bool true", true, "test", false, true, false},
{"valid bool false", false, "test", false, false, false},
{"valid string true", "true", "test", false, true, false},
{"valid string false", "false", "test", false, false, false},
{"nil with allowEmpty", nil, "test", true, false, false},
{"nil without allowEmpty", nil, "test", false, false, true},
{"invalid type", 123, "test", false, false, true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := util.ParseBool(tt.input, tt.path, tt.allowEmpty)
if (err != nil) != tt.wantErr {
t.Errorf("ParseBool() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ParseBool() = %v, want %v", got, tt.want)
}
})
}
}

func TestParseInt64(t *testing.T) {
t.Parallel()
tests := []parseTest[int64]{
{"valid int", 42, "test", false, 42, false},
{"valid int32", int32(42), "test", false, 42, false},
{"valid int64", int64(42), "test", false, 42, false},
{"valid float64", 42.0, "test", false, 42, false},
{"valid string", "42", "test", false, 42, false},
{"invalid string", "not a number", "test", false, 0, true},
{"nil with allowEmpty", nil, "test", true, 0, false},
{"nil without allowEmpty", nil, "test", false, 0, true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := util.ParseInt64(tt.input, tt.path, tt.allowEmpty)
if (err != nil) != tt.wantErr {
t.Errorf("ParseInt64() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ParseInt64() = %v, want %v", got, tt.want)
}
})
}
}

func TestParseTime(t *testing.T) {
t.Parallel()
now := util.TimeCurrent()
tests := []parseTest[*time.Time]{
{"valid time", now, "test", false, &now, false},
{"valid pointer", &now, "test", false, &now, false},
{"invalid string", "not a time", "test", false, nil, true},
{"nil with allowEmpty", nil, "test", true, nil, false},
{"nil without allowEmpty", nil, "test", false, nil, true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := util.ParseTime(tt.input, tt.path, tt.allowEmpty)
if (err != nil) != tt.wantErr {
t.Errorf("ParseTime() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && got != nil && tt.want != nil {
if !got.Equal(*tt.want) {
t.Errorf("ParseTime() = %v, want %v", got, tt.want)
}
}
})
}
}

func TestParseUUID(t *testing.T) {
t.Parallel()
validUUID := uuid.New()
tests := []parseTest[*uuid.UUID]{
{"valid UUID", validUUID, "test", false, &validUUID, false},
{"valid UUID pointer", &validUUID, "test", false, &validUUID, false},
{"valid string", validUUID.String(), "test", false, &validUUID, false},
{"invalid string", "not-a-uuid", "test", false, nil, true},
{"nil with allowEmpty", nil, "test", true, nil, false},
{"nil without allowEmpty", nil, "test", false, nil, true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := util.ParseUUID(tt.input, tt.path, tt.allowEmpty)
if (err != nil) != tt.wantErr {
t.Errorf("ParseUUID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && got != nil && tt.want != nil {
if *got != *tt.want {
t.Errorf("ParseUUID() = %v, want %v", got, tt.want)
}
}
})
}
}

func TestParseMap(t *testing.T) {
t.Parallel()
validMap := util.ValueMap{"key": "value"}
tests := []parseTest[util.ValueMap]{
{"valid map", validMap, "test", false, validMap, false},
{"empty map not allowed", util.ValueMap{}, "test", false, nil, true},
{"empty map allowed", util.ValueMap{}, "test", true, util.ValueMap{}, false},
{"valid JSON string", `{"key":"value"}`, "test", false, validMap, false},
{"invalid JSON string", "not-json", "test", false, nil, true},
{"nil with allowEmpty", nil, "test", true, nil, false},
{"nil without allowEmpty", nil, "test", false, nil, true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := util.ParseMap(tt.input, tt.path, tt.allowEmpty)
if (err != nil) != tt.wantErr {
t.Errorf("ParseMap() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if len(got) != len(tt.want) {
t.Errorf("ParseMap() = %v, want %v", got, tt.want)
}
}
})
}
}