-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite_test.go
131 lines (113 loc) · 2.77 KB
/
sqlite_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
package httpcache
import (
"database/sql"
"io"
"net/http"
"net/url"
"strings"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
)
const (
testHost = "www.test.com"
testPath = "/test-path"
testBody = "this is a test body"
)
const (
insertQuery = `INSERT INTO responses \(request_url, request_method, request_body, response_body, status_code\) VALUES \(\?, \?, \?, \?, \?\)`
selectQuery = `SELECT response_body, status_code FROM responses WHERE request_url = \? AND request_method = \? AND request_body = \?`
)
func Test_Save_HappyPath(t *testing.T) {
// Given
response := aDummyResponse()
db, mock, closeFunc := aDatabaseMock(t)
defer closeFunc()
subject := &sqliteResponseStore{database: db}
mock.ExpectBegin()
mock.ExpectExec(insertQuery).WithArgs(
testHost+testPath,
http.MethodGet,
[]byte{},
[]byte(testBody),
http.StatusOK,
).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
// When
err := subject.Save(response)
// Then
assert.Nil(t, err)
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func Test_Read_HappyPath(t *testing.T) {
// Given
request := aDummyRequest()
db, mock, closeFunc := aDatabaseMock(t)
defer closeFunc()
subject := &sqliteResponseStore{database: db}
mockRows := sqlmock.NewRows([]string{"response_body", "status_code"}).
AddRow("mock-body", 200).
AddRow("mock-body", 200)
mock.ExpectQuery(selectQuery).WithArgs(
testHost+testPath,
http.MethodGet,
[]byte{},
).WillReturnRows(mockRows)
// When
response, err := subject.Read(request)
// Then
assert.Nil(t, err)
assert.NotNil(t, response)
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
func aDatabaseMock(t *testing.T) (*sql.DB, sqlmock.Sqlmock, func() error) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("%v", err)
}
return db, mock, db.Close
}
func aDummyResponse() *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(testBody)),
Request: &http.Request{
URL: &url.URL{
Scheme: "https",
Host: testHost,
Opaque: "",
User: nil,
Path: testPath,
RawPath: "",
OmitHost: false,
ForceQuery: false,
RawQuery: "",
Fragment: "",
RawFragment: "",
},
Method: http.MethodGet,
},
}
}
func aDummyRequest() *http.Request {
return &http.Request{
URL: &url.URL{
Scheme: "https",
Host: testHost,
Opaque: "",
User: nil,
Path: testPath,
RawPath: "",
OmitHost: false,
ForceQuery: false,
RawQuery: "",
Fragment: "",
RawFragment: "",
},
Method: http.MethodGet,
}
}