Skip to content

Commit

Permalink
Fix repo panic (#1139)
Browse files Browse the repository at this point in the history
  • Loading branch information
vimukthi-git committed Jun 19, 2019
1 parent c6f4b33 commit 9a2e9ad
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
7 changes: 6 additions & 1 deletion documents/repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package documents

import (
"github.com/centrifuge/go-centrifuge/errors"
"github.com/centrifuge/go-centrifuge/storage"
"github.com/ethereum/go-ethereum/common/hexutil"
)
Expand Down Expand Up @@ -59,7 +60,11 @@ func (r *repo) Get(accountID, id []byte) (Model, error) {
if err != nil {
return nil, err
}
return model.(Model), nil
m, ok := model.(Model)
if !ok {
return nil, errors.New("docID %s for account %s is not a model object", hexutil.Encode(id), hexutil.Encode(accountID))
}
return m, nil
}

// Create creates the model if not present in the DB.
Expand Down
44 changes: 36 additions & 8 deletions documents/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ type doc struct {
SomeString string `json:"some_string"`
}

type unknownDoc struct {
SomeString string `json:"some_string"`
}

func (unknownDoc) Type() reflect.Type {
return reflect.TypeOf(unknownDoc{})
}

func (u *unknownDoc) JSON() ([]byte, error) {
return json.Marshal(u)
}

func (u *unknownDoc) FromJSON(j []byte) error {
return json.Unmarshal(j, u)
}

func (m *doc) JSON() ([]byte, error) {
return json.Marshal(m)
}
Expand Down Expand Up @@ -67,34 +83,46 @@ func TestLevelDBRepo_Update_Exists(t *testing.T) {
}

func TestLevelDBRepo_Get_Create_Update(t *testing.T) {
repo := getRepository(ctx)
repor := getRepository(ctx)

accountID, id := utils.RandomSlice(32), utils.RandomSlice(32)
m, err := repo.Get(accountID, id)
m, err := repor.Get(accountID, id)
assert.Error(t, err, "must return error")
assert.Nil(t, m)

d := &doc{SomeString: "Hello, Repo!"}
err = repo.Create(accountID, id, d)
err = repor.Create(accountID, id, d)
assert.Nil(t, err, "Create: unknown error")

m, err = repo.Get(accountID, id)
m, err = repor.Get(accountID, id)
assert.Error(t, err, "doc is not registered yet")
assert.Nil(t, m)

repo.Register(&doc{})
m, err = repo.Get(accountID, id)
repor.Register(&doc{})
m, err = repor.Get(accountID, id)
assert.Nil(t, err)
assert.NotNil(t, m)
nd := m.(*doc)
assert.Equal(t, d, nd, "must be equal")

d.SomeString = "Hello, World!"
err = repo.Update(accountID, id, d)
err = repor.Update(accountID, id, d)
assert.Nil(t, err, "Update: unknown error")

m, err = repo.Get(accountID, id)
m, err = repor.Get(accountID, id)
assert.Nil(t, err, "Get: unknown error")
nd = m.(*doc)
assert.Equal(t, d, nd, "must be equal")

// a document id sent which is not a model
repor.(*repo).db.Register(&unknownDoc{})
unid := utils.RandomSlice(32)
u := unknownDoc{SomeString: "unknown"}
//hexKey := hexutil.Encode(append(accountID, unid...))
err = repor.(*repo).db.Create(repor.(*repo).getKey(accountID, unid), &u)
assert.NoError(t, err)
m, err = repor.Get(accountID, unid)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "is not a model object")
}
}

0 comments on commit 9a2e9ad

Please sign in to comment.