1 package exp_test
2
3 import (
4 "testing"
5
6 "github.com/doug-martin/goqu/v9/exp"
7 "github.com/stretchr/testify/suite"
8 )
9
10 type updateExpressionTestSuite struct {
11 suite.Suite
12 }
13
14 func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withInvalidValue() {
15 _, err := exp.NewUpdateExpressions(true)
16 uets.EqualError(err, "goqu: unsupported update interface type bool")
17 }
18
19 func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withRecords() {
20 ie, err := exp.NewUpdateExpressions(exp.Record{"c": "a", "b": "d"})
21 uets.NoError(err)
22 eie := []exp.UpdateExpression{
23 exp.NewIdentifierExpression("", "", "b").Set("d"),
24 exp.NewIdentifierExpression("", "", "c").Set("a"),
25 }
26 uets.Equal(eie, ie)
27 }
28
29 func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withMap() {
30 ie, err := exp.NewUpdateExpressions(map[string]interface{}{"c": "a", "b": "d"})
31 uets.NoError(err)
32 eie := []exp.UpdateExpression{
33 exp.NewIdentifierExpression("", "", "b").Set("d"),
34 exp.NewIdentifierExpression("", "", "c").Set("a"),
35 }
36 uets.Equal(eie, ie)
37 }
38
39 func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withStructs() {
40 type testRecord struct {
41 C string `db:"c"`
42 B string `db:"b"`
43 }
44 ie, err := exp.NewUpdateExpressions(testRecord{C: "a", B: "d"})
45 uets.NoError(err)
46 eie := []exp.UpdateExpression{
47 exp.NewIdentifierExpression("", "", "b").Set("d"),
48 exp.NewIdentifierExpression("", "", "c").Set("a"),
49 }
50 uets.Equal(eie, ie)
51 }
52
53 func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withStructsWithoutTags() {
54 type testRecord struct {
55 FieldA int64
56 FieldB bool
57 FieldC string
58 }
59 ie, err := exp.NewUpdateExpressions(testRecord{FieldA: 1, FieldB: true, FieldC: "a"})
60 uets.NoError(err)
61 eie := []exp.UpdateExpression{
62 exp.NewIdentifierExpression("", "", "fielda").Set(int64(1)),
63 exp.NewIdentifierExpression("", "", "fieldb").Set(true),
64 exp.NewIdentifierExpression("", "", "fieldc").Set("a"),
65 }
66 uets.Equal(eie, ie)
67 }
68
69 func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withStructsIgnoredDbTag() {
70 type testRecord struct {
71 FieldA int64 `db:"-"`
72 FieldB bool
73 FieldC string
74 }
75 ie, err := exp.NewUpdateExpressions(testRecord{FieldA: 1, FieldB: true, FieldC: "a"})
76 uets.NoError(err)
77 eie := []exp.UpdateExpression{
78 exp.NewIdentifierExpression("", "", "fieldb").Set(true),
79 exp.NewIdentifierExpression("", "", "fieldc").Set("a"),
80 }
81 uets.Equal(eie, ie)
82 }
83
84 func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withStructsWithGoquSkipUpdate() {
85 type testRecord struct {
86 FieldA int64
87 FieldB bool `goqu:"skipupdate"`
88 FieldC string `goqu:"skipinsert"`
89 }
90 ie, err := exp.NewUpdateExpressions(testRecord{FieldA: 1, FieldB: true, FieldC: "a"})
91 uets.NoError(err)
92 eie := []exp.UpdateExpression{
93 exp.NewIdentifierExpression("", "", "fielda").Set(int64(1)),
94 exp.NewIdentifierExpression("", "", "fieldc").Set("a"),
95 }
96 uets.Equal(eie, ie)
97 }
98
99 func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withStructPointers() {
100 type testRecord struct {
101 C string `db:"c"`
102 B string `db:"b"`
103 }
104 ie, err := exp.NewUpdateExpressions(&testRecord{C: "a", B: "d"})
105 uets.NoError(err)
106 eie := []exp.UpdateExpression{
107 exp.NewIdentifierExpression("", "", "b").Set("d"),
108 exp.NewIdentifierExpression("", "", "c").Set("a"),
109 }
110 uets.Equal(eie, ie)
111 }
112
113 func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withStructsWithEmbeddedStructs() {
114 type Phone struct {
115 Primary string `db:"primary_phone"`
116 Home string `db:"home_phone"`
117 }
118 type item struct {
119 Phone
120 Address string `db:"address"`
121 Name string `db:"name"`
122 }
123 ie, err := exp.NewUpdateExpressions(
124 item{Address: "111 Test Addr", Name: "Test1", Phone: Phone{Home: "123123", Primary: "456456"}},
125 )
126 uets.NoError(err)
127 eie := []exp.UpdateExpression{
128 exp.NewIdentifierExpression("", "", "address").Set("111 Test Addr"),
129 exp.NewIdentifierExpression("", "", "home_phone").Set("123123"),
130 exp.NewIdentifierExpression("", "", "name").Set("Test1"),
131 exp.NewIdentifierExpression("", "", "primary_phone").Set("456456"),
132 }
133 uets.Equal(eie, ie)
134 }
135
136 func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withStructsWithEmbeddedStructPointers() {
137 type Phone struct {
138 Primary string `db:"primary_phone"`
139 Home string `db:"home_phone"`
140 }
141 type item struct {
142 *Phone
143 Address string `db:"address"`
144 Name string `db:"name"`
145 }
146 ie, err := exp.NewUpdateExpressions(
147 item{Address: "111 Test Addr", Name: "Test1", Phone: &Phone{Home: "123123", Primary: "456456"}},
148 )
149 uets.NoError(err)
150 eie := []exp.UpdateExpression{
151 exp.NewIdentifierExpression("", "", "address").Set("111 Test Addr"),
152 exp.NewIdentifierExpression("", "", "home_phone").Set("123123"),
153 exp.NewIdentifierExpression("", "", "name").Set("Test1"),
154 exp.NewIdentifierExpression("", "", "primary_phone").Set("456456"),
155 }
156 uets.Equal(eie, ie)
157 }
158
159 func (uets *updateExpressionTestSuite) TestNewUpdateExpressions_withNilEmbeddedStructPointers() {
160 type Phone struct {
161 Primary string `db:"primary_phone"`
162 Home string `db:"home_phone"`
163 }
164 type item struct {
165 *Phone
166 Address string `db:"address"`
167 Name string `db:"name"`
168 }
169 ie, err := exp.NewUpdateExpressions(
170 item{Address: "111 Test Addr", Name: "Test1"},
171 )
172 uets.NoError(err)
173 eie := []exp.UpdateExpression{
174 exp.NewIdentifierExpression("", "", "address").Set("111 Test Addr"),
175 exp.NewIdentifierExpression("", "", "name").Set("Test1"),
176 }
177 uets.Equal(eie, ie)
178 }
179
180 func TestUpdateExpressionSuite(t *testing.T) {
181 suite.Run(t, new(updateExpressionTestSuite))
182 }
183
View as plain text