added tests

This commit is contained in:
2021-11-25 01:57:38 +01:00
parent 47befe6db1
commit 0971301562
4 changed files with 276 additions and 0 deletions

109
internal/types/id_test.go Normal file
View File

@@ -0,0 +1,109 @@
package types_test
import (
"testing"
"git.kapelle.org/niklas/s3browser/internal/types"
"github.com/stretchr/testify/assert"
)
// TODO: test version component (not yet used in code)
func TestIDParse(t *testing.T) {
assert := assert.New(t)
id := types.ParseID("test:/path/key")
assert.NotNil(id)
assert.True(id.Valid())
assert.Equal("test", id.Bucket)
assert.Equal("/path/key", id.Key)
assert.False(id.IsDirectory())
assert.Equal("key", id.Name())
assert.Equal("test:/path/key", id.String())
}
func TestIDParseInvalid(t *testing.T) {
assert := assert.New(t)
assert.Nil(types.ParseID("/asd/ad"))
assert.Nil(types.ParseID("test"))
assert.Nil(types.ParseID("test:"))
assert.Nil(types.ParseID(""))
assert.Nil(types.ParseID("/"))
}
func TestIDIsDir(t *testing.T) {
assert := assert.New(t)
idFile := types.ParseID("test:/path/key")
assert.NotNil(idFile)
assert.False(idFile.IsDirectory())
idDir := types.ParseID("test:/path/key/")
assert.NotNil(idDir)
assert.True(idDir.IsDirectory())
}
func TestIDRoot(t *testing.T) {
assert := assert.New(t)
id := types.ParseID("test:/")
assert.NotNil(id)
assert.True(id.Valid())
assert.Equal("test", id.Bucket)
assert.Equal("/", id.Key)
assert.True(id.IsDirectory())
assert.Equal("/", id.Name())
assert.Equal("test:/", id.String())
assert.Nil(id.Parent())
}
func TestIDParentFromFile(t *testing.T) {
assert := assert.New(t)
id := types.ParseID("test:/path1/path2/key")
assert.NotNil(id)
parent := id.Parent()
assert.NotNil(parent)
assert.True(parent.Valid())
assert.Equal("test", parent.Bucket)
assert.Equal("/path1/path2/", parent.Key)
assert.True(parent.IsDirectory())
assert.Equal("path2", parent.Name())
assert.Equal("test:/path1/path2/", parent.String())
}
func TestIDParentFromDir(t *testing.T) {
assert := assert.New(t)
id := types.ParseID("test:/path1/path2/")
assert.NotNil(id)
parent := id.Parent()
assert.NotNil(parent)
assert.True(parent.Valid())
assert.Equal("test", parent.Bucket)
assert.Equal("/path1/", parent.Key)
assert.True(parent.IsDirectory())
assert.Equal("path1", parent.Name())
assert.Equal("test:/path1/", parent.String())
}
func TestIDParentRoot(t *testing.T) {
assert := assert.New(t)
id := types.ParseID("test:/key1")
parent := id.Parent()
assert.NotNil(parent)
assert.Equal("/", parent.Key)
}