-
Notifications
You must be signed in to change notification settings - Fork 13
/
user_queries.resolvers_test.go
167 lines (151 loc) · 4.42 KB
/
user_queries.resolvers_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
158
159
160
161
162
163
164
165
166
167
package resolver_test
import (
"context"
"errors"
"fmt"
"go-template/pkg/utl/cnvrttogql"
"regexp"
"testing"
"go-template/gqlmodels"
fm "go-template/gqlmodels"
"go-template/models"
// "go-template/pkg/utl/cnvrttogql"
"go-template/pkg/utl/rediscache"
"go-template/resolver"
"go-template/testutls"
"github.com/DATA-DOG/go-sqlmock"
"github.com/agiledragon/gomonkey/v2"
. "github.com/agiledragon/gomonkey/v2"
"github.com/stretchr/testify/assert"
)
type args struct {
user *models.User
}
type queryMeTestArgs struct {
name string
wantResp *fm.User
wantErr bool
args args
init func(args) *Patches
}
func initializeCases() []queryMeTestArgs {
return []queryMeTestArgs{
{
name: SuccessCase,
args: args{user: testutls.MockUser()},
wantResp: cnvrttogql.UserToGraphQlUser(testutls.MockUser(), 4),
init: func(args args) *Patches {
return gomonkey.ApplyFunc(rediscache.GetUser,
func(userID int, ctx context.Context) (*models.User, error) {
return args.user, nil
})
},
},
{
name: ErrorFromRedisCache,
args: args{user: testutls.MockUser()},
wantErr: true,
wantResp: &gqlmodels.User{},
init: func(args args) *Patches {
return gomonkey.ApplyFunc(rediscache.GetUser,
func(userID int, ctx context.Context) (*models.User, error) {
return nil, errors.New("redis cache")
})
},
},
}
}
func TestMe(t *testing.T) {
cases := initializeCases()
_, cleanup, _ := testutls.SetupMockDB(t)
defer cleanup()
resolver1 := &resolver.Resolver{}
for _, tt := range cases {
patches := tt.init(tt.args)
response, err := resolver1.Query().Me(context.TODO())
if tt.wantResp != nil && response != nil {
assert.Equal(t, tt.wantResp, response)
}
assert.Equal(t, tt.wantErr, err != nil)
if patches != nil {
patches.Reset()
}
}
}
type queryUsersArgs struct {
name string
pagination *fm.UserPagination
wantResp []*models.User
wantErr bool
init func(sqlmock.Sqlmock)
}
// TestUsers is a unit test function for testing user queries.
func initializeTestCases() []queryUsersArgs {
return []queryUsersArgs{
{
name: ErrorFindingUser,
wantErr: true,
init: func(mock sqlmock.Sqlmock) {
mock.ExpectQuery(regexp.QuoteMeta(`SELECT "users".* FROM "users";`)).
WithArgs().
WillReturnError(fmt.Errorf(""))
},
},
{
name: "Paginated Users are returned when the request is paginated",
wantErr: false,
pagination: &fm.UserPagination{
Limit: 1,
Page: 1,
},
wantResp: testutls.MockUsers(),
init: func(mock sqlmock.Sqlmock) {
rows := sqlmock.
NewRows([]string{"id", "email", "first_name", "last_name", "mobile", "username", "address"}).
AddRow(testutls.MockID, testutls.MockEmail, "First", "Last", "+911234567890", "username", "22 Jump Street")
mock.ExpectQuery(regexp.QuoteMeta(`SELECT "users".* FROM "users" LIMIT 1 OFFSET 1;`)).WithArgs().WillReturnRows(rows)
rowCount := sqlmock.NewRows([]string{"count"}).
AddRow(1)
mock.ExpectQuery(regexp.QuoteMeta(`SELECT COUNT(*) FROM "users"`)).
WithArgs().
WillReturnRows(rowCount)
},
},
{
name: "Successfully fetches paginated users even without pagination request payload",
wantErr: false,
wantResp: testutls.MockUsers(),
init: func(mock sqlmock.Sqlmock) {
rows := sqlmock.
NewRows([]string{"id", "email", "first_name", "last_name", "mobile", "username", "address"}).
AddRow(testutls.MockID, testutls.MockEmail, "First", "Last", "+911234567890", "username", "22 Jump Street")
mock.ExpectQuery(regexp.QuoteMeta(`SELECT "users".* FROM "users";`)).WithArgs().WillReturnRows(rows)
rowCount := sqlmock.NewRows([]string{"count"}).
AddRow(1)
mock.ExpectQuery(regexp.QuoteMeta(`SELECT COUNT(*) FROM "users";`)).
WithArgs().
WillReturnRows(rowCount)
},
},
}
}
func executeQuery(resolver1 *resolver.Resolver,
ctx context.Context, pagination *fm.UserPagination) (*gqlmodels.UsersPayload, error) {
return resolver1.Query().Users(ctx, pagination)
}
func TestUsers(
t *testing.T,
) {
cases := initializeTestCases()
resolver1 := resolver.Resolver{}
for _, tt := range cases {
mock, cleanup, _ := testutls.SetupMockDB(t)
tt.init(mock)
response, err := executeQuery(&resolver1, context.Background(), tt.pagination)
if tt.wantResp != nil && response != nil {
assert.Equal(t, len(tt.wantResp), len(response.Users))
}
assert.Equal(t, tt.wantErr, err != nil)
cleanup()
}
}