-
-
Save egonelbre/04c7a33b60ea791862ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package foo | |
type Foo struct { | |
} | |
type DB interface { | |
Find(string) (Foo, error) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mongodb | |
type DB struct { | |
// ... | |
} | |
func New() DB { | |
return &DB{} | |
} | |
func (db *DB) Find(id string) (Foo, error) { | |
// ... | |
return Foo{}, nil | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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