-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
identifier_test.go
55 lines (49 loc) · 2.25 KB
/
identifier_test.go
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
package caches
import (
"testing"
"gorm.io/gorm"
)
func Test_buildIdentifier(t *testing.T) {
db := &gorm.DB{}
db.Statement = &gorm.Statement{}
db.Statement.SQL.WriteString("TEST-SQL")
db.Statement.Vars = append(db.Statement.Vars, "test", 123, 12.3, true, false, []string{"test", "me"})
actual := buildIdentifier(db)
expected := "gorm-caches::TEST-SQL-[test 123 12.3 true false [test me]]"
if actual != expected {
t.Errorf("buildIdentifier expected to return `%s` but got `%s`", expected, actual)
}
}
func Test_sliceToString(t *testing.T) {
expected := "[test-val test-val 1 1 true true [test-val] [1] [true] [test-val] [1] [true] [test-val] [1] [true] [test-val] [1] [true] {test-val: test-val} {1: 1} {true: true} {test-val: test-val} {1: 1} {true: true} {test-val: test-val} {1: 1} {true: true} {test-val: test-val} {1: 1} {true: true}]"
strVal := "test-val"
intVal := 1
boolVal := true
sliceOfStr := []string{strVal}
sliceOfInt := []int{intVal}
sliceOfBool := []bool{boolVal}
sliceOfPointerStr := []*string{&strVal}
sliceOfPointerInt := []*int{&intVal}
sliceOfPointerBool := []*bool{&boolVal}
mapOfStr := map[string]string{strVal: strVal}
mapOfInt := map[int]int{intVal: intVal}
mapOfBool := map[bool]bool{boolVal: boolVal}
mapOfPointerStr := map[*string]*string{&strVal: &strVal}
mapOfPointerInt := map[*int]*int{&intVal: &intVal}
mapOfPointerBool := map[*bool]*bool{&boolVal: &boolVal}
actual := valueToString([]interface{}{
strVal, &strVal, intVal, // Primitives
&intVal, boolVal, &boolVal, // Pointer passed primitives
sliceOfStr, sliceOfInt, sliceOfBool, // Slices of primitives
&sliceOfStr, &sliceOfInt, &sliceOfBool, // Pointer passed slices of primitives
sliceOfPointerStr, sliceOfPointerInt, sliceOfPointerBool, // Slices of pointer primitives
&sliceOfPointerStr, &sliceOfPointerInt, &sliceOfPointerBool, // Pointer passed slices of pointer primitives
mapOfStr, mapOfInt, mapOfBool, // Map of primitives
mapOfPointerStr, mapOfPointerInt, mapOfPointerBool, // Map of pointer primitives
&mapOfStr, &mapOfInt, &mapOfBool, // Map of primitives
&mapOfPointerStr, &mapOfPointerInt, &mapOfPointerBool, // Map of pointer primitives
})
if expected != actual {
t.Errorf("sliceToString expected to return `%s` but got `%s`", expected, actual)
}
}