Google App Engine数据存储 - 测试查询失败 [英] Google App Engine Datastore - Testing Queries fails

查看:128
本文介绍了Google App Engine数据存储 - 测试查询失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试测试一段在数据存储上运行查询的代码,然后再放入新的实体以确保不会创建重复项。我写的代码在应用程序的上下文中工作正常,但我为这些方法编写的测试失败。看起来我无法通过测试包上下文中的查询来访问放入数据存储区的数据。

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 wrote works fine in the context of the app, but the tests I wrote for that methods are failing. It seems that I cannot access data put into the datastore through queries in the context of the testing package.

一种可能性可能在于 goapp test 其中的内容如下:应用所有挂起的事务并保存数据存储。在调用get和put方法后,打印出此行(我使用log语句验证了这一点)。

One possibility might lie in the output from goapp test which reads: Applying all pending transactions and saving the datastore. This line prints out after both the get and put methods are called (I verified this with log statements).

我尝试关闭上下文并为不同的方法创建一个新的方法操作,但不幸的是,这也没有帮助。下面是一个简单的测试用例,它放入一个对象,然后在其上运行查询。任何帮助将不胜感激。

I tried closing the context and creating a new one for the different operations, but unfortunately that didn't help either. Below is a simple test case that Puts in an object and then runs a query on it. Any help would be appreciated.

type Entity struct {
    Value string
}

func TestEntityQuery(t *testing.T) {
    c, err := aetest.NewContext(nil)
    if err != nil {
        t.Fatal(err)
    }
    defer c.Close()

    key := datastore.NewIncompleteKey(c, "Entity", nil)
    key, err = datastore.Put(c, key, &Entity{Value: "test"})
    if err != nil {
        t.Fatal(err)
    }

    q := datastore.NewQuery("Entity").Filter("Value =", "test")
    var entities []Entity
    keys, err := q.GetAll(c, &entities)
    if err != nil {
        t.Fatal(err)
    }
    if len(keys) == 0 {
        t.Error("No keys found in query")
    }
    if len(entities) == 0 {
        t.Error("No entities found in query")
    }
}


推荐答案

您的测试代码没有问题。问题在于数据存储本身。 HR数据存储中的大多数查询都不是立即一致,而是最终一致。您可以在数据存储文档中阅读关于此的更多信息。

There is nothing wrong with your test code. The issue lies in the Datastore itself. Most queries in the HR Datastore are not "immediately consistent" but eventually consistent. You can read more about this in the Datastore documentation.

因此,基本上发生的情况是您将实体放入数据存储区,并且SDK的数据存储区会模拟您在生产中可能观察到的延迟,因此如果在此之后运行查询这不是一个祖先查询),查询结果将不包括您刚刚保存的新实体。

So basically what happens is that you put an entity into the Datastore, and the SDK's Datastore "simulates" the latency that you can observe in production, so if you run a query right after that (which is not an ancestor query), the query result will not include the new entity you just saved.

如果您在 datastore.Put() q.GetAll(),你会看到测试通过。尝试一下。在我的测试中,它足以让人睡上100毫秒,并且测试总是通过。但是当为这种情况编写测试时,可以使用 StronglyConsistentDatastore:true 选项,正如JonhGB的答案中所看到的那样

If you put a few seconds sleep between the datastore.Put() and q.GetAll(), you will see the test passes. Try it. In my test it was enough to sleep just 100ms, and the test always passed. But when writing tests for such cases, use the StronglyConsistentDatastore: true option as can be seen in JonhGB's answer.

如果您使用祖先,您还会看到测试通过而无法入睡查询,因为它们强烈一致

You would also see the test pass without sleep if you'd use Ancestor queries because they are strongly consistent.

这篇关于Google App Engine数据存储 - 测试查询失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆