253 lines
5.3 KiB
Go
253 lines
5.3 KiB
Go
package driver
|
|
|
|
import (
|
|
"context"
|
|
"io/ioutil"
|
|
"local/dndex/storage/entity"
|
|
"local/dndex/storage/operator"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/boltdb/bolt"
|
|
"github.com/google/uuid"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
)
|
|
|
|
const (
|
|
testN = 5
|
|
testNS = "col"
|
|
)
|
|
|
|
func TestNewBoltDB(t *testing.T) {
|
|
_, can := tempBoltDB(t)
|
|
defer can()
|
|
}
|
|
|
|
func TestBoltDBCount(t *testing.T) {
|
|
bdb, can := tempBoltDB(t)
|
|
defer can()
|
|
|
|
ch, err := bdb.Find(context.TODO(), testNS, map[string]string{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ones := make([]entity.One, testN)
|
|
i := 0
|
|
for j := range ch {
|
|
var o entity.One
|
|
if err := bson.Unmarshal(j, &o); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ones[i] = o
|
|
i++
|
|
}
|
|
|
|
for name, filter := range map[string]struct {
|
|
filter interface{}
|
|
match bool
|
|
}{
|
|
"one.Query": {
|
|
filter: ones[0].Query(),
|
|
match: true,
|
|
},
|
|
"title:title": {
|
|
filter: map[string]interface{}{entity.Title: ones[1].Title},
|
|
match: true,
|
|
},
|
|
"title:title, text:text": {
|
|
filter: map[string]interface{}{entity.Title: ones[2].Title, entity.Text: ones[2].Text},
|
|
match: true,
|
|
},
|
|
"title:title, text:gibberish": {
|
|
filter: map[string]interface{}{entity.Title: ones[3].Title, entity.Text: ones[2].Text},
|
|
match: false,
|
|
},
|
|
"name:$in[gibberish]": {
|
|
filter: operator.NewFilterIn(entity.Name, []string{ones[0].Name + ones[1].Name}),
|
|
match: false,
|
|
},
|
|
"name:$in[name]": {
|
|
filter: operator.NewFilterIn(entity.Name, []string{ones[0].Name}),
|
|
match: true,
|
|
},
|
|
"name:$regex[gibberish]": {
|
|
filter: operator.Regex{Key: entity.Name, Value: ones[3].Name + ones[4].Name},
|
|
match: false,
|
|
},
|
|
"name:$regex[name]": {
|
|
filter: operator.Regex{Key: entity.Name, Value: ones[3].Name},
|
|
match: true,
|
|
},
|
|
} {
|
|
f := filter
|
|
t.Run(name, func(t *testing.T) {
|
|
n, err := bdb.count(context.TODO(), testNS, f.filter)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if f.match && n != 1 {
|
|
t.Fatalf("%v results for %+v, want match=%v", n, f, f.match)
|
|
} else if !f.match && n != 0 {
|
|
t.Fatalf("%v results for %+v, want match=%v", n, f, f.match)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBoltDBFind(t *testing.T) {
|
|
bdb, can := tempBoltDB(t)
|
|
defer can()
|
|
|
|
ch, err := bdb.Find(context.TODO(), testNS, map[string]string{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
n := 0
|
|
for b := range ch {
|
|
n++
|
|
o := entity.One{}
|
|
if err := bson.Unmarshal(b, &o); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if o.Type == "" {
|
|
t.Error(o.Type)
|
|
}
|
|
if o.Title == "" {
|
|
t.Error(o.Title)
|
|
}
|
|
if o.Image == "" {
|
|
t.Error(o.Image)
|
|
}
|
|
if o.Text == "" {
|
|
t.Error(o.Text)
|
|
}
|
|
if o.Relationship != "" {
|
|
t.Error(o.Relationship)
|
|
}
|
|
if o.Modified == 0 {
|
|
t.Error(o.Modified)
|
|
}
|
|
if len(o.Connections) == 0 {
|
|
t.Error(o.Connections)
|
|
}
|
|
for k := range o.Connections {
|
|
if o.Connections[k].Name == "" {
|
|
t.Error(o.Connections[k])
|
|
}
|
|
if o.Connections[k].Title == "" {
|
|
t.Error(o.Connections[k])
|
|
}
|
|
if o.Connections[k].Relationship == "" {
|
|
t.Error(o.Connections[k])
|
|
}
|
|
if o.Connections[k].Type == "" {
|
|
t.Error(o.Connections[k])
|
|
}
|
|
}
|
|
}
|
|
if n != testN {
|
|
t.Fatal(n)
|
|
}
|
|
}
|
|
|
|
func TestBoltDBUpdate(t *testing.T) {
|
|
t.Fatal("not impl")
|
|
}
|
|
|
|
func TestBoltDBInsert(t *testing.T) {
|
|
t.Fatal("not impl")
|
|
}
|
|
|
|
func TestBoltDBDelete(t *testing.T) {
|
|
bdb, can := tempBoltDB(t)
|
|
defer can()
|
|
|
|
ch, err := bdb.Find(context.TODO(), testNS, map[string]string{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ones := make([]entity.One, testN)
|
|
i := 0
|
|
for j := range ch {
|
|
var o entity.One
|
|
if err := bson.Unmarshal(j, &o); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ones[i] = o
|
|
i++
|
|
}
|
|
|
|
wantN := testN
|
|
for _, filter := range []interface{}{
|
|
ones[0].Query(),
|
|
operator.NewFilterIn(entity.Title, []string{ones[1].Title}),
|
|
operator.Regex{Key: entity.Text, Value: ones[2].Text},
|
|
} {
|
|
err = bdb.Delete(context.TODO(), testNS, filter)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wantN--
|
|
n, err := bdb.count(context.TODO(), testNS, map[string]string{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n != wantN {
|
|
t.Error(n, filter)
|
|
}
|
|
}
|
|
}
|
|
|
|
func tempBoltDB(t *testing.T) (*BoltDB, func()) {
|
|
f, err := ioutil.TempFile(os.TempDir(), "*.bolt.db")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.Close()
|
|
os.Args = []string{"a", "-dburi", f.Name()}
|
|
bdb := NewBoltDB()
|
|
fillBoltDB(t, bdb)
|
|
return bdb, func() {
|
|
bdb.db.Close()
|
|
os.Remove(f.Name())
|
|
}
|
|
}
|
|
|
|
func fillBoltDB(t *testing.T, bdb *BoltDB) {
|
|
if err := bdb.db.Update(func(tx *bolt.Tx) error {
|
|
bucket, err := tx.CreateBucketIfNotExists([]byte(testNS))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for i := 0; i < testN; i++ {
|
|
o := entity.One{
|
|
Name: "name-" + uuid.New().String()[:5],
|
|
Type: "type-" + uuid.New().String()[:5],
|
|
Title: "titl-" + uuid.New().String()[:5],
|
|
Image: "imge-" + uuid.New().String()[:5],
|
|
Text: "text-" + uuid.New().String()[:5],
|
|
Modified: time.Now().UnixNano(),
|
|
Connections: map[string]entity.One{},
|
|
}
|
|
p := entity.One{
|
|
Name: "name-" + uuid.New().String()[:5],
|
|
Type: "type-" + uuid.New().String()[:5],
|
|
Relationship: "rshp-" + uuid.New().String()[:5],
|
|
Title: "titl-" + uuid.New().String()[:5],
|
|
}
|
|
o.Connections[p.Name] = p
|
|
b, err := bson.Marshal(o)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := bucket.Put([]byte(o.Name), b); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|