mirror of
https://github.com/dstotijn/hetty.git
synced 2025-07-01 18:47:29 -04:00
Remove database mocks, replace Service
interface indirection
This commit is contained in:
@ -1,7 +1,5 @@
|
||||
package reqlog_test
|
||||
|
||||
//go:generate go run github.com/matryer/moq -out repo_mock_test.go -pkg reqlog_test . Repository:RepoMock
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
@ -15,6 +13,8 @@ import (
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/oklog/ulid"
|
||||
|
||||
badgerdb "github.com/dgraph-io/badger/v3"
|
||||
"github.com/dstotijn/hetty/pkg/db/badger"
|
||||
"github.com/dstotijn/hetty/pkg/proxy"
|
||||
"github.com/dstotijn/hetty/pkg/reqlog"
|
||||
"github.com/dstotijn/hetty/pkg/scope"
|
||||
@ -25,13 +25,13 @@ var ulidEntropy = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
//nolint:paralleltest
|
||||
func TestRequestModifier(t *testing.T) {
|
||||
repoMock := &RepoMock{
|
||||
StoreRequestLogFunc: func(_ context.Context, _ reqlog.RequestLog) error {
|
||||
return nil
|
||||
},
|
||||
db, err := badger.OpenDatabase(badgerdb.DefaultOptions("").WithInMemory(true))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open database: %v", err)
|
||||
}
|
||||
|
||||
svc := reqlog.NewService(reqlog.Config{
|
||||
Repository: repoMock,
|
||||
Repository: db,
|
||||
Scope: &scope.Scope{},
|
||||
})
|
||||
svc.SetActiveProjectID(ulid.MustNew(ulid.Timestamp(time.Now()), ulidEntropy))
|
||||
@ -47,13 +47,8 @@ func TestRequestModifier(t *testing.T) {
|
||||
reqModFn(req)
|
||||
|
||||
t.Run("request log was stored in repository", func(t *testing.T) {
|
||||
gotCount := len(repoMock.StoreRequestLogCalls())
|
||||
if expCount := 1; expCount != gotCount {
|
||||
t.Fatalf("incorrect `proj.Service.AddRequestLog` calls (expected: %v, got: %v)", expCount, gotCount)
|
||||
}
|
||||
|
||||
exp := reqlog.RequestLog{
|
||||
ID: ulid.ULID{}, // Empty value
|
||||
ID: reqID,
|
||||
ProjectID: svc.ActiveProjectID(),
|
||||
Method: req.Method,
|
||||
URL: req.URL,
|
||||
@ -61,8 +56,11 @@ func TestRequestModifier(t *testing.T) {
|
||||
Header: req.Header,
|
||||
Body: []byte("modified body"),
|
||||
}
|
||||
got := repoMock.StoreRequestLogCalls()[0].ReqLog
|
||||
got.ID = ulid.ULID{} // Override to empty value so we can compare against expected value.
|
||||
|
||||
got, err := svc.FindRequestLogByID(context.Background(), reqID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to find request by id: %v", err)
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(exp, got); diff != "" {
|
||||
t.Fatalf("request log not equal (-exp, +got):\n%v", diff)
|
||||
@ -72,13 +70,13 @@ func TestRequestModifier(t *testing.T) {
|
||||
|
||||
//nolint:paralleltest
|
||||
func TestResponseModifier(t *testing.T) {
|
||||
repoMock := &RepoMock{
|
||||
StoreResponseLogFunc: func(_ context.Context, _ ulid.ULID, _ reqlog.ResponseLog) error {
|
||||
return nil
|
||||
},
|
||||
db, err := badger.OpenDatabase(badgerdb.DefaultOptions("").WithInMemory(true))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open database: %v", err)
|
||||
}
|
||||
|
||||
svc := reqlog.NewService(reqlog.Config{
|
||||
Repository: repoMock,
|
||||
Repository: db,
|
||||
})
|
||||
svc.SetActiveProjectID(ulid.MustNew(ulid.Timestamp(time.Now()), ulidEntropy))
|
||||
|
||||
@ -92,6 +90,13 @@ func TestResponseModifier(t *testing.T) {
|
||||
reqLogID := ulid.MustNew(ulid.Timestamp(time.Now()), ulidEntropy)
|
||||
req = req.WithContext(context.WithValue(req.Context(), reqlog.ReqLogIDKey, reqLogID))
|
||||
|
||||
err = db.StoreRequestLog(context.Background(), reqlog.RequestLog{
|
||||
ID: reqLogID,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to store request log: %v", err)
|
||||
}
|
||||
|
||||
res := &http.Response{
|
||||
Request: req,
|
||||
Body: io.NopCloser(strings.NewReader("bar")),
|
||||
@ -104,23 +109,15 @@ func TestResponseModifier(t *testing.T) {
|
||||
t.Run("request log was stored in repository", func(t *testing.T) {
|
||||
// Dirty (but simple) wait for other goroutine to finish calling repository.
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
got := len(repoMock.StoreResponseLogCalls())
|
||||
if exp := 1; exp != got {
|
||||
t.Fatalf("incorrect `proj.Service.AddResponseLog` calls (expected: %v, got: %v)", exp, got)
|
||||
|
||||
got, err := svc.FindRequestLogByID(context.Background(), reqLogID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to find request by id: %v", err)
|
||||
}
|
||||
|
||||
t.Run("ran next modifier first, before calling repository", func(t *testing.T) {
|
||||
got := repoMock.StoreResponseLogCalls()[0].ResLog.Body
|
||||
if exp := "modified body"; exp != string(got) {
|
||||
t.Fatalf("incorrect `ResponseLog.Body` value (expected: %v, got: %v)", exp, string(got))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("called repository with request log id", func(t *testing.T) {
|
||||
got := repoMock.StoreResponseLogCalls()[0].ReqLogID
|
||||
if exp := reqLogID; exp.Compare(got) != 0 {
|
||||
t.Fatalf("incorrect `reqLogID` argument for `Repository.AddResponseLogCalls` (expected: %v, got: %v)",
|
||||
exp.String(), got.String())
|
||||
if exp := "modified body"; exp != string(got.Response.Body) {
|
||||
t.Fatalf("incorrect `ResponseLog.Body` value (expected: %v, got: %v)", exp, string(got.Response.Body))
|
||||
}
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user