-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquery_sql_update_test.go
97 lines (86 loc) · 1.97 KB
/
query_sql_update_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package lore
import (
"fmt"
"testing"
"github.com/Masterminds/squirrel"
)
func TestBuildSqlUpdate(t *testing.T) {
SetConfigDefault()
tm := createTestModelInstance()
tmTestValueId := 1
tmTestValueField := tm.Field*2 + 1
// Build query and SQL.
q := NewQuery(tm)
q.SetSqlBuilder(
q.BuildSqlUpdate().
Set(_TEST_DB_FIELDNAME_FIELD, tmTestValueField).
Where(squirrel.Eq{
_TEST_DB_FIELDNAME_ID: tmTestValueId,
}).
Suffix(RETURNING_STAR),
)
// Delegate to SQL test helper.
err := testBuildSqlHelper(
q,
fmt.Sprintf(
"UPDATE %s SET %s = $1 WHERE %s = $2 RETURNING *",
_TEST_DB_TABLENAME,
_TEST_DB_FIELDNAME_FIELD,
_TEST_DB_FIELDNAME_ID,
),
[]interface{}{tmTestValueField, tmTestValueId},
)
if err != nil {
t.Error(err)
return
}
}
func TestBuildSqlUpdateModelByPrimaryKey(t *testing.T) {
SetConfigDefault()
tm := createTestModelInstance()
tmTestValueId := 1
tmTestValueField := tm.Field*2 + 1
tm.Id = tmTestValueId
tm.Field = tmTestValueField
// Build query and SQL.
q := NewQuery(tm)
qSqlBuilder, err := q.BuildSqlUpdateModelByPrimaryKey()
if err != nil {
t.Error(err)
return
}
q.SetSqlBuilder(
qSqlBuilder.Suffix(RETURNING_STAR),
)
// Delegate to SQL test helper.
err = testBuildSqlHelper(
q,
fmt.Sprintf(
"UPDATE %s SET %s = $1 WHERE %s = $2 RETURNING *",
_TEST_DB_TABLENAME,
_TEST_DB_FIELDNAME_FIELD,
_TEST_DB_FIELDNAME_ID,
),
[]interface{}{tm.Field, tm.Id},
)
if err != nil {
t.Error(err)
return
}
}
func TestBuildSqlUpdateModelByPrimaryKeyInvalid(t *testing.T) {
SetConfigDefault()
tm := createTestModelInvalidInstance()
// Ensure that primary key is empty for this invalid instance.
if tm.DbPrimaryFieldKey() != "" {
t.Error("Expected empty primary field key for testing")
return
}
// Expect error since empty primary key.
q := NewQuery(tm)
_, err := q.BuildSqlUpdateModelByPrimaryKey()
if err == nil {
t.Error("Expected non-nil err since invalid empty primary key", err)
return
}
}