Skip to content
This repository was archived by the owner on Jan 31, 2022. It is now read-only.

Commit e320d0f

Browse files
author
Clément Le Provost
committed
[test][offline] Add unit test for MirroredIndex.buildOffline()
1 parent 50b5278 commit e320d0f

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

algoliasearch/src/testOffline/java/com/algolia/search/saas/MirroredIndexTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
import org.json.JSONException;
3232
import org.json.JSONObject;
3333
import org.junit.Test;
34+
import org.robolectric.RuntimeEnvironment;
3435

36+
import java.io.File;
3537
import java.util.Arrays;
3638
import java.util.HashMap;
3739
import java.util.Map;
@@ -43,6 +45,7 @@
4345
import static org.junit.Assert.assertNotEquals;
4446
import static org.junit.Assert.assertNotNull;
4547
import static org.junit.Assert.assertNull;
48+
import static org.junit.Assert.assertTrue;
4649
import static org.junit.Assert.fail;
4750

4851

@@ -314,6 +317,60 @@ public void doRequestCompleted(JSONObject content, AlgoliaException error) {
314317
});
315318
}
316319

320+
@Test
321+
public void testBuildOffline() {
322+
final CountDownLatch signal = new CountDownLatch(4);
323+
324+
// Retrieve data files from resources.
325+
File resourcesDir = new File(RuntimeEnvironment.application.getPackageResourcePath() + "/src/testOffline/res");
326+
File rawDir = new File(resourcesDir, "raw");
327+
File settingsFile = new File(rawDir, "settings.json");
328+
File objectFile = new File(rawDir, "objects.json");
329+
330+
// Create the index.
331+
final MirroredIndex index = client.getIndex(Helpers.safeIndexName(Helpers.getMethodName()));
332+
index.setMirrored(true);
333+
334+
// Check that no offline data exists.
335+
assertFalse(index.hasOfflineData());
336+
337+
// Build the index.
338+
index.addBuildListener(new BuildListener() {
339+
@Override
340+
public void buildDidStart(@NonNull MirroredIndex index) {
341+
signal.countDown();
342+
}
343+
344+
@Override
345+
public void buildDidFinish(@NonNull MirroredIndex index, @Nullable Throwable error) {
346+
assertNull(error);
347+
signal.countDown();
348+
}
349+
});
350+
index.buildOfflineFromFiles(settingsFile, new File[]{ objectFile }, new AssertCompletionHandler() {
351+
@Override
352+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
353+
assertNull(error);
354+
355+
// Check that offline data exists now.
356+
assertTrue(index.hasOfflineData());
357+
358+
// Search.
359+
Query query = new Query().setQuery("peanuts").setFilters("kind:animal");
360+
index.searchOfflineAsync(query, new AssertCompletionHandler() {
361+
@Override
362+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
363+
assertNull(error);
364+
assertEquals(2, content.optInt("nbHits"));
365+
signal.countDown();
366+
}
367+
});
368+
signal.countDown();
369+
}
370+
});
371+
}
372+
373+
317374
@Test
318375
public void testGetObject() {
319376
final CountDownLatch signal = new CountDownLatch(4);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[
2+
{
3+
"objectID": "1",
4+
"name": "Snoopy",
5+
"kind": [ "dog", "animal" ],
6+
"born": 1967,
7+
"series": "Peanuts"
8+
},
9+
{
10+
"objectID": "2",
11+
"name": "Woodstock",
12+
"kind": ["bird", "animal" ],
13+
"born": 1970,
14+
"series": "Peanuts"
15+
},
16+
{
17+
"objectID": "3",
18+
"name": "Charlie Brown",
19+
"kind": [ "human" ],
20+
"born": 1950,
21+
"series": "Peanuts"
22+
},
23+
{
24+
"objectID": "4",
25+
"name": "Hobbes",
26+
"kind": [ "tiger", "animal", "teddy" ],
27+
"born": 1985,
28+
"series": "Calvin & Hobbes"
29+
},
30+
{
31+
"objectID": "5",
32+
"name": "Calvin",
33+
"kind": [ "human" ],
34+
"born": 1985,
35+
"series": "Calvin & Hobbes"
36+
}
37+
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"attributesToIndex": [
3+
"name",
4+
"kind",
5+
"series"
6+
],
7+
"attributesForFaceting": [
8+
"kind"
9+
]
10+
}

0 commit comments

Comments
 (0)