-
Notifications
You must be signed in to change notification settings - Fork 7
/
select_test.go
130 lines (122 loc) · 4.97 KB
/
select_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package goqux_test
import (
"testing"
"github.com/doug-martin/goqu/v9"
"github.com/roneli/goqux"
"github.com/stretchr/testify/assert"
)
type selectModel struct {
IntField int
invisibleField bool
fieldToSkip int `goqux:"skip_select"`
}
type joinModel struct {
T2 selectModel `db:"select_models"`
Table1 *Table1
}
type Table1 struct {
IntField int
StringField string `db:"cool_field"`
}
type doubleJoinModel struct {
T2 selectModel `db:"select_models"`
Table1 *Table1
T3 Table2 `db:"table_2"`
}
type Table2 struct {
IntField int
StringField string `db:"cool_field"`
}
func TestBuildSelect(t *testing.T) {
tableTests := []struct {
name string
dst interface{}
options []goqux.SelectOption
expectedQuery string
expectedArgs []interface{}
expectedError error
}{
{
name: "simple_select",
dst: selectModel{},
expectedQuery: `SELECT "select_models"."int_field" FROM "select_models"`,
expectedArgs: []interface{}{},
expectedError: nil,
},
{
name: "select_with_filters",
dst: selectModel{},
options: []goqux.SelectOption{goqux.WithSelectFilters(goqux.Column("select_models", "int_field").Eq(1))},
expectedQuery: `SELECT "select_models"."int_field" FROM "select_models" WHERE ("select_models"."int_field" = $1)`,
expectedArgs: []interface{}{int64(1)},
},
{
name: "select_with_limit",
dst: selectModel{},
options: []goqux.SelectOption{goqux.WithSelectLimit(1)},
expectedQuery: `SELECT "select_models"."int_field" FROM "select_models" LIMIT $1`,
expectedArgs: []interface{}{int64(1)},
},
{
name: "select_with_offset",
dst: selectModel{},
options: []goqux.SelectOption{goqux.WithSelectOffset(1)},
expectedQuery: `SELECT "select_models"."int_field" FROM "select_models" OFFSET $1`,
expectedArgs: []interface{}{int64(1)},
},
{
name: "select_with_dialect",
dst: selectModel{},
options: []goqux.SelectOption{goqux.WithSelectDialect("postgres"), goqux.WithSelectOffset(1)},
expectedQuery: `SELECT "select_models"."int_field" FROM "select_models" OFFSET $1`,
expectedArgs: []interface{}{int64(1)},
},
{
name: "select_with_order",
dst: selectModel{},
options: []goqux.SelectOption{goqux.WithSelectOrder(goqux.Column("select_models", "int_field").Desc())},
expectedQuery: `SELECT "select_models"."int_field" FROM "select_models" ORDER BY "select_models"."int_field" DESC`,
expectedArgs: []interface{}{},
},
{
name: "select_with_inner_join_selection",
dst: joinModel{},
options: []goqux.SelectOption{goqux.WithInnerJoinSelection[joinModel](goqux.JoinOp{
Table: "table_1",
On: goqu.On(goqux.Column("table_1", "int_field").Eq(goqux.Column("select_models", "int_field"))),
})},
expectedQuery: `SELECT "select_models"."int_field" AS "select_models.int_field", "table_1"."int_field" AS "table_1.int_field", "table_1"."cool_field" AS "table_1.cool_field" FROM "select_models" INNER JOIN "table_1" ON ("table_1"."int_field" = "select_models"."int_field")`,
},
{
name: "select_with_left_selection",
dst: joinModel{},
options: []goqux.SelectOption{goqux.WithLeftJoinSelection[joinModel](goqux.JoinOp{
Table: "table_1",
On: goqu.On(goqux.Column("table_1", "int_field").Eq(goqux.Column("select_models", "int_field"))),
})},
expectedQuery: `SELECT "select_models"."int_field" AS "select_models.int_field", "table_1"."int_field" AS "table_1.int_field", "table_1"."cool_field" AS "table_1.cool_field" FROM "select_models" LEFT JOIN "table_1" ON ("table_1"."int_field" = "select_models"."int_field")`,
},
{
name: "select_with_double_join_selection",
dst: doubleJoinModel{},
options: []goqux.SelectOption{goqux.WithInnerJoinSelection[doubleJoinModel](goqux.JoinOp{
Table: "table_1",
On: goqu.On(goqux.Column("table_1", "int_field").Eq(goqux.Column("select_models", "int_field"))),
}, goqux.JoinOp{
Table: "table_2",
On: goqu.On(goqux.Column("table_2", "int_field").Eq(goqux.Column("select_models", "int_field"))),
})},
expectedQuery: `SELECT "select_models"."int_field" AS "select_models.int_field", "table_1"."int_field" AS "table_1.int_field", "table_1"."cool_field" AS "table_1.cool_field", "table_2"."int_field" AS "table_2.int_field", "table_2"."cool_field" AS "table_2.cool_field" FROM "select_models" INNER JOIN "table_1" ON ("table_1"."int_field" = "select_models"."int_field") INNER JOIN "table_2" ON ("table_2"."int_field" = "select_models"."int_field")`,
},
}
for _, tableTest := range tableTests {
t.Run(tableTest.name, func(t *testing.T) {
query, args, err := goqux.BuildSelect("select_models", tableTest.dst, tableTest.options...)
if tableTest.expectedError != nil {
assert.ErrorIs(t, tableTest.expectedError, err)
}
assert.Equal(t, tableTest.expectedQuery, query)
assert.ElementsMatch(t, tableTest.expectedArgs, args)
})
}
}