Skip to content

Commit

Permalink
#102, add test for invalid json
Browse files Browse the repository at this point in the history
  • Loading branch information
asabya committed Nov 24, 2021
1 parent b8e3eed commit 421cdb1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
7 changes: 1 addition & 6 deletions pkg/collection/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,11 +1080,6 @@ func (d *Document) DocBatchPut(docBatch *DocBatch, doc []byte, index int64) erro
return err
}
switch t.(type) {
case []interface{}:
// it's an array
// TODO: handle array of objects
d.logger.Errorf("inserting in batch: unknown json format")
return errors.New("unknown json format")
case map[string]interface{}:
// it's an object
docMap := t.(map[string]interface{})
Expand Down Expand Up @@ -1283,7 +1278,7 @@ func (d *Document) DocBatchPut(docBatch *DocBatch, doc []byte, index int64) erro
default:
// it's something else
d.logger.Errorf("inserting in batch: unknown json format")
return errors.New("unknown json format")
return ErrUnknownJsonFormat
}

return nil
Expand Down
64 changes: 45 additions & 19 deletions pkg/collection/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,25 +863,51 @@ func addDocument(t *testing.T, docStore *collection.Document, dbName, id, fname,

func addBatchDocument(t *testing.T, docStore *collection.Document, docBatch *collection.DocBatch, id, fname, lname string, age float64, tagMap map[string]string, tagList []string) {
t.Helper()
// create the doc
doc := &TestDocument{
ID: id,
FirstName: fname,
LastName: lname,
Age: age,
TagMap: tagMap,
TagList: tagList,
}
t.Run("valid-json", func(t *testing.T) {
// create the doc
doc := &TestDocument{
ID: id,
FirstName: fname,
LastName: lname,
Age: age,
TagMap: tagMap,
TagList: tagList,
}

// marshall the doc
data, err := json.Marshal(doc)
if err != nil {
t.Fatal(err)
}
// marshall the doc
data, err := json.Marshal(doc)
if err != nil {
t.Fatal(err)
}

// insert the document in the batch
err = docStore.DocBatchPut(docBatch, data, 0)
if err != nil {
t.Fatal(err)
}
})
t.Run("invalid-json", func(t *testing.T) {
// create the doc
doc := TestDocument{
ID: id,
FirstName: fname,
LastName: lname,
Age: age,
TagMap: tagMap,
TagList: tagList,
}

// marshall the doc
data, err := json.Marshal([]TestDocument{doc})
if err != nil {
t.Fatal(err)
}

// insert the document in the batch
err = docStore.DocBatchPut(docBatch, data, 0)
if err != collection.ErrUnknownJsonFormat {
t.Fatal(err)
}
})

// insert the document in the batch
err = docStore.DocBatchPut(docBatch, data, 0)
if err != nil {
t.Fatal(err)
}
}
1 change: 1 addition & 0 deletions pkg/collection/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ var (
ErrReadOnlyIndex = errors.New("read only index")
ErrCannotModifyImmutableIndex = errors.New("trying to modify immutable index")
ErrCouldNotUpdatePostageBatch = errors.New("could not procure new postage batch")
ErrUnknownJsonFormat = errors.New("unknown json format")
)

0 comments on commit 421cdb1

Please sign in to comment.