Google App Engine Datastore - Testing Queries fails

后端 未结 3 1126
终归单人心
终归单人心 2020-12-11 07:42

I am currently trying to test a piece of my code that runs a query on the datastore before putting in a new entity to ensure that duplicates are not created. The code I wrot

3条回答
  •  囚心锁ツ
    2020-12-11 08:24

    The way to do this is to force the datastore to be strongly consistent by setting up the context like this:

    c, err := aetest.NewContext(&aetest.Options{StronglyConsistentDatastore: true})
        if err != nil {
            t.Fatal(err)
        }
    

    Now the datastore won't need any sleep to work, which is faster, and better practice in general.


    Update: This only works with the old aetest package which was imported via appengine/aetest. It does not work with the newer aetest package which is imported with google.golang.org/appengine/aetest. App Engine has changed from using an appengine.Context to using a context.Context, and consequently the way that the test package now works is quite different.

提交回复
热议问题