Skip to content

Commit 22482fb

Browse files
committed
Add tests for similar documents
1 parent b8b3c88 commit 22482fb

File tree

2 files changed

+195
-12
lines changed

2 files changed

+195
-12
lines changed

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,17 @@ public SimilarDocumentRequest() {}
3131

3232
@Override
3333
public String toString() {
34-
JSONObject jsonObject =
35-
new JSONObject()
36-
.put("id", this.id)
37-
.put("embedder", this.embedder)
38-
.put("attributesToRetrieve", this.attributesToRetrieve)
39-
.put("offset", this.offset)
40-
.put("limit", this.limit)
41-
.put("filter", this.filter)
42-
.put("showRankingScore", this.showRankingScore)
43-
.put("showRankingScoreDetails", this.showRankingScoreDetails)
44-
.put("rankingScoreThreshold", this.rankingScoreThreshold)
45-
.put("retrieveVectors", this.retrieveVectors);
34+
JSONObject jsonObject = new JSONObject();
35+
jsonObject.putOpt("id", this.id);
36+
jsonObject.putOpt("embedder", this.embedder);
37+
jsonObject.putOpt("attributesToRetrieve", this.attributesToRetrieve);
38+
jsonObject.putOpt("offset", this.offset);
39+
jsonObject.putOpt("limit", this.limit);
40+
jsonObject.putOpt("filter", this.filter);
41+
jsonObject.putOpt("showRankingScore", this.showRankingScore);
42+
jsonObject.putOpt("showRankingScoreDetails", this.showRankingScoreDetails);
43+
jsonObject.putOpt("rankingScoreThreshold", this.rankingScoreThreshold);
44+
jsonObject.putOpt("retrieveVectors", this.retrieveVectors);
4645

4746
return jsonObject.toString();
4847
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package com.meilisearch.sdk;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.equalTo;
5+
import static org.hamcrest.Matchers.is;
6+
import static org.hamcrest.Matchers.nullValue;
7+
8+
import org.json.JSONObject;
9+
import org.junit.jupiter.api.Test;
10+
11+
class SimilarDocumentRequestTest {
12+
13+
@Test
14+
void toStringSimpleRequest() {
15+
SimilarDocumentRequest classToTest = new SimilarDocumentRequest().setId("123");
16+
17+
JSONObject jsonObject = new JSONObject(classToTest.toString());
18+
assertThat(jsonObject.getString("id"), is(equalTo("123")));
19+
}
20+
21+
@Test
22+
void toStringWithEmbedder() {
23+
SimilarDocumentRequest classToTest =
24+
new SimilarDocumentRequest().setId("123").setEmbedder("custom");
25+
26+
JSONObject jsonObject = new JSONObject(classToTest.toString());
27+
assertThat(jsonObject.getString("id"), is(equalTo("123")));
28+
assertThat(jsonObject.getString("embedder"), is(equalTo("custom")));
29+
}
30+
31+
@Test
32+
void toStringWithAttributesToRetrieve() {
33+
SimilarDocumentRequest classToTest =
34+
new SimilarDocumentRequest()
35+
.setId("123")
36+
.setAttributesToRetrieve(new String[] {"title", "description"});
37+
38+
JSONObject jsonObject = new JSONObject(classToTest.toString());
39+
assertThat(jsonObject.getString("id"), is(equalTo("123")));
40+
assertThat(
41+
jsonObject.getJSONArray("attributesToRetrieve").getString(0), is(equalTo("title")));
42+
assertThat(
43+
jsonObject.getJSONArray("attributesToRetrieve").getString(1),
44+
is(equalTo("description")));
45+
}
46+
47+
@Test
48+
void toStringWithOffsetAndLimit() {
49+
SimilarDocumentRequest classToTest =
50+
new SimilarDocumentRequest().setId("123").setOffset(10).setLimit(20);
51+
52+
JSONObject jsonObject = new JSONObject(classToTest.toString());
53+
assertThat(jsonObject.getString("id"), is(equalTo("123")));
54+
assertThat(jsonObject.getInt("offset"), is(equalTo(10)));
55+
assertThat(jsonObject.getInt("limit"), is(equalTo(20)));
56+
}
57+
58+
@Test
59+
void toStringWithFilter() {
60+
SimilarDocumentRequest classToTest =
61+
new SimilarDocumentRequest().setId("123").setFilter("genre = 'action'");
62+
63+
JSONObject jsonObject = new JSONObject(classToTest.toString());
64+
assertThat(jsonObject.getString("id"), is(equalTo("123")));
65+
assertThat(jsonObject.getString("filter"), is(equalTo("genre = 'action'")));
66+
}
67+
68+
@Test
69+
void toStringWithShowRankingScore() {
70+
SimilarDocumentRequest classToTest =
71+
new SimilarDocumentRequest().setId("123").setShowRankingScore(true);
72+
73+
JSONObject jsonObject = new JSONObject(classToTest.toString());
74+
assertThat(jsonObject.getString("id"), is(equalTo("123")));
75+
assertThat(jsonObject.getBoolean("showRankingScore"), is(equalTo(true)));
76+
}
77+
78+
@Test
79+
void toStringWithShowRankingScoreDetails() {
80+
SimilarDocumentRequest classToTest =
81+
new SimilarDocumentRequest().setId("123").setShowRankingScoreDetails(true);
82+
83+
JSONObject jsonObject = new JSONObject(classToTest.toString());
84+
assertThat(jsonObject.getString("id"), is(equalTo("123")));
85+
assertThat(jsonObject.getBoolean("showRankingScoreDetails"), is(equalTo(true)));
86+
}
87+
88+
@Test
89+
void toStringWithRankingScoreThreshold() {
90+
SimilarDocumentRequest classToTest =
91+
new SimilarDocumentRequest().setId("123").setRankingScoreThreshold(0.5);
92+
93+
JSONObject jsonObject = new JSONObject(classToTest.toString());
94+
assertThat(jsonObject.getString("id"), is(equalTo("123")));
95+
assertThat(jsonObject.getDouble("rankingScoreThreshold"), is(equalTo(0.5)));
96+
}
97+
98+
@Test
99+
void toStringWithRetrieveVectors() {
100+
SimilarDocumentRequest classToTest =
101+
new SimilarDocumentRequest().setId("123").setRetrieveVectors(true);
102+
103+
JSONObject jsonObject = new JSONObject(classToTest.toString());
104+
assertThat(jsonObject.getString("id"), is(equalTo("123")));
105+
assertThat(jsonObject.getBoolean("retrieveVectors"), is(equalTo(true)));
106+
}
107+
108+
@Test
109+
void toStringWithAllParameters() {
110+
SimilarDocumentRequest classToTest =
111+
new SimilarDocumentRequest()
112+
.setId("123")
113+
.setEmbedder("custom")
114+
.setAttributesToRetrieve(new String[] {"title", "description"})
115+
.setOffset(10)
116+
.setLimit(20)
117+
.setFilter("genre = 'action'")
118+
.setShowRankingScore(true)
119+
.setShowRankingScoreDetails(true)
120+
.setRankingScoreThreshold(0.5)
121+
.setRetrieveVectors(true);
122+
123+
JSONObject jsonObject = new JSONObject(classToTest.toString());
124+
assertThat(jsonObject.getString("id"), is(equalTo("123")));
125+
assertThat(jsonObject.getString("embedder"), is(equalTo("custom")));
126+
assertThat(
127+
jsonObject.getJSONArray("attributesToRetrieve").getString(0), is(equalTo("title")));
128+
assertThat(
129+
jsonObject.getJSONArray("attributesToRetrieve").getString(1),
130+
is(equalTo("description")));
131+
assertThat(jsonObject.getInt("offset"), is(equalTo(10)));
132+
assertThat(jsonObject.getInt("limit"), is(equalTo(20)));
133+
assertThat(jsonObject.getString("filter"), is(equalTo("genre = 'action'")));
134+
assertThat(jsonObject.getBoolean("showRankingScore"), is(equalTo(true)));
135+
assertThat(jsonObject.getBoolean("showRankingScoreDetails"), is(equalTo(true)));
136+
assertThat(jsonObject.getDouble("rankingScoreThreshold"), is(equalTo(0.5)));
137+
assertThat(jsonObject.getBoolean("retrieveVectors"), is(equalTo(true)));
138+
}
139+
140+
@Test
141+
void gettersAndSetters() {
142+
SimilarDocumentRequest classToTest =
143+
new SimilarDocumentRequest()
144+
.setId("123")
145+
.setEmbedder("custom")
146+
.setAttributesToRetrieve(new String[] {"title", "description"})
147+
.setOffset(10)
148+
.setLimit(20)
149+
.setFilter("genre = 'action'")
150+
.setShowRankingScore(true)
151+
.setShowRankingScoreDetails(true)
152+
.setRankingScoreThreshold(0.5)
153+
.setRetrieveVectors(true);
154+
155+
assertThat(classToTest.getId(), is(equalTo("123")));
156+
assertThat(classToTest.getEmbedder(), is(equalTo("custom")));
157+
assertThat(
158+
classToTest.getAttributesToRetrieve(),
159+
is(equalTo(new String[] {"title", "description"})));
160+
assertThat(classToTest.getOffset(), is(equalTo(10)));
161+
assertThat(classToTest.getLimit(), is(equalTo(20)));
162+
assertThat(classToTest.getFilter(), is(equalTo("genre = 'action'")));
163+
assertThat(classToTest.getShowRankingScore(), is(equalTo(true)));
164+
assertThat(classToTest.getShowRankingScoreDetails(), is(equalTo(true)));
165+
assertThat(classToTest.getRankingScoreThreshold(), is(equalTo(0.5)));
166+
assertThat(classToTest.getRetrieveVectors(), is(equalTo(true)));
167+
}
168+
169+
@Test
170+
void defaultValues() {
171+
SimilarDocumentRequest classToTest = new SimilarDocumentRequest();
172+
173+
assertThat(classToTest.getId(), is(nullValue()));
174+
assertThat(classToTest.getEmbedder(), is(nullValue()));
175+
assertThat(classToTest.getAttributesToRetrieve(), is(nullValue()));
176+
assertThat(classToTest.getOffset(), is(nullValue()));
177+
assertThat(classToTest.getLimit(), is(nullValue()));
178+
assertThat(classToTest.getFilter(), is(nullValue()));
179+
assertThat(classToTest.getShowRankingScore(), is(nullValue()));
180+
assertThat(classToTest.getShowRankingScoreDetails(), is(nullValue()));
181+
assertThat(classToTest.getRankingScoreThreshold(), is(nullValue()));
182+
assertThat(classToTest.getRetrieveVectors(), is(nullValue()));
183+
}
184+
}

0 commit comments

Comments
 (0)