Last active
August 29, 2015 14:10
-
-
Save rgarcia/5bff218b34b6c1a60a44 to your computer and use it in GitHub Desktop.
testing read-only interfaces
This file contains 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 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 injectableDB interface { | |
DB | |
Insert(string, Foo) error | |
} | |
func testDBFind(t *testing.T, db injectableDB) { | |
// Find("asdf") -> returns nothing | |
// Insert("asdf", Foo{}) -> successfully inserts | |
// Find("asdf") -> returns same Foo inserted above | |
} |
This file contains 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 "errors" | |
type MemoryDB struct { | |
foos map[string]Foo | |
} | |
func NewMemoryDB() DB { | |
return &MemoryDB{ | |
foos: make(map[string]Foo), | |
} | |
} | |
func (mdb *MemoryDB) Find(id string) (Foo, error) { | |
if foo, ok := mdb.foos[id]; ok { | |
return foo, nil | |
} | |
return Foo{}, errors.New("unknown foo") | |
} |
This file contains 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 injectableMemoryDB struct { | |
*MemoryDB | |
} | |
func (imdb *injectableMemoryDB) Insert(id string, foo Foo) error { | |
// insert into imdb.MemoryDB.foos | |
return nil | |
} | |
func TestMemoryDB(t *testing.T) { | |
db := NewMemoryDB() | |
mdb := db.(*MemoryDB) | |
testDBFind(t, &injectableMemoryDB{mdb}) | |
} |
This file contains 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 MongoDB struct { | |
// ... | |
} | |
func NewMongoDB() DB { | |
return &MongoDB{} | |
} | |
func (mdb *MongoDB) Find(id string) (Foo, error) { | |
// ... | |
return Foo{}, nil | |
} |
This file contains 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 injectableMongoDB struct { | |
*MongoDB | |
} | |
func (imdb *injectableMongoDB) Insert(id string, foo Foo) error { | |
// insert using imdb.MongoDB internals | |
return nil | |
} | |
func TestMongoDB(t *testing.T) { | |
db := NewMongoDB() | |
mdb := db.(*MongoDB) | |
testDBFind(t, &injectableMongoDB{mdb}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment