Skip to content

Commit 5c5df44

Browse files
authored
Merge pull request #1526 from dimagi/bulkQueryTests
Adds tests for bulk query storage
2 parents df4e1ef + 9fb6626 commit 5c5df44

2 files changed

Lines changed: 118 additions & 5 deletions

File tree

src/main/java/org/javarosa/core/services/storage/util/DummyIndexedStorageUtility.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import java.util.Collection;
2222
import java.util.Enumeration;
2323
import java.util.HashMap;
24+
import java.util.HashSet;
2425
import java.util.Hashtable;
2526
import java.util.LinkedHashSet;
2627
import java.util.List;
2728
import java.util.NoSuchElementException;
29+
import java.util.Set;
2830
import java.util.Vector;
2931

3032
/**
@@ -363,15 +365,20 @@ public String[] getMetaDataForRecord(int recordId, String[] fieldNames) {
363365

364366
@Override
365367
public Vector<T> getBulkRecordsForIndex(String metaFieldName, Collection<String> matchingValues) {
366-
// we don't care about bulk retrieval for dummy storage, so just call normal method to get records here
367-
return getRecordsForValues(new String[]{metaFieldName}, matchingValues.toArray());
368+
Vector<T> result = new Vector<>();
369+
for (String value : matchingValues) {
370+
result.addAll(getRecordsForValues(new String[]{metaFieldName}, new Object[]{value}));
371+
}
372+
return result;
368373
}
369374

370375
@Override
371376
public Vector<Integer> getBulkIdsForIndex(String metaFieldName, Collection<String> matchingValues) {
372-
// we don't care about bulk retrieval for dummy storage, so just call normal method to get records here
373-
List<Integer> result = getIDsForValues(new String[]{metaFieldName}, matchingValues.toArray());
374-
return new Vector<>(result);
377+
Set<Integer> resultSet = new HashSet<>();
378+
for (String value : matchingValues) {
379+
resultSet.addAll(getIDsForValue(metaFieldName, value));
380+
}
381+
return new Vector<>(resultSet);
375382
}
376383

377384
@Override

src/test/java/org/javarosa/core/storage/IndexedStorageUtilityTests.java

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.junit.Before;
1010
import org.junit.Test;
1111

12+
import java.util.Arrays;
1213
import java.util.HashSet;
1314
import java.util.LinkedHashSet;
1415
import java.util.List;
@@ -138,6 +139,111 @@ public void testReadingAllEntries() {
138139
Assert.assertEquals("Failed index match for all entries", expectedMatches, new HashSet<>(matches));
139140
}
140141

142+
@Test
143+
public void testGetBulkRecordsForIndex_multipleValues() {
144+
writeBulkSets();
145+
146+
Set<Shoe> expected = new HashSet<>(Arrays.asList(
147+
tenSizesOfMensNikes[0], tenSizesOfMensNikes[1],
148+
eightSizesOfWomensNikes[0], eightSizesOfWomensNikes[1],
149+
fiveSizesOfMensVans[0], fiveSizesOfMensVans[1]
150+
));
151+
152+
Vector<Shoe> result = storage.getBulkRecordsForIndex(Shoe.META_SIZE, Arrays.asList("1", "2"));
153+
Assert.assertEquals("getBulkRecordsForIndex should return records for all matching values",
154+
expected, new HashSet<>(result));
155+
}
156+
157+
@Test
158+
public void testGetBulkRecordsForIndex_singleValue() {
159+
writeBulkSets();
160+
161+
Set<Shoe> expected = new HashSet<>(Arrays.asList(
162+
tenSizesOfMensNikes[4],
163+
eightSizesOfWomensNikes[4],
164+
fiveSizesOfMensVans[4]
165+
));
166+
167+
Vector<Shoe> result = storage.getBulkRecordsForIndex(Shoe.META_SIZE, Arrays.asList("5"));
168+
Assert.assertEquals("getBulkRecordsForIndex should return records matching the given value",
169+
expected, new HashSet<>(result));
170+
}
171+
172+
@Test
173+
public void testGetBulkRecordsForIndex_noMatches() {
174+
writeBulkSets();
175+
176+
Vector<Shoe> result = storage.getBulkRecordsForIndex(Shoe.META_BRAND, Arrays.asList("adidas"));
177+
Assert.assertTrue("getBulkRecordsForIndex should return empty result when no records match",
178+
result.isEmpty());
179+
}
180+
181+
@Test
182+
public void testGetBulkRecordsForIndex_allValuesForField() {
183+
writeBulkSets();
184+
185+
Set<Shoe> expected = new HashSet<>();
186+
expected.addAll(Arrays.asList(tenSizesOfMensNikes));
187+
expected.addAll(Arrays.asList(eightSizesOfWomensNikes));
188+
189+
Vector<Shoe> result = storage.getBulkRecordsForIndex(Shoe.META_BRAND, Arrays.asList("nike"));
190+
Assert.assertEquals("getBulkRecordsForIndex should return all nike shoes",
191+
expected, new HashSet<>(result));
192+
}
193+
194+
@Test
195+
public void testGetBulkIdsForIndex_multipleValues() {
196+
writeBulkSets();
197+
198+
// Request IDs for sizes "1" and "2" — should match one shoe from each of the 3 sets
199+
Set<Integer> expected = new HashSet<>();
200+
expected.add(tenSizesOfMensNikes[0].getID());
201+
expected.add(tenSizesOfMensNikes[1].getID());
202+
expected.add(eightSizesOfWomensNikes[0].getID());
203+
expected.add(eightSizesOfWomensNikes[1].getID());
204+
expected.add(fiveSizesOfMensVans[0].getID());
205+
expected.add(fiveSizesOfMensVans[1].getID());
206+
207+
Vector<Integer> result = storage.getBulkIdsForIndex(Shoe.META_SIZE, Arrays.asList("1", "2"));
208+
Assert.assertEquals("getBulkIdsForIndex should return IDs for all records matching any of the given values",
209+
expected, new HashSet<>(result));
210+
}
211+
212+
@Test
213+
public void testGetBulkIdsForIndex_singleValue() {
214+
writeBulkSets();
215+
216+
Set<Integer> expected = new HashSet<>();
217+
expected.add(tenSizesOfMensNikes[4].getID());
218+
expected.add(eightSizesOfWomensNikes[4].getID());
219+
expected.add(fiveSizesOfMensVans[4].getID());
220+
221+
Vector<Integer> result = storage.getBulkIdsForIndex(Shoe.META_SIZE, Arrays.asList("5"));
222+
Assert.assertEquals("getBulkIdsForIndex should return IDs for all records matching the given value",
223+
expected, new HashSet<>(result));
224+
}
225+
226+
@Test
227+
public void testGetBulkIdsForIndex_noMatches() {
228+
writeBulkSets();
229+
230+
Vector<Integer> result = storage.getBulkIdsForIndex(Shoe.META_BRAND, Arrays.asList("adidas"));
231+
Assert.assertTrue("getBulkIdsForIndex should return empty result when no records match",
232+
result.isEmpty());
233+
}
234+
235+
@Test
236+
public void testGetBulkIdsForIndex_allValuesForField() {
237+
writeBulkSets();
238+
239+
Set<Integer> allIds = getIdsFromModels(tenSizesOfMensNikes);
240+
allIds.addAll(getIdsFromModels(eightSizesOfWomensNikes));
241+
242+
Vector<Integer> result = storage.getBulkIdsForIndex(Shoe.META_BRAND, Arrays.asList("nike"));
243+
Assert.assertEquals("getBulkIdsForIndex should return IDs for all nike shoes",
244+
allIds, new HashSet<>(result));
245+
}
246+
141247
private void writeBulkSets() {
142248
writeAll(tenSizesOfMensNikes);
143249
writeAll(eightSizesOfWomensNikes);

0 commit comments

Comments
 (0)