-
-
Notifications
You must be signed in to change notification settings - Fork 648
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring: unit tests for background tasks (#438)
- Loading branch information
Showing
10 changed files
with
396 additions
and
62 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package noop | ||
|
||
import ( | ||
"github.com/getfider/fider/app/models" | ||
"github.com/getfider/fider/app/pkg/email" | ||
) | ||
|
||
type request struct { | ||
Tenant *models.Tenant | ||
TemplateName string | ||
Params email.Params | ||
From string | ||
To []email.Recipient | ||
} | ||
|
||
// Sender does not send emails | ||
type Sender struct { | ||
Requests []*request | ||
} | ||
|
||
// NewSender creates a new NoopSender | ||
func NewSender() *Sender { | ||
return &Sender{ | ||
Requests: make([]*request, 0), | ||
} | ||
} | ||
|
||
// Send an email | ||
func (s *Sender) Send(tenant *models.Tenant, templateName string, params email.Params, from string, to email.Recipient) error { | ||
s.Requests = append(s.Requests, &request{ | ||
Tenant: tenant, | ||
TemplateName: templateName, | ||
Params: params, | ||
From: from, | ||
To: []email.Recipient{to}, | ||
}) | ||
return nil | ||
|
||
} | ||
|
||
// BatchSend an email to multiple recipients | ||
func (s *Sender) BatchSend(tenant *models.Tenant, templateName string, params email.Params, from string, to []email.Recipient) error { | ||
if len(to) > 0 { | ||
s.Requests = append(s.Requests, &request{ | ||
Tenant: tenant, | ||
TemplateName: templateName, | ||
Params: params, | ||
From: from, | ||
To: to, | ||
}) | ||
} | ||
|
||
return nil | ||
} |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package inmemory | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/gosimple/slug" | ||
|
@@ -16,7 +15,7 @@ type IdeaStorage struct { | |
lastCommentID int | ||
ideas []*models.Idea | ||
ideasSupportedBy map[int][]int | ||
ideaSubscribers map[int][]int | ||
ideaSubscribers map[int][]*models.User | ||
ideaComments map[int][]*models.Comment | ||
tenant *models.Tenant | ||
user *models.User | ||
|
@@ -27,7 +26,7 @@ func NewIdeaStorage() *IdeaStorage { | |
return &IdeaStorage{ | ||
ideas: make([]*models.Idea, 0), | ||
ideasSupportedBy: make(map[int][]int, 0), | ||
ideaSubscribers: make(map[int][]int, 0), | ||
ideaSubscribers: make(map[int][]*models.User, 0), | ||
ideaComments: make(map[int][]*models.Comment, 0), | ||
} | ||
} | ||
|
@@ -112,6 +111,7 @@ func (s *IdeaStorage) Add(title, description string) (*models.Idea, error) { | |
} | ||
s.ideas = append(s.ideas, idea) | ||
s.ideasSupportedBy[s.user.ID] = append(s.ideasSupportedBy[s.user.ID], idea.ID) | ||
s.AddSubscriber(idea, s.user) | ||
return idea, nil | ||
} | ||
|
||
|
@@ -225,15 +225,15 @@ func (s *IdeaStorage) SupportedBy() ([]int, error) { | |
|
||
// AddSubscriber adds user to the idea list of subscribers | ||
func (s *IdeaStorage) AddSubscriber(idea *models.Idea, user *models.User) error { | ||
s.ideaSubscribers[idea.ID] = append(s.ideaSubscribers[idea.ID], user.ID) | ||
s.ideaSubscribers[idea.ID] = append(s.ideaSubscribers[idea.ID], user) | ||
return nil | ||
} | ||
|
||
// RemoveSubscriber removes user from idea list of subscribers | ||
func (s *IdeaStorage) RemoveSubscriber(idea *models.Idea, user *models.User) error { | ||
for i, id := range s.ideaSubscribers[idea.ID] { | ||
if id == user.ID { | ||
s.ideaSubscribers[idea.ID] = append(s.ideasSupportedBy[idea.ID][:i], s.ideasSupportedBy[idea.ID][i+1:]...) | ||
for i, u := range s.ideaSubscribers[idea.ID] { | ||
if u.ID == user.ID { | ||
s.ideaSubscribers[idea.ID] = append(s.ideaSubscribers[idea.ID][:i], s.ideaSubscribers[idea.ID][i+1:]...) | ||
break | ||
} | ||
} | ||
|
@@ -249,13 +249,10 @@ func (s *IdeaStorage) GetActiveSubscribers(number int, channel models.Notificati | |
subscribers, ok := s.ideaSubscribers[idea.ID] | ||
if ok { | ||
users := make([]*models.User, len(subscribers)) | ||
for i, id := range subscribers { | ||
users[i] = &models.User{ | ||
ID: id, | ||
Name: fmt.Sprintf("User %d", id), | ||
Email: fmt.Sprintf("user%[email protected]", id), | ||
} | ||
for i, user := range subscribers { | ||
users[i] = user | ||
} | ||
return users, nil | ||
} | ||
return make([]*models.User, 0), nil | ||
} |
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
Oops, something went wrong.