Skip to content

Conversation

@Sriep
Copy link
Contributor

@Sriep Sriep commented Apr 18, 2021

Issue #81
This warps the the *gorm.DB object in an interface Transaction. Also the state object has also been wrapped in an interface.

This allows mock transactions to be used for unit tests and will make it easier to use different databases, if needed, in the future.

This has required changes in a lot of files. The key changes are in the datastore package, elsewhere they amount to

  • datastore.GetStore().GetTransaction(ctx) changed to datastore.GetTransaction(ctx* )
  • datastore.GetStore().CreateTransaction(ctx) changed to datastore.CreateTransaction(ctx)
  • .Error changed to .Error()

To mock you can derive from MockTransaction and implicate mock code for any transaction methods you need in your test.

type MyMockTransaction struct{
   datasotre.MockTransaction
}
func (t MyMockTransaction) Select(query interface{}, args ...interface{}) (tx Transaction) {
  ... some mock code
}


func TestSomething(t *testing.T) {
   datastore.MockTheStore(MockStore{})
   ... use Select somehow
}

Alternately if your unit test makes more use of the database, you can create a new MyMockStore object that implements the Store object with a mock database object, if the mock database object has the same methods as a gorm.DB then you can mostly cut and pastes on the datastore.store and database.transaction objects

type myMockStore{db mocksqldb.DB}
func (store myMockStore) Open() error{
   ...
}
func (store *myMockStore) Close() {
   ...
}

func (store *myMockStore) CreateTransaction(ctx context.Context) context.Context {
   ...
}

func (store *myMockStore) GetTransaction(ctx context.Context) Transaction {
   ...
}

and somewhere in your unit test

datastore.MockTheStore(MyMockStore)

Sriep added 4 commits April 18, 2021 11:35
# Conflicts:
#	code/go/0chain.net/blobbercore/allocation/deletefilechange.go
#	code/go/0chain.net/blobbercore/challenge/worker.go
#	code/go/0chain.net/blobbercore/datastore/store.go
#	code/go/0chain.net/blobbercore/go.sum
#	code/go/0chain.net/blobbercore/handler/worker.go
#	code/go/0chain.net/blobbercore/readmarker/entity.go
@Sriep Sriep requested a review from MurashovVen April 18, 2021 14:06
@Sriep Sriep changed the title Mock store [WIP] Mock store Apr 19, 2021
@Sriep
Copy link
Contributor Author

Sriep commented Apr 19, 2021

Replaced with #87

@Sriep Sriep closed this Apr 19, 2021
@platsko platsko deleted the mock-store branch May 9, 2021 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants