-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper_test.go
87 lines (70 loc) · 2.01 KB
/
helper_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
package main
import (
"bytes"
"context"
"database/sql"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/DATA-DOG/go-txdb"
internal "github.com/minond/captainslog/shared/service"
"github.com/DATA-DOG/go-sqlmock"
)
func init() {
txdb.Register("txdb", "postgres", "testconn")
}
func assertEqual(t *testing.T, expected, returned interface{}) {
t.Helper()
if !reflect.DeepEqual(expected, returned) {
t.Errorf(`error: not equal:
expected: %v
returned: %v`, expected, returned)
}
}
func newMockDB(t *testing.T) (*sql.DB, sqlmock.Sqlmock) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
return db, mock
}
func newMockRepo(t *testing.T) (Repository, *sql.DB, sqlmock.Sqlmock) {
db, mock := newMockDB(t)
return NewRepository(db), db, mock
}
func makeRequest(t *testing.T, rawBody string, repo Repository) *httptest.ResponseRecorder {
body := bytes.NewBufferString(rawBody)
req, err := http.NewRequest(http.MethodPost, "/", body)
if err != nil {
t.Fatalf("unexpected error from http.NewRequest: %v", err)
}
rr := httptest.NewRecorder()
service := NewService(repo, NewProcessor())
server := internal.NewServer("", service)
if err != nil {
t.Fatalf("unexpected error from NewServer: %v", err)
}
server.ServeHTTP(rr, req)
return rr
}
type testRepository struct {
extractorLookupError error
shorthandLookupError error
extractors []Extractor
shorthands []Shorthand
}
func (r testRepository) FindExtractors(ctx context.Context, bookID int64) ([]Extractor, error) {
return r.extractors, r.extractorLookupError
}
func (r testRepository) FindShorthands(ctx context.Context, bookID int64) ([]Shorthand, error) {
return r.shorthands, r.shorthandLookupError
}
type testProcessor struct {
text string
data map[string]interface{}
err error
}
func (r testProcessor) Process(text string, ss []Shorthand, es []Extractor) (string, map[string]interface{}, error) {
return r.text, r.data, r.err
}