@Test public void testCountDistinctWorkaround() { Entity fromOrder = from(Entity.class); Query<Long> select = select(function("count", Long.class, distinct(fromOrder.getInterface()))); assertEquals( "select count(distinct entity_0.interface) from Entity entity_0", select.getQuery()); }
@Test public void testCountFunction_withSpecifiedField_plusOneSelect_inverse() { Entity from = from(Entity.class); Query<Object[]> select = select(from.getCode(), count(from.getCode())); assertEquals( "select entity_0.code, count(entity_0.code) from Entity entity_0", select.getQuery()); }
@Test public void testCoalesceFunction() { Entity from = from(Entity.class); Query<String> select = select(coalesce(from.getCode(), from.getName())); assertEquals( "select coalesce(entity_0.code,entity_0.name) from Entity entity_0", select.getQuery()); }
@Test public void testCountDistinct() { Entity fromOrder = from(Entity.class); // throws an NPE Query<Long> select = select(count(distinct(fromOrder.getInterface()))); assertEquals( "select count(distinct entity_0.interface) from Entity entity_0", select.getQuery()); }
@Test public void testIndexFunction() { Entity from = from(Entity.class); SubEntity innerJoin = innerJoin(from.getSubEntities()); Query<Object[]> select = select(innerJoin, index(innerJoin)); assertEquals( "select subEntity_1, index(subEntity_1) from Entity entity_0 inner join entity_0.subEntities subEntity_1", select.getQuery()); }
@Test public void testCombiningFunctionWithInnerJoin() { Entity from = from(Entity.class); SubEntity innerJoin = innerJoin(from.getSubEntities()); Query<Object[]> select = select(distinct(from), innerJoin); assertEquals( "select distinct entity_0, subEntity_1 from Entity entity_0 inner join entity_0.subEntities subEntity_1", select.getQuery()); }
@Test public void testSpecifyFieldBeforeFunctionCount() { Entity from = from(Entity.class); groupBy(from.getCode()); Query<Object[]> select = select(from.getCode(), count(from)); assertEquals( "select entity_0.code, count(entity_0) from Entity entity_0 group by entity_0.code", select.getQuery()); }
@Test public void testFunctionOnObjectWithoutConstructor() { Entity from = from(Entity.class); groupBy(from.getCode()); Query<BigDecimal> select = select(sum(from.getBigDecimalField())); assertEquals( "select sum(entity_0.bigDecimalField) from Entity entity_0 group by entity_0.code", select.getQuery()); }
@Test public void testIndexFunction_into_Or() { Entity from = from(Entity.class); SubEntity innerJoin = innerJoin(from.getSubEntities()); where(index(innerJoin)).eq(5).or(index(innerJoin)).lt(2); Query<Object[]> select = select(innerJoin, index(innerJoin)); assertEquals( "select subEntity_1, index(subEntity_1) from Entity entity_0 inner join entity_0.subEntities subEntity_1 where index(subEntity_1) = :function_2 or index(subEntity_1) < :function_3", select.getQuery()); assertEquals(2, select.getParameters().get("function_3")); }
@Test public void testIndexFunction_in_where() { Entity from = from(Entity.class); SubEntity innerJoin = innerJoin(from.getSubEntities()); where(index(innerJoin)).gt(5); Query<Object[]> select = select(innerJoin, index(innerJoin)); assertEquals( "select subEntity_1, index(subEntity_1) from Entity entity_0 inner join entity_0.subEntities subEntity_1 where index(subEntity_1) > :function_2", select.getQuery()); assertEquals(5, select.getParameters().get("function_2")); }
/** Test method for number to return determination logic. */ @Test public void testNumberToReturnWhenNoBatchAndSmallishLimit() { final Random random = new Random(System.currentTimeMillis()); final Document doc1 = BuilderFactory.start().addInteger("1", 0).build(); final Document doc2 = BuilderFactory.start().addInteger("1", 1).build(); final String databaseName = "db"; final String collectionName = "collection"; final Document query = doc1; final Document returnFields = doc2; final int numberToSkip = random.nextInt(); final boolean tailable = random.nextBoolean(); final ReadPreference readPreference = random.nextBoolean() ? ReadPreference.PRIMARY : ReadPreference.SECONDARY; final boolean noCursorTimeout = random.nextBoolean(); final boolean awaitData = random.nextBoolean(); final boolean exhaust = random.nextBoolean(); final boolean partial = random.nextBoolean(); final int batchSize = 0; final int limit = 5; final Query message = new Query( databaseName, collectionName, query, returnFields, batchSize, limit, numberToSkip, tailable, readPreference, noCursorTimeout, awaitData, exhaust, partial); assertEquals(databaseName, message.getDatabaseName()); assertEquals(collectionName, message.getCollectionName()); assertEquals(numberToSkip, message.getNumberToSkip()); assertEquals(query, message.getQuery()); assertEquals(returnFields, message.getReturnFields()); assertEquals(Boolean.valueOf(awaitData), Boolean.valueOf(message.isAwaitData())); assertEquals(Boolean.valueOf(exhaust), Boolean.valueOf(message.isExhaust())); assertEquals(Boolean.valueOf(noCursorTimeout), Boolean.valueOf(message.isNoCursorTimeout())); assertEquals(Boolean.valueOf(partial), Boolean.valueOf(message.isPartial())); assertSame(readPreference, message.getReadPreference()); assertEquals(Boolean.valueOf(tailable), Boolean.valueOf(message.isTailable())); assertEquals(batchSize, message.getBatchSize()); assertEquals(limit, message.getLimit()); assertEquals(5, message.getNumberToReturn()); }
private QueryResult executeQueryControl() { Query query = queryPair.getControl(); QueryResult queryResult = new QueryResult(State.INVALID, null, null, ImmutableList.<List<Object>>of()); try { // startup queryResult = setup( query, controlPreQueryResults, controlPrequery -> executeQuery( controlGateway, controlUsername, controlPassword, queryPair.getControl(), controlPrequery, controlTimeout, sessionProperties)); // if startup is successful -> execute query if (queryResult.getState() == State.SUCCESS) { queryResult = executeQuery( controlGateway, controlUsername, controlPassword, queryPair.getControl(), query.getQuery(), controlTimeout, sessionProperties); } } finally { // teardown no matter what QueryResult tearDownResult = tearDown( query, controlPostQueryResults, controlPostquery -> executeQuery( controlGateway, controlUsername, controlPassword, queryPair.getControl(), controlPostquery, controlTimeout, sessionProperties)); // if teardown is not successful the query fails queryResult = tearDownResult.getState() == State.SUCCESS ? queryResult : tearDownResult; } return queryResult; }
@Test public void testCustomFunctionWithFunction() { Entity from = from(Entity.class); Query<String> select = select(function("toto", String.class, max(from.getIntegerField()))); assertEquals("select toto(max(entity_0.integerField)) from Entity entity_0", select.getQuery()); }
@Test public void testAvgFunction() { Entity from = from(Entity.class); Query<Integer> select = select(avg(from.getIntegerField())); assertEquals("select avg(entity_0.integerField) from Entity entity_0", select.getQuery()); }
@Test public void testSupportCustomFunction() { Entity from = from(Entity.class); Query<String> select = select(function("toto", String.class, from.getName())); assertEquals("select toto(entity_0.name) from Entity entity_0", select.getQuery()); }
@Test public void testCountFunction_withSpecifiedField() { Entity from = from(Entity.class); Query<Long> select = select(count(from.getCode())); assertEquals("select count(entity_0.code) from Entity entity_0", select.getQuery()); }
@Test public void testCountFunction_defaultCount() { Entity from = from(Entity.class); Query<Long> select = select(count(from)); assertEquals("select count(entity_0) from Entity entity_0", select.getQuery()); }
@Test public void testDistinctOnField() { Entity from = from(Entity.class); Query<String> select = select(distinct(from.getCode())); assertEquals("select distinct entity_0.code from Entity entity_0", select.getQuery()); }
@Test public void testDistinctOnInterface() { Entity fromOrder = from(Entity.class); Query select = select(distinct(fromOrder.getInterface())); assertEquals("select distinct entity_0.interface from Entity entity_0", select.getQuery()); }
@Test public void testDistinctEntity() { Entity from = from(Entity.class); Query<Entity> select = select(distinct(from)); assertEquals("select distinct entity_0 from Entity entity_0", select.getQuery()); }