Skip to content

Commit

Permalink
Merge branch '169-test-date-property' into 'dev'
Browse files Browse the repository at this point in the history
Resolve "Test Date property" #169

See merge request objectbox/objectbox-java!139
  • Loading branch information
greenrobot-team committed Aug 20, 2024
2 parents 19193db + c95901c commit 88e3122
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 32 deletions.
10 changes: 10 additions & 0 deletions objectbox-java/src/main/java/io/objectbox/Property.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,16 @@ public PropertyQueryCondition<ENTITY> lessOrEqual(Date value) {
return new LongCondition<>(this, LongCondition.Operation.LESS_OR_EQUAL, value);
}

/** Creates an "IN (..., ..., ...)" condition for this property. */
public PropertyQueryCondition<ENTITY> oneOf(Date[] value) {
return new LongArrayCondition<>(this, LongArrayCondition.Operation.IN, value);
}

/** Creates a "NOT IN (..., ..., ...)" condition for this property. */
public PropertyQueryCondition<ENTITY> notOneOf(Date[] value) {
return new LongArrayCondition<>(this, LongArrayCondition.Operation.NOT_IN, value);
}

/**
* Creates a "BETWEEN ... AND ..." condition for this property.
* Finds objects with property value between and including the first and second value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ public LongArrayCondition(Property<T> property, Operation op, long[] value) {
this.value = value;
}

public LongArrayCondition(Property<T> property, Operation op, Date[] value) {
super(property);
this.op = op;
this.value = new long[value.length];
for (int i = 0; i < value.length; i++) {
this.value[i] = value[i].getTime();
}
}

@Override
void applyCondition(QueryBuilder<T> builder) {
switch (op) {
Expand Down
10 changes: 10 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Tests for `objectbox-java`

## Naming convention for tests

All new tests which will be added to the `tests/objectbox-java-test` module must have the names of their methods in the
following format: `{attribute}_{queryCondition}_{expectation}`

For ex. `date_lessAndGreater_works`

Note: due to historic reasons (JUnit 3) existing test methods may be named differently (with the `test` prefix).
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 ObjectBox Ltd. All rights reserved.
* Copyright 2017-2024 ObjectBox Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,11 +16,13 @@

package io.objectbox;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

/** In "real" entity would be annotated with @Entity. */
public class TestEntity {

Expand Down Expand Up @@ -59,6 +61,7 @@ public class TestEntity {
private long[] longArray;
private float[] floatArray;
private double[] doubleArray;
private Date date;

transient boolean noArgsConstructorCalled;

Expand Down Expand Up @@ -92,7 +95,8 @@ public TestEntity(long id,
int[] intArray,
long[] longArray,
float[] floatArray,
double[] doubleArray
double[] doubleArray,
Date date
) {
this.id = id;
this.simpleBoolean = simpleBoolean;
Expand All @@ -117,6 +121,7 @@ public TestEntity(long id,
this.longArray = longArray;
this.floatArray = floatArray;
this.doubleArray = doubleArray;
this.date = date;
if (STRING_VALUE_THROW_IN_CONSTRUCTOR.equals(simpleString)) {
throw new RuntimeException(EXCEPTION_IN_CONSTRUCTOR_MESSAGE);
}
Expand Down Expand Up @@ -324,6 +329,14 @@ public void setDoubleArray(@Nullable double[] doubleArray) {
this.doubleArray = doubleArray;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

@Override
public String toString() {
return "TestEntity{" +
Expand All @@ -350,6 +363,7 @@ public String toString() {
", longArray=" + Arrays.toString(longArray) +
", floatArray=" + Arrays.toString(floatArray) +
", doubleArray=" + Arrays.toString(doubleArray) +
", date=" + date +
", noArgsConstructorCalled=" + noArgsConstructorCalled +
'}';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public Cursor<TestEntity> createCursor(io.objectbox.Transaction tx, long cursorH
private final static int __ID_longArray = TestEntity_.longArray.id;
private final static int __ID_floatArray = TestEntity_.floatArray.id;
private final static int __ID_doubleArray = TestEntity_.doubleArray.id;
private final static int __ID_date = TestEntity_.date.id;

public TestEntityCursor(io.objectbox.Transaction tx, long cursor, BoxStore boxStore) {
super(tx, cursor, TestEntity_.__INSTANCE, boxStore);
Expand Down Expand Up @@ -150,17 +151,20 @@ public long put(TestEntity entity) {
__id9, simpleByteArray, __id15, __id15 != 0 ? stringObjectMapConverter.convertToDatabaseValue(stringObjectMap) : null,
__id16, __id16 != 0 ? flexPropertyConverter.convertToDatabaseValue(flexProperty) : null);

java.util.Date date = entity.getDate();
int __id23 = date != null ? __ID_date : 0;

collect313311(cursor, 0, 0,
0, null, 0, null,
0, null, 0, null,
__ID_simpleLong, entity.getSimpleLong(), __ID_simpleLongU, entity.getSimpleLongU(),
INT_NULL_HACK ? 0 : __ID_simpleInt, entity.getSimpleInt(), __ID_simpleIntU, entity.getSimpleIntU(),
__ID_simpleShort, entity.getSimpleShort(), __ID_simpleShortU, entity.getSimpleShortU(),
__id23, __id23 != 0 ? date.getTime() : 0, INT_NULL_HACK ? 0 : __ID_simpleInt, entity.getSimpleInt(),
__ID_simpleIntU, entity.getSimpleIntU(), __ID_simpleShort, entity.getSimpleShort(),
__ID_simpleFloat, entity.getSimpleFloat(), __ID_simpleDouble, entity.getSimpleDouble());

long __assignedId = collect004000(cursor, entity.getId(), PUT_FLAG_COMPLETE,
__ID_simpleByte, entity.getSimpleByte(), __ID_simpleBoolean, entity.getSimpleBoolean() ? 1 : 0,
0, 0, 0, 0);
__ID_simpleShortU, entity.getSimpleShortU(), __ID_simpleByte, entity.getSimpleByte(),
__ID_simpleBoolean, entity.getSimpleBoolean() ? 1 : 0, 0, 0);

entity.setId(__assignedId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public final class TestEntity_ implements EntityInfo<TestEntity> {
public final static io.objectbox.Property<TestEntity> doubleArray =
new io.objectbox.Property<>(__INSTANCE, 22, 23, double[].class, "doubleArray");

public final static io.objectbox.Property<TestEntity> date =
new io.objectbox.Property<>(__INSTANCE, 23, 24, java.util.Date.class, "date");

@SuppressWarnings("unchecked")
public final static io.objectbox.Property<TestEntity>[] __ALL_PROPERTIES = new io.objectbox.Property[]{
id,
Expand All @@ -144,7 +147,8 @@ public final class TestEntity_ implements EntityInfo<TestEntity> {
intArray,
longArray,
floatArray,
doubleArray
doubleArray,
date
};

public final static io.objectbox.Property<TestEntity> __ID_PROPERTY = id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -299,7 +300,10 @@ private void addTestEntity(ModelBuilder modelBuilder, @Nullable IndexType simple
entityBuilder.property("floatArray", PropertyType.FloatVector).id(TestEntity_.floatArray.id, ++lastUid);
entityBuilder.property("doubleArray", PropertyType.DoubleVector).id(TestEntity_.doubleArray.id, ++lastUid);

int lastId = TestEntity_.doubleArray.id;
// Date property
entityBuilder.property("date", PropertyType.Date).id(TestEntity_.date.id, ++lastUid);

int lastId = TestEntity_.date.id;
entityBuilder.lastPropertyId(lastId, lastUid);
addOptionalFlagsToTestEntity(entityBuilder);
entityBuilder.entityDone();
Expand Down Expand Up @@ -352,6 +356,7 @@ protected TestEntity createTestEntity(@Nullable String simpleString, int nr) {
entity.setLongArray(new long[]{-entity.getSimpleLong(), entity.getSimpleLong()});
entity.setFloatArray(new float[]{-entity.getSimpleFloat(), entity.getSimpleFloat()});
entity.setDoubleArray(new double[]{-entity.getSimpleDouble(), entity.getSimpleDouble()});
entity.setDate(new Date(1000 + nr));
return entity;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public void maxDataSize() {
DbMaxDataSizeExceededException.class,
() -> getTestEntityBox().put(testEntity2)
);
assertEquals("Exceeded user-set maximum by [bytes]: 528", maxDataExc.getMessage());
assertEquals("Exceeded user-set maximum by [bytes]: 544", maxDataExc.getMessage());

// Remove to get below max data size, then put again.
getTestEntityBox().remove(testEntity1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -85,6 +86,7 @@ public void testPutAndGet() {
assertArrayEquals(new long[]{-valLong, valLong}, entity.getLongArray());
assertArrayEquals(new float[]{-valFloat, valFloat}, entityRead.getFloatArray(), 0);
assertArrayEquals(new double[]{-valDouble, valDouble}, entity.getDoubleArray(), 0);
assertEquals(new Date(1000 + simpleInt), entity.getDate());
}

// Note: There is a similar test using the Cursor API directly (which is deprecated) in CursorTest.
Expand Down Expand Up @@ -135,6 +137,7 @@ public void testPutAndGet_defaultOrNullValues() {
assertNull(defaultEntity.getLongArray());
assertNull(defaultEntity.getFloatArray());
assertNull(defaultEntity.getDoubleArray());
assertNull(defaultEntity.getDate());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void setUpBox() {
* <li>longArray = [{-3000,3000}..{-3009,3009}]</li>
* <li>floatArray = [{-400.0,400.0}..{-400.9,400.9}]</li>
* <li>doubleArray = [{-2020.00,2020.00}..{-2020.09,2020.09}] (approximately)</li>
* <li>date = [Date(3000)..Date(3009)]</li>
*/
public List<TestEntity> putTestEntitiesScalars() {
return putTestEntities(10, null, 2000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,12 @@
import io.objectbox.TestEntity;
import io.objectbox.TestEntity_;
import io.objectbox.TestUtils;
import io.objectbox.config.DebugFlags;
import io.objectbox.exception.DbExceptionListener;
import io.objectbox.exception.NonUniqueResultException;
import io.objectbox.query.QueryBuilder.StringOrder;
import io.objectbox.relation.MyObjectBox;
import io.objectbox.relation.Order;
import io.objectbox.relation.Order_;


import static io.objectbox.TestEntity_.date;
import static io.objectbox.TestEntity_.simpleBoolean;
import static io.objectbox.TestEntity_.simpleByteArray;
import static io.objectbox.TestEntity_.simpleFloat;
Expand Down Expand Up @@ -1253,29 +1250,84 @@ public void testQueryAttempts() {
}

@Test
public void testDateParam() {
store.close();
assertTrue(store.deleteAllFiles());
store = MyObjectBox.builder().baseDirectory(boxStoreDir).debugFlags(DebugFlags.LOG_QUERY_PARAMETERS).build();

public void date_equal_and_setParameter_works() {
Date now = new Date();
Order order = new Order();
order.setDate(now);
Box<Order> box = store.boxFor(Order.class);
box.put(order);
TestEntity entity = new TestEntity();
entity.setDate(now);
Box<TestEntity> box = store.boxFor(TestEntity.class);
box.put(entity);

try (Query<TestEntity> query = box.query(TestEntity_.date.equal(0)).build()) {
assertEquals(0, query.count());
query.setParameter(TestEntity_.date, now);
assertEquals(1, query.count());
}

Query<Order> query = box.query().equal(Order_.date, 0).build();
assertEquals(0, query.count());
// Again, but using alias
try (Query<TestEntity> aliasQuery = box.query(TestEntity_.date.equal(0)).parameterAlias("date").build()) {
assertEquals(0, aliasQuery.count());
aliasQuery.setParameter("date", now);
assertEquals(1, aliasQuery.count());
}
}

query.setParameter(Order_.date, now);
assertEquals(1, query.count());
@Test
public void date_between_works() {
putTestEntitiesScalars();
try (Query<TestEntity> query = box.query(date.between(new Date(3002L), new Date(3008L))).build()) {
assertEquals(7, query.count());
}
}

// Again, but using alias
Query<Order> aliasQuery = box.query().equal(Order_.date, 0).parameterAlias("date").build();
assertEquals(0, aliasQuery.count());
@Test
public void date_lessAndGreater_works() {
putTestEntitiesScalars();
try (Query<TestEntity> query = box.query(date.less(new Date(3002L))).build()) {
assertEquals(2, query.count());
}
try (Query<TestEntity> query = box.query(date.lessOrEqual(new Date(3003L))).build()) {
assertEquals(4, query.count());
}
try (Query<TestEntity> query = box.query(date.greater(new Date(3008L))).build()) {
assertEquals(1, query.count());
}
try (Query<TestEntity> query = box.query(date.greaterOrEqual(new Date(3008L))).build()) {
assertEquals(2, query.count());
}
}

@Test
public void date_oneOf_works() {
putTestEntitiesScalars();
Date[] valuesDate = new Date[]{new Date(3002L), new Date(), new Date(0)};
try (Query<TestEntity> query = box.query(date.oneOf(valuesDate)).build()) {
assertEquals(1, query.count());
}
Date[] valuesDate2 = new Date[]{new Date()};
try (Query<TestEntity> query = box.query(date.oneOf(valuesDate2)).build()) {
assertEquals(0, query.count());
}
Date[] valuesDate3 = new Date[]{new Date(3002L), new Date(3009L)};
try (Query<TestEntity> query = box.query(date.oneOf(valuesDate3)).build()) {
assertEquals(2, query.count());
}
}

aliasQuery.setParameter("date", now);
assertEquals(1, aliasQuery.count());
@Test
public void date_notOneOf_works() {
putTestEntitiesScalars();
Date[] valuesDate = new Date[]{new Date(3002L), new Date(), new Date(0)};
try (Query<TestEntity> query = box.query(date.notOneOf(valuesDate)).build()) {
assertEquals(9, query.count());
}
Date[] valuesDate2 = new Date[]{new Date()};
try (Query<TestEntity> query = box.query(date.notOneOf(valuesDate2)).build()) {
assertEquals(10, query.count());
}
Date[] valuesDate3 = new Date[]{new Date(3002L), new Date(3009L)};
try (Query<TestEntity> query = box.query(date.notOneOf(valuesDate3)).build()) {
assertEquals(8, query.count());
}
}

@Test
Expand Down

0 comments on commit 88e3122

Please sign in to comment.