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

Commit

Permalink
fix: assets are not saved when files are uploaded (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Jun 3, 2022
1 parent 602ec07 commit e444e4e
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 24 deletions.
6 changes: 2 additions & 4 deletions internal/infrastructure/memory/container.go
Expand Up @@ -4,10 +4,8 @@ import (
"github.com/reearth/reearth-backend/internal/usecase/repo"
)

func InitRepos(c *repo.Container) *repo.Container {
if c == nil {
c = &repo.Container{}
}
func New() *repo.Container {
c := &repo.Container{}
c.Asset = NewAsset()
c.Config = NewConfig()
c.DatasetSchema = NewDatasetSchema()
Expand Down
11 changes: 7 additions & 4 deletions internal/infrastructure/memory/transaction.go
Expand Up @@ -43,13 +43,16 @@ func (t *Transaction) Begin() (repo.Tx, error) {

func (t *Tx) Commit() {
t.committed = true
if t.t != nil {
t.t.committed++
}
}

func (t *Tx) End(_ context.Context) error {
return t.enderror
if t.enderror != nil {
return t.enderror
}
if t.t != nil && t.committed {
t.t.committed++
}
return nil
}

func (t *Tx) IsCommitted() bool {
Expand Down
4 changes: 2 additions & 2 deletions internal/infrastructure/memory/transaction_test.go
Expand Up @@ -14,9 +14,9 @@ func TestTransaction_Committed(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 0, tr.Committed())
tx.Commit()
assert.Equal(t, 1, tr.Committed())
assert.Equal(t, 0, tr.Committed())
assert.NoError(t, tx.End(context.Background()))
assert.NoError(t, err)
assert.Equal(t, 1, tr.Committed())
}

func TestTransaction_SetBeginError(t *testing.T) {
Expand Down
14 changes: 11 additions & 3 deletions internal/usecase/interactor/asset.go
Expand Up @@ -47,25 +47,33 @@ func (i *Asset) Create(ctx context.Context, inp interfaces.CreateAssetParam, ope
if inp.File == nil {
return nil, interfaces.ErrFileNotIncluded
}

return Run1(
ctx, operator, i.repos,
Usecase().
WithReadableTeams(inp.TeamID).
WithWritableTeams(inp.TeamID).
Transaction(),
func() (*asset.Asset, error) {
url, err := i.gateways.File.UploadAsset(ctx, inp.File)
if err != nil {
return nil, err
}

return asset.New().
a, err := asset.New().
NewID().
Team(inp.TeamID).
Name(path.Base(inp.File.Path)).
Size(inp.File.Size).
URL(url.String()).
Build()
if err != nil {
return nil, err
}

if err := i.repos.Asset.Save(ctx, a); err != nil {
return nil, err
}

return a, nil
})
}

Expand Down
69 changes: 69 additions & 0 deletions internal/usecase/interactor/asset_test.go
@@ -0,0 +1,69 @@
package interactor

import (
"bytes"
"context"
"io"
"testing"

"github.com/reearth/reearth-backend/internal/infrastructure/fs"
"github.com/reearth/reearth-backend/internal/infrastructure/memory"
"github.com/reearth/reearth-backend/internal/usecase"
"github.com/reearth/reearth-backend/internal/usecase/gateway"
"github.com/reearth/reearth-backend/internal/usecase/interfaces"
"github.com/reearth/reearth-backend/pkg/asset"
"github.com/reearth/reearth-backend/pkg/file"
"github.com/reearth/reearth-backend/pkg/id"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
)

func TestAsset_Create(t *testing.T) {
ctx := context.Background()
tid := asset.NewTeamID()
aid := asset.NewID()
newID := asset.NewID
asset.NewID = func() asset.ID { return aid }
t.Cleanup(func() { asset.NewID = newID })

mfs := afero.NewMemMapFs()
f, _ := fs.NewFile(mfs, "")
repos := memory.New()
transaction := memory.NewTransaction()
repos.Transaction = transaction
uc := &Asset{
repos: repos,
gateways: &gateway.Container{
File: f,
},
}
buf := bytes.NewBufferString("Hello")
buflen := int64(buf.Len())
res, err := uc.Create(ctx, interfaces.CreateAssetParam{
TeamID: tid,
File: &file.File{
Content: io.NopCloser(buf),
Path: "hoge.txt",
ContentType: "",
Size: buflen,
},
}, &usecase.Operator{
WritableTeams: id.TeamIDList{tid},
})

want := asset.New().
ID(aid).
Team(tid).
URL(res.URL()).
CreatedAt(aid.Timestamp()).
Name("hoge.txt").
Size(buflen).
ContentType("").
MustBuild()

assert.NoError(t, err)
assert.Equal(t, want, res)
assert.Equal(t, 1, transaction.Committed())
a, _ := repos.Asset.FindByID(ctx, aid)
assert.Equal(t, want, a)
}
2 changes: 1 addition & 1 deletion internal/usecase/interactor/layer_test.go
Expand Up @@ -15,7 +15,7 @@ import (
func TestCreateInfobox(t *testing.T) {
ctx := context.Background()

db := memory.InitRepos(nil)
db := memory.New()
scene, _ := scene.New().NewID().Team(id.NewTeamID()).Project(id.NewProjectID()).RootLayer(id.NewLayerID()).Build()
_ = db.Scene.Save(ctx, scene)
il := NewLayer(db)
Expand Down
6 changes: 3 additions & 3 deletions internal/usecase/interactor/plugin_upload_test.go
Expand Up @@ -89,7 +89,7 @@ func TestPlugin_Upload_New(t *testing.T) {
sid := id.NewSceneID()
pid := mockPluginID.WithScene(sid.Ref())

repos := memory.InitRepos(nil)
repos := memory.New()
mfs := mockFS(nil)
files, err := fs.NewFile(mfs, "")
assert.NoError(t, err)
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestPlugin_Upload_SameVersion(t *testing.T) {
eid2 := id.PluginExtensionID("widget2")
wid1 := id.NewWidgetID()

repos := memory.InitRepos(nil)
repos := memory.New()
mfs := mockFS(map[string]string{
"plugins/" + pid.String() + "/hogehoge": "foobar",
})
Expand Down Expand Up @@ -263,7 +263,7 @@ func TestPlugin_Upload_DiffVersion(t *testing.T) {
nlpsid2 := id.NewPropertySchemaID(pid, eid2.String())
wid := id.NewWidgetID()

repos := memory.InitRepos(nil)
repos := memory.New()
mfs := mockFS(map[string]string{
"plugins/" + oldpid.String() + "/hogehoge": "foobar",
})
Expand Down
6 changes: 3 additions & 3 deletions internal/usecase/interactor/property_test.go
Expand Up @@ -15,7 +15,7 @@ import (

func TestProperty_AddItem(t *testing.T) {
ctx := context.Background()
memory := memory.InitRepos(nil)
memory := memory.New()

team := id.NewTeamID()
scene := scene.New().NewID().Team(team).RootLayer(id.NewLayerID()).MustBuild()
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestProperty_AddItem(t *testing.T) {

func TestProperty_RemoveItem(t *testing.T) {
ctx := context.Background()
memory := memory.InitRepos(nil)
memory := memory.New()

team := id.NewTeamID()
scene := scene.New().NewID().Team(team).RootLayer(id.NewLayerID()).MustBuild()
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestProperty_RemoveItem(t *testing.T) {

func TestProperty_UpdateValue_FieldOfGroupInList(t *testing.T) {
ctx := context.Background()
memory := memory.InitRepos(nil)
memory := memory.New()

team := id.NewTeamID()
scene := scene.New().NewID().Team(team).RootLayer(id.NewLayerID()).MustBuild()
Expand Down
2 changes: 1 addition & 1 deletion internal/usecase/interactor/team_test.go
Expand Up @@ -14,7 +14,7 @@ import (
func TestCreateTeam(t *testing.T) {
ctx := context.Background()

db := memory.InitRepos(nil)
db := memory.New()

u := user.New().NewID().Email("aaa@bbb.com").Team(id.NewTeamID()).MustBuild()
teamUC := NewTeam(db)
Expand Down
2 changes: 1 addition & 1 deletion internal/usecase/interactor/usecase_test.go
Expand Up @@ -198,5 +198,5 @@ func TestRun(t *testing.T) {
},
)
assert.Same(t, err, goterr)
assert.Equal(t, 2, tr.Committed()) // committed but fails
assert.Equal(t, 1, tr.Committed()) // fails
}
4 changes: 2 additions & 2 deletions internal/usecase/interactor/user_signup_test.go
Expand Up @@ -263,7 +263,7 @@ func TestUser_Signup(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
// t.Parallel() cannot be used
r := memory.InitRepos(nil)
r := memory.New()
if tt.createUserBefore != nil {
assert.NoError(t, r.User.Save(
context.Background(),
Expand Down Expand Up @@ -516,7 +516,7 @@ func TestUser_SignupOIDC(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
// t.Parallel() cannot be used
r := memory.InitRepos(nil)
r := memory.New()
if tt.createUserBefore != nil {
assert.NoError(t, r.User.Save(
context.Background(),
Expand Down
10 changes: 10 additions & 0 deletions pkg/util/now.go
@@ -0,0 +1,10 @@
package util

import "time"

var Now = time.Now

func MockNow(t time.Time) func() {
Now = func() time.Time { return t }
return func() { Now = time.Now }
}

0 comments on commit e444e4e

Please sign in to comment.