Skip to content

Instantly share code, notes, and snippets.

@jshirley
Created January 4, 2016 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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

To run this, install the go_appengine package, put this in a directory in $GOPATH and run goapp test, output is:

2016/01/03 19:48:46 appengine: not running under devappserver2; using some default configuration
INFO     2016-01-04 03:48:47,077 devappserver2.py:769] Skipping SDK update check.
WARNING  2016-01-04 03:48:47,077 devappserver2.py:785] DEFAULT_VERSION_HOSTNAME will not be set correctly with --port=0
WARNING  2016-01-04 03:48:47,106 simple_search_stub.py:1126] Could not read search indexes from /var/folders/4j/2s8jcbwd2yx7xxjvjmmp76mm0000gn/T/appengine.testapp.jshirley/search_indexes
INFO     2016-01-04 03:48:47,109 api_server.py:205] Starting API server at: http://localhost:51080
INFO     2016-01-04 03:48:47,113 dispatcher.py:197] Starting module "default" running at: http://localhost:51081
INFO     2016-01-04 03:48:47,115 admin_server.py:116] Starting admin server at: http://localhost:51082
INFO     2016-01-04 03:48:48,005 datastore_stub_index.py:301] Updating /private/var/folders/4j/2s8jcbwd2yx7xxjvjmmp76mm0000gn/T/appengine-aetest881550037/index.yaml
INFO     2016-01-04 03:48:48,116 api_server.py:648] Applying all pending transactions and saving the datastore
INFO     2016-01-04 03:48:48,116 api_server.py:651] Saving search indexes
--- FAIL: TestScheduleCraziness (2.92s)
        item_test.go:37: No record stored. Expected {admin@gmail.com}
FAIL
exit status 1
FAIL    bitbucket.org/jsshirley/weird   2.928s

@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