s3-share/internal/s3/mock_test.go
2022-06-01 11:55:33 +02:00

73 lines
1.6 KiB
Go

package s3_test
import (
"context"
"io/ioutil"
"testing"
"git.kapelle.org/niklas/s3share/internal/s3"
"github.com/stretchr/testify/assert"
)
func setup(t *testing.T) (s3.S3, context.Context, *assert.Assertions) {
service := s3.NewMockS3()
ctx := context.Background()
assert := assert.New(t)
return service, ctx, assert
}
func TestGetObject(t *testing.T) {
service, ctx, assert := setup(t)
reader, err := service.GetObject(ctx, "test.txt")
assert.NoError(err)
assert.NotNil(reader)
content, err := ioutil.ReadAll(reader)
assert.NoError(err)
assert.Equal("test.txt", string(content))
}
func TestGetObjectNotFound(t *testing.T) {
service, ctx, assert := setup(t)
reader, err := service.GetObject(ctx, "not_existing.txt")
assert.Error(err)
assert.Nil(reader)
}
func TestGetMetadata(t *testing.T) {
service, ctx, assert := setup(t)
metadata, err := service.GetObjectMetadata(ctx, "test.txt")
assert.NoError(err)
assert.NotNil(metadata)
assert.Equal("text/plain", metadata.ContentType)
assert.Equal(int64(8), metadata.Size)
}
func TestGetMetadataNotFound(t *testing.T) {
service, ctx, assert := setup(t)
metadata, err := service.GetObjectMetadata(ctx, "not_existing.txt")
assert.NoError(err)
assert.Nil(metadata)
}
func TestKeyExists(t *testing.T) {
service, ctx, assert := setup(t)
exists, err := service.KeyExists(ctx, "test.txt")
assert.NoError(err)
assert.True(exists)
}
func TestKeyNotExist(t *testing.T) {
service, ctx, assert := setup(t)
exists, err := service.KeyExists(ctx, "not_existing.txt")
assert.NoError(err)
assert.False(exists)
}