GeezerVison is showing github.com — Date: add oneOf conditions, update and add query tests · Hancony/objectbox-java@c95901c
File treeExpand file treeCollapse file tree Open diff view settings objectbox-java/src/main/java/io/objectbox
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: objectbox-java/src/main/java/io/objectbox/Property.java +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -354,6 +354,16 @@ public PropertyQueryCondition<ENTITY> lessOrEqual(Date value) { | 354 | 354 | return new LongCondition<>(this, LongCondition.Operation.LESS_OR_EQUAL, value); | 355 | 355 | } | 356 | 356 |
| | 357 | + /** Creates an "IN (..., ..., ...)" condition for this property. */ | | 358 | + public PropertyQueryCondition<ENTITY> oneOf(Date[] value) { | | 359 | + return new LongArrayCondition<>(this, LongArrayCondition.Operation.IN, value); | | 360 | + } | | 361 | + | | 362 | + /** Creates a "NOT IN (..., ..., ...)" condition for this property. */ | | 363 | + public PropertyQueryCondition<ENTITY> notOneOf(Date[] value) { | | 364 | + return new LongArrayCondition<>(this, LongArrayCondition.Operation.NOT_IN, value); | | 365 | + } | | 366 | + | 357 | 367 | /** | 358 | 368 | * Creates a "BETWEEN ... AND ..." condition for this property. | 359 | 369 | * Finds objects with property value between and including the first and second value. | |
Collapse file Copy file name to clipboardExpand all lines: objectbox-java/src/main/java/io/objectbox/query/PropertyQueryConditionImpl.java +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -212,6 +212,15 @@ public LongArrayCondition(Property<T> property, Operation op, long[] value) { | 212 | 212 | this.value = value; | 213 | 213 | } | 214 | 214 |
| | 215 | + public LongArrayCondition(Property<T> property, Operation op, Date[] value) { | | 216 | + super(property); | | 217 | + this.op = op; | | 218 | + this.value = new long[value.length]; | | 219 | + for (int i = 0; i < value.length; i++) { | | 220 | + this.value[i] = value[i].getTime(); | | 221 | + } | | 222 | + } | | 223 | + | 215 | 224 | @Override | 216 | 225 | void applyCondition(QueryBuilder<T> builder) { | 217 | 226 | switch (op) { | |
Collapse file Copy file name to clipboard +10Lines changed: 10 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Tests for `objectbox-java` | | 2 | + | | 3 | +## Naming convention for tests | | 4 | + | | 5 | +All new tests which will be added to the `tests/objectbox-java-test` module must have the names of their methods in the | | 6 | +following format: `{attribute}_{queryCondition}_{expectation}` | | 7 | + | | 8 | +For ex. `date_lessAndGreater_works` | | 9 | + | | 10 | +Note: due to historic reasons (JUnit 3) existing test methods may be named differently (with the `test` prefix). |
Collapse file Copy file name to clipboardExpand all lines: tests/objectbox-java-test/src/main/java/io/objectbox/TestEntity.java +17-3Lines changed: 17 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | /* | 2 | | - * Copyright 2017 ObjectBox Ltd. All rights reserved. | | 2 | + * Copyright 2017-2024 ObjectBox Ltd. All rights reserved. | 3 | 3 | * | 4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 5 | 5 | * you may not use this file except in compliance with the License. | | 16 | 16 |
| 17 | 17 | package io.objectbox; | 18 | 18 |
| 19 | | -import javax.annotation.Nullable; | 20 | 19 | import java.util.Arrays; | | 20 | +import java.util.Date; | 21 | 21 | import java.util.List; | 22 | 22 | import java.util.Map; | 23 | 23 |
| | 24 | +import javax.annotation.Nullable; | | 25 | + | 24 | 26 | /** In "real" entity would be annotated with @Entity. */ | 25 | 27 | public class TestEntity { | 26 | 28 |
| @@ -59,6 +61,7 @@ public class TestEntity { | 59 | 61 | private long[] longArray; | 60 | 62 | private float[] floatArray; | 61 | 63 | private double[] doubleArray; | | 64 | + private Date date; | 62 | 65 |
| 63 | 66 | transient boolean noArgsConstructorCalled; | 64 | 67 |
| @@ -92,7 +95,8 @@ public TestEntity(long id, | 92 | 95 | int[] intArray, | 93 | 96 | long[] longArray, | 94 | 97 | float[] floatArray, | 95 | | - double[] doubleArray | | 98 | + double[] doubleArray, | | 99 | + Date date | 96 | 100 | ) { | 97 | 101 | this.id = id; | 98 | 102 | this.simpleBoolean = simpleBoolean; | @@ -117,6 +121,7 @@ public TestEntity(long id, | 117 | 121 | this.longArray = longArray; | 118 | 122 | this.floatArray = floatArray; | 119 | 123 | this.doubleArray = doubleArray; | | 124 | + this.date = date; | 120 | 125 | if (STRING_VALUE_THROW_IN_CONSTRUCTOR.equals(simpleString)) { | 121 | 126 | throw new RuntimeException(EXCEPTION_IN_CONSTRUCTOR_MESSAGE); | 122 | 127 | } | @@ -324,6 +329,14 @@ public void setDoubleArray(@Nullable double[] doubleArray) { | 324 | 329 | this.doubleArray = doubleArray; | 325 | 330 | } | 326 | 331 |
| | 332 | + public Date getDate() { | | 333 | + return date; | | 334 | + } | | 335 | + | | 336 | + public void setDate(Date date) { | | 337 | + this.date = date; | | 338 | + } | | 339 | + | 327 | 340 | @Override | 328 | 341 | public String toString() { | 329 | 342 | return "TestEntity{" + | @@ -350,6 +363,7 @@ public String toString() { | 350 | 363 | ", longArray=" + Arrays.toString(longArray) + | 351 | 364 | ", floatArray=" + Arrays.toString(floatArray) + | 352 | 365 | ", doubleArray=" + Arrays.toString(doubleArray) + | | 366 | + ", date=" + date + | 353 | 367 | ", noArgsConstructorCalled=" + noArgsConstructorCalled + | 354 | 368 | '}'; | 355 | 369 | } | |
Collapse file Copy file name to clipboardExpand all lines: tests/objectbox-java-test/src/main/java/io/objectbox/TestEntityCursor.java +8-4Lines changed: 8 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -70,6 +70,7 @@ public Cursor<TestEntity> createCursor(io.objectbox.Transaction tx, long cursorH | 70 | 70 | private final static int __ID_longArray = TestEntity_.longArray.id; | 71 | 71 | private final static int __ID_floatArray = TestEntity_.floatArray.id; | 72 | 72 | private final static int __ID_doubleArray = TestEntity_.doubleArray.id; | | 73 | + private final static int __ID_date = TestEntity_.date.id; | 73 | 74 |
| 74 | 75 | public TestEntityCursor(io.objectbox.Transaction tx, long cursor, BoxStore boxStore) { | 75 | 76 | super(tx, cursor, TestEntity_.__INSTANCE, boxStore); | @@ -150,17 +151,20 @@ public long put(TestEntity entity) { | 150 | 151 | __id9, simpleByteArray, __id15, __id15 != 0 ? stringObjectMapConverter.convertToDatabaseValue(stringObjectMap) : null, | 151 | 152 | __id16, __id16 != 0 ? flexPropertyConverter.convertToDatabaseValue(flexProperty) : null); | 152 | 153 |
| | 154 | + java.util.Date date = entity.getDate(); | | 155 | + int __id23 = date != null ? __ID_date : 0; | | 156 | + | 153 | 157 | collect313311(cursor, 0, 0, | 154 | 158 | 0, null, 0, null, | 155 | 159 | 0, null, 0, null, | 156 | 160 | __ID_simpleLong, entity.getSimpleLong(), __ID_simpleLongU, entity.getSimpleLongU(), | 157 | | - INT_NULL_HACK ? 0 : __ID_simpleInt, entity.getSimpleInt(), __ID_simpleIntU, entity.getSimpleIntU(), | 158 | | - __ID_simpleShort, entity.getSimpleShort(), __ID_simpleShortU, entity.getSimpleShortU(), | | 161 | + __id23, __id23 != 0 ? date.getTime() : 0, INT_NULL_HACK ? 0 : __ID_simpleInt, entity.getSimpleInt(), | | 162 | + __ID_simpleIntU, entity.getSimpleIntU(), __ID_simpleShort, entity.getSimpleShort(), | 159 | 163 | __ID_simpleFloat, entity.getSimpleFloat(), __ID_simpleDouble, entity.getSimpleDouble()); | 160 | 164 |
| 161 | 165 | long __assignedId = collect004000(cursor, entity.getId(), PUT_FLAG_COMPLETE, | 162 | | - __ID_simpleByte, entity.getSimpleByte(), __ID_simpleBoolean, entity.getSimpleBoolean() ? 1 : 0, | 163 | | - 0, 0, 0, 0); | | 166 | + __ID_simpleShortU, entity.getSimpleShortU(), __ID_simpleByte, entity.getSimpleByte(), | | 167 | + __ID_simpleBoolean, entity.getSimpleBoolean() ? 1 : 0, 0, 0); | 164 | 168 |
| 165 | 169 | entity.setId(__assignedId); | 166 | 170 |
| |
Collapse file Copy file name to clipboardExpand all lines: tests/objectbox-java-test/src/main/java/io/objectbox/TestEntity_.java +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -120,6 +120,9 @@ public final class TestEntity_ implements EntityInfo<TestEntity> { | 120 | 120 | public final static io.objectbox.Property<TestEntity> doubleArray = | 121 | 121 | new io.objectbox.Property<>(__INSTANCE, 22, 23, double[].class, "doubleArray"); | 122 | 122 |
| | 123 | + public final static io.objectbox.Property<TestEntity> date = | | 124 | + new io.objectbox.Property<>(__INSTANCE, 23, 24, java.util.Date.class, "date"); | | 125 | + | 123 | 126 | @SuppressWarnings("unchecked") | 124 | 127 | public final static io.objectbox.Property<TestEntity>[] __ALL_PROPERTIES = new io.objectbox.Property[]{ | 125 | 128 | id, | @@ -144,7 +147,8 @@ public final class TestEntity_ implements EntityInfo<TestEntity> { | 144 | 147 | intArray, | 145 | 148 | longArray, | 146 | 149 | floatArray, | 147 | | - doubleArray | | 150 | + doubleArray, | | 151 | + date | 148 | 152 | }; | 149 | 153 |
| 150 | 154 | public final static io.objectbox.Property<TestEntity> __ID_PROPERTY = id; | |
Collapse file Copy file name to clipboardExpand all lines: tests/objectbox-java-test/src/test/java/io/objectbox/AbstractObjectBoxTest.java +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 26 | 26 | import java.util.ArrayList; | 27 | 27 | import java.util.Arrays; | 28 | 28 | import java.util.Comparator; | | 29 | +import java.util.Date; | 29 | 30 | import java.util.HashMap; | 30 | 31 | import java.util.List; | 31 | 32 | import java.util.Map; | @@ -299,7 +300,10 @@ private void addTestEntity(ModelBuilder modelBuilder, @Nullable IndexType simple | 299 | 300 | entityBuilder.property("floatArray", PropertyType.FloatVector).id(TestEntity_.floatArray.id, ++lastUid); | 300 | 301 | entityBuilder.property("doubleArray", PropertyType.DoubleVector).id(TestEntity_.doubleArray.id, ++lastUid); | 301 | 302 |
| 302 | | - int lastId = TestEntity_.doubleArray.id; | | 303 | + // Date property | | 304 | + entityBuilder.property("date", PropertyType.Date).id(TestEntity_.date.id, ++lastUid); | | 305 | + | | 306 | + int lastId = TestEntity_.date.id; | 303 | 307 | entityBuilder.lastPropertyId(lastId, lastUid); | 304 | 308 | addOptionalFlagsToTestEntity(entityBuilder); | 305 | 309 | entityBuilder.entityDone(); | @@ -352,6 +356,7 @@ protected TestEntity createTestEntity(@Nullable String simpleString, int nr) { | 352 | 356 | entity.setLongArray(new long[]{-entity.getSimpleLong(), entity.getSimpleLong()}); | 353 | 357 | entity.setFloatArray(new float[]{-entity.getSimpleFloat(), entity.getSimpleFloat()}); | 354 | 358 | entity.setDoubleArray(new double[]{-entity.getSimpleDouble(), entity.getSimpleDouble()}); | | 359 | + entity.setDate(new Date(1000 + nr)); | 355 | 360 | return entity; | 356 | 361 | } | 357 | 362 |
| |
Collapse file Copy file name to clipboardExpand all lines: tests/objectbox-java-test/src/test/java/io/objectbox/BoxStoreBuilderTest.java +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -291,7 +291,7 @@ public void maxDataSize() { | 291 | 291 | DbMaxDataSizeExceededException.class, | 292 | 292 | () -> getTestEntityBox().put(testEntity2) | 293 | 293 | ); | 294 | | - assertEquals("Exceeded user-set maximum by [bytes]: 528", maxDataExc.getMessage()); | | 294 | + assertEquals("Exceeded user-set maximum by [bytes]: 544", maxDataExc.getMessage()); | 295 | 295 |
| 296 | 296 | // Remove to get below max data size, then put again. | 297 | 297 | getTestEntityBox().remove(testEntity1); | |
Collapse file Copy file name to clipboardExpand all lines: tests/objectbox-java-test/src/test/java/io/objectbox/BoxTest.java +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | import java.util.ArrayList; | 23 | 23 | import java.util.Arrays; | 24 | 24 | import java.util.Collection; | | 25 | +import java.util.Date; | 25 | 26 | import java.util.List; | 26 | 27 | import java.util.Map; | 27 | 28 |
| @@ -85,6 +86,7 @@ public void testPutAndGet() { | 85 | 86 | assertArrayEquals(new long[]{-valLong, valLong}, entity.getLongArray()); | 86 | 87 | assertArrayEquals(new float[]{-valFloat, valFloat}, entityRead.getFloatArray(), 0); | 87 | 88 | assertArrayEquals(new double[]{-valDouble, valDouble}, entity.getDoubleArray(), 0); | | 89 | + assertEquals(new Date(1000 + simpleInt), entity.getDate()); | 88 | 90 | } | 89 | 91 |
| 90 | 92 | // Note: There is a similar test using the Cursor API directly (which is deprecated) in CursorTest. | @@ -135,6 +137,7 @@ public void testPutAndGet_defaultOrNullValues() { | 135 | 137 | assertNull(defaultEntity.getLongArray()); | 136 | 138 | assertNull(defaultEntity.getFloatArray()); | 137 | 139 | assertNull(defaultEntity.getDoubleArray()); | | 140 | + assertNull(defaultEntity.getDate()); | 138 | 141 | } | 139 | 142 |
| 140 | 143 | @Test | |
Collapse file Copy file name to clipboardExpand all lines: tests/objectbox-java-test/src/test/java/io/objectbox/query/AbstractQueryTest.java +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -60,6 +60,7 @@ public void setUpBox() { | 60 | 60 | * <li>longArray = [{-3000,3000}..{-3009,3009}]</li> | 61 | 61 | * <li>floatArray = [{-400.0,400.0}..{-400.9,400.9}]</li> | 62 | 62 | * <li>doubleArray = [{-2020.00,2020.00}..{-2020.09,2020.09}] (approximately)</li> | | 63 | + * <li>date = [Date(3000)..Date(3009)]</li> | 63 | 64 | */ | 64 | 65 | public List<TestEntity> putTestEntitiesScalars() { | 65 | 66 | return putTestEntities(10, null, 2000); | |
|