Search in sources :

Example 76 with Query

use of org.skife.jdbi.v2.Query in project packages-jpl by SWI-Prolog.

the class TestJUnit method testHasMoreElements1.

@SuppressWarnings("unchecked")
public void testHasMoreElements1() {
    StringBuffer sb = new StringBuffer();
    Query q = new Query("atom_chars(prolog, Cs), member(C, Cs)");
    Map<String, Term> soln;
    q.open();
    while (q.hasMoreElements()) {
        soln = (Map<String, Term>) q.nextElement();
        sb.append(((Atom) soln.get("C")).name());
    }
    q.close();
    assertEquals("Query#hasMoreElements() + Query#nextElement() work as intended", "prolog", sb.toString());
}
Also used : Query(org.jpl7.Query) Term(org.jpl7.Term)

Example 77 with Query

use of org.skife.jdbi.v2.Query in project packages-jpl by SWI-Prolog.

the class TestJUnit method testGetSolution2.

public void testGetSolution2() {
    // this query has no solutions
    Query q = new Query("fail");
    // this opens the query
    q.open();
    // this finds no solution, and closes the query
    q.getSolution();
    try {
        // this call is invalid, as the query is closed
        q.getSolution();
        // shouldn't get to here
        fail("jpl.Query#getSolution() shoulda thrown JPLException");
    } catch (JPLException e) {
        // in detail?
        if (e.getMessage().endsWith("Query is not open")) {
        // ...which
        // should throw
        // a
        // JPLException
        // like this
        // OK: an appropriate exception was thrown
        } else {
            fail("jpl.Query#getSolution() threw incorrect JPLException: " + e);
        }
    } catch (Exception e) {
        fail("jpl.Query#getSolution() threw wrong class of exception: " + e);
    }
}
Also used : Query(org.jpl7.Query) JPLException(org.jpl7.JPLException) PrologException(org.jpl7.PrologException) JPLException(org.jpl7.JPLException)

Example 78 with Query

use of org.skife.jdbi.v2.Query in project packages-jpl by SWI-Prolog.

the class TestJUnit method testVariableBinding1.

public void testVariableBinding1() {
    Term lhs = new Compound("p", new Term[] { new Variable("X"), new Variable("Y") });
    Term rhs = new Compound("p", new Term[] { new Atom("a"), new Atom("b") });
    Term goal = new Compound("=", new Term[] { lhs, rhs });
    Map<String, Term> soln = new Query(goal).oneSolution();
    assertTrue("two Variables with different names can bind to distinct atoms", soln != null && (soln.get("X")).name().equals("a") && (soln.get("Y")).name().equals("b"));
}
Also used : Variable(org.jpl7.Variable) Query(org.jpl7.Query) Compound(org.jpl7.Compound) Term(org.jpl7.Term) Atom(org.jpl7.Atom)

Example 79 with Query

use of org.skife.jdbi.v2.Query in project beam by apache.

the class DatastoreV1Test method readFnTest.

/**
 * Helper function to run a test reading from a {@link ReadFn}.
 */
private void readFnTest(int numEntities) throws Exception {
    // An empty query to read entities.
    Query query = Query.newBuilder().setLimit(Int32Value.newBuilder().setValue(numEntities)).build();
    // Use mockResponseForQuery to generate results.
    when(mockDatastore.runQuery(any(RunQueryRequest.class))).thenAnswer(invocationOnMock -> {
        Query q = ((RunQueryRequest) invocationOnMock.getArguments()[0]).getQuery();
        return mockResponseForQuery(q);
    });
    ReadFn readFn = new ReadFn(V_1_OPTIONS, mockDatastoreFactory);
    DoFnTester<Query, Entity> doFnTester = DoFnTester.of(readFn);
    /**
     * Although Datastore client is marked transient in {@link ReadFn}, when injected through mock
     * factory using a when clause for unit testing purposes, it is not serializable because it
     * doesn't have a no-arg constructor. Thus disabling the cloning to prevent the test object from
     * being serialized.
     */
    doFnTester.setCloningBehavior(CloningBehavior.DO_NOT_CLONE);
    List<Entity> entities = doFnTester.processBundle(query);
    int expectedNumCallsToRunQuery = (int) Math.ceil((double) numEntities / QUERY_BATCH_LIMIT);
    verify(mockDatastore, times(expectedNumCallsToRunQuery)).runQuery(any(RunQueryRequest.class));
    // Validate the number of results.
    assertEquals(numEntities, entities.size());
}
Also used : Entity(com.google.datastore.v1.Entity) DeleteEntity(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.DeleteEntity) GqlQuery(com.google.datastore.v1.GqlQuery) Query(com.google.datastore.v1.Query) ReadFn(org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.Read.ReadFn) RunQueryRequest(com.google.datastore.v1.RunQueryRequest)

Example 80 with Query

use of org.skife.jdbi.v2.Query in project beam by apache.

the class DatastoreV1Test method testReadValidationFailsQueryLimitNegative.

@Test
public void testReadValidationFailsQueryLimitNegative() throws Exception {
    Query invalidLimit = Query.newBuilder().setLimit(Int32Value.newBuilder().setValue(-5)).build();
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Invalid query limit -5: must be positive");
    DatastoreIO.v1().read().withQuery(invalidLimit);
}
Also used : GqlQuery(com.google.datastore.v1.GqlQuery) Query(com.google.datastore.v1.Query) Test(org.junit.Test)

Aggregations

Query (org.jpl7.Query)56 Term (org.jpl7.Term)33 Variable (org.jpl7.Variable)23 Map (java.util.Map)18 Atom (org.jpl7.Atom)16 Compound (org.jpl7.Compound)16 Query (com.google.datastore.v1.Query)12 Handle (org.skife.jdbi.v2.Handle)12 Test (org.junit.Test)10 GqlQuery (com.google.datastore.v1.GqlQuery)7 Integer (org.jpl7.Integer)6 SQLException (java.sql.SQLException)5 ArrayList (java.util.ArrayList)5 Datastore (com.google.datastore.v1.client.Datastore)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 List (java.util.List)4 SplitQueryFn (org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.Read.SplitQueryFn)4 RunQueryRequest (com.google.datastore.v1.RunQueryRequest)3 Nullable (javax.annotation.Nullable)3