Debian Codegraph @

callbacks/helper_test.go

raw
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 }
98
99 func TestConvertSliceOfMapToValuesForCreate(t *testing.T) {
100 testCase := []struct {
101 name string
102 input []map[string]interface{}
103 expect clause.Values
104 }{
105 {
106 name: "Test convert slice of string value",
107 input: []map[string]interface{}{
108 {"name": "my name"},
109 },
110 expect: clause.Values{
111 Columns: []clause.Column{{Name: "name"}},
112 Values: [][]interface{}{{"my name"}},
113 },
114 },
115 {
116 name: "Test convert slice of int value",
117 input: []map[string]interface{}{
118 {"age": 18},
119 },
120 expect: clause.Values{
121 Columns: []clause.Column{{Name: "age"}},
122 Values: [][]interface{}{{18}},
123 },
124 },
125 {
126 name: "Test convert slice of float value",
127 input: []map[string]interface{}{
128 {"score": 99.5},
129 },
130 expect: clause.Values{
131 Columns: []clause.Column{{Name: "score"}},
132 Values: [][]interface{}{{99.5}},
133 },
134 },
135 {
136 name: "Test convert slice of bool value",
137 input: []map[string]interface{}{
138 {"active": true},
139 },
140 expect: clause.Values{
141 Columns: []clause.Column{{Name: "active"}},
142 Values: [][]interface{}{{true}},
143 },
144 },
145 }
146
147 for _, tc := range testCase {
148 t.Run(tc.name, func(t *testing.T) {
149 actual := ConvertSliceOfMapToValuesForCreate(&gorm.Statement{}, tc.input)
150
151 if !reflect.DeepEqual(actual, tc.expect) {
152 t.Errorf("expected %v but got %v", tc.expect, actual)
153 }
154 })
155 }
156
157 }