Adding mock services
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
0c9924102b
commit
32190b7036
@ -0,0 +1,35 @@
|
|||||||
|
package mock
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.runcible.io/learning/ratchet/internal/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
var mockSnippet = model.Snippet{
|
||||||
|
ID: 1,
|
||||||
|
Title: sql.NullString{String: "Hello golang mocking", Valid: true},
|
||||||
|
Content: sql.NullString{String: "Hello golang mocking", Valid: true},
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
UpdatedAt: time.Now(),
|
||||||
|
ExpiresAt: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
type SnippetService struct{}
|
||||||
|
|
||||||
|
func (s *SnippetService) Insert(title, content string, expiresAt int) (int, error) {
|
||||||
|
return 2, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SnippetService) Get(id int) (model.Snippet, error) {
|
||||||
|
if id == mockSnippet.ID {
|
||||||
|
return mockSnippet, nil
|
||||||
|
} else {
|
||||||
|
return model.Snippet{}, model.ErrNoRecord
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SnippetService) Lastest() ([]model.Snippet, error) {
|
||||||
|
return []model.Snippet{mockSnippet}, nil
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package mock
|
||||||
|
|
||||||
|
import "git.runcible.io/learning/ratchet/internal/model"
|
||||||
|
|
||||||
|
var MockEmail = "drew@fake.com"
|
||||||
|
var MockPassword = "thisisinsecure"
|
||||||
|
|
||||||
|
type UserService struct{}
|
||||||
|
|
||||||
|
func (u *UserService) Insert(name, email, password string) (int, error) {
|
||||||
|
switch email {
|
||||||
|
case "dupe@example.com":
|
||||||
|
return 0, model.ErrDuplicateEmail
|
||||||
|
default:
|
||||||
|
return 1, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserService) Authenticate(email, password string) (int, error) {
|
||||||
|
if email == MockEmail && password == MockPassword {
|
||||||
|
return 1, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, model.ErrInvalidCredentials
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserService) Exists(id int) (bool, error) {
|
||||||
|
switch id {
|
||||||
|
case 1:
|
||||||
|
return true, nil
|
||||||
|
default:
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue