Skip to content

Commit

Permalink
refs #34: archive usecaseの単体テストを実装
Browse files Browse the repository at this point in the history
  • Loading branch information
CityBear3 committed Dec 8, 2023
1 parent 304cf4d commit 4bf89dc
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 4 deletions.
101 changes: 97 additions & 4 deletions internal/usecase/archive_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,100 @@
package usecase
package usecase_test

import "testing"
import (
"context"
"testing"

func TestArchiveInteractor_UploadArchive(t *testing.T) {
// TODO implement this test
mock_repository "github.com/CityBear3/satellite/internal/adaptor/gateway/repository/mock"
"github.com/CityBear3/satellite/internal/domain/entity"
"github.com/CityBear3/satellite/internal/domain/primitive"
"github.com/CityBear3/satellite/internal/domain/primitive/archive"
"github.com/CityBear3/satellite/internal/domain/primitive/device"
"github.com/CityBear3/satellite/internal/usecase"
mock_usecase "github.com/CityBear3/satellite/internal/usecase/mock"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)

func TestArchiveInteractor_CreateArchive(t *testing.T) {
type args struct {
ctx context.Context
req usecase.CreateArchiveInput
device entity.Device
}

ctrl := gomock.NewController(t)
mockArchiveRepository := mock_repository.NewMockIArchiveRepository(ctrl)
mockEventRepository := mock_repository.NewMockIEventRepository(ctrl)
mockITxManager := mock_usecase.NewMockITxManager(ctrl)

sut := usecase.NewArchiveInteractor(mockArchiveRepository, mockEventRepository, mockITxManager)

contentType, err := archive.NewContentType("image/png")
if err != nil {
t.Fatal(err)
}

data, err := archive.NewData([]byte("test"))
if err != nil {
t.Fatal(err)
}

deviceName, err := device.NewDeviceName("test")
if err != nil {
t.Fatal(err)
}

device := entity.NewDevice(primitive.NewID(), deviceName, nil, primitive.NewID())

tests := []struct {
name string
args args
wantErr error
assertion assert.ErrorAssertionFunc
runMock func(ctx context.Context, req usecase.CreateArchiveInput, device entity.Device)
}{
{
name: "success to create archive",
args: args{
ctx: context.Background(),
req: usecase.CreateArchiveInput{
ArchiveEventID: primitive.NewID(),
ContentType: contentType,
Data: data,
},
device: device,
},

assertion: assert.NoError,
runMock: func(ctx context.Context, req usecase.CreateArchiveInput, device entity.Device) {
mockITxManager.
EXPECT().
DoInTx(ctx, gomock.Any()).
DoAndReturn(func(ctx context.Context, f func(ctx2 context.Context) error) (interface{}, error) {
return gomock.Any(), f(ctx)
})

mockEventRepository.
EXPECT().
GetArchiveEvent(ctx, req.ArchiveEventID).
Return(entity.NewArchiveEvent(req.ArchiveEventID, device.ID, device.ClientID), nil)

mockArchiveRepository.
EXPECT().Save(ctx, gomock.Any()).
Return(nil)
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.runMock(tt.args.ctx, tt.args.req, tt.args.device)
err = sut.CreateArchive(tt.args.ctx, tt.args.req, tt.args.device)
tt.assertion(t, err)

if tt.wantErr != nil {
assert.EqualError(t, err, tt.wantErr.Error())
}
})
}
}
55 changes: 55 additions & 0 deletions internal/usecase/mock/transaction.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4bf89dc

Please sign in to comment.