|
| 1 | +/* |
| 2 | + * Copyright (c) 2017-2021 Nitrite author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package org.dizitart.no2.integration.collection; |
| 19 | + |
| 20 | +import org.dizitart.no2.Nitrite; |
| 21 | +import org.dizitart.no2.collection.Document; |
| 22 | +import org.dizitart.no2.collection.NitriteCollection; |
| 23 | +import org.dizitart.no2.index.IndexOptions; |
| 24 | +import org.dizitart.no2.index.IndexType; |
| 25 | +import org.junit.After; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import static org.dizitart.no2.filters.FluentFilter.where; |
| 30 | +import static org.junit.Assert.assertEquals; |
| 31 | + |
| 32 | +/** |
| 33 | + * Test case for numeric filter consistency across different numeric types. |
| 34 | + * |
| 35 | + * @author Nitrite Team |
| 36 | + */ |
| 37 | +public class NumericFilterTest { |
| 38 | + private Nitrite db; |
| 39 | + private NitriteCollection collection; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setUp() { |
| 43 | + db = Nitrite.builder().openOrCreate(); |
| 44 | + collection = db.getCollection("numericTest"); |
| 45 | + } |
| 46 | + |
| 47 | + @After |
| 48 | + public void tearDown() { |
| 49 | + if (collection != null) { |
| 50 | + collection.close(); |
| 51 | + } |
| 52 | + if (db != null) { |
| 53 | + db.close(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testIntLongComparisonWithoutIndex() { |
| 59 | + Document doc = Document.createDocument("value", 42); |
| 60 | + collection.insert(doc); |
| 61 | + |
| 62 | + // All filters should work consistently without index |
| 63 | + assertEquals(1, collection.find(where("value").eq(42L)).size()); |
| 64 | + assertEquals(1, collection.find(where("value").lte(42L)).size()); |
| 65 | + assertEquals(1, collection.find(where("value").gte(42L)).size()); |
| 66 | + assertEquals(1, collection.find(where("value").lt(43L)).size()); |
| 67 | + assertEquals(1, collection.find(where("value").gt(41L)).size()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testIntLongComparisonWithIndex() { |
| 72 | + Document doc = Document.createDocument("value", 42); |
| 73 | + collection.insert(doc); |
| 74 | + |
| 75 | + collection.createIndex(IndexOptions.indexOptions(IndexType.NON_UNIQUE), "value"); |
| 76 | + |
| 77 | + // All filters should work consistently with index |
| 78 | + assertEquals(1, collection.find(where("value").eq(42L)).size()); |
| 79 | + assertEquals(1, collection.find(where("value").lte(42L)).size()); |
| 80 | + assertEquals(1, collection.find(where("value").gte(42L)).size()); |
| 81 | + assertEquals(1, collection.find(where("value").lt(43L)).size()); |
| 82 | + assertEquals(1, collection.find(where("value").gt(41L)).size()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testIntDoubleComparisonWithoutIndex() { |
| 87 | + Document doc = Document.createDocument("value", 42); |
| 88 | + collection.insert(doc); |
| 89 | + |
| 90 | + // All filters should work consistently without index |
| 91 | + assertEquals(1, collection.find(where("value").eq(42.0)).size()); |
| 92 | + assertEquals(1, collection.find(where("value").lte(42.0)).size()); |
| 93 | + assertEquals(1, collection.find(where("value").gte(42.0)).size()); |
| 94 | + assertEquals(1, collection.find(where("value").lt(43.0)).size()); |
| 95 | + assertEquals(1, collection.find(where("value").gt(41.0)).size()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testIntDoubleComparisonWithIndex() { |
| 100 | + Document doc = Document.createDocument("value", 42); |
| 101 | + collection.insert(doc); |
| 102 | + |
| 103 | + collection.createIndex(IndexOptions.indexOptions(IndexType.NON_UNIQUE), "value"); |
| 104 | + |
| 105 | + // All filters should work consistently with index |
| 106 | + assertEquals(1, collection.find(where("value").eq(42.0)).size()); |
| 107 | + assertEquals(1, collection.find(where("value").lte(42.0)).size()); |
| 108 | + assertEquals(1, collection.find(where("value").gte(42.0)).size()); |
| 109 | + assertEquals(1, collection.find(where("value").lt(43.0)).size()); |
| 110 | + assertEquals(1, collection.find(where("value").gt(41.0)).size()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testLongIntComparisonWithoutIndex() { |
| 115 | + Document doc = Document.createDocument("value", 42L); |
| 116 | + collection.insert(doc); |
| 117 | + |
| 118 | + // All filters should work consistently without index |
| 119 | + assertEquals(1, collection.find(where("value").eq(42)).size()); |
| 120 | + assertEquals(1, collection.find(where("value").lte(42)).size()); |
| 121 | + assertEquals(1, collection.find(where("value").gte(42)).size()); |
| 122 | + assertEquals(1, collection.find(where("value").lt(43)).size()); |
| 123 | + assertEquals(1, collection.find(where("value").gt(41)).size()); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testLongIntComparisonWithIndex() { |
| 128 | + Document doc = Document.createDocument("value", 42L); |
| 129 | + collection.insert(doc); |
| 130 | + |
| 131 | + collection.createIndex(IndexOptions.indexOptions(IndexType.NON_UNIQUE), "value"); |
| 132 | + |
| 133 | + // All filters should work consistently with index |
| 134 | + assertEquals(1, collection.find(where("value").eq(42)).size()); |
| 135 | + assertEquals(1, collection.find(where("value").lte(42)).size()); |
| 136 | + assertEquals(1, collection.find(where("value").gte(42)).size()); |
| 137 | + assertEquals(1, collection.find(where("value").lt(43)).size()); |
| 138 | + assertEquals(1, collection.find(where("value").gt(41)).size()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testMultipleNumericTypesWithoutIndex() { |
| 143 | + collection.insert(Document.createDocument("value", 10)); |
| 144 | + collection.insert(Document.createDocument("value", 20L)); |
| 145 | + collection.insert(Document.createDocument("value", 30.0)); |
| 146 | + |
| 147 | + // Query with different numeric types |
| 148 | + assertEquals(1, collection.find(where("value").eq(10L)).size()); |
| 149 | + assertEquals(1, collection.find(where("value").eq(20)).size()); |
| 150 | + assertEquals(1, collection.find(where("value").eq(30)).size()); |
| 151 | + |
| 152 | + assertEquals(3, collection.find(where("value").gte(10)).size()); |
| 153 | + assertEquals(2, collection.find(where("value").gt(10L)).size()); |
| 154 | + assertEquals(2, collection.find(where("value").lte(20.0)).size()); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void testMultipleNumericTypesWithIndex() { |
| 159 | + collection.insert(Document.createDocument("value", 10)); |
| 160 | + collection.insert(Document.createDocument("value", 20L)); |
| 161 | + collection.insert(Document.createDocument("value", 30.0)); |
| 162 | + |
| 163 | + collection.createIndex(IndexOptions.indexOptions(IndexType.NON_UNIQUE), "value"); |
| 164 | + |
| 165 | + // Query with different numeric types |
| 166 | + assertEquals(1, collection.find(where("value").eq(10L)).size()); |
| 167 | + assertEquals(1, collection.find(where("value").eq(20)).size()); |
| 168 | + assertEquals(1, collection.find(where("value").eq(30)).size()); |
| 169 | + |
| 170 | + assertEquals(3, collection.find(where("value").gte(10)).size()); |
| 171 | + assertEquals(2, collection.find(where("value").gt(10L)).size()); |
| 172 | + assertEquals(2, collection.find(where("value").lte(20.0)).size()); |
| 173 | + } |
| 174 | +} |
0 commit comments