11package org .typesense .api ;
22
3+ import com .fasterxml .jackson .databind .JsonNode ;
4+ import com .fasterxml .jackson .databind .ObjectMapper ;
35import junit .framework .TestCase ;
4- import org .typesense . api . exceptions . TypesenseError ;
6+ import org .junit . Assert ;
57import org .typesense .model .*;
8+ import org .typesense .api .exceptions .ObjectNotFound ;
69
7- import java .io .File ;
8- import java .io .FileNotFoundException ;
910import java .util .ArrayList ;
1011import java .util .HashMap ;
1112import java .util .List ;
1213import java .util .Map ;
13- import java .util .Scanner ;
1414
1515public class DocumentsTest extends TestCase {
1616
@@ -21,87 +21,116 @@ public void setUp() throws Exception {
2121 super .setUp ();
2222 helper = new Helper ();
2323 this .client = helper .getClient ();
24- helper .createTestCollection ();
25- }
26-
27- public void tearDown () throws Exception {
28- super .tearDown ();
2924 helper .teardown ();
25+ helper .createTestCollection ();
3026 }
3127
3228 public void testRetrieveDocument () throws Exception {
3329 helper .createTestDocument ();
34- System .out .println (client .collections ("books" ).documents ("1" ).retrieve ());
30+ Map <String , Object > resp = client .collections ("books" ).documents ("1" ).retrieve ();
31+ Assert .assertEquals (6 , resp .size ());
32+ Assert .assertEquals ("1" , resp .get ("id" ));
3533 }
3634
3735 public void testCreateDocument () throws Exception {
38-
39- String [] authors = {"shakspeare" ,"william" };
36+ String [] authors = {"shakspeare" , "william" };
4037 HashMap <String , Object > hmap = new HashMap <>();
41- hmap .put ("title" ,"Romeo and juliet" );
42- hmap .put ("authors" ,authors );
43- hmap .put ("image_url" ,"fgfg" );
44- hmap .put ("publication_year" ,1666 );
45- hmap .put ("ratings_count" ,124 );
46- hmap .put ("average_rating" ,3.2 );
47- hmap .put ("publication_year_facet" ,"dff" );
48- hmap .put ("authors_facet" ,authors );
49- hmap .put ("id" ,"1" );
50-
51- System .out .println (client .collections ("books" ).documents ().create (hmap ));
38+ hmap .put ("title" , "Romeo and juliet" );
39+ hmap .put ("authors" , authors );
40+ hmap .put ("image_url" , "fgfg" );
41+ hmap .put ("publication_year" , 1666 );
42+ hmap .put ("ratings_count" , 124 );
43+ hmap .put ("average_rating" , 3.2 );
44+ hmap .put ("publication_year_facet" , "dff" );
45+ hmap .put ("authors_facet" , authors );
46+ hmap .put ("id" , "1" );
47+
48+ Map <String , Object > resp = client .collections ("books" ).documents ().create (hmap );
49+ Assert .assertEquals (9 , resp .size ());
50+ Assert .assertEquals ("1" , resp .get ("id" ));
5251 }
5352
5453 public void testUpsertDocument () throws Exception {
54+ helper .createTestDocument ();
55+
56+ Map <String , Object > resp = client .collections ("books" ).documents ("1" ).retrieve ();
57+ Assert .assertEquals ("Romeo and juliet" , resp .get ("title" ));
5558
5659 String [] authors = new String []{"jk" , "Rowling" };
5760 HashMap <String , Object > hmap = new HashMap <>();
58- hmap .put ("title" ,"harry potter" );
59- hmap .put ("authors" ,authors );
60- hmap .put ("image_url" ,"fgfg" );
61- hmap .put ("publication_year" ,2001 );
62- hmap .put ("ratings_count" ,231 );
63- hmap .put ("average_rating" ,5.6 );
64- hmap .put ("publication_year_facet" ,"2001" );
65- hmap .put ("authors_facet" ,authors );
66- hmap .put ("id" ,"3" );
67-
68- System .out .println (client .collections ("books" ).documents ().upsert (hmap ));
61+ hmap .put ("title" , "harry potter" );
62+ hmap .put ("authors" , authors );
63+ hmap .put ("image_url" , "fgfg" );
64+ hmap .put ("publication_year" , 2001 );
65+ hmap .put ("ratings_count" , 231 );
66+ hmap .put ("average_rating" , 5.6 );
67+ hmap .put ("publication_year_facet" , "2001" );
68+ hmap .put ("authors_facet" , authors );
69+ hmap .put ("id" , "1" );
70+
71+ 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" ));
75+
76+ // try fetching the document back
77+ resp = client .collections ("books" ).documents ("1" ).retrieve ();
78+ Assert .assertEquals ("harry potter" , resp .get ("title" ));
79+ }
6980
81+ public void testUpdateDocument () throws Exception {
82+ helper .createTestDocument ();
83+
84+ String [] authors = new String []{"JK Rowling" };
85+ HashMap <String , Object > document = new HashMap <>();
86+ document .put ("title" , "harry potter" );
87+ document .put ("authors" , authors );
88+ document .put ("publication_year" , 2000 );
89+ document .put ("id" , "1" );
90+ client .collections ("books" ).documents ("1" ).update (document );
91+
92+ // try fetching the document back
93+ 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" ));
7096 }
7197
7298 public void testDeleteDocument () throws Exception {
7399 helper .createTestDocument ();
74- System .out .println (client .collections ("books" ).documents ("1" ).delete ());
100+ client .collections ("books" ).documents ("1" ).delete ();
101+
102+ try {
103+ client .collections ("books" ).documents ("1" ).retrieve ();
104+ fail ("Delete document failed." );
105+ } catch (ObjectNotFound expectedException ) {
106+
107+ }
75108 }
76109
77110 public void testDeleteDocumentByQuery () throws Exception {
78111 helper .createTestDocument ();
79112 DeleteDocumentsParameters deleteDocumentsParameters = new DeleteDocumentsParameters ();
80113 deleteDocumentsParameters .filterBy ("publication_year:=[1666]" );
81114 deleteDocumentsParameters .batchSize (10 );
82- System .out .println (client .collections ("books" ).documents ().delete (deleteDocumentsParameters ));
83- }
115+ client .collections ("books" ).documents ().delete (deleteDocumentsParameters );
116+ try {
117+ client .collections ("books" ).documents ("1" ).retrieve ();
118+ fail ("Delete document by query failed." );
119+ } catch (ObjectNotFound expectedException ) {
84120
85- public void testUpdateDocument () throws Exception {
86- helper .createTestDocument ();
87- String [] authors = new String []{"Shakespeare" , "william" };
88- HashMap <String , Object > document = new HashMap <>();
89- document .put ("title" ,"Romeo and juliet" );
90- document .put ("authors" ,authors );
91- document .put ("id" ,"1" );
92- client .collections ("books" ).documents ("1" ).update (document );
93- //System.out.println(client.collections("books").documents("1").update(document));
121+ }
94122 }
95123
96124 public void testSearchDocuments () throws Exception {
97125 helper .createTestDocument ();
98126 SearchParameters searchParameters = new SearchParameters ()
99- .q ("romeo" )
100- .queryBy ("title,authors" )
101- .prefix ("false,true" );
102- org .typesense .model .SearchResult searchResult = client .collections ("books" ).documents ().search (searchParameters );
127+ .q ("romeo" )
128+ .queryBy ("title,authors" )
129+ .prefix ("false,true" );
103130
104- System .out .println (searchResult );
131+ SearchResult searchResult = client .collections ("books" ).documents ().search (searchParameters );
132+ Assert .assertEquals (1 , searchResult .getFound ().intValue ());
133+ Assert .assertEquals (1 , searchResult .getHits ().size ());
105134 }
106135
107136 public void testImport () throws Exception {
@@ -110,49 +139,91 @@ public void testImport() throws Exception {
110139 ImportDocumentsParameters queryParameters = new ImportDocumentsParameters ();
111140 List <Map <String , Object >> documentList = new ArrayList <>();
112141
113- document1 .put ("countryName" ,"India" );
114- document1 .put ("capital" ,"Delhi" );
115- document1 .put ("gdp" ,23 );
116- document2 .put ("countryName" ,"Us" );
117- document2 .put ("capital" ,"Washington" );
118- document2 .put ("gdp" ,233 );
142+ document1 .put ("countryName" , "India" );
143+ document1 .put ("capital" , "Delhi" );
144+ document1 .put ("gdp" , 23 );
145+ document2 .put ("countryName" , "Us" );
146+ document2 .put ("capital" , "Washington" );
147+ document2 .put ("gdp" , 233 );
119148
120149 documentList .add (document1 );
121150 documentList .add (document2 );
122-
123151 queryParameters .action ("create" );
124- System .out .println (this .client .collections ("books" ).documents ().import_ (documentList , queryParameters ));
152+
153+ String countriesImport = this .client .collections ("books" ).documents ()
154+ .import_ (documentList , queryParameters );
155+ Assert .assertFalse (countriesImport .contains ("\" success\" :false" ));
125156 }
126157
127158 public void testImportAsString () throws Exception {
128159 ImportDocumentsParameters queryParameters = new ImportDocumentsParameters ();
129160 queryParameters .action ("create" );
130161 String documentList = "{\" countryName\" : \" India\" , \" capital\" : \" Washington\" , \" gdp\" : 5215}\n " +
131162 "{\" countryName\" : \" Iran\" , \" capital\" : \" London\" , \" gdp\" : 5215}" ;
132- System .out .println (this .client .collections ("books" ).documents ().import_ (documentList , queryParameters ));
163+ String booksImport = this .client .collections ("books" ).documents ().import_ (documentList , queryParameters );
164+ Assert .assertFalse (booksImport .contains ("\" success\" :false" ));
133165 }
134166
135167 public void testExportDocuments () throws Exception {
136168 helper .createTestDocument ();
137169 ExportDocumentsParameters exportDocumentsParameters = new ExportDocumentsParameters ();
138170 exportDocumentsParameters .setExcludeFields ("id,publication_year,authors" );
139- System .out .println (client .collections ("books" ).documents ().export (exportDocumentsParameters ));
171+ String exportStr = client .collections ("books" ).documents ().export (exportDocumentsParameters );
172+ String expectedExportStr = "{\" average_rating\" :3.2,\" ratings_count\" :124,\" title\" :\" Romeo and juliet\" }" ;
173+ Assert .assertEquals (expectedExportStr , exportStr );
140174 }
141175
142176 public void testDirtyCreate () throws Exception {
143177 helper .createTestDocument ();
178+
144179 ImportDocumentsParameters queryParameters = new ImportDocumentsParameters ();
145180 queryParameters .dirtyValues (ImportDocumentsParameters .DirtyValuesEnum .COERCE_OR_REJECT );
146181 queryParameters .action ("upsert" );
147- String [] authors = {"shakspeare" ,"william" };
182+
183+ String [] authors = {"shakspeare" , "william" };
148184 HashMap <String , Object > hmap = new HashMap <>();
149- hmap .put ("title" , 111 );
150- hmap .put ("authors" ,authors );
151- hmap .put ("publication_year" ,1666 );
152- hmap .put ("ratings_count" ,124 );
153- hmap .put ("average_rating" ,3.2 );
154- hmap .put ("id" ,"2" );
155-
156- System .out .println (this .client .collections ("books" ).documents ().create (hmap ,queryParameters ));
185+
186+ hmap .put ("id" , "1" );
187+ hmap .put ("authors" , authors );
188+ hmap .put ("publication_year" , 1666 );
189+ hmap .put ("ratings_count" , 124 );
190+ hmap .put ("average_rating" , 3.2 );
191+ // title is sent as an integer and not as string for testing coercion
192+ hmap .put ("title" , 1984 );
193+
194+ this .client .collections ("books" ).documents ().create (hmap , queryParameters );
195+ Map <String , Object > resp = client .collections ("books" ).documents ("1" ).retrieve ();
196+ Assert .assertEquals ("1984" , resp .get ("title" ));
197+ }
198+
199+ public void testNestedObjectImport () throws Exception {
200+ // create collection with nested objects support
201+ List <Field > fields = new ArrayList <>();
202+ fields .add (new Field ().name ("address" ).type (FieldTypes .OBJECT ).optional (true ));
203+ fields .add (new Field ().name ("tags" ).type (FieldTypes .OBJECT_ARRAY ).optional (true ));
204+
205+ CollectionSchema collectionSchema = new CollectionSchema ();
206+ collectionSchema .name ("items" ).fields (fields ).setEnableNestedFields (true );
207+ client .collections ().create (collectionSchema );
208+
209+ ImportDocumentsParameters queryParameters = new ImportDocumentsParameters ();
210+ queryParameters .action ("create" );
211+
212+ List <NestedDocument > docs = new ArrayList <>();
213+ NestedDocument doc = new NestedDocument ("LA" , "CA" , "USA" )
214+ .addTag ("color" , "Red" )
215+ .addTag ("weight" , "LOW" );
216+ docs .add (doc );
217+ String itemImport = this .client .collections ("items" ).documents ().import_ (docs , queryParameters );
218+ Assert .assertFalse (itemImport .contains ("\" success\" :false" ));
219+
220+ // try searching on the nested document
221+ SearchParameters searchParameters = new SearchParameters ()
222+ .q ("red" )
223+ .queryBy ("tags" );
224+
225+ SearchResult searchResult = client .collections ("items" ).documents ().search (searchParameters );
226+ Assert .assertEquals (1 , searchResult .getFound ().intValue ());
227+ Assert .assertEquals (1 , searchResult .getHits ().size ());
157228 }
158229}
0 commit comments