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

Commit

Permalink
refactor: unit tests (#99)
Browse files Browse the repository at this point in the history
* rename widget system and plugin system

* refactor tests
  • Loading branch information
rot1024 authored Jan 18, 2022
1 parent bbe13d8 commit 0d112c5
Show file tree
Hide file tree
Showing 136 changed files with 3,831 additions and 3,477 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
4 changes: 2 additions & 2 deletions internal/adapter/gql/gqlmodel/convert_scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func ToScene(scene *scene.Scene) *Scene {
return nil
}

sceneWidgets := scene.WidgetSystem().Widgets()
sceneWidgets := scene.Widgets().Widgets()
widgets := make([]*SceneWidget, 0, len(sceneWidgets))
for _, w := range sceneWidgets {
widgets = append(widgets, ToSceneWidget(w))
Expand All @@ -55,7 +55,7 @@ func ToScene(scene *scene.Scene) *Scene {
clusters = append(clusters, ToCluster(c))
}

scenePlugins := scene.PluginSystem().Plugins()
scenePlugins := scene.Plugins().Plugins()
plugins := make([]*ScenePlugin, 0, len(scenePlugins))
for _, sp := range scenePlugins {
plugins = append(plugins, ToScenePlugin(sp))
Expand Down
4 changes: 2 additions & 2 deletions internal/adapter/gql/resolver_mutation_scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (r *mutationResolver) UploadPlugin(ctx context.Context, input gqlmodel.Uplo
return &gqlmodel.UploadPluginPayload{
Plugin: gqlmodel.ToPlugin(p),
Scene: gqlmodel.ToScene(s),
ScenePlugin: gqlmodel.ToScenePlugin(s.PluginSystem().Plugin(p.ID())),
ScenePlugin: gqlmodel.ToScenePlugin(s.Plugins().Plugin(p.ID())),
}, nil
}

Expand Down Expand Up @@ -192,7 +192,7 @@ func (r *mutationResolver) UpgradePlugin(ctx context.Context, input gqlmodel.Upg

return &gqlmodel.UpgradePluginPayload{
Scene: gqlmodel.ToScene(s),
ScenePlugin: gqlmodel.ToScenePlugin(s.PluginSystem().Plugin(input.ToPluginID)),
ScenePlugin: gqlmodel.ToScenePlugin(s.Plugins().Plugin(input.ToPluginID)),
}, nil
}

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
8 changes: 4 additions & 4 deletions internal/infrastructure/mongo/mongodoc/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ func (c *SceneIDConsumer) Consume(raw bson.Raw) error {
}

func NewScene(scene *scene.Scene) (*SceneDocument, string) {
widgets := scene.WidgetSystem().Widgets()
plugins := scene.PluginSystem().Plugins()
widgets := scene.Widgets().Widgets()
plugins := scene.Plugins().Plugins()
clusters := scene.Clusters().Clusters()

widgetsDoc := make([]SceneWidgetDocument, 0, len(widgets))
Expand Down Expand Up @@ -223,9 +223,9 @@ func (d *SceneDocument) Model() (*scene.Scene, error) {
Team(tid).
RootLayer(lid).
Clusters(cl).
WidgetSystem(scene.NewWidgetSystem(ws)).
Widgets(scene.NewWidgets(ws)).
WidgetAlignSystem(d.AlignSystem.Model()).
PluginSystem(scene.NewPluginSystem(ps)).
Plugins(scene.NewPlugins(ps)).
UpdatedAt(d.UpdateAt).
Property(prid).
Build()
Expand Down
Loading

0 comments on commit 0d112c5

Please sign in to comment.