added unit testing
This commit is contained in:
54
internal/db/mock.go
Normal file
54
internal/db/mock.go
Normal file
@@ -0,0 +1,54 @@
|
||||
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
|
||||
}
|
||||
111
internal/db/mock_test.go
Normal file
111
internal/db/mock_test.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package db_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"git.kapelle.org/niklas/s3share/internal/db"
|
||||
"git.kapelle.org/niklas/s3share/internal/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func setup(t *testing.T) (db.DB, context.Context, *assert.Assertions) {
|
||||
service := db.NewMock()
|
||||
ctx := context.Background()
|
||||
assert := assert.New(t)
|
||||
|
||||
return service, ctx, assert
|
||||
}
|
||||
|
||||
func TestCreateShare(t *testing.T) {
|
||||
service, ctx, assert := setup(t)
|
||||
defer service.Close()
|
||||
|
||||
share := &types.Share{
|
||||
Slug: "123456",
|
||||
Key: "test.txt",
|
||||
}
|
||||
|
||||
err := service.CreateShare(ctx, share)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(service.GetShare(ctx, share.Slug))
|
||||
}
|
||||
|
||||
func TestCreateShareDup(t *testing.T) {
|
||||
service, ctx, assert := setup(t)
|
||||
defer service.Close()
|
||||
|
||||
share := &types.Share{
|
||||
Slug: "123456",
|
||||
Key: "test.txt",
|
||||
}
|
||||
|
||||
err := service.CreateShare(ctx, share)
|
||||
assert.NoError(err)
|
||||
|
||||
err = service.CreateShare(ctx, share)
|
||||
assert.Error(err)
|
||||
}
|
||||
|
||||
func TestDeleteShare(t *testing.T) {
|
||||
service, ctx, assert := setup(t)
|
||||
defer service.Close()
|
||||
|
||||
share := &types.Share{
|
||||
Slug: "123456",
|
||||
Key: "test.txt",
|
||||
}
|
||||
|
||||
err := service.CreateShare(ctx, share)
|
||||
assert.NoError(err)
|
||||
|
||||
err = service.DeleteShare(ctx, share.Slug)
|
||||
assert.NoError(err)
|
||||
assert.Nil(service.GetShare(ctx, share.Slug))
|
||||
}
|
||||
|
||||
func TestDeleteShareNotFound(t *testing.T) {
|
||||
service, ctx, assert := setup(t)
|
||||
defer service.Close()
|
||||
|
||||
share := &types.Share{
|
||||
Slug: "123456",
|
||||
Key: "test.txt",
|
||||
}
|
||||
|
||||
err := service.DeleteShare(ctx, share.Slug)
|
||||
assert.Error(err)
|
||||
}
|
||||
|
||||
func TestGetAllShares(t *testing.T) {
|
||||
service, ctx, assert := setup(t)
|
||||
defer service.Close()
|
||||
|
||||
share := &types.Share{
|
||||
Slug: "123456",
|
||||
Key: "test.txt",
|
||||
}
|
||||
|
||||
err := service.CreateShare(ctx, share)
|
||||
assert.NoError(err)
|
||||
|
||||
shares, err := service.GetAllShares(ctx)
|
||||
assert.NoError(err)
|
||||
assert.Len(shares, 1)
|
||||
|
||||
assert.Equal(share.Slug, shares[0].Slug)
|
||||
assert.Equal(share.Key, shares[0].Key)
|
||||
|
||||
// Create 2nd share
|
||||
share2 := &types.Share{
|
||||
Slug: "abcdef",
|
||||
Key: "test2",
|
||||
}
|
||||
|
||||
err = service.CreateShare(ctx, share2)
|
||||
assert.NoError(err)
|
||||
|
||||
shares, err = service.GetAllShares(ctx)
|
||||
assert.NoError(err)
|
||||
assert.Len(shares, 2)
|
||||
}
|
||||
Reference in New Issue
Block a user