miniauth/pkg/userstore/store_test.go

55 lines
1.4 KiB
Go

package userstore
import (
"os"
"testing"
_ "embed"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
func TestNewStore(t *testing.T) {
name, err := os.MkdirTemp("", "miniauth-teststore")
assert.Nil(t, err, "[setup] should create tmp folder without errror")
tts := []struct {
name string
sqlitePath string
exptError error
exptErrorString string
}{
{
name: "empty path should return error",
sqlitePath: "",
exptError: ErrCantOpenDatabase,
exptErrorString: "failed to open database: unsupported URL scheme: \nThis driver supports only URLs that start with libsql://, file://, https://, http://, wss:// and ws://",
},
{
name: "should be abel to create and ping locale database",
sqlitePath: "file://" + name + "/" + uuid.NewString(),
exptError: nil,
exptErrorString: "",
},
}
for _, tt := range tts {
t.Run(tt.name, func(t *testing.T) {
c := Config{}
c.SQLite.Path = tt.sqlitePath
_, err := NewStore(c)
if tt.exptError == nil {
assert.Nil(t, err, "should return no error while creating store")
} else {
assert.NotNil(t, err, "should return an errror")
assert.ErrorIs(t, err, tt.exptError)
assert.Equal(t, tt.exptErrorString, err.Error())
}
})
}
err = os.RemoveAll(name)
assert.Nil(t, err, "[setup] should be abel to remove sqlite file from hdd after test")
}