-
Notifications
You must be signed in to change notification settings - Fork 3
/
query_sql_insert_test.go
85 lines (75 loc) · 1.72 KB
/
query_sql_insert_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
package lore
import (
"fmt"
"testing"
)
func TestBuildSqlInsertColumnsAndValues(t *testing.T) {
SetConfigDefault()
tm := createTestModelInstance()
tmTestValueField := tm.Field*2 + 1
// Build query and SQL.
q := NewQuery(tm)
q.SetSqlBuilder(
q.BuildSqlInsertColumnsAndValues(
[]string{_TEST_DB_FIELDNAME_FIELD},
[]interface{}{tmTestValueField},
).Suffix(RETURNING_STAR),
)
// Delegate to SQL test helper.
expectedSql := fmt.Sprintf(
"INSERT INTO %s (%s) VALUES ($1) RETURNING *",
_TEST_DB_TABLENAME,
_TEST_DB_FIELDNAME_FIELD,
)
expectedArgs := []interface{}{tmTestValueField}
err := testBuildSqlHelper(q, expectedSql, expectedArgs)
if err != nil {
t.Error(err)
return
}
}
func TestBuildSqlInsertModel(t *testing.T) {
SetConfigDefault()
tm := createTestModelInstance()
tmTestValueField := tm.Field*2 + 1
tm.Field = tmTestValueField
// Build query and SQL.
q := NewQuery(tm)
qSqlBuilder, err := q.BuildSqlInsertModel()
if err != nil {
t.Error(err)
return
}
q.SetSqlBuilder(
qSqlBuilder.Suffix(RETURNING_STAR),
)
// Delegate to SQL test helper.
err = testBuildSqlHelper(
q,
fmt.Sprintf(
"INSERT INTO tests (%s) VALUES ($1) RETURNING *",
_TEST_DB_FIELDNAME_FIELD,
),
[]interface{}{tm.Field},
)
if err != nil {
t.Error(err)
return
}
}
func TestBuildSqlInsertModelInvalid(t *testing.T) {
SetConfigDefault()
tm := createTestModelInvalidInstance()
// Ensure that nil DbFieldMap for this invalid instance.
if tm.DbFieldMap() != nil {
t.Error("Expected nil db field map for testing")
return
}
// Expect error since nil db field map.
q := NewQuery(tm)
_, err := q.BuildSqlInsertModel()
if err == nil {
t.Error("Expected non-nil err since nil DbFieldMap", err)
return
}
}