lets-go:14.5 get some mocks in place
This commit is contained in:
parent
01bfefac13
commit
cd40158cc4
34
snippetbox/internal/models/mocks/snippets.go
Normal file
34
snippetbox/internal/models/mocks/snippets.go
Normal file
@ -0,0 +1,34 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"snippetbox.alexedwards.net/internal/models"
|
||||
)
|
||||
|
||||
var mockSnippet = models.Snippet{
|
||||
ID: 1,
|
||||
Title: "An old silent pond",
|
||||
Content: "An old silent pond...",
|
||||
Created: time.Now(),
|
||||
Expires: time.Now(),
|
||||
}
|
||||
|
||||
type SnippetModel struct{}
|
||||
|
||||
func (m *SnippetModel) Insert(title string, content string, expires int) (int, error) {
|
||||
return 2, nil
|
||||
}
|
||||
|
||||
func (m *SnippetModel) Get(id int) (models.Snippet, error) {
|
||||
switch id {
|
||||
case 1:
|
||||
return mockSnippet, nil
|
||||
default:
|
||||
return models.Snippet{}, models.ErrNoRecord
|
||||
}
|
||||
}
|
||||
|
||||
func (m *SnippetModel) Latest() ([]models.Snippet, error) {
|
||||
return []models.Snippet{mockSnippet}, nil
|
||||
}
|
31
snippetbox/internal/models/mocks/users.go
Normal file
31
snippetbox/internal/models/mocks/users.go
Normal file
@ -0,0 +1,31 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"snippetbox.alexedwards.net/internal/models"
|
||||
)
|
||||
|
||||
type UserModel struct{}
|
||||
|
||||
func (m *UserModel) Insert(name, email, password string) error {
|
||||
switch email {
|
||||
case "dupe@example.com":
|
||||
return models.ErrDuplicateEmail
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserModel) Authenticate(email, password string) (int, error) {
|
||||
if email == "alice@example.com" && password == "pa$$word" {
|
||||
return 1, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m *UserModel) Exists(id int) (bool, error) {
|
||||
switch id {
|
||||
case 1:
|
||||
return true, nil
|
||||
default:
|
||||
return false, nil
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user