Search in sources :

Example 31 with Query

use of com.google.appengine.api.datastore.Query 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 32 with Query

use of com.google.appengine.api.datastore.Query 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 33 with Query

use of com.google.appengine.api.datastore.Query 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 34 with Query

use of com.google.appengine.api.datastore.Query in project iosched by google.

the class ServingUrlManagerTest method testGetServingUrl_withSourceUrl.

@Test
public void testGetServingUrl_withSourceUrl() throws Exception {
    PreparedQuery mockPreparedQuery = mock(PreparedQuery.class);
    when(mockDatastoreService.prepare(any(Query.class))).thenReturn(mockPreparedQuery);
    when(mockPreparedQuery.asList(any(FetchOptions.class))).thenAnswer(new Answer<List<Entity>>() {

        @Override
        public List<Entity> answer(InvocationOnMock invocation) throws Throwable {
            Key key = mock(Key.class);
            when(key.getKind()).thenReturn(ServingUrlManager.ENTITY_KIND);
            when(key.getName()).thenReturn(GCS_FULLPATH);
            Entity entity = new Entity(key);
            entity.setProperty(ServingUrlManager.SERVING_URL_PROPERTY, SERVING_URL);
            return new ArrayList<>(Arrays.asList(entity));
        }
    });
    assertEquals(SERVING_URL, ServingUrlManager.INSTANCE.getServingUrl(SOURCE_URL));
    ArgumentCaptor<Query> queryCaptor = ArgumentCaptor.forClass(Query.class);
    verify(mockDatastoreService).prepare(queryCaptor.capture());
    Query query = queryCaptor.getValue();
    assertEquals(ServingUrlManager.ENTITY_KIND, query.getKind());
    assertEquals(new FilterPredicate(ServingUrlManager.SOURCE_URL_PROPERTY, FilterOperator.EQUAL, SOURCE_URL), query.getFilter());
}
Also used : FetchOptions(com.google.appengine.api.datastore.FetchOptions) Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArrayList(java.util.ArrayList) List(java.util.List) FilterPredicate(com.google.appengine.api.datastore.Query.FilterPredicate) Key(com.google.appengine.api.datastore.Key) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 35 with Query

use of com.google.appengine.api.datastore.Query in project CodeU-Spring-2018 by maksymko.

the class PersistentDataStore method loadUsers.

/**
 * Loads all User objects from the Datastore service and returns them in a List.
 *
 * @throws PersistentDataStoreException if an error was detected during the load from the
 *     Datastore service
 */
public List<User> loadUsers() throws PersistentDataStoreException {
    List<User> users = new ArrayList<>();
    // Retrieve all users from the datastore.
    Query query = new Query("chat-users");
    PreparedQuery results = datastore.prepare(query);
    for (Entity entity : results.asIterable()) {
        try {
            UUID uuid = UUID.fromString((String) entity.getProperty("uuid"));
            String userName = (String) entity.getProperty("username");
            String password = (String) entity.getProperty("password");
            Instant creationTime = Instant.parse((String) entity.getProperty("creation_time"));
            User user = new User(uuid, userName, password, creationTime);
            users.add(user);
        } catch (Exception e) {
            // database entity definition mismatches, or service mismatches.
            throw new PersistentDataStoreException(e);
        }
    }
    return users;
}
Also used : Entity(com.google.appengine.api.datastore.Entity) User(codeu.model.data.User) PersistentDataStoreException(codeu.model.store.persistence.PersistentDataStoreException) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Query(com.google.appengine.api.datastore.Query) Instant(java.time.Instant) ArrayList(java.util.ArrayList) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) UUID(java.util.UUID) PersistentDataStoreException(codeu.model.store.persistence.PersistentDataStoreException)

Aggregations

Query (com.google.appengine.api.datastore.Query)74 Entity (com.google.appengine.api.datastore.Entity)59 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)47 Test (org.junit.Test)38 FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)31 Filter (com.google.appengine.api.datastore.Query.Filter)20 Key (com.google.appengine.api.datastore.Key)16 CompositeFilter (com.google.appengine.api.datastore.Query.CompositeFilter)16 DatastoreService (com.google.appengine.api.datastore.DatastoreService)13 ArrayList (java.util.ArrayList)13 PersistentDataStoreException (codeu.model.store.persistence.PersistentDataStoreException)6 FetchOptions (com.google.appengine.api.datastore.FetchOptions)6 PrintWriter (java.io.PrintWriter)6 Instant (java.time.Instant)6 UUID (java.util.UUID)6 Conversation (codeu.model.data.Conversation)2 Message (codeu.model.data.Message)2 User (codeu.model.data.User)2 Book (com.example.getstarted.objects.Book)2 Result (com.example.getstarted.objects.Result)2