Search in sources :

Example 6 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.

the class LocalDatastoreTest method doTest.

// Run this test twice to prove we're not leaking any state across tests.
private void doTest() {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    assertEquals(0, ds.prepare(new Query("yam")).countEntities(withLimit(10)));
    ds.put(new Entity("yam"));
    ds.put(new Entity("yam"));
    assertEquals(2, ds.prepare(new Query("yam")).countEntities(withLimit(10)));
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) DatastoreService(com.google.appengine.api.datastore.DatastoreService)

Example 7 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.

the class ShortTest method testDisabledDatastore.

@Test(expected = ApiProxy.CapabilityDisabledException.class)
public void testDisabledDatastore() {
    Capability testOne = new Capability("datastore_v3");
    CapabilityStatus testStatus = CapabilityStatus.DISABLED;
    // Initialize the test configuration.
    LocalCapabilitiesServiceTestConfig config = new LocalCapabilitiesServiceTestConfig().setCapabilityStatus(testOne, testStatus);
    helper = new LocalServiceTestHelper(config);
    helper.setUp();
    FetchOptions fo = FetchOptions.Builder.withLimit(10);
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    assertEquals(0, ds.prepare(new Query("yam")).countEntities(fo));
}
Also used : FetchOptions(com.google.appengine.api.datastore.FetchOptions) Capability(com.google.appengine.api.capabilities.Capability) Query(com.google.appengine.api.datastore.Query) DatastoreService(com.google.appengine.api.datastore.DatastoreService) LocalServiceTestHelper(com.google.appengine.tools.development.testing.LocalServiceTestHelper) CapabilityStatus(com.google.appengine.api.capabilities.CapabilityStatus) LocalCapabilitiesServiceTestConfig(com.google.appengine.tools.development.testing.LocalCapabilitiesServiceTestConfig) Test(org.junit.Test)

Example 8 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.

the class LocalCustomPolicyHighRepDatastoreTest method testEventuallyConsistentGlobalQueryResult.

@Test
public void testEventuallyConsistentGlobalQueryResult() {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    // applies
    ds.put(new Entity("yam"));
    // does not apply
    ds.put(new Entity("yam"));
    // First global query only sees the first Entity.
    assertEquals(1, ds.prepare(new Query("yam")).countEntities(withLimit(10)));
    // Second global query sees both Entities because we "groom" (attempt to
    // apply unapplied jobs) after every query.
    assertEquals(2, ds.prepare(new Query("yam")).countEntities(withLimit(10)));
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) DatastoreService(com.google.appengine.api.datastore.DatastoreService) Test(org.junit.Test)

Example 9 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.

the class ReadPolicyTest method readPolicy_eventual_returnsNoResults.

@Test
public void readPolicy_eventual_returnsNoResults() {
    // [START data_consistency]
    double deadline = 5.0;
    // Construct a read policy for eventual consistency
    ReadPolicy policy = new ReadPolicy(ReadPolicy.Consistency.EVENTUAL);
    // Set the read policy
    DatastoreServiceConfig eventuallyConsistentConfig = DatastoreServiceConfig.Builder.withReadPolicy(policy);
    // Set the call deadline
    DatastoreServiceConfig deadlineConfig = DatastoreServiceConfig.Builder.withDeadline(deadline);
    // Set both the read policy and the call deadline
    DatastoreServiceConfig datastoreConfig = DatastoreServiceConfig.Builder.withReadPolicy(policy).deadline(deadline);
    // Get Datastore service with the given configuration
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(datastoreConfig);
    // [END data_consistency]
    Entity parent = new Entity("Person", "a");
    Entity child = new Entity("Person", "b", parent.getKey());
    datastore.put(ImmutableList.<Entity>of(parent, child));
    // Even though we are using an ancestor query, the policy is set to
    // eventual, so we should get eventually-consistent results. Since the
    // local data store test config is set to 100% unapplied jobs, there
    // should be no results.
    Query q = new Query("Person").setAncestor(parent.getKey());
    List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
    assertWithMessage("query results").that(results).isEmpty();
}
Also used : ReadPolicy(com.google.appengine.api.datastore.ReadPolicy) Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) DatastoreServiceConfig(com.google.appengine.api.datastore.DatastoreServiceConfig) DatastoreService(com.google.appengine.api.datastore.DatastoreService) Test(org.junit.Test)

Example 10 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project java-docs-samples by GoogleCloudPlatform.

the class ReadPolicyTest method readPolicy_strong_returnsAllResults.

@Test
public void readPolicy_strong_returnsAllResults() {
    double deadline = 5.0;
    ReadPolicy policy = new ReadPolicy(ReadPolicy.Consistency.STRONG);
    DatastoreServiceConfig datastoreConfig = DatastoreServiceConfig.Builder.withReadPolicy(policy).deadline(deadline);
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(datastoreConfig);
    Entity parent = new Entity("Person", "a");
    Entity child = new Entity("Person", "b", parent.getKey());
    datastore.put(ImmutableList.<Entity>of(parent, child));
    Query q = new Query("Person").setAncestor(parent.getKey());
    List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
    assertWithMessage("query results").that(results).hasSize(2);
}
Also used : ReadPolicy(com.google.appengine.api.datastore.ReadPolicy) Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) DatastoreServiceConfig(com.google.appengine.api.datastore.DatastoreServiceConfig) DatastoreService(com.google.appengine.api.datastore.DatastoreService) Test(org.junit.Test)

Aggregations

DatastoreService (com.google.appengine.api.datastore.DatastoreService)59 Entity (com.google.appengine.api.datastore.Entity)38 Test (org.junit.Test)29 Key (com.google.appengine.api.datastore.Key)22 Query (com.google.appengine.api.datastore.Query)20 Transaction (com.google.appengine.api.datastore.Transaction)15 EntityNotFoundException (com.google.appengine.api.datastore.EntityNotFoundException)6 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)6 LocalDatastoreService (com.google.appengine.api.datastore.dev.LocalDatastoreService)6 Date (java.util.Date)5 Filter (com.google.appengine.api.datastore.Query.Filter)4 FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)3 ArrayList (java.util.ArrayList)3 FilterChain (javax.servlet.FilterChain)3 BlobKey (com.google.appengine.api.blobstore.BlobKey)2 DatastoreServiceConfig (com.google.appengine.api.datastore.DatastoreServiceConfig)2 FetchOptions (com.google.appengine.api.datastore.FetchOptions)2 CompositeFilter (com.google.appengine.api.datastore.Query.CompositeFilter)2 ReadPolicy (com.google.appengine.api.datastore.ReadPolicy)2 Text (com.google.appengine.api.datastore.Text)2