Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Jan 18, 2022
1 parent ddc1928 commit 5f54e71
Show file tree
Hide file tree
Showing 122 changed files with 3,401 additions and 3,045 deletions.
3 changes: 3 additions & 0 deletions internal/adapter/gql/gqlmodel/convert_property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func TestFromPropertyValueAndType(t *testing.T) {
v interface{}
t ValueType
}

tests := []struct {
name string
args args
Expand All @@ -37,7 +38,9 @@ func TestFromPropertyValueAndType(t *testing.T) {
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.want, FromPropertyValueAndType(tt.args.v, tt.args.t))
})
}
Expand Down
3 changes: 3 additions & 0 deletions internal/adapter/gql/resolver_property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ func Test_actualValue(t *testing.T) {
false,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := actualValue(tt.args.datasetLoader, tt.args.value, tt.args.links, tt.args.overridden)
if (err != nil) != tt.wantErr {
t.Errorf("actualValue() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
31 changes: 16 additions & 15 deletions internal/app/published_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestPublishedAuthMiddleware(t *testing.T) {
return c.String(http.StatusOK, "test")
})

testCases := []struct {
tests := []struct {
Name string
PublishedName string
BasicAuthUsername string
Expand Down Expand Up @@ -74,12 +74,12 @@ func TestPublishedAuthMiddleware(t *testing.T) {
},
}

for _, tc := range testCases {
for _, tc := range tests {
tc := tc
t.Run(tc.Name, func(tt *testing.T) {
tt.Parallel()
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()

assert := assert.New(tt)
assert := assert.New(t)
req := httptest.NewRequest(http.MethodGet, "/", nil)
if tc.BasicAuthUsername != "" {
req.Header.Set(echo.HeaderAuthorization, "basic "+base64.StdEncoding.EncodeToString([]byte(tc.BasicAuthUsername+":"+tc.BasicAuthPassword)))
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestPublishedData(t *testing.T) {
return nil, rerror.ErrNotFound
})

testCases := []struct {
tests := []struct {
Name string
PublishedName string
Error error
Expand All @@ -130,11 +130,12 @@ func TestPublishedData(t *testing.T) {
},
}

for _, tc := range testCases {
for _, tc := range tests {
tc := tc
t.Run(tc.Name, func(tt *testing.T) {
tt.Parallel()
assert := assert.New(tt)
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()

assert := assert.New(t)
req := httptest.NewRequest(http.MethodGet, "/", nil)
res := httptest.NewRecorder()
e := echo.New()
Expand All @@ -156,7 +157,7 @@ func TestPublishedData(t *testing.T) {
}

func TestPublishedIndex(t *testing.T) {
testCases := []struct {
tests := []struct {
Name string
PublishedName string
Error error
Expand All @@ -182,12 +183,12 @@ func TestPublishedIndex(t *testing.T) {
},
}

for _, tc := range testCases {
for _, tc := range tests {
tc := tc
t.Run(tc.Name, func(tt *testing.T) {
tt.Parallel()
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()

assert := assert.New(tt)
assert := assert.New(t)
req := httptest.NewRequest(http.MethodGet, "/aaa/bbb", nil)
res := httptest.NewRecorder()
e := echo.New()
Expand Down
12 changes: 6 additions & 6 deletions internal/infrastructure/fs/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func TestFile_RemoveAsset(t *testing.T) {

for _, tc := range cases {
tc := tc
t.Run(tc.Name, func(tt *testing.T) {
tt.Parallel()
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()

fs := mockFs()
f, _ := NewFile(fs, "https://example.com/assets")
Expand All @@ -98,16 +98,16 @@ func TestFile_RemoveAsset(t *testing.T) {
err := f.RemoveAsset(context.Background(), u)

if tc.Err == nil {
assert.NoError(tt, err)
assert.NoError(t, err)
} else {
assert.Same(tt, tc.Err, err)
assert.Same(t, tc.Err, err)
}

_, err = fs.Stat(filepath.Join("assets", "xxx.txt"))
if tc.Deleted {
assert.ErrorIs(tt, err, os.ErrNotExist)
assert.ErrorIs(t, err, os.ErrNotExist)
} else {
assert.NoError(tt, err)
assert.NoError(t, err)
}
})
}
Expand Down
35 changes: 19 additions & 16 deletions internal/infrastructure/github/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package github

import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -18,47 +17,51 @@ func TestFetchURL(t *testing.T) {
server2 := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusBadRequest)
}))
testCases := []struct {

defer func() {
server.Close()
server2.Close()
}()

tests := []struct {
Name, URL string
Ctx context.Context
ExpectedErr error
ExpectedErr string
}{
{
Name: "Fail: nil context",
Ctx: nil,
URL: server.URL,
ExpectedErr: errors.New("nil Context"),
ExpectedErr: "net/http: nil Context",
},
{
Name: "Fail: nil unsupported protocol scheme ",
Name: "Fail: nil unsupported protocol scheme",
Ctx: context.Background(),
URL: "",
ExpectedErr: errors.New("unsupported protocol scheme"),
ExpectedErr: "Get \"\": unsupported protocol scheme \"\"",
},
{
Name: "Fail: bad request ",
Ctx: context.Background(),
URL: server2.URL,
ExpectedErr: errors.New("StatusCode=400"),
ExpectedErr: "StatusCode=400",
},
{
Name: "Success",
Ctx: context.Background(),
URL: server.URL,
},
}
defer func() {
server.Close()
server2.Close()
}()
for _, tc := range testCases {

for _, tc := range tests {
tc := tc
t.Run(tc.Name, func(tt *testing.T) {
t.Run(tc.Name, func(t *testing.T) {
body, err := fetchURL(tc.Ctx, tc.URL)
if err != nil {
assert.True(tt, errors.As(tc.ExpectedErr, &err))
if tc.ExpectedErr != "" {
assert.EqualError(t, err, tc.ExpectedErr)
} else {
assert.NotNil(tt, body)
_ = body.Close()
assert.NotNil(t, body)
}
})
}
Expand Down
5 changes: 3 additions & 2 deletions internal/infrastructure/github/plugin_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"

"github.com/reearth/reearth-backend/internal/usecase/gateway"
"github.com/reearth/reearth-backend/pkg/log"
"github.com/reearth/reearth-backend/pkg/plugin"
)

Expand All @@ -17,7 +18,6 @@ func NewPluginRegistry() gateway.PluginRegistry {
const source = `https://raw.githubusercontent.com/reearth/plugins/main/plugins.json`

func (d *pluginRegistry) FetchMetadata(ctx context.Context) ([]*plugin.Metadata, error) {

response, err := fetchURL(ctx, source)
if err != nil {
return nil, err
Expand All @@ -28,7 +28,8 @@ func (d *pluginRegistry) FetchMetadata(ctx context.Context) ([]*plugin.Metadata,
var result []*plugin.Metadata
err = json.NewDecoder(response).Decode(&result)
if err != nil {
return nil, err
log.Errorf("plugin_registry: error: %s", err)
return nil, gateway.ErrFailedToFetchDataFromPluginRegistry
}
return result, nil
}
19 changes: 14 additions & 5 deletions internal/infrastructure/github/plugin_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package github

import (
"context"
"errors"
"testing"
"time"

"github.com/jarcoal/httpmock"
"github.com/reearth/reearth-backend/internal/usecase/gateway"
"github.com/reearth/reearth-backend/pkg/plugin"
"github.com/stretchr/testify/assert"
)
Expand All @@ -19,8 +19,16 @@ func TestNewPluginRegistry(t *testing.T) {
func TestPluginRegistry_FetchMetadata(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("GET", "https://raw.githubusercontent.com/reearth/plugins/main/plugins.json",
httpmock.NewStringResponder(200, `[{"name": "reearth","description": "Official Plugin", "author": "reearth", "thumbnailUrl": "", "createdAt": "2021-03-16T04:19:57.592Z"}]`))

httpmock.RegisterResponder(
"GET",
"https://raw.githubusercontent.com/reearth/plugins/main/plugins.json",
httpmock.NewStringResponder(
200,
`[{"name": "reearth","description": "Official Plugin", "author": "reearth", "thumbnailUrl": "", "createdAt": "2021-03-16T04:19:57.592Z"}]`,
),
)

d := NewPluginRegistry()
res, err := d.FetchMetadata(context.Background())
tm, _ := time.Parse(time.RFC3339, "2021-03-16T04:19:57.592Z")
Expand All @@ -40,12 +48,13 @@ func TestPluginRegistry_FetchMetadata(t *testing.T) {
httpmock.RegisterResponder("GET", "https://raw.githubusercontent.com/reearth/plugins/main/plugins.json",
httpmock.NewStringResponder(400, `mock bad request`))
_, err = d.FetchMetadata(context.Background())
assert.True(t, errors.As(errors.New("StatusCode=400"), &err))

assert.EqualError(t, err, "StatusCode=400")

// fail: unable to marshal
httpmock.RegisterResponder("GET", "https://raw.githubusercontent.com/reearth/plugins/main/plugins.json",
httpmock.NewStringResponder(200, `{"hoge": "test"}`))
_, err = d.FetchMetadata(context.Background())
assert.True(t, errors.As(errors.New("cannot unmarshal object into Go value of type []*plugin.Metadata"), &err))

assert.Equal(t, gateway.ErrFailedToFetchDataFromPluginRegistry, err)
}
2 changes: 2 additions & 0 deletions internal/infrastructure/google/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func Test_fetchCSV(t *testing.T) {
fileId string
sheetName string
}

tests := []struct {
name string
setup func()
Expand Down Expand Up @@ -69,6 +70,7 @@ func Test_fetchCSV(t *testing.T) {
wantErr: false,
},
}

for _, tt := range tests {
tt := tt

Expand Down
Loading

0 comments on commit 5f54e71

Please sign in to comment.