/
model_fake.go
58 lines (45 loc) · 1.49 KB
/
model_fake.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// The account model that is actually used in production will be connected to a
// database like Postgres or MySQL. This fake account model mimics data access
// to the database using the in-code map data type.
package mocks
import (
"fmt"
"github.com/cybersamx/go-recipes/fake-mock/model"
)
// FakeAccountModel implements the AccountModel by faking the data access layer
// with map.
type FakeAccountModel struct {
accounts map[string]*model.Account
}
// NewFakeAccountModel creates an instance of FakeAccountModel.
func NewFakeAccountModel() *FakeAccountModel {
return &FakeAccountModel{
accounts: make(map[string]*model.Account),
}
}
// AddAccount adds a new account to the map.
func (fam *FakeAccountModel) AddAccount(email, password string) error {
account := model.Account{
Email: email,
Password: password,
}
fam.accounts[email] = &account
return nil
}
// UpdateAccount updates the account in the map with passed email and password.
func (fam *FakeAccountModel) UpdateAccount(email, password string) error {
foundAcct := fam.accounts[email]
if foundAcct == nil {
return fmt.Errorf("can't find account with eamil %s", email)
}
foundAcct.Password = password
return nil
}
// GetAccount retrieves the account in the map associated with the passed email.
func (fam *FakeAccountModel) GetAccount(email string) (*model.Account, error) {
foundAcct := fam.accounts[email]
if foundAcct == nil {
return nil, fmt.Errorf("can't find account with eamil %s", email)
}
return foundAcct, nil
}