如果需要复合索引,有没有办法为Java Google App Engine SDK配置测试失败?

Luk*_*ncl 6 java testing google-app-engine google-cloud-datastore

我想配置LocalDatastoreServiceTestConfig如果需要复合索引(例如,对多个属性进行排序的查询),查询将失败.有没有办法做到这一点?

我试过new LocalDatastoreServiceTestConfig().setNoIndexAutoGen(true)但它没有效果.

(使用Python SDK相应的方法.)

Rei*_*Rei 1

我认为“失败”的意思是“抛出异常”或类似的东西。如果是这样,您应该将autoGenerate您的属性设置WEB-INF/datastore-indexes.xml为 false。

例子WEB-INF/datastore-indexes.xml

<datastore-indexes autoGenerate="false">
</datastore-indexes>
Run Code Online (Sandbox Code Playgroud)

设置autoGenerate为 false 将使需要复合索引的查询抛出异常。示例代码:

try {
    Query q = new Query("Action")
            .addSort("encrypter", Query.SortDirection.ASCENDING)
            .addSort("requester", Query.SortDirection.ASCENDING)
            .addSort("time", Query.SortDirection.DESCENDING);

    //...snip...

} catch (Exception e) {
    log.severe(e.toString());
}
Run Code Online (Sandbox Code Playgroud)

我对此进行了测试并按预期记录了异常:

SEVERE: com.google.appengine.api.datastore.DatastoreNeedIndexException: Query com.google.appengine.api.datastore.dev.LocalCompositeIndexManager$IndexComponentsO
nlyQuery@f9f81ad3 requires a composite index that is not defined. You must update C:\appengine-java-sdk\dev\core1\war\WEB-INF\datastore-indexes.xml or enable au
toGenerate to have it automatically added.
The suggested index for this query is:
    <datastore-index kind="Action" ancestor="false" source="manual">
        <property name="encrypter" direction="asc"/>
        <property name="requester" direction="asc"/>
        <property name="time" direction="desc"/>
    </datastore-index>
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅datastore-indexes.xml 参考