-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec_test.go
More file actions
152 lines (144 loc) · 4.96 KB
/
Copy pathexec_test.go
File metadata and controls
152 lines (144 loc) · 4.96 KB
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
package libQuery
import (
"context"
"database/sql/driver"
"errors"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/hmmftg/requestCore/response"
"github.com/hmmftg/requestCore/webFramework"
"gotest.tools/v3/assert"
)
type AnyString string
// Match satisfies sqlmock.Argument interface
func (a AnyString) Match(v driver.Value) bool {
return true
}
func getMock(sqlType DmlCommandType, err error, args ...driver.Value) QueryRunnerInterface {
db, mockDb, _ := sqlmock.New() //sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
var anyS AnyString
switch sqlType {
case QueryCheckExists:
if err == nil {
mockDb.ExpectPrepare("queryE").ExpectQuery().WillReturnRows(
sqlmock.NewRows([]string{"key", "value"}).AddRow("1", "2"))
} else {
mockDb.ExpectPrepare("queryE").ExpectQuery().WillReturnError(err)
}
case QueryCheckNotExists:
if err == nil {
mockDb.ExpectPrepare("queryN").ExpectQuery().WillReturnRows(
sqlmock.NewRows([]string{"key", "value"}))
} else {
mockDb.ExpectPrepare("queryE").ExpectQuery().WillReturnError(err)
}
case Insert:
mockDb.ExpectBegin()
mockDb.ExpectExec("").WithArgs(anyS, anyS).WillReturnResult(sqlmock.NewResult(0, 1))
mockDb.ExpectExec("").WithArgs(anyS, anyS).WillReturnResult(sqlmock.NewResult(0, 1))
mockDb.ExpectExec("").WithArgs(anyS, anyS).WillReturnResult(sqlmock.NewResult(0, 1))
mockDb.ExpectExec("").WithArgs(anyS, anyS).WillReturnResult(sqlmock.NewResult(0, 1))
if err == nil {
if len(args) == 0 {
mockDb.ExpectExec("insert").WillReturnResult(sqlmock.NewResult(0, 1))
} else {
mockDb.ExpectExec("insert").WithArgs(args...).WillReturnResult(sqlmock.NewResult(0, 1))
}
} else {
mockDb.ExpectExec("queryE").WillReturnError(err)
}
mockDb.ExpectCommit()
case Update:
mockDb.ExpectBegin()
mockDb.ExpectExec("").WithArgs(anyS, anyS).WillReturnResult(sqlmock.NewResult(0, 1))
mockDb.ExpectExec("").WithArgs(anyS, anyS).WillReturnResult(sqlmock.NewResult(0, 1))
mockDb.ExpectExec("").WithArgs(anyS, anyS).WillReturnResult(sqlmock.NewResult(0, 1))
mockDb.ExpectExec("").WithArgs(anyS, anyS).WillReturnResult(sqlmock.NewResult(0, 1))
if err == nil {
mockDb.ExpectExec("update").WillReturnResult(sqlmock.NewResult(0, 1))
} else {
mockDb.ExpectExec("queryE").WillReturnError(err)
}
mockDb.ExpectCommit()
case Delete:
mockDb.ExpectBegin()
mockDb.ExpectExec("").WithArgs(anyS, anyS).WillReturnResult(sqlmock.NewResult(0, 1))
mockDb.ExpectExec("").WithArgs(anyS, anyS).WillReturnResult(sqlmock.NewResult(0, 1))
mockDb.ExpectExec("").WithArgs(anyS, anyS).WillReturnResult(sqlmock.NewResult(0, 1))
mockDb.ExpectExec("").WithArgs(anyS, anyS).WillReturnResult(sqlmock.NewResult(0, 1))
if err == nil {
mockDb.ExpectExec("delete").WillReturnResult(sqlmock.NewResult(0, 1))
} else {
mockDb.ExpectExec("queryE").WillReturnError(err)
}
mockDb.ExpectCommit()
}
return QueryRunnerModel{
DB: db,
}
}
func TestExecuteWithContext(t *testing.T) {
type TestCase struct {
Name string
Context context.Context
Command DmlCommand
Model QueryRunnerInterface
Result any
Error response.ErrorState
}
testCases := []TestCase{{
Name: "Valid Query Exists",
Command: DmlCommand{
Name: "q1",
Command: "queryE",
Type: QueryCheckExists,
},
Model: getMock(QueryCheckExists, nil),
Result: []QueryData{{Key: "1", Value: "2"}},
}, {
Name: "Invalid Query Exists",
Command: DmlCommand{
Name: "q2",
Command: "queryE",
Type: QueryCheckExists,
CustomError: &response.ErrorData{Description: "eeeerrr", Status: 1, Message: "mmm"},
},
Model: getMock(QueryCheckExists, errors.New("error happened")),
Error: &response.ErrorData{Description: "eeeerrr", Status: 1, Message: "mmm"},
}, {
Name: "Valid Query Not Exists",
Command: DmlCommand{
Name: "q1",
Command: "queryN",
Type: QueryCheckNotExists,
},
Model: getMock(QueryCheckNotExists, nil),
}, {
Name: "Invalid Query Not Exists",
Command: DmlCommand{
Name: "q2",
Command: "queryN",
Type: QueryCheckNotExists,
CustomError: &response.ErrorData{Description: "eeeerrr", Status: 1, Message: "mmm"},
},
Model: getMock(QueryCheckNotExists, errors.New("error happened")),
Error: &response.ErrorData{Description: "eeeerrr", Status: 1, Message: "mmm"},
}}
for _, testCase := range testCases {
result, err := testCase.Command.ExecuteWithContext(webFramework.FakeParser{}, testCase.Context, "", "", testCase.Model)
assert.DeepEqual(t, result, testCase.Result)
if err == nil && testCase.Error != nil {
t.Fatal("error wanted", testCase.Error, "got", err)
}
if err != nil && testCase.Error == nil {
t.Fatal("error wanted", testCase.Error, "got", err)
}
if err != nil {
if ok, err := response.Unwrap(err); ok {
assert.Equal(t, err.GetDescription(), testCase.Error.GetDescription())
assert.Equal(t, err.GetStatus(), testCase.Error.GetStatus())
assert.Equal(t, err.GetMessage(), testCase.Error.GetMessage())
}
}
}
}