More experimenting with tiedot

This commit is contained in:
Deluan
2016-02-27 03:35:01 -05:00
parent 4f9054b738
commit ecc0df9e7c
14 changed files with 175 additions and 35 deletions
+1 -1
View File
@@ -56,7 +56,7 @@ func (r *BaseRepository) saveOrUpdate(rec interface{}) error {
if err != nil {
return err
}
docId, err := r.queryFirstKey(`{"in": ["Id"], "eq": "%s"}`, m["Id"])
docId, err := r.queryFirstKey(`{"in": ["Id"], "eq": "%s", "limit": 1}`, m["Id"])
if docId == 0 {
_, err = r.col.Insert(m)
return err
+27 -9
View File
@@ -11,20 +11,38 @@ var (
once sync.Once
)
func createCollection(name string) *db.Col {
func createCollection(name string, ix ...interface{}) *db.Col {
log := false
if dbInstance().Use(name) == nil {
if err := dbInstance().Create(name); err != nil {
beego.Error(err)
}
log = true
}
col := dbInstance().Use(name)
if col != nil {
return col
}
if err := dbInstance().Create(name); err != nil {
beego.Error(err)
}
if err := col.Index([]string{"Id"}); err != nil {
beego.Error(name, err)
createIndex(col, []string{"Id"}, log)
for _, i := range ix {
switch i := i.(type) {
case string:
createIndex(col, []string{i}, log)
case []string:
createIndex(col, i, log)
default:
beego.Error("Trying to create an Index with an invalid type: ", i)
}
}
return col
}
func createIndex(col *db.Col, path []string, log bool) (err error) {
if err := col.Index(path); err != nil && log {
beego.Error(path, err)
}
return err
}
func dbInstance() *db.DB {
once.Do(func() {
instance, err := db.OpenDB(beego.AppConfig.String("dbPath"))
+54
View File
@@ -0,0 +1,54 @@
package repositories
import (
. "github.com/smartystreets/goconvey/convey"
_ "github.com/deluan/gosonic/tests"
"testing"
)
const (
testCollectionName = "TestCollection"
)
func TestCreateCollection(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
dbInstance().Drop(testCollectionName)
Convey("Given an empty DB", t, func() {
Convey("When creating a new collection", func() {
newCol := createCollection(testCollectionName)
Convey("Then it should create the collection", func() {
So(dbInstance().Use(testCollectionName), ShouldNotBeNil)
})
Convey("And it should create a default index on Id", func() {
allIndexes := newCol.AllIndexes()
So(len(allIndexes), ShouldEqual, 1)
So(len(allIndexes[0]), ShouldEqual, 1)
So(allIndexes[0], ShouldContain, "Id")
})
})
Convey("When creating a new collection with a 'Name' index", func() {
newCol := createCollection(testCollectionName, "Name")
Convey("Then it should create a default index [Id] and an index on 'Name'", func() {
listOfIndexes := newCol.AllIndexes()
So(len(listOfIndexes), ShouldEqual, 2)
var allPaths = make(map[string]bool)
for _, i := range listOfIndexes {
allPaths[i[0]] = true
}
So(allPaths, ShouldContainKey, "Id")
So(allPaths, ShouldContainKey, "Name")
})
})
})
}