Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix repo panic #1139

Merged
merged 1 commit into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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")
}
}