Last active
July 15, 2016 18:48
-
-
Save squirly/ca101d0dbe31ef77dfe0a350f981a19e to your computer and use it in GitHub Desktop.
Mock dependency implementations for blog post example. Copy and paste from the blog post to complete
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 main | |
import ( | |
"net/http" | |
"fmt" | |
"errors" | |
"context" | |
) | |
// mock implementations | |
func NewPooledDatabaseConnection(url string) Database { | |
return mockDatabase{} | |
} | |
func NewDatabaseUserManager(databaseConn Database) UserManager { | |
return mockUserManager{} | |
} | |
type mockDatabase struct {} | |
type mockUserManager struct {} | |
func (um mockUserManager) GetUser(id int) (user User, e error) { | |
if id == 1 { | |
user = mockUser{id: 1, name: "John Doe"} | |
} else { | |
e = errors.New("User not found.") | |
} | |
return | |
} | |
func (um mockUserManager) AuthenticateUser(username, password string) (id int, e error) { | |
if username == "john.d" && password == "1234" { | |
id = 1 | |
} else { | |
e = errors.New("Invalid username or password.") | |
} | |
return | |
} | |
type mockUser struct { | |
id int | |
name string | |
} | |
func (u mockUser) Id() int { | |
return u.id | |
} | |
func (u mockUser) Name() string { | |
return u.name | |
} | |
// dependency interfaces | |
// authentication middleware | |
// http handler | |
// main function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment