Skip to content

Instantly share code, notes, and snippets.

@stephanos
Last active December 31, 2015 10:39
Show Gist options
  • Save stephanos/7974853 to your computer and use it in GitHub Desktop.
Save stephanos/7974853 to your computer and use it in GitHub Desktop.
// +build e2e
// +build appengine
package hrd
import (
. "101loops/bdd"
"appengine/memcache"
)
var _ = Sequence("HRD Read/Write", func() {
With("default settings", func() {
basicTests()
})
With("w/o cache", func() {
basicTests(NO_CACHE)
})
With("w/o local cache", func() {
basicTests(NO_LOCAL_CACHE)
})
With("w/o global cache", func() {
basicTests(NO_GLOBAL_CACHE)
})
With("with transaction", func() {
basicTests(TX)
})
})
func basicTests(flags ...Flag) {
var (
coll *Collection
newLoader func() *Loader
entity = &SimpleModel{Text: "test", Data: []byte{1, 2, 3}}
)
BeforeEach(func() {
store.ClearCache()
memcache.Flush(ctx)
newLoader = func() *Loader {
return coll.Load().Flags(flags...)
}
if coll == nil {
coll = randomColl()
}
})
It("saves an entity", func() {
key, err := coll.Save().Entity(entity)
Check(err, IsNil)
Check(key, NotNil)
Check(key.IntID(), IsGreaterThan, 0)
Check(entity.lifecycle, Equals, "after-save")
})
It("saves complete entity", func() {
entity.SetID(42)
key, err := coll.Save().EntityComplete(entity)
Check(err, IsNil)
Check(key, NotNil)
Check(key.IntID(), IsNum, 42)
Check(entity.ID(), IsNum, 42)
})
It("loads an entity", func() {
loaded := &SimpleModel{id: 42}
key, err := newLoader().GetOne(&loaded)
Check(err, IsNil)
Check(key, NotNil)
Check(loaded, NotNil)
Check(loaded.Text, Equals, "test")
})
It("deletes an entity", func() {
err := coll.Delete().ID(42)
Check(err, IsNil)
})
It("does not load deleted entity", func() {
loaded := &SimpleModel{id: 42}
key, err := newLoader().GetOne(&loaded)
Check(err, IsNil)
Check(key, IsNil)
Check(loaded, IsNil)
})
}
// +build e2e
// +build appengine
package hrd
import (
_ "fmt"
. "101loops/bdd"
"appengine/memcache"
)
var _ = Sequence("HRD Query", func() {
With("default settings", func() {
queryTests(true)
})
With("w/o hybrid", func() {
queryTests(false)
})
With("w/o cache", func() {
queryTests(true, NO_CACHE)
})
With("with transaction", func() {
queryTests(true, TX)
})
})
func queryTests(hybrid bool, flags ...Flag) {
var (
coll *Collection
newQuery func() *Query
)
BeforeEach(func() {
store.ClearCache()
memcache.Flush(ctx)
newQuery = func() *Query {
return coll.Query().Hybrid(hybrid).Flags(flags...)
}
if coll == nil {
coll = store.Coll("C")
}
})
Sequence("multiple simple models", func() {
simpleMdls := []*SimpleModel{
&SimpleModel{id: 1, Text: "test1"}, &SimpleModel{id: 2, Text: "test2"},
&SimpleModel{id: 3, Text: "test3"}, &SimpleModel{id: 4, Text: "test4"},
}
It("saves 4 entities", func() {
coll.Save().EntityComplete(simpleMdls[0])
coll.Save().EntityComplete(simpleMdls[1])
coll.Save().EntityComplete(simpleMdls[2])
coll.Save().EntityComplete(simpleMdls[3])
// TODO: doesn't work
keys, err := coll.Save().EntitiesComplete(simpleMdls)
Check(err, IsNil)
Check(keys, HasLen, 4)
Check(keys[0].IntID(), IsNum, 1)
Check(keys[1].IntID(), IsNum, 2)
Check(keys[2].IntID(), IsNum, 3)
Check(keys[3].IntID(), IsNum, 4)
})
It("counts entities", func() {
count, err := newQuery().GetCount()
Check(err, IsNil)
Check(count, IsNum, 4)
})
It("queries entity keys", func() {
keys, err := newQuery().GetKeys()
Check(err, IsNil)
Check(keys, HasLen, 4)
Check(keys[0].IntID(), IsNum, 1)
})
It("queries all entities", func() {
loaded := []*SimpleModel{}
keys, err := newQuery().GetAll(&loaded)
Check(err, IsNil)
Check(keys, HasLen, 4)
Check(loaded, HasLen, 4)
Check(keys[0].Source(), Equals, SOURCE_DATASTORE)
})
// It("queries a projection", func() {
// loaded := &SimpleModel{}
// err := coll.Query().Project("text").GetFirst(&loaded)
//
// Check(err, IsNil)
// fmt.Printf("%#v", loaded)
// Check(loaded.id, IsNum, 42)
// Check(loaded.Data, IsEmpty)
// Check(loaded.Text, Equals, "text")
// })
// It("queries an entity", func() {
// loaded := &SimpleModel{}
// err := coll.Query().Filter("html =", "test").GetFirst(&loaded)
//
// Check(err, IsNil)
// Check(loaded, NotNil)
// Check(loaded.Text, Equals, "text")
// })
It("queries filtered entities", func() {
loaded := []*SimpleModel{}
keys, err := newQuery().Filter("html =", "test1").GetAll(&loaded)
Check(err, IsNil)
Check(keys, HasLen, 1)
Check(loaded, HasLen, 1)
Check(loaded[0].Text, Equals, "test1")
})
It("queries by ascending order", func() {
loaded := []*SimpleModel{}
keys, err := newQuery().OrderAsc("html").GetAll(&loaded)
Check(err, IsNil)
Check(keys, HasLen, 4)
Check(loaded, HasLen, 4)
Check(loaded[0].Text, Equals, "test1")
Check(loaded[3].Text, Equals, "test4")
})
It("queries by descending order", func() {
loaded := []*SimpleModel{}
keys, err := newQuery().OrderDesc("html").GetAll(&loaded)
Check(err, IsNil)
Check(keys, HasLen, 4)
Check(loaded, HasLen, 4)
Check(loaded[0].Text, Equals, "test4")
Check(loaded[3].Text, Equals, "test1")
})
// It("query with cursor", func() {
// loaded := []*SimpleModel{}
// it := newQuery().Limit(2).Run()
// keys, err := it.GetAll(&loaded)
//
// Check(err, IsNil)
// Check(keys, HasLen, 2)
//
// loaded = []*SimpleModel{}
// cursor, _ := it.Cursor()
// keys, err = newQuery().Start(cursor).GetAll(&loaded)
//
// Check(err, IsNil)
// Check(keys, HasLen, 2)
// })
// It("deletes entity", func() {
// err := coll.Delete().ID(1)
// Check(err, IsNil)
//
// count, err := newQuery().GetCount()
// Check(err, IsNil)
// Check(count, IsNum, 3)
// })
})
Sequence("multiple complex models", func() {
// TODO
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment