Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to configure tests for the Java Google App Engine SDK to fail if a compound index is needed?

I'd like to configure LocalDatastoreServiceTestConfig such that queries will fail if a compound index is needed (e.g., a query with a sort on multiple properties). Is there a way to do this?

I tried new LocalDatastoreServiceTestConfig().setNoIndexAutoGen(true) but it had no effect.

(There is a corresponding way to do this with the Python SDK.)

like image 952
Luke Francl Avatar asked Dec 22 '16 21:12

Luke Francl


1 Answers

I assume by "fail" you mean "throw an exception" or something similar. If so, you should set the autoGenerate attribute in your WEB-INF/datastore-indexes.xml to false.

Example WEB-INF/datastore-indexes.xml:

<datastore-indexes autoGenerate="false">
</datastore-indexes>

Setting autoGenerate to false will make a query that requires a composite index throw an exception. Example code:

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());
}

I tested this and got an exception logged as expected:

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>

For more information, see datastore-indexes.xml reference.

like image 133
Rei Avatar answered Oct 15 '22 23:10

Rei