tdb/db_test.go
2020-04-15 23:53:50 -05:00

120 lines
2.3 KiB
Go

package tdb
import (
"errors"
"testing"
// "encoding/ascii85"
// "log"
// "reflect"
// "strconv"
// "git.keganmyers.com/terribleplan/tdb/stringy"
// bolt "go.etcd.io/bbolt"
"github.com/golang/protobuf/proto"
)
func TestInvariants(t *testing.T) {
setupTestDb()
defer cleanupTestDb()
if tdb == nil {
t.Error("DB is nil")
}
if tdb.TEST_Main == nil {
t.Error("TEST_Main is nil")
}
if tdb.TEST_OwnedBy == nil {
t.Error("TEST_OwnedBy is nil")
}
}
func TestGet(t *testing.T) {
setupTestDb()
defer cleanupTestDb()
guarantee := randomString(16)
id := tdb.TEST_Main.CreateOrPanic(&TEST_Main{Guarantee: guarantee})
if assertNotEqualEnd(t, id, uint64(0), "Invalid inserted ID") {
return
}
item, err := tdb.TEST_Main.Get(id)
if assertNilEnd(t, err, "Unable to get record") {
return
}
if assertNotNilEnd(t, item, "Invalid result") {
return
}
tmi, ok := item.(*TEST_Main)
if assertOkEnd(t, ok, "Unable to cast returned to *TEST_Main") {
return
}
assertEqual(t, tmi.Guarantee, guarantee, "Mismatched guarantee strings")
}
func TestGetNil(t *testing.T) {
setupTestDb()
defer cleanupTestDb()
item, err := tdb.TEST_Main.Get(1)
if err != nil {
t.Errorf("WAT")
}
if assertNilEnd(t, err, "Unable to get record") {
return
}
assertNil(t, item, "Invalid result")
}
func TestUpdateAndGet(t *testing.T) {
setupTestDb()
defer cleanupTestDb()
guarantee := randomString(16)
id := tdb.TEST_Main.CreateOrPanic(&TEST_Main{Guarantee: guarantee})
if assertNilEnd(t, tdb.TEST_Main.Update(id, func(item proto.Message) error {
if assertNotNilEnd(t, item, "Invalid result") {
return errors.New("invoked with nil")
}
tmi, ok := item.(*TEST_Main)
if assertOkEnd(t, ok, "Unable to cast returned to *TEST_Main") {
return errors.New("bad type/cast")
}
if assertEqualEnd(t, tmi.Guarantee, guarantee, "Mismatched guarantee strings") {
return errors.New("bad guarantee")
}
guarantee = randomString(16)
tmi.Guarantee = guarantee
return nil
}), "Unable to update record") {
return
}
item, err := tdb.TEST_Main.Get(id)
if assertNilEnd(t, err, "Unable to get record") {
return
}
if assertNotNilEnd(t, item, "Invalid result") {
return
}
tmi, ok := item.(*TEST_Main)
if assertOkEnd(t, ok, "Unable to cast returned to *TEST_Main") {
return
}
assertEqual(t, tmi.Guarantee, guarantee, "Mismatched guarantee strings")
}