Debian Codegraph @

struct_test.go

raw symbols SCIP
1 // Copyright 2016 Qiang Xue. All rights reserved.
2 // Use of this source code is governed by a MIT-style
3 // license that can be found in the LICENSE file.
4
5 package dbx
6
7 import (
8 "database/sql"
9 "reflect"
10 "testing"
11
12 "github.com/stretchr/testify/assert"
13 )
14
15 func TestDefaultFieldMapFunc(t *testing.T) {
16 tests := []struct {
17 input, output string
18 }{
19 {"Name", "name"},
20 {"FirstName", "first_name"},
21 {"Name0", "name0"},
22 {"ID", "id"},
23 {"UserID", "user_id"},
24 {"User0ID", "user0_id"},
25 {"MyURL", "my_url"},
26 {"URLPath", "urlpath"},
27 {"MyURLPath", "my_urlpath"},
28 {"First_Name", "first_name"},
29 {"first_name", "first_name"},
30 {"_FirstName", "_first_name"},
31 {"_First_Name", "_first_name"},
32 }
33 for _, test := range tests {
34 r := DefaultFieldMapFunc(test.input)
35 assert.Equal(t, test.output, r, test.input)
36 }
37 }
38
39 func Test_concat(t *testing.T) {
40 assert.Equal(t, "a.b", concat("a", "b"))
41 assert.Equal(t, "a", concat("a", ""))
42 assert.Equal(t, "b", concat("", "b"))
43 }
44
45 func Test_parseTag(t *testing.T) {
46 name, pk := parseTag("abc")
47 assert.Equal(t, "abc", name)
48 assert.False(t, pk)
49
50 name, pk = parseTag("pk,abc")
51 assert.Equal(t, "abc", name)
52 assert.True(t, pk)
53
54 name, pk = parseTag("pk")
55 assert.Equal(t, "", name)
56 assert.True(t, pk)
57 }
58
59 func Test_indirect(t *testing.T) {
60 var a int
61 assert.Equal(t, reflect.ValueOf(a).Kind(), indirect(reflect.ValueOf(a)).Kind())
62 var b *int
63 bi := indirect(reflect.ValueOf(&b))
64 assert.Equal(t, reflect.ValueOf(a).Kind(), bi.Kind())
65 if assert.NotNil(t, b) {
66 assert.Equal(t, 0, *b)
67 }
68 }
69
70 func Test_structValue_columns(t *testing.T) {
71 customer := Customer{
72 ID: 1,
73 Name: "abc",
74 Status: 2,
75 Email: "[email protected]",
76 }
77 sv := newStructValue(&customer, DefaultFieldMapFunc, GetTableName)
78 cols := sv.columns(nil, nil)
79 assert.Equal(t, map[string]interface{}{"id": 1, "name": "abc", "status": 2, "email": "[email protected]", "address": sql.NullString{}}, cols)
80
81 cols = sv.columns([]string{"ID", "name"}, nil)
82 assert.Equal(t, map[string]interface{}{"id": 1}, cols)
83
84 cols = sv.columns([]string{"ID", "Name"}, []string{"ID"})
85 assert.Equal(t, map[string]interface{}{"name": "abc"}, cols)
86
87 cols = sv.columns(nil, []string{"ID", "Address"})
88 assert.Equal(t, map[string]interface{}{"name": "abc", "status": 2, "email": "[email protected]"}, cols)
89
90 sv = newStructValue(&customer, nil, GetTableName)
91 cols = sv.columns([]string{"ID", "Name"}, []string{"ID"})
92 assert.Equal(t, map[string]interface{}{"Name": "abc"}, cols)
93 }
94
95 func TestIssue37(t *testing.T) {
96 customer := Customer{
97 ID: 1,
98 Name: "abc",
99 Status: 2,
100 Email: "[email protected]",
101 }
102 ev := struct {
103 Customer
104 Status string
105 }{customer, "20"}
106 sv := newStructValue(&ev, nil, GetTableName)
107 cols := sv.columns([]string{"ID", "Status"}, nil)
108 assert.Equal(t, map[string]interface{}{"ID": 1, "Status": "20"}, cols)
109
110 ev2 := struct {
111 Status string
112 Customer
113 }{"20", customer}
114 sv = newStructValue(&ev2, nil, GetTableName)
115 cols = sv.columns([]string{"ID", "Status"}, nil)
116 assert.Equal(t, map[string]interface{}{"ID": 1, "Status": "20"}, cols)
117 }
118
119 type MyCustomer struct{}
120
121 func TestGetTableName(t *testing.T) {
122 var c1 Customer
123 assert.Equal(t, "customer", GetTableName(c1))
124
125 var c2 *Customer
126 assert.Equal(t, "customer", GetTableName(c2))
127
128 var c3 MyCustomer
129 assert.Equal(t, "my_customer", GetTableName(c3))
130
131 var c4 []Customer
132 assert.Equal(t, "customer", GetTableName(c4))
133
134 var c5 *[]Customer
135 assert.Equal(t, "customer", GetTableName(c5))
136
137 var c6 []MyCustomer
138 assert.Equal(t, "my_customer", GetTableName(c6))
139
140 var c7 []CustomerPtr
141 assert.Equal(t, "customer", GetTableName(c7))
142
143 var c8 **int
144 assert.Equal(t, "", GetTableName(c8))
145 }
146
147 type FA struct {
148 A1 string
149 A2 int
150 }
151
152 type FB struct {
153 B1 string
154 }