golang-lib/web/file/fs/fs_test.go

53 lines
1.3 KiB
Go
Raw Normal View History

2021-07-30 13:30:40 +02:00
package fs_test
2021-07-26 17:48:20 +02:00
import (
"io"
"os"
"strings"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
2023-10-23 21:40:10 +02:00
"codeberg.org/genofire/golang-lib/web/file"
"codeberg.org/genofire/golang-lib/web/file/fs"
2021-07-26 17:48:20 +02:00
)
func TestOpenStat(t *testing.T) {
assert := assert.New(t)
2021-07-30 13:30:40 +02:00
var fs file.FS = &fs.FS{Root: "./testdata"}
2021-07-26 17:48:20 +02:00
assert.NoError(fs.Check())
f, err := fs.Open("glenda")
assert.NoError(err)
assert.NotNil(f)
fi, err := f.Stat()
assert.NoError(err)
assert.NotNil(fi)
assert.Equal(uuid.MustParse("d2750ced-4bdc-41d0-8c2f-5b9de44b84ef"), fi.(file.FileInfo).ID())
assert.Equal("text/plain", fi.(file.FileInfo).ContentType())
assert.Equal("glenda", fi.Name())
assert.Equal(int64(99), fi.Size())
2021-07-26 17:48:20 +02:00
}
func TestCreateOpenUUIDRead(t *testing.T) {
assert := assert.New(t)
2021-07-30 13:30:40 +02:00
var fs file.FS = &fs.FS{Root: "./testdata"}
2021-07-26 17:48:20 +02:00
assert.NoError(fs.Check())
err := fs.Store(uuid.MustParse("f9375ccb-ee09-4ecf-917e-b88725efcb68"), "$name", "text/plain", strings.NewReader("hello, world\n"))
assert.NoError(err)
f, err := fs.OpenUUID(uuid.MustParse("f9375ccb-ee09-4ecf-917e-b88725efcb68"))
assert.NoError(err)
assert.NotNil(f)
buf, err := io.ReadAll(f)
assert.NoError(err)
assert.Equal("hello, world\n", string(buf))
os.RemoveAll("./testdata/f9375ccb-ee09-4ecf-917e-b88725efcb68")
}