Skip to content

Commit 720e0d1

Browse files
authored
Merge pull request #31 from niemannd/feat/integration-tests
split integration and unit tests
2 parents 0474852 + 97213cb commit 720e0d1

File tree

15 files changed

+637
-288
lines changed

15 files changed

+637
-288
lines changed

.github/workflows/gradle.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ jobs:
1616

1717
steps:
1818
- uses: actions/checkout@v2
19+
- name: Docker setup
20+
run: docker run -d -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --no-analytics=true --no-sentry=true --master-key='masterKey'
1921
- name: Set up JDK 1.8
2022
uses: actions/setup-java@v1
2123
with:
2224
java-version: 1.8
2325
- name: Grant execute permission for gradlew
2426
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
2727
- name: Build with Gradle
28-
run: ./gradlew build
28+
run: ./gradlew build integrationTest

build.gradle

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ plugins {
1111
id 'java-library'
1212
}
1313

14+
group = 'com.meilisearch.sdk'
15+
version = '0.1.0-SNAPSHOT'
16+
17+
java {
18+
sourceCompatibility = JavaVersion.VERSION_1_8
19+
targetCompatibility = JavaVersion.VERSION_1_8
20+
}
21+
1422
repositories {
1523
// Use jcenter for resolving dependencies.
1624
// You can declare any Maven/Ivy/file repository here.
@@ -48,7 +56,18 @@ task buildJar(type: Jar) {
4856
}
4957

5058
test {
51-
useJUnitPlatform()
59+
useJUnitPlatform {
60+
excludeTags("integration")
61+
}
62+
testLogging {
63+
events "passed", "skipped", "failed"
64+
}
65+
}
66+
67+
task integrationTest(type: Test) {
68+
useJUnitPlatform {
69+
includeTags("integration")
70+
}
5271
testLogging {
5372
events "passed", "skipped", "failed"
5473
}

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

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
* Search request query string builder
55
*/
66
class SearchRequest {
7-
String q;
8-
int offset;
9-
int limit;
10-
String attributesToRetrieve;
11-
String attributesToCrop;
12-
int cropLength;
13-
String attributesToHighlight;
14-
String filters;
15-
boolean matches;
7+
private String q;
8+
private int offset;
9+
private int limit;
10+
private String attributesToRetrieve;
11+
private String attributesToCrop;
12+
private int cropLength;
13+
private String attributesToHighlight;
14+
private String filters;
15+
private boolean matches;
1616

1717
SearchRequest(String q) {
1818
this(q, 0);
@@ -50,6 +50,42 @@ class SearchRequest {
5050
this.matches = matches;
5151
}
5252

53+
public String getQ() {
54+
return q;
55+
}
56+
57+
public int getOffset() {
58+
return offset;
59+
}
60+
61+
public int getLimit() {
62+
return limit;
63+
}
64+
65+
public String getAttributesToRetrieve() {
66+
return attributesToRetrieve;
67+
}
68+
69+
public String getAttributesToCrop() {
70+
return attributesToCrop;
71+
}
72+
73+
public int getCropLength() {
74+
return cropLength;
75+
}
76+
77+
public String getAttributesToHighlight() {
78+
return attributesToHighlight;
79+
}
80+
81+
public String getFilters() {
82+
return filters;
83+
}
84+
85+
public boolean isMatches() {
86+
return matches;
87+
}
88+
5389
String getQuery() {
5490
StringBuilder sb = new StringBuilder();
5591

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.meilisearch.integration;
2+
3+
4+
import com.google.gson.Gson;
5+
import com.google.gson.reflect.TypeToken;
6+
import com.meilisearch.sdk.Client;
7+
import com.meilisearch.sdk.Config;
8+
import com.meilisearch.sdk.Index;
9+
import com.meilisearch.sdk.utils.Movie;
10+
11+
import java.io.*;
12+
import java.net.URL;
13+
import java.util.HashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
import java.util.stream.Collectors;
17+
18+
public abstract class AbstractIT {
19+
protected Client client;
20+
protected final Gson gson = new Gson();
21+
22+
private final Map<String, TestData<?>> testData = new HashMap<>();
23+
24+
public static final String MOVIES_INDEX = "movies.json";
25+
26+
public AbstractIT() {
27+
try {
28+
loadResource(MOVIES_INDEX);
29+
} catch (IOException e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
34+
public void setUp() {
35+
if (client == null)
36+
client = new Client(new Config("http://localhost:7700", "masterKey"));
37+
}
38+
39+
40+
public static void cleanup() {
41+
deleteAllIndexes();
42+
}
43+
44+
public void loadResource(String fileName) throws IOException {
45+
ClassLoader classLoader = getClass().getClassLoader();
46+
47+
URL resource = classLoader.getResource(fileName);
48+
if (resource == null) {
49+
throw new IllegalArgumentException("file is not found!");
50+
}
51+
File file = new File(resource.getFile());
52+
String rawData = new BufferedReader(new InputStreamReader(new FileInputStream(file))).lines().collect(Collectors.joining());
53+
List<Movie> movies = gson.fromJson(rawData, TypeToken.getParameterized(List.class, Movie.class).getType());
54+
testData.put(fileName, new TestData<>(rawData, movies));
55+
}
56+
57+
static public void deleteAllIndexes() {
58+
try {
59+
Client ms = new Client(new Config("http://localhost:7700", "masterKey"));
60+
Index[] indexes = ms.getIndexList();
61+
for (Index index : indexes) {
62+
ms.deleteIndex(index.getUid());
63+
}
64+
} catch (Exception e) {
65+
e.printStackTrace();
66+
}
67+
}
68+
69+
@SuppressWarnings("unchecked")
70+
public <T> TestData<T> getTestData(String index, Class<T> tClass) {
71+
return (TestData<T>) testData.get(index);
72+
}
73+
74+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.meilisearch.integration;
2+
3+
import com.meilisearch.sdk.Index;
4+
import com.meilisearch.sdk.UpdateStatus;
5+
import com.meilisearch.sdk.utils.Movie;
6+
import org.junit.jupiter.api.AfterAll;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Tag;
9+
import org.junit.jupiter.api.Test;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertNotNull;
13+
14+
@Tag("integration")
15+
public class DocumentsTest extends AbstractIT {
16+
17+
@BeforeEach
18+
public void initialize() {
19+
this.setUp();
20+
}
21+
22+
@AfterAll
23+
static void cleanMeiliSearch() {
24+
cleanup();
25+
}
26+
27+
/**
28+
* Test Add single document
29+
*/
30+
@Test
31+
public void testAddDocumentsSingle() throws Exception {
32+
33+
String indexUid = "addSingleDocument";
34+
client.createIndex(indexUid);
35+
Index index = client.getIndex(indexUid);
36+
37+
TestData<Movie> testData = this.getTestData(MOVIES_INDEX, Movie.class);
38+
String singleDocument = this.gson.toJson(testData.getData().get(0));
39+
UpdateStatus updateInfo = this.gson.fromJson(
40+
index.addDocuments("[" + singleDocument + "]"),
41+
UpdateStatus.class
42+
);
43+
44+
index.waitForPendingUpdate(updateInfo.getUpdateId());
45+
Movie[] movies = this.gson.fromJson(index.getDocuments(), Movie[].class);
46+
47+
assertEquals(1, movies.length);
48+
assertEquals(419704, movies[0].getId());
49+
assertEquals("Ad Astra", movies[0].getTitle());
50+
assertEquals("https://image.tmdb.org/t/p/original/xBHvZcjRiWyobQ9kxBhO6B2dtRI.jpg", movies[0].getPoster());
51+
assertEquals("The near future, a time when both hope and hardships drive humanity to look to the stars and beyond. While a mysterious phenomenon menaces to destroy life on planet Earth, astronaut Roy McBride undertakes a mission across the immensity of space and its many perils to uncover the truth about a lost expedition that decades before boldly faced emptiness and silence in search of the unknown.", movies[0].getOverview());
52+
assertEquals("2019-09-17", movies[0].getRelease_date());
53+
assertEquals("en", movies[0].getLanguage());
54+
assertNotNull(movies[0].getGenres());
55+
assertEquals(2, movies[0].getGenres().length);
56+
assertEquals("Science Fiction", movies[0].getGenres()[0]);
57+
assertEquals("Drama", movies[0].getGenres()[1]);
58+
}
59+
60+
/**
61+
* Test Add multiple documents
62+
*/
63+
@Test
64+
public void testAddDocumentsMultiple() throws Exception {
65+
66+
String indexUid = "addMultipleDocuments";
67+
client.createIndex(indexUid);
68+
Index index = client.getIndex(indexUid);
69+
70+
TestData<Movie> testData = this.getTestData(MOVIES_INDEX, Movie.class);
71+
UpdateStatus updateInfo = this.gson.fromJson(
72+
index.addDocuments(testData.getRaw()),
73+
UpdateStatus.class
74+
);
75+
76+
index.waitForPendingUpdate(updateInfo.getUpdateId());
77+
Movie[] movies = this.gson.fromJson(index.getDocuments(), Movie[].class);
78+
for (int i = 0; i < movies.length; i++) {
79+
assertEquals(movies[i].getTitle(), testData.getData().get(i).getTitle());
80+
}
81+
}
82+
83+
}
84+

0 commit comments

Comments
 (0)