/** * Creates a {@link PropertyQuery} for the given property. * <p> * A {@link PropertyQuery} uses the same conditions as this Query object, * but returns only the value(s) of a single property (not an entity objects). * * @param property the property for which to return values */ public PropertyQuery property(Property property) { return new PropertyQuery(this, property); }
@Test public void testFindString() { Query<TestEntity> query = box.query().greater(simpleLong, 1002).build(); PropertyQuery propertyQuery = query.property(simpleString); assertNull(propertyQuery.findString()); assertNull(propertyQuery.reset().unique().findString()); putTestEntities(5); assertEquals("foo3", propertyQuery.reset().findString()); query = box.query().greater(simpleLong, 1004).build(); propertyQuery = query.property(simpleString); assertEquals("foo5", propertyQuery.reset().unique().findString()); putTestEntity(null, 6); putTestEntity(null, 7); query.setParameter(simpleLong, 1005); assertEquals("nope", propertyQuery.reset().distinct().nullValue("nope").unique().findString()); }
@Test public void testAggregates() { putTestEntitiesScalars(); Query<TestEntity> query = box.query().less(simpleInt, 2002).build(); PropertyQuery intQuery = query.property(simpleInt); PropertyQuery floatQuery = query.property(simpleFloat); assertEquals(2000.5, intQuery.avg(), 0.0001); assertEquals(2000, intQuery.min(), 0.0001); assertEquals(400, floatQuery.minDouble(), 0.001); assertEquals(2001, intQuery.max(), 0.0001); assertEquals(400.1, floatQuery.maxDouble(), 0.001); assertEquals(4001, intQuery.sum(), 0.0001); assertEquals(800.1, floatQuery.sumDouble(), 0.001); }
@Test public void testFindInt() { Query<TestEntity> query = box.query().greater(simpleLong, 1002).build(); assertNull(query.property(simpleInt).findInt()); assertNull(query.property(simpleInt).unique().findInt()); putTestEntities(5); assertEquals(3, (int) query.property(simpleInt).findInt()); query = box.query().greater(simpleLong, 1004).build(); assertEquals(5, (int) query.property(simpleInt).distinct().unique().findInt()); TestEntityCursor.INT_NULL_HACK = true; try { putTestEntity(null, 6); } finally { TestEntityCursor.INT_NULL_HACK = false; } query.setParameter(simpleLong, 1005); assertEquals(-99, (int) query.property(simpleInt).nullValue(-99).unique().findInt()); }
@Test public void testFindByte() { Query<TestEntity> query = box.query().greater(simpleLong, 1002).build(); assertNull(query.property(simpleByte).findByte()); assertNull(query.property(simpleByte).unique().findByte()); putTestEntities(5); assertEquals((byte) 13, (byte) query.property(simpleByte).findByte()); query = box.query().greater(simpleLong, 1004).build(); assertEquals((byte) 15, (byte) query.property(simpleByte).distinct().unique().findByte()); }
@Test public void testFindBoolean() { Query<TestEntity> query = box.query().greater(simpleLong, 1002).build(); assertNull(query.property(simpleBoolean).findBoolean()); assertNull(query.property(simpleBoolean).unique().findBoolean()); putTestEntities(5); assertFalse(query.property(simpleBoolean).findBoolean()); query = box.query().greater(simpleLong, 1004).build(); assertFalse(query.property(simpleBoolean).distinct().unique().findBoolean()); }
@Test public void testFindFloat() { Query<TestEntity> query = box.query().greater(simpleLong, 1002).build(); assertNull(query.property(simpleFloat).findFloat()); assertNull(query.property(simpleFloat).unique().findFloat()); putTestEntities(5); assertEquals(200.3f, query.property(simpleFloat).findFloat(), 0.001f); query = box.query().greater(simpleLong, 1004).build(); assertEquals(200.5f, query.property(simpleFloat).distinct().unique().findFloat(), 0.001f); }
@Test public void testFindDouble() { Query<TestEntity> query = box.query().greater(simpleLong, 1002).build(); assertNull(query.property(simpleDouble).findDouble()); assertNull(query.property(simpleDouble).unique().findDouble()); putTestEntities(5); assertEquals(2000.03, query.property(simpleDouble).findDouble(), 0.001); query = box.query().greater(simpleLong, 1004).build(); assertEquals(2000.05, query.property(simpleDouble).distinct().unique().findDouble(), 0.001); }
@Test public void testFindShort() { Query<TestEntity> query = box.query().greater(simpleLong, 1002).build(); assertNull(query.property(simpleShort).findShort()); assertNull(query.property(simpleShort).unique().findShort()); putTestEntities(5); assertEquals(103, (short) query.property(simpleShort).findShort()); query = box.query().greater(simpleLong, 1004).build(); assertEquals(105, (short) query.property(simpleShort).distinct().unique().findShort()); }
@Test public void testFindStrings_nullValue() { putTestEntity(null, 3); putTestEntitiesStrings(); Query<TestEntity> query = box.query().equal(simpleInt, 3).build(); String[] strings = query.property(simpleString).findStrings(); assertEquals(1, strings.length); assertEquals("bar", strings[0]); strings = query.property(simpleString).nullValue("****").findStrings(); assertEquals(2, strings.length); assertEquals("****", strings[0]); assertEquals("bar", strings[1]); putTestEntity(null, 3); assertEquals(3, query.property(simpleString).nullValue("****").findStrings().length); assertEquals(2, query.property(simpleString).nullValue("****").distinct().findStrings().length); }
@Test(expected = DbException.class) public void testFindBoolean_uniqueFails() { putTestEntity(null, 1); putTestEntity(null, 1); box.query().build().property(simpleBoolean).unique().findBoolean(); }
@Test(expected = DbException.class) public void testFindDouble_uniqueFails() { putTestEntity(null, 1); putTestEntity(null, 1); box.query().build().property(simpleDouble).unique().findDouble(); }
@Test public void testFindLong() { Query<TestEntity> query = box.query().greater(simpleLong, 1002).build(); assertNull(query.property(simpleLong).findLong()); assertNull(query.property(simpleLong).findLong()); putTestEntities(5); assertEquals(1003, (long) query.property(simpleLong).findLong()); query = box.query().greater(simpleLong, 1004).build(); assertEquals(1005, (long) query.property(simpleLong).distinct().findLong()); }
@Test public void testCount() { putTestEntity(null, 1000); putTestEntity("BAR", 100); putTestEntitiesStrings(); putTestEntity("banana", 101); Query<TestEntity> query = box.query().build(); PropertyQuery stringQuery = query.property(simpleString); assertEquals(8, query.count()); assertEquals(7, stringQuery.count()); assertEquals(6, stringQuery.distinct().count()); }
@Test public void testFindFloats() { putTestEntities(5); Query<TestEntity> query = box.query().greater(simpleInt, 2).build(); float[] result = query.property(simpleFloat).findFloats(); assertEquals(3, result.length); assertEquals(200.3f, result[0], 0.0001f); assertEquals(200.4f, result[1], 0.0001f); assertEquals(200.5f, result[2], 0.0001f); putTestEntity(null, 5); query = box.query().greater(simpleInt, 4).build(); assertEquals(2, query.property(simpleFloat).findFloats().length); assertEquals(1, query.property(simpleFloat).distinct().findFloats().length); }
@Test public void testFindBytes() { putTestEntities(5); Query<TestEntity> query = box.query().greater(simpleByte, 12).build(); byte[] result = query.property(simpleByte).findBytes(); assertEquals(3, result.length); assertEquals(13, result[0]); assertEquals(14, result[1]); assertEquals(15, result[2]); putTestEntity(null, 5); query = box.query().greater(simpleByte, 14).build(); assertEquals(2, query.property(simpleByte).findBytes().length); assertEquals(1, query.property(simpleByte).distinct().findBytes().length); }
@Test public void testFindDoubles() { putTestEntities(5); Query<TestEntity> query = box.query().greater(simpleInt, 2).build(); double[] result = query.property(simpleDouble).findDoubles(); assertEquals(3, result.length); assertEquals(2000.03, result[0], 0.0001); assertEquals(2000.04, result[1], 0.0001); assertEquals(2000.05, result[2], 0.0001); putTestEntity(null, 5); query = box.query().greater(simpleInt, 4).build(); assertEquals(2, query.property(simpleDouble).findDoubles().length); assertEquals(1, query.property(simpleDouble).distinct().findDoubles().length); }
@Test public void testFindLongs() { putTestEntities(5); Query<TestEntity> query = box.query().greater(simpleLong, 1002).build(); long[] result = query.property(simpleLong).findLongs(); assertEquals(3, result.length); assertEquals(1003, result[0]); assertEquals(1004, result[1]); assertEquals(1005, result[2]); putTestEntity(null, 5); query = box.query().greater(simpleLong, 1004).build(); assertEquals(2, query.property(simpleLong).findLongs().length); assertEquals(1, query.property(simpleLong).distinct().findLongs().length); }