mirror of
https://github.com/foomo/shop.git
synced 2026-08-13 05:00:27 +00:00
implemented NewPersistorWithIndex() and test
This commit is contained in:
@@ -24,6 +24,18 @@ type Persistor struct {
|
||||
// ~ CONSTRUCTOR
|
||||
//------------------------------------------------------------------
|
||||
|
||||
func NewPersistorWithIndex(mongoURL string, collection string, index mgo.Index) (p *Persistor, err error) {
|
||||
p, err = NewPersistor(mongoURL, collection)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = p.GetCollection().EnsureIndex(index)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// NewPersistor constructor
|
||||
func NewPersistor(mongoURL string, collection string) (p *Persistor, err error) {
|
||||
parsedURL, err := url.Parse(mongoURL)
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gopkg.in/mgo.v2"
|
||||
)
|
||||
|
||||
type Foo struct {
|
||||
FirstName string
|
||||
LastName string
|
||||
}
|
||||
|
||||
func TestPersistenceIndex(t *testing.T) {
|
||||
index := mgo.Index{
|
||||
Key: []string{"firstname", "lastname"},
|
||||
Unique: true,
|
||||
Background: true,
|
||||
}
|
||||
p, err := NewPersistorWithIndex("mongodb://dockerhost/test", "testindex", index)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = p.GetCollection().EnsureIndex(index)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = p.GetCollection().Insert(&Foo{
|
||||
FirstName: "Foo",
|
||||
LastName: "Bar",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = p.GetCollection().Insert(&Foo{
|
||||
FirstName: "Flo",
|
||||
LastName: "Bar",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = p.GetCollection().Insert(&Foo{
|
||||
FirstName: "Flo",
|
||||
LastName: "Bar",
|
||||
})
|
||||
if err == nil {
|
||||
t.Fail()
|
||||
t.Log("Did not expected that one to work!")
|
||||
}
|
||||
if err != nil {
|
||||
t.Log("Error: " + err.Error())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user