s3-share/internal/db/mock.go

55 lines
1.1 KiB
Go
Raw Normal View History

2022-06-01 09:55:33 +00:00
package db
import (
"context"
"errors"
"git.kapelle.org/niklas/s3share/internal/types"
)
type mockDB struct {
shares map[string]*types.Share
}
func NewMock() DB {
return &mockDB{
shares: make(map[string]*types.Share),
}
}
func (d *mockDB) Close() error {
return nil
}
func (d *mockDB) CreateShare(ctx context.Context, share *types.Share) error {
if d.shares[share.Slug] != nil {
return errors.New("share already exists")
}
d.shares[share.Slug] = share
return nil
}
func (d *mockDB) DeleteShare(ctx context.Context, slug string) error {
if d.shares[slug] == nil {
return errors.New("share does not exist")
}
delete(d.shares, slug)
return nil
}
func (d *mockDB) GetAllShares(ctx context.Context) ([]*types.Share, error) {
// convert map to slice
shares := make([]*types.Share, 0, len(d.shares))
for _, share := range d.shares {
shares = append(shares, share)
}
return shares, nil
}
func (d *mockDB) GetShare(ctx context.Context, slug string) (*types.Share, error) {
if d.shares[slug] == nil {
return nil, nil
}
return d.shares[slug], nil
}