Skip to content

Instantly share code, notes, and snippets.

@jshirley
Created January 4, 2016 03:49
Show Gist options
  • Save jshirley/713e6272f7d7c7ea997a to your computer and use it in GitHub Desktop.
Save jshirley/713e6272f7d7c7ea997a to your computer and use it in GitHub Desktop.
This shows a failing test that I'm unable to figure out what I'm doing wrong.
package hello
import (
"appengine/aetest"
"appengine/datastore"
"reflect"
"testing"
)
type Foo struct {
Account string
}
func TestScheduleCraziness(t *testing.T) {
collectionName := "Foo"
accountName := "user@example.com"
ctx, err := aetest.NewContext(nil)
if err != nil {
t.Fatalf("Failed to create context: %v", err)
}
defer ctx.Close()
item := Foo{Account: "admin@gmail.com"}
ancestorKey := datastore.NewKey(ctx, "Account", accountName, 0, nil)
_, err = datastore.Put(ctx, datastore.NewIncompleteKey(ctx, collectionName, ancestorKey), &item)
if err != nil {
t.Fatalf("Got an error from datastore.Put: %s\n", err)
return
}
var s Foo
query := datastore.NewQuery(collectionName).Ancestor(ancestorKey).Order("-Date").Limit(1)
_, err = query.Run(ctx).Next(&s)
if err == datastore.Done {
t.Errorf("No record stored. Expected %v", item)
return
}
if err != nil {
t.Errorf("Failed to fetch record: %v", err)
return
}
if !reflect.DeepEqual(s, item) {
t.Errorf("Records don't match.\nGot: %v\nWant: %v", s, item)
}
}
@jshirley
Copy link
Author

jshirley commented Jan 4, 2016

As soon as I posted this gist, I realized the errors of my ways.

Date is not indexed (in fact, I took it out to repro this case) and Order(...) will fail on any unindexed value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment