Skip to content

Commit 52404cd

Browse files
authored
CHORE add unittest test function ConvertMapToValueForCreate (#6846)
* CHORE add unittest test function ConvertMapToValueForCreate * CHORE move the test cases located in the files convert_map_test.go and visit_map_test.go into the file helper_test.go.
1 parent d81ae6f commit 52404cd

File tree

2 files changed

+97
-36
lines changed

2 files changed

+97
-36
lines changed

callbacks/helper_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package callbacks
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"gorm.io/gorm"
8+
"gorm.io/gorm/clause"
9+
)
10+
11+
func TestLoadOrStoreVisitMap(t *testing.T) {
12+
var vm visitMap
13+
var loaded bool
14+
type testM struct {
15+
Name string
16+
}
17+
18+
t1 := testM{Name: "t1"}
19+
t2 := testM{Name: "t2"}
20+
t3 := testM{Name: "t3"}
21+
22+
vm = make(visitMap)
23+
if loaded = loadOrStoreVisitMap(&vm, reflect.ValueOf(&t1)); loaded {
24+
t.Fatalf("loaded should be false")
25+
}
26+
27+
if loaded = loadOrStoreVisitMap(&vm, reflect.ValueOf(&t1)); !loaded {
28+
t.Fatalf("loaded should be true")
29+
}
30+
31+
// t1 already exist but t2 not
32+
if loaded = loadOrStoreVisitMap(&vm, reflect.ValueOf([]*testM{&t1, &t2, &t3})); loaded {
33+
t.Fatalf("loaded should be false")
34+
}
35+
36+
if loaded = loadOrStoreVisitMap(&vm, reflect.ValueOf([]*testM{&t2, &t3})); !loaded {
37+
t.Fatalf("loaded should be true")
38+
}
39+
}
40+
41+
func TestConvertMapToValuesForCreate(t *testing.T) {
42+
testCase := []struct {
43+
name string
44+
input map[string]interface{}
45+
expect clause.Values
46+
}{
47+
{
48+
name: "Test convert string value",
49+
input: map[string]interface{}{
50+
"name": "my name",
51+
},
52+
expect: clause.Values{
53+
Columns: []clause.Column{{Name: "name"}},
54+
Values: [][]interface{}{{"my name"}},
55+
},
56+
},
57+
{
58+
name: "Test convert int value",
59+
input: map[string]interface{}{
60+
"age": 18,
61+
},
62+
expect: clause.Values{
63+
Columns: []clause.Column{{Name: "age"}},
64+
Values: [][]interface{}{{18}},
65+
},
66+
},
67+
{
68+
name: "Test convert float value",
69+
input: map[string]interface{}{
70+
"score": 99.5,
71+
},
72+
expect: clause.Values{
73+
Columns: []clause.Column{{Name: "score"}},
74+
Values: [][]interface{}{{99.5}},
75+
},
76+
},
77+
{
78+
name: "Test convert bool value",
79+
input: map[string]interface{}{
80+
"active": true,
81+
},
82+
expect: clause.Values{
83+
Columns: []clause.Column{{Name: "active"}},
84+
Values: [][]interface{}{{true}},
85+
},
86+
},
87+
}
88+
89+
for _, tc := range testCase {
90+
t.Run(tc.name, func(t *testing.T) {
91+
actual := ConvertMapToValuesForCreate(&gorm.Statement{}, tc.input)
92+
if !reflect.DeepEqual(actual, tc.expect) {
93+
t.Errorf("expect %v got %v", tc.expect, actual)
94+
}
95+
})
96+
}
97+
}

callbacks/visit_map_test.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)