s3-share/internal/db/mock_test.go

112 lines
2.1 KiB
Go
Raw Normal View History

2022-06-01 09:55:33 +00:00
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)
}