Skip to content

Commit

Permalink
Add service interface with Mock (#7)
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Shishkin <me@teran.dev>
  • Loading branch information
teran authored Jul 7, 2024
1 parent 56e01e7 commit 423de2c
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
78 changes: 78 additions & 0 deletions service/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package service

import (
"context"
"io"

"github.com/stretchr/testify/mock"
)

var _ ManageService = (*Mock)(nil)

type Mock struct {
mock.Mock
}

func NewMock() *Mock {
return &Mock{}
}

func (m *Mock) CreateContainer(_ context.Context, name string) error {
args := m.Called(name)
return args.Error(0)
}

func (m *Mock) ListContainers(context.Context) ([]string, error) {
args := m.Called()
return args.Get(0).([]string), args.Error(1)
}

func (m *Mock) DeleteContainer(_ context.Context, name string) error {
args := m.Called(name)
return args.Error(0)
}

func (m *Mock) CreateVersion(_ context.Context, container string) (id string, err error) {
args := m.Called(container)
return args.Get(0).(string), args.Error(1)
}

func (m *Mock) ListVersions(_ context.Context, container string) ([]string, error) {
args := m.Called(container)
return args.Get(0).([]string), args.Error(1)
}

func (m *Mock) PublishVersion(_ context.Context, container, id string) error {
args := m.Called(container, id)
return args.Error(0)
}

func (m *Mock) DeleteVersion(_ context.Context, container, id string) error {
args := m.Called(container, id)
return args.Error(0)
}

func (m *Mock) AddObject(_ context.Context, container, versionID, key string, objReader io.Reader) error {
data, err := io.ReadAll(objReader)
if err != nil {
return err
}

args := m.Called(container, versionID, key, data)
return args.Error(0)
}

func (m *Mock) ListObjects(_ context.Context, container, versionID string) ([]string, error) {
args := m.Called(container, versionID)
return args.Get(0).([]string), args.Error(1)
}

func (m *Mock) GetObjectURL(ctx context.Context, container, versionID, key string) (string, error) {
args := m.Called(container, versionID, key)
return args.String(0), args.Error(1)
}

func (m *Mock) DeleteObject(_ context.Context, container, versionID, key string) error {
args := m.Called(container)
return args.Error(0)
}
89 changes: 89 additions & 0 deletions service/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package service

import (
"context"
"io"

"github.com/teran/archived/repositories/blob"
"github.com/teran/archived/repositories/metadata"
)

type ManageService interface {
AccessService

CreateContainer(ctx context.Context, name string) error
DeleteContainer(ctx context.Context, name string) error

CreateVersion(ctx context.Context, container string) (id string, err error)
PublishVersion(ctx context.Context, container, id string) error
DeleteVersion(ctx context.Context, container, id string) error

AddObject(ctx context.Context, container, versionID, key string, objReader io.Reader) error
DeleteObject(ctx context.Context, container, versionID, key string) error
}

type AccessService interface {
ListContainers(ctx context.Context) ([]string, error)

ListVersions(ctx context.Context, container string) ([]string, error)

ListObjects(ctx context.Context, container, versionID string) ([]string, error)
GetObjectURL(ctx context.Context, container, versionID, key string) (string, error)
}

type service struct {
mdRepo metadata.Repository
blobRepo blob.Repository
}

func NewManageService(mdRepo metadata.Repository, blobRepo blob.Repository) ManageService {
return &service{}
}

func NewAccessService(mdRepo metadata.Repository, blobRepo blob.Repository) AccessService {
return &service{}
}

func (s *service) CreateContainer(ctx context.Context, name string) error {
return s.mdRepo.CreateContainer(ctx, name)
}

func (s *service) ListContainers(ctx context.Context) ([]string, error) {
return s.mdRepo.ListContainers(ctx)
}

func (s *service) DeleteContainer(ctx context.Context, name string) error {
return s.mdRepo.DeleteContainer(ctx, name)
}

func (s *service) CreateVersion(ctx context.Context, container string) (id string, err error) {
panic("not implemented")
}

func (s *service) ListVersions(ctx context.Context, container string) ([]string, error) {
panic("not implemented")
}

func (s *service) PublishVersion(ctx context.Context, container, id string) error {
panic("not implemented")
}

func (s *service) DeleteVersion(ctx context.Context, container, id string) error {
panic("not implemented")
}

func (s *service) AddObject(ctx context.Context, container, versionID, key string, objReader io.Reader) error {
panic("not implemented")
}

func (s *service) ListObjects(ctx context.Context, container, versionID string) ([]string, error) {
panic("not implemented")
}

func (s *service) GetObjectURL(ctx context.Context, container, versionID, key string) (string, error) {
panic("not implemented")
}

func (s *service) DeleteObject(ctx context.Context, container, versionID, key string) error {
panic("not implemented")
}

0 comments on commit 423de2c

Please sign in to comment.