Skip to content

Commit f4e3b6f

Browse files
committed
Add basic Indexes and Documents tests, run integration tests in CI with docker
1 parent 91ed341 commit f4e3b6f

File tree

5 files changed

+163
-67
lines changed

5 files changed

+163
-67
lines changed

.github/workflows/gradle.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ jobs:
2222
java-version: 1.8
2323
- name: Grant execute permission for gradlew
2424
run: chmod +x gradlew
25+
- name: Docker setup
26+
run: docker run -d -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --no-analytics=true --master-key=masterKey
2527
- name: Build with Gradle
2628
run: ./gradlew build

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ dependencies {
2222
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
2323
implementation 'com.google.guava:guava:29.0-jre'
2424
implementation 'com.google.code.gson:gson:2.8.6'
25+
implementation 'org.json:json:20200518'
2526
// Use JUnit test framework
2627
testImplementation(platform('org.junit:junit-bom:5.7.0'))
2728
testImplementation('org.junit.jupiter:junit-jupiter')

src/test/java/com/meilisearch/sdk/DeleteAllIndexes.java

Whitespace-only changes.
Lines changed: 95 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,118 @@
11
package com.meilisearch.sdk;
22

3+
import com.google.gson.Gson;
34

45
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.AfterAll;
57
import org.junit.jupiter.api.Test;
68

7-
public class DocumentsTest {
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
810

9-
Index meilisearchIndex;
11+
import org.json.JSONArray;
12+
import org.json.JSONObject;
1013

11-
@BeforeEach
12-
public void initialize() {
13-
Client ms = new Client(new Config("http://localhost:7700", ""));
1414

15-
try {
16-
// TODO: add uid of index for test
17-
this.meilisearchIndex = ms.getIndex("movies");
18-
} catch (Exception e) {
15+
public class DocumentsTest {
16+
17+
Client ms;
18+
Index index;
19+
Gson gson = new Gson();
20+
21+
String primaryKey = "id";
1922

20-
}
23+
@BeforeEach
24+
public void initialize() throws Exception {
25+
ms = new Client(new Config("http://localhost:7700", "masterKey"));
2126
}
2227

23-
@Test
24-
public void get() throws Exception {
25-
// TODO: input identifier for test
26-
System.out.println(this.meilisearchIndex.getDocument("9999"));
28+
@AfterAll
29+
static void cleanMeiliSearch() throws Exception {
30+
Client ms = new Client(new Config("http://localhost:7700", "masterKey"));
31+
Index[] indexes = ms.getIndexList();
32+
for (int i = 0; i < indexes.length; i++) {
33+
ms.deleteIndex(indexes[i].uid);
34+
}
2735
}
2836

37+
/**
38+
* Test Add single document
39+
*/
2940
@Test
30-
public void getAll() throws Exception {
31-
System.out.println(this.meilisearchIndex.getDocuments());
32-
}
41+
public void testAddDocumentsSingle() throws Exception {
3342

34-
@Test
35-
public void add() throws Exception {
36-
String testDoc = "[{\n" +
37-
" \"id\": 9999,\n" +
38-
" \"title\": \"Shazam\",\n" +
39-
" \"poster\": \"https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg\",\n" +
40-
" \"overview\": \"A boy is given the ability to become an adult superhero in times of need with a single magic word.\",\n" +
41-
" \"release_date\": \"2019-03-23\"\n" +
42-
" }]";
43-
// TODO: setup test document for 'add'
44-
System.out.println(this.meilisearchIndex.addDocuments(""));
45-
}
43+
String indexUid = "addSingleDocument";
44+
ms.createIndex(indexUid);
45+
this.index = ms.getIndex(indexUid);
4646

47-
@Test
48-
public void delete() throws Exception {
49-
// TODO: input identifier for test
50-
System.out.println(this.meilisearchIndex.deleteDocument(""));
51-
}
47+
JSONArray jsonArray = new JSONArray();
48+
JSONObject jsonObject = new JSONObject();
5249

53-
@Test
54-
public void search() throws Exception {
55-
System.out.println(this.meilisearchIndex.search("Batman"));
50+
jsonObject.put("id", "1111");
51+
jsonObject.put("title", "Alice in wonderland");
52+
jsonArray.put(jsonObject);
53+
54+
UpdateStatus updateInfo = this.gson.fromJson(
55+
index.addDocuments(jsonArray.toString()),
56+
UpdateStatus.class
57+
);
58+
59+
// TODO: Replace by WaitForPendingUpdate()
60+
String status = "";
61+
while (!status.equals("processed")){
62+
UpdateStatus updateStatus = this.gson.fromJson(
63+
index.getUpdate(updateInfo.getUpdateId()),
64+
UpdateStatus.class
65+
);
66+
status = updateStatus.getStatus();
67+
Thread.sleep(20);
68+
}
69+
assertEquals(index.getDocuments(), jsonArray.toString());
5670
}
5771

72+
/**
73+
* Test Add multiple documents
74+
*/
5875
@Test
59-
public void updates() throws Exception {
60-
System.out.println(this.meilisearchIndex.getUpdates()[0].toString());
76+
public void testAddDocumentsMultiple() throws Exception {
77+
78+
String indexUid = "addMultipleDocuments";
79+
ms.createIndex(indexUid);
80+
this.index = ms.getIndex(indexUid);
81+
82+
JSONArray jsonArray2 = new JSONArray();
83+
JSONObject jsonObject2 = new JSONObject();
84+
85+
jsonObject2.put("id", "1111");
86+
jsonObject2.put("title", "Alice in wonderland");
87+
jsonArray2.put(jsonObject2);
88+
89+
jsonObject2 = new JSONObject();
90+
jsonObject2.put("id", "222");
91+
jsonObject2.put("title", "Blice in wonderland");
92+
jsonArray2.put(jsonObject2);
93+
94+
jsonObject2 = new JSONObject();
95+
jsonObject2.put("id", "333");
96+
jsonObject2.put("title", "Clice in wonderland");
97+
jsonArray2.put(jsonObject2);
98+
99+
UpdateStatus updateInfo = this.gson.fromJson(
100+
index.addDocuments(jsonArray2.toString()),
101+
UpdateStatus.class
102+
);
103+
104+
// TODO: Replace by WaitForPendingUpdate()
105+
String status = "";
106+
while (!status.equals("processed")){
107+
UpdateStatus updateStatus = this.gson.fromJson(
108+
index.getUpdate(updateInfo.getUpdateId()),
109+
UpdateStatus.class
110+
);
111+
status = updateStatus.getStatus();
112+
Thread.sleep(20);
113+
}
114+
assertEquals(index.getDocuments(), jsonArray2.toString());
61115
}
116+
62117
}
118+
Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,91 @@
1-
/*
2-
* This Java source file was generated by the Gradle 'init' task.
3-
*/
41
package com.meilisearch.sdk;
52

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Arrays;
6+
import com.google.gson.Gson;
67
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.AfterAll;
79
import org.junit.jupiter.api.Test;
810

911
public class IndexesTest {
1012

1113
Client ms;
14+
Gson gson = new Gson();
15+
String primaryKey = "id";
1216

1317
@BeforeEach
14-
public void initialize() {
15-
ms = new Client(new Config("http://localhost:7700", ""));
18+
public void initializeClient() throws Exception {
19+
ms = new Client(new Config("http://localhost:7700", "masterKey"));
1620
}
1721

18-
@Test
19-
public void createIndex() throws Exception {
20-
System.out.println(ms.createIndex("videos"));
21-
}
22-
23-
@Test
24-
public void getIndexes() throws Exception {
25-
Index[] meilisearchIndices = ms.getIndexList();
26-
for (int a = 0; a < meilisearchIndices.length; a++) {
27-
System.out.println(meilisearchIndices[a]);
22+
@AfterAll
23+
static void cleanMeiliSearch() throws Exception {
24+
Client ms = new Client(new Config("http://localhost:7700", "masterKey"));
25+
Index[] indexes = ms.getIndexList();
26+
for (int i = 0; i < indexes.length; i++) {
27+
ms.deleteIndex(indexes[i].uid);
2828
}
29-
3029
}
3130

31+
/**
32+
* Test Index creation without PrimaryKey
33+
*/
3234
@Test
33-
public void getIndex() throws Exception {
34-
// TODO: input uid for test
35-
Index meilisearchIndex = ms.getIndex("movies");
36-
System.out.println(meilisearchIndex);
35+
public void testCreateIndexWithoutPrimaryKey() throws Exception {
36+
String indexUid = "IndexesTest";
37+
ms.createIndex(indexUid);
38+
Index index = ms.getIndex(indexUid);
39+
assertEquals(index.uid, indexUid);
40+
assertEquals(index.primaryKey, null);
41+
ms.deleteIndex(index.uid);
3742
}
3843

44+
/**
45+
* Test Index creation with PrimaryKey
46+
*/
3947
@Test
40-
public void put() throws Exception {
41-
48+
public void testCreateIndexWithPrimaryKey() throws Exception {
49+
String indexUid = "IndexesTest";
50+
ms.createIndex(indexUid, this.primaryKey);
51+
Index index = ms.getIndex(indexUid);
52+
assertEquals(index.uid, indexUid);
53+
assertEquals(index.primaryKey, this.primaryKey);
54+
ms.deleteIndex(index.uid);
4255
}
4356

57+
/**
58+
* Test update Index PrimaryKey
59+
*/
4460
@Test
45-
public void update() throws Exception {
46-
System.out.println(ms.updateIndex("video", "videos_key"));
47-
61+
public void testUpdateIndexPrimaryKey() throws Exception {
62+
String indexUid = "IndexesTest";
63+
ms.createIndex(indexUid);
64+
Index index = ms.getIndex(indexUid);
65+
assertEquals(index.uid, indexUid);
66+
assertEquals(index.primaryKey, null);
67+
ms.updateIndex(indexUid, this.primaryKey);
68+
index = ms.getIndex(indexUid);
69+
assertEquals(index.uid, indexUid);
70+
assertEquals(index.primaryKey, this.primaryKey);
71+
ms.deleteIndex(index.uid);
4872
}
4973

74+
/**
75+
* Test getIndexList
76+
*/
5077
@Test
51-
public void delete() throws Exception {
52-
System.out.println(ms.deleteIndex("videos"));
53-
}
78+
public void testGetIndexList() throws Exception {
79+
String[] indexUids = {"IndexesTest", "IndexesTest2"};
80+
ms.createIndex(indexUids[0]);
81+
ms.createIndex(indexUids[1], this.primaryKey);
82+
Index index1 = ms.getIndex(indexUids[0]);
83+
Index index2 = ms.getIndex(indexUids[1]);
84+
Index[] indexes = ms.getIndexList();
85+
assertEquals(indexes.length, 2);
86+
assert(Arrays.stream(indexUids).anyMatch(indexUids[0]::equals));
87+
assert(Arrays.stream(indexUids).anyMatch(indexUids[1]::equals));
88+
ms.deleteIndex(indexUids[0]);
89+
ms.deleteIndex(indexUids[1]);
90+
}
5491
}

0 commit comments

Comments
 (0)