Skip to content

Commit dcbac9a

Browse files
committed
Add tests for pagination
1 parent dfda630 commit dcbac9a

File tree

4 files changed

+50
-38
lines changed

4 files changed

+50
-38
lines changed

src/main/java/com/meilisearch/sdk/Client.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public TaskInfo createIndex(String uid) throws MeilisearchException {
6464
* @throws MeilisearchException if an error occurs
6565
*/
6666
public TaskInfo createIndex(String uid, String primaryKey) throws MeilisearchException {
67-
return this.indexesHandler.create(uid, primaryKey);
67+
return this.indexesHandler.createIndex(uid, primaryKey);
6868
}
6969

7070
/**
@@ -75,7 +75,7 @@ public TaskInfo createIndex(String uid, String primaryKey) throws MeilisearchExc
7575
* @throws MeilisearchException if an error occurs
7676
*/
7777
public Results<Index> getIndexes() throws MeilisearchException {
78-
Results<Index> indexes = this.indexesHandler.getAll();
78+
Results<Index> indexes = this.indexesHandler.getIndexes();
7979
for (Index index : indexes.getResults()) {
8080
index.setConfig(this.config);
8181
}
@@ -91,7 +91,7 @@ public Results<Index> getIndexes() throws MeilisearchException {
9191
* @throws MeilisearchException if an error occurs
9292
*/
9393
public Results<Index> getIndexes(IndexesQuery param) throws MeilisearchException {
94-
Results<Index> indexes = this.indexesHandler.getAll(param);
94+
Results<Index> indexes = this.indexesHandler.getIndexes(param);
9595
for (Index index : indexes.getResults()) {
9696
index.setConfig(this.config);
9797
}
@@ -106,7 +106,7 @@ public Results<Index> getIndexes(IndexesQuery param) throws MeilisearchException
106106
* @throws MeilisearchException if an error occurs
107107
*/
108108
public String getRawIndexes() throws MeilisearchException {
109-
return this.indexesHandler.getAllRaw();
109+
return this.indexesHandler.getRawIndexes();
110110
}
111111

112112
/**
@@ -134,7 +134,7 @@ public Index index(String uid) throws MeilisearchException {
134134
* @throws MeilisearchException if an error occurs
135135
*/
136136
public Index getIndex(String uid) throws MeilisearchException {
137-
Index index = this.indexesHandler.get(uid);
137+
Index index = this.indexesHandler.getIndex(uid);
138138
index.setConfig(this.config);
139139
return index;
140140
}
@@ -161,7 +161,7 @@ public TaskInfo updateIndex(String uid, String primaryKey) throws MeilisearchExc
161161
* @throws MeilisearchException if an error occurs
162162
*/
163163
public TaskInfo deleteIndex(String uid) throws MeilisearchException {
164-
return this.indexesHandler.delete(uid);
164+
return this.indexesHandler.deleteIndex(uid);
165165
}
166166

167167
// TODO createDump will return a Task in v0.28

src/main/java/com/meilisearch/sdk/IndexesHandler.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class IndexesHandler {
3232
* @return Meilisearch API response
3333
* @throws MeilisearchException if an error occurs
3434
*/
35-
TaskInfo create(String uid) throws MeilisearchException {
36-
return this.create(uid, null);
35+
TaskInfo createIndex(String uid) throws MeilisearchException {
36+
return this.createIndex(uid, null);
3737
}
3838

3939
/**
@@ -44,7 +44,7 @@ TaskInfo create(String uid) throws MeilisearchException {
4444
* @return Meilisearch API response
4545
* @throws MeilisearchException if an error occurs
4646
*/
47-
TaskInfo create(String uid, String primaryKey) throws MeilisearchException {
47+
TaskInfo createIndex(String uid, String primaryKey) throws MeilisearchException {
4848
HashMap<String, String> index = new HashMap<String, String>();
4949
index.put("uid", uid);
5050
index.put("primaryKey", primaryKey);
@@ -59,7 +59,7 @@ TaskInfo create(String uid, String primaryKey) throws MeilisearchException {
5959
* @return Meilisearch API response
6060
* @throws MeilisearchException if an error occurs
6161
*/
62-
Index get(String uid) throws MeilisearchException {
62+
Index getIndex(String uid) throws MeilisearchException {
6363
URLBuilder urlb = new URLBuilder();
6464
urlb.addSubroute("indexes").addSubroute(uid);
6565
String urlPath = urlb.getURL();
@@ -72,7 +72,7 @@ Index get(String uid) throws MeilisearchException {
7272
* @return Meilisearch API response
7373
* @throws MeilisearchException if an error occurs
7474
*/
75-
Results<Index> getAll() throws MeilisearchException {
75+
Results<Index> getIndexes() throws MeilisearchException {
7676
return httpClient.get("/indexes", Results.class, Index.class);
7777
}
7878

@@ -83,7 +83,7 @@ Results<Index> getAll() throws MeilisearchException {
8383
* @return Meilisearch API response
8484
* @throws MeilisearchException if an error occurs
8585
*/
86-
Results<Index> getAll(IndexesQuery param) throws MeilisearchException {
86+
Results<Index> getIndexes(IndexesQuery param) throws MeilisearchException {
8787
URLBuilder urlb = new URLBuilder();
8888
urlb.addSubroute("indexes")
8989
.addParameter("limit", param.getLimit())
@@ -98,7 +98,7 @@ Results<Index> getAll(IndexesQuery param) throws MeilisearchException {
9898
* @return Meilisearch API response
9999
* @throws MeilisearchException if an error occurs
100100
*/
101-
String getAllRaw() throws MeilisearchException {
101+
String getRawIndexes() throws MeilisearchException {
102102
return httpClient.get("/indexes", String.class);
103103
}
104104

@@ -127,7 +127,7 @@ TaskInfo updatePrimaryKey(String uid, String primaryKey) throws MeilisearchExcep
127127
* @return Meilisearch API response
128128
* @throws MeilisearchException if an error occurs
129129
*/
130-
TaskInfo delete(String uid) throws MeilisearchException {
130+
TaskInfo deleteIndex(String uid) throws MeilisearchException {
131131
URLBuilder urlb = new URLBuilder();
132132
urlb.addSubroute("indexes").addSubroute(uid);
133133
String urlPath = urlb.getURL();

src/test/java/com/meilisearch/integration/ClientTest.java

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
import com.meilisearch.integration.classes.TestData;
1010
import com.meilisearch.sdk.Index;
1111
import com.meilisearch.sdk.exceptions.MeilisearchApiException;
12+
import com.meilisearch.sdk.model.IndexesQuery;
1213
import com.meilisearch.sdk.model.Results;
1314
import com.meilisearch.sdk.model.TaskInfo;
1415
import com.meilisearch.sdk.utils.Movie;
1516
import java.util.Arrays;
16-
import org.junit.jupiter.api.AfterAll;
17+
import org.junit.jupiter.api.AfterEach;
1718
import org.junit.jupiter.api.BeforeEach;
1819
import org.junit.jupiter.api.Tag;
1920
import org.junit.jupiter.api.Test;
@@ -31,8 +32,8 @@ public void initialize() {
3132
if (testData == null) testData = this.getTestData(MOVIES_INDEX, Movie.class);
3233
}
3334

34-
@AfterAll
35-
static void cleanMeilisearch() {
35+
@AfterEach
36+
public void cleanMeilisearch() {
3637
cleanup();
3738
}
3839

@@ -44,8 +45,6 @@ public void testCreateIndexWithoutPrimaryKey() throws Exception {
4445

4546
assertEquals(index.getUid(), indexUid);
4647
assertNull(index.getPrimaryKey());
47-
48-
client.deleteIndex(index.getUid());
4948
}
5049

5150
/** Test Index creation without PrimaryKey with Jackson Json Handler */
@@ -58,8 +57,6 @@ public void testCreateIndexWithoutPrimaryKeyWithJacksonJsonHandler() throws Exce
5857

5958
assertEquals(index.getUid(), indexUid);
6059
assertNull(index.getPrimaryKey());
61-
62-
clientJackson.deleteIndex(index.getUid());
6360
}
6461

6562
/** Test Index creation with PrimaryKey */
@@ -70,8 +67,6 @@ public void testCreateIndexWithPrimaryKey() throws Exception {
7067

7168
assertEquals(index.getUid(), indexUid);
7269
assertEquals(index.getPrimaryKey(), this.primaryKey);
73-
74-
client.deleteIndex(index.getUid());
7570
}
7671

7772
/** Test Index creation with PrimaryKey with Jackson Json Handler */
@@ -84,8 +79,6 @@ public void testCreateIndexWithPrimaryKeyWithJacksonJsonHandler() throws Excepti
8479

8580
assertEquals(index.getUid(), indexUid);
8681
assertEquals(index.getPrimaryKey(), this.primaryKey);
87-
88-
clientJackson.deleteIndex(index.getUid());
8982
}
9083

9184
/** Test Index creation twice doesn't throw an error: already exists */
@@ -103,8 +96,6 @@ public void testCreateIndexAlreadyExists() throws Exception {
10396
assertEquals(indexDuplicate.getUid(), indexUid);
10497
assertEquals(index.getPrimaryKey(), this.primaryKey);
10598
assertEquals(indexDuplicate.getPrimaryKey(), this.primaryKey);
106-
107-
client.deleteIndex(index.getUid());
10899
}
109100

110101
/** Test update Index PrimaryKey */
@@ -123,8 +114,6 @@ public void testUpdateIndexPrimaryKey() throws Exception {
123114
assertTrue(index instanceof Index);
124115
assertEquals(index.getUid(), indexUid);
125116
assertEquals(index.getPrimaryKey(), this.primaryKey);
126-
127-
client.deleteIndex(index.getUid());
128117
}
129118

130119
/** Test getIndex */
@@ -136,8 +125,6 @@ public void testGetIndex() throws Exception {
136125

137126
assertEquals(index.getUid(), getIndex.getUid());
138127
assertEquals(index.getPrimaryKey(), getIndex.getPrimaryKey());
139-
140-
client.deleteIndex(index.getUid());
141128
}
142129

143130
/** Test getIndexes */
@@ -151,9 +138,36 @@ public void testGetIndexes() throws Exception {
151138
assertEquals(2, indexes.getResults().length);
152139
assert (Arrays.asList(indexUids).contains(indexUids[0]));
153140
assert (Arrays.asList(indexUids).contains(indexUids[1]));
141+
}
154142

155-
client.deleteIndex(indexUids[0]);
156-
client.deleteIndex(indexUids[1]);
143+
/** Test getIndexes with limit */
144+
@Test
145+
public void testGetIndexesLimit() throws Exception {
146+
int limit = 1;
147+
String[] indexUids = {"GetIndexesLimit", "GetIndexesLimit2"};
148+
IndexesQuery query = new IndexesQuery().setLimit(limit);
149+
createEmptyIndex(indexUids[0]);
150+
createEmptyIndex(indexUids[1], this.primaryKey);
151+
Results<Index> indexes = client.getIndexes(query);
152+
153+
assertEquals(limit, indexes.getResults().length);
154+
assertEquals(limit, indexes.getLimit());
155+
}
156+
157+
/** Test getIndexes with limit and offset */
158+
@Test
159+
public void testGetIndexesLimitAndOffset() throws Exception {
160+
int limit = 1;
161+
int offset = 1;
162+
String[] indexUids = {"GetIndexesLimitOffset", "GetIndexesLimitOffset2"};
163+
IndexesQuery query = new IndexesQuery().setLimit(limit).setOffset(offset);
164+
createEmptyIndex(indexUids[0]);
165+
createEmptyIndex(indexUids[1], this.primaryKey);
166+
Results<Index> indexes = client.getIndexes(query);
167+
168+
assertEquals(limit, indexes.getResults().length);
169+
assertEquals(limit, indexes.getLimit());
170+
assertEquals(offset, indexes.getOffset());
157171
}
158172

159173
/** Test getRawIndexes */
@@ -167,14 +181,11 @@ public void testGetRawIndexes() throws Exception {
167181
JsonObject jsonIndexObject = JsonParser.parseString(indexes).getAsJsonObject();
168182
JsonArray jsonIndexArray = jsonIndexObject.getAsJsonArray("results");
169183

170-
assertEquals(4, jsonIndexArray.size());
184+
assertEquals(2, jsonIndexArray.size());
171185
assert (Arrays.asList(indexUids)
172186
.contains(jsonIndexArray.get(0).getAsJsonObject().get("uid").getAsString()));
173187
assert (Arrays.asList(indexUids)
174188
.contains(jsonIndexArray.get(1).getAsJsonObject().get("uid").getAsString()));
175-
176-
client.deleteIndex(indexUids[0]);
177-
client.deleteIndex(indexUids[1]);
178189
}
179190

180191
/** Test deleteIndex */

src/test/java/com/meilisearch/integration/classes/AbstractIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ public static void deleteAllIndexes() {
105105
Client ms = new Client(new Config(getMeilisearchHost(), "masterKey"));
106106
Results<Index> indexes = ms.getIndexes();
107107
for (Index index : indexes.getResults()) {
108-
ms.deleteIndex(index.getUid());
108+
TaskInfo task = ms.deleteIndex(index.getUid());
109+
ms.waitForTask(task.getTaskUid());
109110
}
110111
} catch (Exception e) {
111112
e.printStackTrace();

0 commit comments

Comments
 (0)