Skip to content

Commit

Permalink
Avoid circular deps
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Bjerring committed Nov 12, 2018
1 parent cbe016a commit 21b2f72
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 250 deletions.
2 changes: 1 addition & 1 deletion api/query/autocomplete_medium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestAutocompleteHandler(t *testing.T) {
defer mockCtrl.Finish()

// TODO(markdittmer): Should this be hitting GCS instead?
store := shared.NewMockReadable(mockCtrl)
store := sharedtest.NewMockReadable(mockCtrl)
rs := []*sharedtest.MockReadCloser{
sharedtest.NewMockReadCloser(t, summaryBytes[0]),
sharedtest.NewMockReadCloser(t, summaryBytes[1]),
Expand Down
5 changes: 3 additions & 2 deletions api/query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/web-platform-tests/wpt.fyi/shared"
"github.com/web-platform-tests/wpt.fyi/shared/sharedtest"
)

func TestGetMemcacheKey(t *testing.T) {
Expand Down Expand Up @@ -44,7 +45,7 @@ func TestLoadSummaries_success(t *testing.T) {
getMemcacheKey(testRuns[1]),
}

cachedStore := shared.NewMockCachedStore(mockCtrl)
cachedStore := sharedtest.NewMockCachedStore(mockCtrl)
sh := unstructuredSearchHandler{queryHandler{dataSource: cachedStore}}
summaryBytes := [][]byte{
[]byte(`{"/a/b/c":[1,2]}`),
Expand Down Expand Up @@ -94,7 +95,7 @@ func TestLoadSummaries_fail(t *testing.T) {
getMemcacheKey(testRuns[1]),
}

cachedStore := shared.NewMockCachedStore(mockCtrl)
cachedStore := sharedtest.NewMockCachedStore(mockCtrl)
sh := unstructuredSearchHandler{queryHandler{dataSource: cachedStore}}
summaryBytes := [][]byte{
[]byte(`{"/a/b/c":[1,2]}`),
Expand Down
4 changes: 2 additions & 2 deletions api/query/search_medium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestUnstructuredSearchHandler(t *testing.T) {
defer mockCtrl.Finish()

// TODO(markdittmer): Should this be hitting GCS instead?
store := shared.NewMockReadable(mockCtrl)
store := sharedtest.NewMockReadable(mockCtrl)
rs := []*sharedtest.MockReadCloser{
sharedtest.NewMockReadCloser(t, summaryBytes[0]),
sharedtest.NewMockReadCloser(t, summaryBytes[1]),
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestStructuredSearchHandler_equivalentToUnstructured(t *testing.T) {
defer mockCtrl.Finish()

// TODO(markdittmer): Should this be hitting GCS instead?
store := shared.NewMockReadable(mockCtrl)
store := sharedtest.NewMockReadable(mockCtrl)
rs := []*sharedtest.MockReadCloser{
sharedtest.NewMockReadCloser(t, summaryBytes[0]),
sharedtest.NewMockReadCloser(t, summaryBytes[1]),
Expand Down
16 changes: 8 additions & 8 deletions api/receiver/receive_results_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/web-platform-tests/wpt.fyi/shared"
"github.com/web-platform-tests/wpt.fyi/shared/sharedtest"
"google.golang.org/appengine/taskqueue"
)

Expand Down Expand Up @@ -92,7 +92,7 @@ func TestHandleResultsUpload_success(t *testing.T) {
mockAE := NewMockAppEngineAPI(mockCtrl)
f := &os.File{}
task := &taskqueue.Task{Name: "task"}
mockAE.EXPECT().Context().Return(shared.NewTestContext()).AnyTimes()
mockAE.EXPECT().Context().Return(sharedtest.NewTestContext()).AnyTimes()
gomock.InOrder(
mockAE.EXPECT().IsAdmin().Return(false),
mockAE.EXPECT().authenticateUploader("blade-runner", "123").Return(true),
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestHandleResultsUpload_extra_params(t *testing.T) {
"os_version": "",
}
task := &taskqueue.Task{Name: "task"}
mockAE.EXPECT().Context().Return(shared.NewTestContext()).AnyTimes()
mockAE.EXPECT().Context().Return(sharedtest.NewTestContext()).AnyTimes()
gomock.InOrder(
mockAE.EXPECT().IsAdmin().Return(false),
mockAE.EXPECT().authenticateUploader("blade-runner", "123").Return(true),
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestHandleURLPayload_single(t *testing.T) {
}

mockAE := NewMockAppEngineAPI(mockCtrl)
mockAE.EXPECT().Context().Return(shared.NewTestContext()).AnyTimes()
mockAE.EXPECT().Context().Return(sharedtest.NewTestContext()).AnyTimes()
gomock.InOrder(
mockAE.EXPECT().fetchWithTimeout("http://wpt.fyi/test.json.gz", DownloadTimeout).Return(f, nil),
mockAE.EXPECT().uploadToGCS(matchRegex(`^/wptd-results-buffer/blade-runner/.*\.json$`), f, true).Return(nil),
Expand All @@ -189,7 +189,7 @@ func TestHandleURLPayload_multiple(t *testing.T) {
urls := []string{"http://wpt.fyi/foo.json.gz", "http://wpt.fyi/bar.json.gz"}

mockAE := NewMockAppEngineAPI(mockCtrl)
mockAE.EXPECT().Context().Return(shared.NewTestContext()).AnyTimes()
mockAE.EXPECT().Context().Return(sharedtest.NewTestContext()).AnyTimes()
gomock.InOrder(
mockAE.EXPECT().fetchWithTimeout(urls[0], DownloadTimeout).Return(f, nil),
mockAE.EXPECT().uploadToGCS(matchRegex(`^/wptd-results-buffer/blade-runner/.*/0\.json$`), f, true).Return(nil),
Expand All @@ -211,7 +211,7 @@ func TestHandleURLPayload_retry_fetching(t *testing.T) {
errTimeout := fmt.Errorf("server timed out")

mockAE := NewMockAppEngineAPI(mockCtrl)
mockAE.EXPECT().Context().Return(shared.NewTestContext()).AnyTimes()
mockAE.EXPECT().Context().Return(sharedtest.NewTestContext()).AnyTimes()
gomock.InOrder(
mockAE.EXPECT().fetchWithTimeout("http://wpt.fyi/test.json.gz", DownloadTimeout).Return(nil, errTimeout),
mockAE.EXPECT().fetchWithTimeout("http://wpt.fyi/test.json.gz", DownloadTimeout).Return(nil, errTimeout),
Expand All @@ -230,7 +230,7 @@ func TestHandleURLPayload_fail_fetching(t *testing.T) {
errTimeout := fmt.Errorf("server timed out")

mockAE := NewMockAppEngineAPI(mockCtrl)
mockAE.EXPECT().Context().Return(shared.NewTestContext()).AnyTimes()
mockAE.EXPECT().Context().Return(sharedtest.NewTestContext()).AnyTimes()
gomock.InOrder(
mockAE.EXPECT().fetchWithTimeout("http://wpt.fyi/test.json.gz", DownloadTimeout).Return(nil, errTimeout),
mockAE.EXPECT().fetchWithTimeout("http://wpt.fyi/test.json.gz", DownloadTimeout).Return(nil, errTimeout),
Expand All @@ -250,7 +250,7 @@ func TestHandleURLPayload_fail_uploading(t *testing.T) {
errGCS := fmt.Errorf("failed to upload to GCS")

mockAE := NewMockAppEngineAPI(mockCtrl)
mockAE.EXPECT().Context().Return(shared.NewTestContext()).AnyTimes()
mockAE.EXPECT().Context().Return(sharedtest.NewTestContext()).AnyTimes()
gomock.InOrder(
mockAE.EXPECT().fetchWithTimeout("http://wpt.fyi/test.json.gz", DownloadTimeout).Return(f, nil),
mockAE.EXPECT().uploadToGCS(matchRegex(`^/wptd-results-buffer/blade-runner/.*\.json$`), f, true).Return(errGCS),
Expand Down
5 changes: 3 additions & 2 deletions shared/models_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// +build medium

package shared
package shared_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/web-platform-tests/wpt.fyi/shared"
"github.com/web-platform-tests/wpt.fyi/shared/sharedtest"
"google.golang.org/appengine/datastore"
)

func TestTestRunIDs_LoadTestRuns(t *testing.T) {
testRuns := make(TestRuns, 2)
testRuns := make(shared.TestRuns, 2)
testRuns[0].BrowserName = "chrome"
testRuns[0].BrowserVersion = "63.0"
testRuns[0].OSName = "linux"
Expand Down
23 changes: 18 additions & 5 deletions shared/request_caching_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package shared
package shared_test

import (
"context"
Expand All @@ -16,6 +16,7 @@ import (

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/web-platform-tests/wpt.fyi/shared"
"github.com/web-platform-tests/wpt.fyi/shared/sharedtest"
)

Expand All @@ -36,9 +37,15 @@ func (okHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func TestNoCaching404(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
cache := NewMockReadWritable(mockCtrl)
cache := sharedtest.NewMockReadWritable(mockCtrl)
cache.EXPECT().NewReadCloser("/some/url").Return(ioutil.NopCloser(failReader{}), nil)
h := NewCachingHandler(context.Background(), http.NotFoundHandler(), cache, AlwaysCachable, URLAsCacheKey, CacheStatusOK)
h := shared.NewCachingHandler(
context.Background(),
http.NotFoundHandler(),
cache,
shared.AlwaysCachable,
shared.URLAsCacheKey,
shared.CacheStatusOK)
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/some/url", nil)
h.ServeHTTP(w, r)
Expand All @@ -48,11 +55,17 @@ func TestNoCaching404(t *testing.T) {
func TestCaching200(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
cache := NewMockReadWritable(mockCtrl)
cache := sharedtest.NewMockReadWritable(mockCtrl)
cache.EXPECT().NewReadCloser("/some/url").Return(ioutil.NopCloser(failReader{}), nil)
wc := sharedtest.NewMockWriteCloser(t)
cache.EXPECT().NewWriteCloser("/some/url").Return(wc, nil)
h := NewCachingHandler(context.Background(), okHandler{}, cache, AlwaysCachable, URLAsCacheKey, CacheStatusOK)
h := shared.NewCachingHandler(
context.Background(),
okHandler{},
cache,
shared.AlwaysCachable,
shared.URLAsCacheKey,
shared.CacheStatusOK)
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/some/url", nil)
h.ServeHTTP(w, r)
Expand Down
8 changes: 8 additions & 0 deletions shared/sharedtest/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package sharedtest
import (
"context"

"github.com/web-platform-tests/wpt.fyi/shared"
"google.golang.org/appengine"
"google.golang.org/appengine/aetest"
)
Expand Down Expand Up @@ -39,3 +40,10 @@ func NewAEContext(stronglyConsistentDatastore bool) (context.Context, func(), er
inst.Close()
}, nil
}

// NewTestContext creates a new context.Context for small tests.
func NewTestContext() context.Context {
ctx := context.Background()
ctx = context.WithValue(ctx, shared.DefaultLoggerCtxKey(), shared.NewNilLogger())
return ctx
}
Loading

0 comments on commit 21b2f72

Please sign in to comment.