I’m making some unit tests over functions that loads an entity from Datastore using Objectify. I wish not to point at the Default database as it belongs to production. To run this tests, it was created a second database in the project importing data from Default db. The datastore config passed to objectify to run local tests points at Test db. The parameter received by the functions to test is a webSafeUrl from which a Key object is created to perform the search through method:
ofy().load().type(Entity.class).ancestor(akey).filterKey(key).first().now();
Now, when I run the tests in my local environment using the LocalDatastoreService, I get:
DatastoreException: mismatched databases within request…
When I inspect the Key object created from passed webSafeUrl, I can see that, even when the webSafeUrl contains the databaseId, the parameter is not passed to the Key builder used by
Key.create()
resulting in a Key object that leaves empty the databaseId and hence (I presume) throwing the mismatched database exception as the key seems to be pointing at the Default database.
My questions:
- is there a way to set the databaseId inside the Key once it was created?
- How could I test the load from the webSafeUrl of an entity in the Test database?
Any help is much appreciated!
I tried to recover the registry from Test db by extracting the id from the webSafeUrl and performing directly a search by id, but I get a NPE as the Key created from calling ofy().load().type(Entity.class).id(id).now() does not contain all the information about ancestors and therefore, is incomplete.
Astrid Perdomo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.