peachy-go/db_test.go

97 lines
2.3 KiB
Go
Raw Normal View History

2024-08-16 06:27:35 +00:00
package peachy_test
import (
"os"
"path/filepath"
"testing"
"git.codemonkeysoftware.net/b/peachy-go"
2024-11-17 01:21:51 +00:00
"github.com/shoenig/test"
2024-08-16 06:27:35 +00:00
"github.com/shoenig/test/must"
"zombiezen.com/go/sqlite"
)
func pending(t *testing.T) {
t.Fatalf("test not implemented")
}
func TestOpen(t *testing.T) {
t.Run("Open succeeds on DB created by Create", func(t *testing.T) {
dir := t.TempDir()
dbPath := filepath.Join(dir, "test.db")
db, err := peachy.Create(dbPath)
must.NoError(t, err)
db.Close()
db, err = peachy.Open(dbPath)
2024-11-17 01:21:51 +00:00
defer db.Close()
2024-08-16 06:27:35 +00:00
must.NoError(t, err)
// TODO Make sure the DB works once it has any methods.
})
t.Run("Open fails on alien SQLite DB", func(t *testing.T) {
dir := t.TempDir()
dbPath := filepath.Join(dir, "test.db")
conn, err := sqlite.OpenConn(dbPath)
must.NoError(t, err)
conn.Close()
db, err := peachy.Open(dbPath)
2024-11-17 01:21:51 +00:00
defer db.Close()
2024-08-16 06:27:35 +00:00
must.ErrorIs(t, err, peachy.ErrInvalidDB)
})
t.Run("Open fails on nonexistent file", func(t *testing.T) {
dir := t.TempDir()
dbPath := filepath.Join(dir, "test.db")
db, err := peachy.Open(dbPath)
2024-11-17 01:21:51 +00:00
defer db.Close()
2024-08-16 06:27:35 +00:00
must.ErrorIs(t, err, peachy.ErrFileNotExist)
must.FileNotExists(t, dbPath)
})
t.Run("Open fails on non-SQLite file", func(t *testing.T) {
path := filepath.Join(t.TempDir(), "test.db")
err := os.WriteFile(path, []byte("I'm not a database."), os.ModePerm)
must.NoError(t, err)
db, err := peachy.Open(path)
2024-11-17 01:21:51 +00:00
defer db.Close()
2024-08-16 06:27:35 +00:00
must.ErrorIs(t, err, peachy.ErrInvalidDB, must.Sprint(sqlite.ErrCode(err)))
})
}
2024-10-31 19:18:25 +00:00
2024-11-17 01:21:51 +00:00
func TestSiteName(t *testing.T) {
db, err := peachy.Create(":memory:")
must.NoError(t, err)
initial, err := db.SiteName()
test.NoError(t, err)
test.EqOp(t, "", initial)
const expected = "aoeuhtns"
test.NoError(t, db.SetSiteName(expected))
actual, err := db.SiteName()
test.NoError(t, err)
test.EqOp(t, expected, actual)
}
2024-11-01 22:32:02 +00:00
func TestCompositeTypes(t *testing.T) {
2024-10-31 19:18:25 +00:00
db, err := peachy.Create(":memory:")
must.NoError(t, err)
defer db.Close()
2024-11-01 22:32:02 +00:00
addedTypes := []peachy.CompositeType{
{Name: "alpha", Kind: peachy.Record},
{Name: `"bravo`, Kind: peachy.Variant},
{Name: " charl!e"},
}
for _, ty := range addedTypes {
err := db.AddCompositeType(ty.Name, ty.Kind)
2024-10-31 19:18:25 +00:00
must.NoError(t, err)
}
2024-11-01 22:32:02 +00:00
gotTypes, err := db.GetCompositeTypes()
2024-10-31 19:18:25 +00:00
must.NoError(t, err)
2024-11-01 22:32:02 +00:00
must.SliceContainsAllOp(t, addedTypes, gotTypes)
2024-10-31 19:18:25 +00:00
}