66 "errors"
77 "io"
88 "net/http"
9- "reflect"
109 "strings"
1110 "testing"
1211
@@ -30,15 +29,15 @@ func eqReader(r io.Reader) gomock.Matcher {
3029}
3130
3231func (m * eqReaderMatcher ) Matches (x interface {}) bool {
33- if _ , ok := x .(io.Reader ); ! ok {
32+ r , ok := x .(io.Reader )
33+ if ! ok {
3434 return false
3535 }
36- r := x .(io.Reader )
3736 allBytes , err := io .ReadAll (r )
3837 if err != nil {
3938 panic (err )
4039 }
41- return reflect . DeepEqual (allBytes , m .readerBytes )
40+ return bytes . Equal (allBytes , m .readerBytes )
4241}
4342
4443func (m * eqReaderMatcher ) String () string {
@@ -197,26 +196,38 @@ func TestDocumentsImportOnHttpStatusErrorCodeReturnsError(t *testing.T) {
197196 assert .NotNil (t , err )
198197}
199198
200- func TestDocumentsImportWithTwoDocuments (t * testing.T ) {
201- expectedParams := & api.ImportDocumentsParams {
199+ func TestDocumentsImportWithTwoSuccessesAndOneFailure (t * testing.T ) {
200+ params := & api.ImportDocumentsParams {
202201 Action : pointer .Any (api .Create ),
203202 BatchSize : pointer .Int (40 ),
204- }
205- expectedBody := strings .NewReader (`{"id":"123","companyName":"Stark Industries","numEmployees":5215,"country":"USA"}` +
206- "\n " + `{"id":"125","companyName":"Stark Industries","numEmployees":5215,"country":"USA"}` + "\n " )
207- expectedResultString := `{"success": true, "id":"123"}` + "\n " + `{"success": false, "id":"125", "error": "Bad JSON.", "document": "[bad doc"}`
203+ ReturnId : pointer .Any (true ),
204+ ReturnDoc : pointer .Any (true ),
205+ }
206+ expectedBody := `{"id":"123","companyName":"Stark Industries","numEmployees":5215,"country":"USA"}
207+ {"id":"125","companyName":"Stark Industries","numEmployees":5215,"country":"USA"}
208+ "[bad doc"
209+ `
210+ expectedResultString := `{"success": true, "id":"123","document": {"id":"123","companyName":"Stark Industries","numEmployees":5215,"country":"USA"}}
211+ {"success": true, "id":"125", "document": {"id":"125","companyName":"Stark Industries","numEmployees":5215,"country":"USA"}}
212+ {"success": false, "id":"", "error": "Bad JSON.", "document": "[bad doc"}
213+ `
208214 expectedResult := []* api.ImportDocumentResponse {
209- {Success : true , Id : "123" },
210- {Success : false , Id : "125" , Error : "Bad JSON." , Document : "[bad doc" },
215+ {Success : true , Id : "123" , Document : map [string ]interface {}{"id" : "123" , "companyName" : "Stark Industries" , "numEmployees" : float64 (5215 ), "country" : "USA" }},
216+ {Success : true , Id : "125" , Document : map [string ]interface {}{"id" : "125" , "companyName" : "Stark Industries" , "numEmployees" : float64 (5215 ), "country" : "USA" }},
217+ {Success : false , Id : "" , Error : "Bad JSON." , Document : "[bad doc" },
211218 }
212219
213220 ctrl := gomock .NewController (t )
214221 defer ctrl .Finish ()
215222 mockAPIClient := mocks .NewMockAPIClientInterface (ctrl )
216223
217224 mockAPIClient .EXPECT ().
218- ImportDocumentsWithBody (gomock .Not (gomock .Nil ()),
219- "companies" , expectedParams , "application/octet-stream" , eqReader (expectedBody )).
225+ ImportDocumentsWithBody (
226+ gomock .Not (gomock .Nil ()),
227+ "companies" ,
228+ params ,
229+ "application/octet-stream" ,
230+ eqReader (strings .NewReader (expectedBody ))).
220231 Return (& http.Response {
221232 StatusCode : http .StatusOK ,
222233 Body : io .NopCloser (strings .NewReader (expectedResultString )),
@@ -227,10 +238,7 @@ func TestDocumentsImportWithTwoDocuments(t *testing.T) {
227238 documents := []interface {}{
228239 createNewDocument ("123" ),
229240 createNewDocument ("125" ),
230- }
231- params := & api.ImportDocumentsParams {
232- Action : pointer .Any (api .Create ),
233- BatchSize : pointer .Int (40 ),
241+ "[bad doc" ,
234242 }
235243 result , err := client .Collection ("companies" ).Documents ().Import (context .Background (), documents , params )
236244
0 commit comments