Skip to content

CreateIndex returns Index instance, tested #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/main/java/com/meilisearch/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ public Client(Config config) {
* @return Meilisearch API response
* @throws Exception If an error occurs
*/
public String createIndex(String uid) throws Exception {
return this.indexesHandler.create(uid);
public Index createIndex(String uid) throws Exception {
Index index = gson.fromJson(this.indexesHandler.create(uid), Index.class);
index.setConfig(this.config);
return index;
}

/**
Expand All @@ -45,8 +47,11 @@ public String createIndex(String uid) throws Exception {
* @return Meilisearch API response
* @throws Exception If an error occurs
*/
public String createIndex(String uid, String primaryKey) throws Exception {
return this.indexesHandler.create(uid, primaryKey);
public Index createIndex(String uid, String primaryKey) throws Exception {
// return this.indexesHandler.create(uid, primaryKey);
Index index = gson.fromJson(this.indexesHandler.create(uid, primaryKey), Index.class);
index.setConfig(this.config);
return index;
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/test/java/com/meilisearch/integration/DocumentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ static void cleanMeiliSearch() {
public void testAddDocumentsSingle() throws Exception {

String indexUid = "addSingleDocument";
client.createIndex(indexUid);
Index index = client.getIndex(indexUid);
Index index = client.createIndex(indexUid);

TestData<Movie> testData = this.getTestData(MOVIES_INDEX, Movie.class);
String singleDocument = this.gson.toJson(testData.getData().get(0));
Expand Down Expand Up @@ -64,8 +63,7 @@ public void testAddDocumentsSingle() throws Exception {
public void testAddDocumentsMultiple() throws Exception {

String indexUid = "addMultipleDocuments";
client.createIndex(indexUid);
Index index = client.getIndex(indexUid);
Index index = client.createIndex(indexUid);

TestData<Movie> testData = this.getTestData(MOVIES_INDEX, Movie.class);
UpdateStatus updateInfo = this.gson.fromJson(
Expand Down
21 changes: 7 additions & 14 deletions src/test/java/com/meilisearch/integration/IndexesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ static void cleanMeiliSearch() {
@Test
public void testCreateIndexWithoutPrimaryKey() throws Exception {
String indexUid = "IndexesTest";
client.createIndex(indexUid);
Index index = client.getIndex(indexUid);
Index index = client.createIndex(indexUid);
assertEquals(index.getUid(), indexUid);
assertNull(index.getPrimaryKey());
client.deleteIndex(index.getUid());
Expand All @@ -49,8 +48,7 @@ public void testCreateIndexWithoutPrimaryKey() throws Exception {
@Test
public void testCreateIndexWithPrimaryKey() throws Exception {
String indexUid = "IndexesTest";
client.createIndex(indexUid, this.primaryKey);
Index index = client.getIndex(indexUid);
Index index = client.createIndex(indexUid, this.primaryKey);
assertEquals(index.getUid(), indexUid);
assertEquals(index.getPrimaryKey(), this.primaryKey);
client.deleteIndex(index.getUid());
Expand All @@ -62,8 +60,7 @@ public void testCreateIndexWithPrimaryKey() throws Exception {
@Test
public void testUpdateIndexPrimaryKey() throws Exception {
String indexUid = "IndexesTest";
client.createIndex(indexUid);
Index index = client.getIndex(indexUid);
Index index = client.createIndex(indexUid);
assertEquals(index.getUid(), indexUid);
assertNull(index.getPrimaryKey());
client.updateIndex(indexUid, this.primaryKey);
Expand All @@ -79,10 +76,8 @@ public void testUpdateIndexPrimaryKey() throws Exception {
@Test
public void testGetIndexList() throws Exception {
String[] indexUids = {"IndexesTest", "IndexesTest2"};
client.createIndex(indexUids[0]);
client.createIndex(indexUids[1], this.primaryKey);
Index index1 = client.getIndex(indexUids[0]);
Index index2 = client.getIndex(indexUids[1]);
Index index1 = client.createIndex(indexUids[0]);
Index index2 = client.createIndex(indexUids[1], this.primaryKey);
Index[] indexes = client.getIndexList();
assertEquals(2, indexes.length);
assert (Arrays.asList(indexUids).contains(indexUids[0]));
Expand All @@ -97,8 +92,7 @@ public void testGetIndexList() throws Exception {
@Test
public void testWaitForPendingUpdate() throws Exception {
String indexUid = "IndexesTest2";
client.createIndex(indexUid);
Index index = client.getIndex(indexUid);
Index index = client.createIndex(indexUid);

UpdateStatus updateInfo = this.gson.fromJson(
index.addDocuments(this.testData.getRaw()),
Expand All @@ -123,8 +117,7 @@ public void testWaitForPendingUpdate() throws Exception {
@Test
public void testWaitForPendingUpdateTimoutInMs() throws Exception {
String indexUid = "IndexesTest2";
client.createIndex(indexUid);
Index index = client.getIndex(indexUid);
Index index = client.createIndex(indexUid);

UpdateStatus updateInfo = this.gson.fromJson(
index.addDocuments(this.testData.getRaw()),
Expand Down