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

REQUEST: Changes to allow testing & mocking #6

Closed
ghost opened this issue Jan 17, 2018 · 1 comment
Closed

REQUEST: Changes to allow testing & mocking #6

ghost opened this issue Jan 17, 2018 · 1 comment

Comments

@ghost
Copy link

ghost commented Jan 17, 2018

Many of the top structures in pogreb are concrete structs rather than interfaces, making it difficult to mock without effort in the using package. Changing some of these to interfaces would make mocking, and therefore, unit testing, easier.

Repository owner deleted a comment from 90minlivehd Jan 21, 2018
@akrylysov
Copy link
Owner

@serussell thanks for your feedback! You can define your own interface in order to mock the DB structure:

type kvStore interface {
    Put(key []byte, value []byte) error
    Get(key []byte) ([]byte, error)
    Close() error
}

func newPogrebDB(path string) (kvStore, error) {
    return pogreb.Open(path, nil)
}

func newMockDB(path string) (kvStore, error) {
    return &mockDB{}
}

func main() {
    db, err := newPogrebDB("test.db")
    db.Put([]byte("testKey"), []byte("testValue"))
}

Keep in mind that pogreb supports pluggable file systems - the FileSystem option (https://godoc.org/github.com/akrylysov/pogreb#Options). The package comes with two implementations:

  • the default OS implementation backed by the operating system's file system
  • Mem which stores everything in memory - it's perfect for testing
import (
	"github.com/akrylysov/pogreb"
	"github.com/akrylysov/pogreb/fs"
)

func main() {
    db, err := pogreb.Open(path, &pogreb.Options{FileSystem: fs.Mem})
    db.Put([]byte("testKey"), []byte("testValue"))
}

@ghost ghost closed this as completed May 1, 2018
This issue was closed.
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

No branches or pull requests

1 participant