Modules
datastore-driver:AcolyteDatastore, a fakecom.google.cloud.datastore.Datastore.datastore-dsl:DatastoreDSL, helpers to declare cases.
The Google Cloud Datastore client (google-cloud-datastore) is not bundled: add it to your own dependencies, as the modules use it as provided.
Get started
Cases are checked in declaration order, and the first matching case answers the call. A call matching no case fails with a DatastoreException.
import static acolyte.datastore.DatastoreDSL.*;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.Key;
import com.google.cloud.datastore.Query;
import acolyte.datastore.AcolyteDatastore;
Key key = Key.newBuilder("project", "Form", "f1").build();
Entity form = Entity.newBuilder(key).set("label", "Foo").build();
AcolyteDatastore datastore = AcolyteDatastore.builder()
.onQuery(queryOf("Form"), q -> entities(form))
.onGet(keyOf(key), k -> form)
.onDelete(keysOf("Form"), k -> { throw failure(409, "Form is locked"); })
.build();
// Code under test uses datastore as any Datastore
datastore.get(key);
Cases
| Operation | Builder method | Answer |
|---|---|---|
run(Query) |
onQuery |
entities returned by the query |
get(Key), fetch(Key...) |
onGet |
entity, or null when absent |
allocateId(IncompleteKey) |
onAllocate |
allocated key |
add, put |
onInsert |
written entity |
update |
onUpdate |
action, may throw an error |
delete |
onDelete |
action, may throw an error |
Use onCommit and onRollback to observe transactions.
runInTransaction runs the callable against a transaction: a thrown exception rolls it back, and is rethrown as a DatastoreException with the original exception as cause.
Limitations
- Entity queries only; projections are not supported.
- Filters are matched by equality: a query matches
queryWith(kind, filter)only if its filter equals the declared one (same property, operator and value). Filters are compared as built, so use the samePropertyFilterfactory calls. - Transaction and execution options are ignored.
- Not supported: aggregations,
batch,reserveIds,newKeyFactory, explain options, and deferred ID allocation. These throwUnsupportedOperationException.
Use with Objectify
An AcolyteDatastore can back an Objectify factory (new ObjectifyFactory(datastore)), so entity mapping can be tested without a Datastore. Objectify saves go through the onInsert case. getOptions() returns offline options for project acolyte, overridable with Builder.options(...).