Last active
December 31, 2015 11:19
-
-
Save onsi/7978758 to your computer and use it in GitHub Desktop.
e2e_basics_test suggestion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +build e2e | |
// +build appengine | |
package hrd | |
import ( | |
. "101loops/bdd" | |
"appengine/memcache" | |
_ "fmt" | |
) | |
var _ = Describe("HRD Query", func() { | |
Context("with default settings", func() { | |
queryTests(true) | |
}) | |
Context("w/o hybrid", func() { | |
queryTests(false) | |
}) | |
Context("w/o cache", func() { | |
queryTests(true, NO_CACHE) | |
}) | |
Context("with transaction", func() { | |
queryTests(true, TX) | |
}) | |
}) | |
func queryTests(hybrid bool, flags ...Flag) { //I like this pattern for sharing tests and I use it myself alot | |
var ( | |
coll *Collection | |
) | |
BeforeEach(func() { | |
store.ClearCache() | |
memcache.Flush(ctx) | |
coll = store.Coll("C") //always make a new collection, this avoids test pollution. | |
}) | |
Context("with 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"}, | |
} | |
BeforeEach(func() { | |
//save the four entities before each test | |
coll.Save().EntityComplete(simpleMdls[0]) | |
coll.Save().EntityComplete(simpleMdls[1]) | |
coll.Save().EntityComplete(simpleMdls[2]) | |
coll.Save().EntityComplete(simpleMdls[3]) | |
keys, err := coll.Save().EntitiesComplete(simpleMdls) | |
Check(err, IsNil) | |
}) | |
It("saves 4 entities", func() { | |
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) | |
}) | |
Describe("datastore queries", func() { //you don't need to nest things this way, but it might make things clearer... | |
var query *Query | |
var loaded *SimpleModel | |
BeforeEach(func() { | |
query = coll.Query().Hybrid(hybrid).Flags(flags...) //make the query here -- we'll get a fresh query for each test. and don't need the newQuery helper function | |
loaded = []*SimpleModel{} //can share this here, too -- it'll always be reset before each test | |
}) | |
It("counts entities", func() { | |
count, err := query.GetCount() | |
Check(err, IsNil) | |
Check(count, IsNum, 4) | |
}) | |
It("queries entity keys", func() { | |
keys, err := query.GetKeys() | |
Check(err, IsNil) | |
Check(keys, HasLen, 4) | |
Check(keys[0].IntID(), IsNum, 1) | |
}) | |
It("queries all entities", func() { | |
keys, err := query.GetAll(&loaded) | |
Check(err, IsNil) | |
Check(keys, HasLen, 4) | |
Check(loaded, HasLen, 4) | |
Check(keys[0].Source(), Equals, SOURCE_DATASTORE) | |
}) | |
It("queries filtered entities", func() { | |
keys, err := query.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() { | |
keys, err := query.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() { | |
keys, err := query.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") | |
}) | |
}) | |
}) | |
Context("with multiple complex models", func() { | |
BeforeEach(func() { | |
//save your complex models here... | |
}) | |
//... | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment