Skip to content

Commit f87a62f

Browse files
committed
Update tests to run with junit5 & add docker-compose for local testing
1 parent c9bcf9c commit f87a62f

File tree

12 files changed

+261
-175
lines changed

12 files changed

+261
-175
lines changed

docker-compose.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: "3"
2+
3+
services:
4+
typesense:
5+
image: typesense/typesense:0.24.1
6+
container_name: "typesense"
7+
ports:
8+
- "8108:8108"
9+
volumes:
10+
- data-dir:/data
11+
environment:
12+
TYPESENSE_DATA_DIR: /data
13+
TYPESENSE_API_KEY: xyz
14+
restart: "no"
15+
16+
volumes:
17+
data-dir:

src/test/java/org/typesense/api/AliasesTest.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,54 @@
11
package org.typesense.api;
22

3-
import junit.framework.TestCase;
4-
import org.typesense.api.exceptions.TypesenseError;
3+
import org.junit.jupiter.api.AfterEach;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
56
import org.typesense.model.CollectionAlias;
67
import org.typesense.model.CollectionAliasSchema;
78
import org.typesense.model.CollectionAliasesResponse;
8-
import org.typesense.resources.Node;
99

10-
import java.time.Duration;
11-
import java.util.ArrayList;
12-
13-
public class AliasesTest extends TestCase {
10+
class AliasesTest {
1411

1512
private Client client;
1613
private Helper helper;
1714

18-
public void setUp() throws Exception {
19-
super.setUp();
15+
@BeforeEach
16+
void setUp() throws Exception {
2017
helper = new Helper();
2118
helper.teardown();
2219
client = helper.getClient();
2320
helper.createTestAlias();
2421
}
2522

26-
public void tearDown() throws Exception {
27-
super.tearDown();
23+
@AfterEach
24+
void tearDown() throws Exception {
2825
helper.teardown();
2926
}
3027

31-
public void testUpsert() throws Exception {
28+
@Test
29+
void testUpsert() throws Exception {
3230
CollectionAliasSchema collectionAliasSchema = new CollectionAliasSchema();
3331
collectionAliasSchema.collectionName("books_june11");
3432

3533
System.out.println(client.aliases().upsert("books1", collectionAliasSchema));
3634
}
3735

38-
public void testRetrieveAll() throws Exception {
36+
@Test
37+
void testRetrieveAll() throws Exception {
3938
CollectionAliasesResponse collectionAliasesResponse = client.aliases().retrieve();
4039

4140
System.out.println(collectionAliasesResponse);
4241
}
4342

44-
public void testRetrieveSingleAlias() throws Exception {
43+
@Test
44+
void testRetrieveSingleAlias() throws Exception {
4545
CollectionAlias collectionAlias = client.aliases("books").retrieve();
4646

4747
System.out.println(collectionAlias);
4848
}
4949

50-
public void testDelete() throws Exception {
50+
@Test
51+
void testDelete() throws Exception {
5152
CollectionAlias collectionAlias = client.aliases("books").delete();
5253

5354
System.out.println(collectionAlias);

src/test/java/org/typesense/api/CollectionsTest.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,55 @@
11
package org.typesense.api;
22

3-
import junit.framework.TestCase;
3+
import org.junit.jupiter.api.AfterEach;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
46
import org.typesense.model.CollectionResponse;
57
import org.typesense.model.CollectionSchema;
68
import org.typesense.model.Field;
79

810
import java.util.ArrayList;
911

1012

11-
public class CollectionsTest extends TestCase {
13+
class CollectionsTest {
1214

13-
public Client client;
15+
Client client;
1416
private Helper helper;
1517

16-
public void setUp() throws Exception {
17-
super.setUp();
18+
@BeforeEach
19+
void setUp() throws Exception {
1820
helper = new Helper();
1921
helper.teardown();
2022
this.client = helper.getClient();
2123
}
2224

23-
public void tearDown() throws Exception {
24-
super.tearDown();
25+
@AfterEach
26+
void tearDown() throws Exception {
2527
helper.teardown();
2628
}
2729

2830

29-
public void testRetrieveAllCollections() throws Exception {
31+
@Test
32+
void testRetrieveAllCollections() throws Exception {
3033
helper.createTestCollection();
3134
CollectionResponse[] collectionResponses = client.collections().retrieve();
3235
for(CollectionResponse c:collectionResponses)
3336
System.out.println(c);
3437
}
3538

36-
public void testRetrieveSingleCollection() throws Exception {
39+
@Test
40+
void testRetrieveSingleCollection() throws Exception {
3741
helper.createTestCollection();
3842
System.out.println(client.collections("books").retrieve());
3943
}
4044

41-
public void testDeleteCollection() throws Exception {
45+
@Test
46+
void testDeleteCollection() throws Exception {
4247
helper.createTestCollection();
4348
System.out.println(client.collections("books").delete());
4449
}
4550

46-
public void testCreateCollection() throws Exception {
51+
@Test
52+
void testCreateCollection() throws Exception {
4753

4854
ArrayList<Field> fields = new ArrayList<>();
4955
fields.add(new Field().name("countryName").type(FieldTypes.STRING));

src/test/java/org/typesense/api/DocumentsTest.java

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
package org.typesense.api;
22

3-
import com.fasterxml.jackson.databind.JsonNode;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import junit.framework.TestCase;
6-
import org.junit.Assert;
7-
import org.typesense.model.*;
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
85
import org.typesense.api.exceptions.ObjectNotFound;
6+
import org.typesense.model.CollectionSchema;
7+
import org.typesense.model.DeleteDocumentsParameters;
8+
import org.typesense.model.ExportDocumentsParameters;
9+
import org.typesense.model.Field;
10+
import org.typesense.model.ImportDocumentsParameters;
11+
import org.typesense.model.SearchParameters;
12+
import org.typesense.model.SearchResult;
913

1014
import java.util.ArrayList;
1115
import java.util.HashMap;
1216
import java.util.List;
1317
import java.util.Map;
1418

15-
public class DocumentsTest extends TestCase {
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.fail;
1622

17-
public Client client;
23+
class DocumentsTest {
24+
25+
Client client;
1826
private Helper helper;
1927

20-
public void setUp() throws Exception {
21-
super.setUp();
28+
@BeforeEach
29+
void setUp() throws Exception {
2230
helper = new Helper();
2331
this.client = helper.getClient();
2432
helper.teardown();
2533
helper.createTestCollection();
2634
}
2735

28-
public void testRetrieveDocument() throws Exception {
36+
@Test
37+
void testRetrieveDocument() throws Exception {
2938
helper.createTestDocument();
3039
Map<String, Object> resp = client.collections("books").documents("1").retrieve();
31-
Assert.assertEquals(6, resp.size());
32-
Assert.assertEquals("1", resp.get("id"));
40+
assertEquals(6, resp.size());
41+
assertEquals("1", resp.get("id"));
3342
}
3443

35-
public void testCreateDocument() throws Exception {
44+
@Test
45+
void testCreateDocument() throws Exception {
3646
String[] authors = {"shakspeare", "william"};
3747
HashMap<String, Object> hmap = new HashMap<>();
3848
hmap.put("title", "Romeo and juliet");
@@ -46,15 +56,16 @@ public void testCreateDocument() throws Exception {
4656
hmap.put("id", "1");
4757

4858
Map<String, Object> resp = client.collections("books").documents().create(hmap);
49-
Assert.assertEquals(9, resp.size());
50-
Assert.assertEquals("1", resp.get("id"));
59+
assertEquals(9, resp.size());
60+
assertEquals("1", resp.get("id"));
5161
}
5262

53-
public void testUpsertDocument() throws Exception {
63+
@Test
64+
void testUpsertDocument() throws Exception {
5465
helper.createTestDocument();
5566

5667
Map<String, Object> resp = client.collections("books").documents("1").retrieve();
57-
Assert.assertEquals("Romeo and juliet", resp.get("title"));
68+
assertEquals("Romeo and juliet", resp.get("title"));
5869

5970
String[] authors = new String[]{"jk", "Rowling"};
6071
HashMap<String, Object> hmap = new HashMap<>();
@@ -69,16 +80,17 @@ public void testUpsertDocument() throws Exception {
6980
hmap.put("id", "1");
7081

7182
resp = client.collections("books").documents().upsert(hmap);
72-
Assert.assertEquals(9, resp.size());
73-
Assert.assertEquals("1", resp.get("id"));
74-
Assert.assertEquals("harry potter", resp.get("title"));
83+
assertEquals(9, resp.size());
84+
assertEquals("1", resp.get("id"));
85+
assertEquals("harry potter", resp.get("title"));
7586

7687
// try fetching the document back
7788
resp = client.collections("books").documents("1").retrieve();
78-
Assert.assertEquals("harry potter", resp.get("title"));
89+
assertEquals("harry potter", resp.get("title"));
7990
}
8091

81-
public void testUpdateDocument() throws Exception {
92+
@Test
93+
void testUpdateDocument() throws Exception {
8294
helper.createTestDocument();
8395

8496
String[] authors = new String[]{"JK Rowling"};
@@ -91,11 +103,12 @@ public void testUpdateDocument() throws Exception {
91103

92104
// try fetching the document back
93105
Map<String, Object> resp = client.collections("books").documents("1").retrieve();
94-
Assert.assertEquals("harry potter", resp.get("title"));
95-
Assert.assertEquals(2000, resp.get("publication_year"));
106+
assertEquals("harry potter", resp.get("title"));
107+
assertEquals(2000, resp.get("publication_year"));
96108
}
97109

98-
public void testDeleteDocument() throws Exception {
110+
@Test
111+
void testDeleteDocument() throws Exception {
99112
helper.createTestDocument();
100113
client.collections("books").documents("1").delete();
101114

@@ -107,7 +120,8 @@ public void testDeleteDocument() throws Exception {
107120
}
108121
}
109122

110-
public void testDeleteDocumentByQuery() throws Exception {
123+
@Test
124+
void testDeleteDocumentByQuery() throws Exception {
111125
helper.createTestDocument();
112126
DeleteDocumentsParameters deleteDocumentsParameters = new DeleteDocumentsParameters();
113127
deleteDocumentsParameters.filterBy("publication_year:=[1666]");
@@ -121,19 +135,21 @@ public void testDeleteDocumentByQuery() throws Exception {
121135
}
122136
}
123137

124-
public void testSearchDocuments() throws Exception {
138+
@Test
139+
void testSearchDocuments() throws Exception {
125140
helper.createTestDocument();
126141
SearchParameters searchParameters = new SearchParameters()
127142
.q("romeo")
128143
.queryBy("title,authors")
129144
.prefix("false,true");
130145

131146
SearchResult searchResult = client.collections("books").documents().search(searchParameters);
132-
Assert.assertEquals(1, searchResult.getFound().intValue());
133-
Assert.assertEquals(1, searchResult.getHits().size());
147+
assertEquals(1, searchResult.getFound().intValue());
148+
assertEquals(1, searchResult.getHits().size());
134149
}
135150

136-
public void testImport() throws Exception {
151+
@Test
152+
void testImport() throws Exception {
137153
HashMap<String, Object> document1 = new HashMap<>();
138154
HashMap<String, Object> document2 = new HashMap<>();
139155
ImportDocumentsParameters queryParameters = new ImportDocumentsParameters();
@@ -152,28 +168,31 @@ public void testImport() throws Exception {
152168

153169
String countriesImport = this.client.collections("books").documents()
154170
.import_(documentList, queryParameters);
155-
Assert.assertFalse(countriesImport.contains("\"success\":false"));
171+
assertFalse(countriesImport.contains("\"success\":false"));
156172
}
157173

158-
public void testImportAsString() throws Exception {
174+
@Test
175+
void testImportAsString() throws Exception {
159176
ImportDocumentsParameters queryParameters = new ImportDocumentsParameters();
160177
queryParameters.action("create");
161178
String documentList = "{\"countryName\": \"India\", \"capital\": \"Washington\", \"gdp\": 5215}\n" +
162179
"{\"countryName\": \"Iran\", \"capital\": \"London\", \"gdp\": 5215}";
163180
String booksImport = this.client.collections("books").documents().import_(documentList, queryParameters);
164-
Assert.assertFalse(booksImport.contains("\"success\":false"));
181+
assertFalse(booksImport.contains("\"success\":false"));
165182
}
166183

167-
public void testExportDocuments() throws Exception {
184+
@Test
185+
void testExportDocuments() throws Exception {
168186
helper.createTestDocument();
169187
ExportDocumentsParameters exportDocumentsParameters = new ExportDocumentsParameters();
170188
exportDocumentsParameters.setExcludeFields("id,publication_year,authors");
171189
String exportStr = client.collections("books").documents().export(exportDocumentsParameters);
172190
String expectedExportStr = "{\"average_rating\":3.2,\"ratings_count\":124,\"title\":\"Romeo and juliet\"}";
173-
Assert.assertEquals(expectedExportStr, exportStr);
191+
assertEquals(expectedExportStr, exportStr);
174192
}
175193

176-
public void testDirtyCreate() throws Exception {
194+
@Test
195+
void testDirtyCreate() throws Exception {
177196
helper.createTestDocument();
178197

179198
ImportDocumentsParameters queryParameters = new ImportDocumentsParameters();
@@ -193,10 +212,11 @@ public void testDirtyCreate() throws Exception {
193212

194213
this.client.collections("books").documents().create(hmap, queryParameters);
195214
Map<String, Object> resp = client.collections("books").documents("1").retrieve();
196-
Assert.assertEquals("1984", resp.get("title"));
215+
assertEquals("1984", resp.get("title"));
197216
}
198217

199-
public void testNestedObjectImport() throws Exception {
218+
@Test
219+
void testNestedObjectImport() throws Exception {
200220
// create collection with nested objects support
201221
List<Field> fields = new ArrayList<>();
202222
fields.add(new Field().name("address").type(FieldTypes.OBJECT).optional(true));
@@ -215,15 +235,15 @@ public void testNestedObjectImport() throws Exception {
215235
.addTag("weight", "LOW");
216236
docs.add(doc);
217237
String itemImport = this.client.collections("items").documents().import_(docs, queryParameters);
218-
Assert.assertFalse(itemImport.contains("\"success\":false"));
238+
assertFalse(itemImport.contains("\"success\":false"));
219239

220240
// try searching on the nested document
221241
SearchParameters searchParameters = new SearchParameters()
222242
.q("red")
223243
.queryBy("tags");
224244

225245
SearchResult searchResult = client.collections("items").documents().search(searchParameters);
226-
Assert.assertEquals(1, searchResult.getFound().intValue());
227-
Assert.assertEquals(1, searchResult.getHits().size());
246+
assertEquals(1, searchResult.getFound().intValue());
247+
assertEquals(1, searchResult.getHits().size());
228248
}
229249
}

0 commit comments

Comments
 (0)