-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
typed_repository_test.go
157 lines (146 loc) · 3.38 KB
/
typed_repository_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package qry_test
import (
"context"
"github.com/DATA-DOG/go-sqlmock"
"github.com/TomWright/qry"
"testing"
)
type model struct {
ID int64
Name string
}
func TestTypedRepository_QueryRow(t *testing.T) {
type def struct {
name string
table string
selectFields []qry.Field
query func(*qry.TypedSelectQuery[model])
exp model
mockFn func(db sqlmock.Sqlmock)
}
tests := []def{
{
name: "Fallback to defaults",
table: "users",
selectFields: []qry.Field{"id", "name"},
query: func(query *qry.TypedSelectQuery[model]) {
},
exp: model{
ID: int64(1),
Name: "Tom",
},
mockFn: func(db sqlmock.Sqlmock) {
db.ExpectPrepare("SELECT id, name FROM users").
ExpectQuery().
WillReturnRows(
sqlmock.NewRows([]string{"id", "name"}).
AddRow(int64(1), "Tom"),
).
RowsWillBeClosed()
},
},
{
name: "Basic select",
table: "users",
selectFields: []qry.Field{"id", "name"},
query: func(query *qry.TypedSelectQuery[model]) {
query.Table = "user"
query.Fields = []qry.Field{"user_id", "username"}
},
exp: model{
ID: 1,
Name: "Tom",
},
mockFn: func(db sqlmock.Sqlmock) {
db.ExpectPrepare("SELECT user_id, username FROM user").
ExpectQuery().
WillReturnRows(
sqlmock.NewRows([]string{"user_id", "username"}).
AddRow(1, "Tom"),
).
RowsWillBeClosed()
},
},
{
name: "Select with equal condition",
query: func(query *qry.TypedSelectQuery[model]) {
query.Table = "users"
query.Fields = []qry.Field{"id", "name"}
query.Condition = qry.Equal("id", 1)
},
exp: model{
ID: 1,
Name: "Tom",
},
mockFn: func(db sqlmock.Sqlmock) {
db.ExpectPrepare("SELECT id, name FROM users WHERE id = ?").
ExpectQuery().
WithArgs(1).
WillReturnRows(
sqlmock.NewRows([]string{"id", "name"}).
AddRow(1, "Tom"),
).
RowsWillBeClosed()
},
},
{
name: "Partial select",
query: func(query *qry.TypedSelectQuery[model]) {
query.Table = "users"
query.Fields = []qry.Field{"name"}
query.Condition = qry.Equal("id", 1)
query.FieldReferences = func(target *model) []any {
return []any{&target.Name}
}
},
exp: model{
ID: 0,
Name: "Tom",
},
mockFn: func(db sqlmock.Sqlmock) {
db.ExpectPrepare("SELECT name FROM users WHERE id = ?").
ExpectQuery().
WithArgs(1).
WillReturnRows(
sqlmock.NewRows([]string{"name"}).
AddRow("Tom"),
).
RowsWillBeClosed()
},
},
}
for _, test := range tests {
tc := test
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
if err != nil {
t.Errorf("could not init db mock")
return
}
defer db.Close()
tc.mockFn(mock)
repo := qry.TypedRepository[model]{
Repository: qry.Repository{
DB: db,
Table: tc.table,
StandardSelectFields: tc.selectFields,
},
StandardSelectFieldReferences: func(target *model) []any {
return []any{
&target.ID,
&target.Name,
}
},
}
got, err := repo.QueryRowFn(context.Background(), tc.query)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
if !checkDiff(t, &tc.exp, got) {
return
}
})
}
}