feat: characterized the existing use cases as a baseline; every repo fake behaves like the real ones and can be told to fail, every use case is covered; gremlins runs clean on useCases
This commit is contained in:
1 parent
09bd5afa4a
commit
7c57173497
12 files changed
+1245
-92
No files matched your search
+19
-18
@@ -7,35 +7,36 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type recordedMail struct {
|
||||
Address string
|
||||
Template string
|
||||
Data any
|
||||
}
|
||||
|
||||
type mockMailRepo struct {
|
||||
SentEmails []struct {
|
||||
Address string
|
||||
Template string
|
||||
Data any
|
||||
}
|
||||
ShouldFail bool
|
||||
SentEmails []recordedMail
|
||||
fail failSwitch
|
||||
}
|
||||
|
||||
func newMockMailRepo() *mockMailRepo {
|
||||
return &mockMailRepo{}
|
||||
}
|
||||
|
||||
func (m *mockMailRepo) Send(receiverAddress string, templateName string, data any) error {
|
||||
if m.ShouldFail {
|
||||
return errors.New("smtp error")
|
||||
if err := m.fail.trigger("Send"); err != nil {
|
||||
return err
|
||||
}
|
||||
m.SentEmails = append(m.SentEmails, struct {
|
||||
Address string
|
||||
Template string
|
||||
Data any
|
||||
}{receiverAddress, templateName, data})
|
||||
m.SentEmails = append(m.SentEmails, recordedMail{receiverAddress, templateName, data})
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ mail.Repository = &mockMailRepo{}
|
||||
|
||||
func TestSendMail(t *testing.T) {
|
||||
repo := &mockMailRepo{}
|
||||
repo := newMockMailRepo()
|
||||
uc := useCases.NewSendMailUseCase(repo)
|
||||
|
||||
t.Run("Successful Email Send", func(t *testing.T) {
|
||||
repo.ShouldFail = false
|
||||
err := uc.SendMail("user@example.com", "welcome", "User123")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
@@ -46,10 +47,10 @@ func TestSendMail(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Failed Email Send", func(t *testing.T) {
|
||||
repo.ShouldFail = true
|
||||
repo.fail.failNext("Send")
|
||||
err := uc.SendMail("fail@example.com", "reset", nil)
|
||||
if err == nil {
|
||||
t.Errorf("Expected an error but got nil")
|
||||
if !errors.Is(err, errRepositoryFailure) {
|
||||
t.Errorf("Expected repository failure, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user