Skip to content

Instantly share code, notes, and snippets.

@egonelbre
Forked from rgarcia/foo.go
Last active August 29, 2015 14:10
Show Gist options
  • Save egonelbre/04c7a33b60ea791862ed to your computer and use it in GitHub Desktop.
Save egonelbre/04c7a33b60ea791862ed to your computer and use it in GitHub Desktop.
package foo
type Foo struct {
}
type DB interface {
Find(string) (Foo, error)
}
package foo
import "testing"
type InsertableDB interface {
DB
Insert(string, Foo) error
}
func RunDBTests(t *testing.T, db InsertableDB) {
// Find("asdf") -> returns nothing
// Insert("asdf", Foo{}) -> successfully inserts
// Find("asdf") -> returns same Foo inserted above
}
package memorydb
import "errors"
type DB struct {
foos map[string]Foo
}
func New() *DB {
return &DB{
foos: make(map[string]Foo),
}
}
func (db *DB) Find(id string) (Foo, error) {
if foo, ok := db.foos[id]; ok {
return foo, nil
}
return Foo{}, errors.New("unknown foo")
}
package memorydb
import "testing"
func (db *DB) Insert(id string, foo Foo) error {
// insert into db.foos
return nil
}
func TestMemoryDB(t *testing.T) {
db := New()
foo.RunDBTests(t, db)
}
package mongodb
type DB struct {
// ...
}
func New() DB {
return &DB{}
}
func (db *DB) Find(id string) (Foo, error) {
// ...
return Foo{}, nil
}
package foo
import "testing"
func (db *DB) Insert(id string, foo Foo) error {
// insert using imdb.MongoDB internals
return nil
}
func TestMongoDB(t *testing.T) {
db := New()
foo.RunDBTests(t, db)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment