1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 09:37:02 +00:00

storage: eliminate mocks, closes #80

This commit is contained in:
James Hillyerd
2018-03-14 21:05:59 -07:00
parent 2cc0da3093
commit 519779b7ba
3 changed files with 16 additions and 193 deletions

View File

@@ -9,12 +9,10 @@ import (
"net/mail" "net/mail"
"time" "time"
"github.com/jhillyerd/enmime"
"github.com/jhillyerd/inbucket/pkg/config" "github.com/jhillyerd/inbucket/pkg/config"
"github.com/jhillyerd/inbucket/pkg/message" "github.com/jhillyerd/inbucket/pkg/message"
"github.com/jhillyerd/inbucket/pkg/msghub" "github.com/jhillyerd/inbucket/pkg/msghub"
"github.com/jhillyerd/inbucket/pkg/server/web" "github.com/jhillyerd/inbucket/pkg/server/web"
"github.com/jhillyerd/inbucket/pkg/storage"
) )
type InputMessageData struct { type InputMessageData struct {
@@ -26,31 +24,6 @@ type InputMessageData struct {
HTML, Text string HTML, Text string
} }
func (d *InputMessageData) MockMessage() *storage.MockMessage {
from, _ := mail.ParseAddress(d.From)
to := make([]*mail.Address, len(d.To))
for i, a := range d.To {
to[i], _ = mail.ParseAddress(a)
}
msg := &storage.MockMessage{}
msg.On("ID").Return(d.ID)
msg.On("From").Return(from)
msg.On("To").Return(to)
msg.On("Subject").Return(d.Subject)
msg.On("Date").Return(d.Date)
msg.On("Size").Return(d.Size)
gomsg := &mail.Message{
Header: d.Header,
}
msg.On("ReadHeader").Return(gomsg, nil)
body := &enmime.Envelope{
Text: d.Text,
HTML: d.HTML,
}
msg.On("ReadBody").Return(body, nil)
return msg
}
// isJSONStringEqual is a utility function to return a nicely formatted message when // isJSONStringEqual is a utility function to return a nicely formatted message when
// comparing a string to a value received from a JSON map. // comparing a string to a value received from a JSON map.
func isJSONStringEqual(key, expected string, received interface{}) (message string, ok bool) { func isJSONStringEqual(key, expected string, received interface{}) (message string, ok bool) {

View File

@@ -6,6 +6,7 @@ import (
"time" "time"
"github.com/jhillyerd/inbucket/pkg/config" "github.com/jhillyerd/inbucket/pkg/config"
"github.com/jhillyerd/inbucket/pkg/message"
"github.com/jhillyerd/inbucket/pkg/storage" "github.com/jhillyerd/inbucket/pkg/storage"
"github.com/jhillyerd/inbucket/pkg/test" "github.com/jhillyerd/inbucket/pkg/test"
) )
@@ -13,12 +14,12 @@ import (
func TestDoRetentionScan(t *testing.T) { func TestDoRetentionScan(t *testing.T) {
ds := test.NewStore() ds := test.NewStore()
// Mockup some different aged messages (num is in hours) // Mockup some different aged messages (num is in hours)
new1 := mockMessage("mb1", 0) new1 := stubMessage("mb1", 0)
new2 := mockMessage("mb2", 1) new2 := stubMessage("mb2", 1)
new3 := mockMessage("mb3", 2) new3 := stubMessage("mb3", 2)
old1 := mockMessage("mb1", 4) old1 := stubMessage("mb1", 4)
old2 := mockMessage("mb1", 12) old2 := stubMessage("mb1", 12)
old3 := mockMessage("mb2", 24) old3 := stubMessage("mb2", 24)
ds.AddMessage(new1) ds.AddMessage(new1)
ds.AddMessage(old1) ds.AddMessage(old1)
ds.AddMessage(old2) ds.AddMessage(old2)
@@ -49,12 +50,13 @@ func TestDoRetentionScan(t *testing.T) {
} }
} }
// Make a MockMessage of a specific age // stubMessage creates a message stub of a specific age
func mockMessage(mailbox string, ageHours int) *storage.MockMessage { func stubMessage(mailbox string, ageHours int) storage.StoreMessage {
msg := &storage.MockMessage{} return &message.Delivery{
msg.On("Mailbox").Return(mailbox) Meta: message.Metadata{
msg.On("ID").Return(fmt.Sprintf("MSG[age=%vh]", ageHours)) Mailbox: mailbox,
msg.On("Date").Return(time.Now().Add(time.Duration(ageHours*-1) * time.Hour)) ID: fmt.Sprintf("MSG[age=%vh]", ageHours),
msg.On("Delete").Return(nil) Date: time.Now().Add(time.Duration(ageHours*-1) * time.Hour),
return msg },
}
} }

View File

@@ -1,152 +0,0 @@
package storage
import (
"io"
"net/mail"
"sync"
"time"
"github.com/jhillyerd/enmime"
"github.com/stretchr/testify/mock"
)
// MockDataStore is a shared mock for unit testing
type MockDataStore struct {
mock.Mock
}
// AddMessage mock function
func (m *MockDataStore) AddMessage(message StoreMessage) (string, error) {
args := m.Called(message)
return args.String(0), args.Error(1)
}
// GetMessage mock function
func (m *MockDataStore) GetMessage(name, id string) (StoreMessage, error) {
args := m.Called(name, id)
return args.Get(0).(StoreMessage), args.Error(1)
}
// GetMessages mock function
func (m *MockDataStore) GetMessages(name string) ([]StoreMessage, error) {
args := m.Called(name)
return args.Get(0).([]StoreMessage), args.Error(1)
}
// RemoveMessage mock function
func (m *MockDataStore) RemoveMessage(name, id string) error {
args := m.Called(name, id)
return args.Error(0)
}
// PurgeMessages mock function
func (m *MockDataStore) PurgeMessages(name string) error {
args := m.Called(name)
return args.Error(0)
}
// LockFor mock function returns a new RWMutex, never errors.
func (m *MockDataStore) LockFor(name string) (*sync.RWMutex, error) {
return &sync.RWMutex{}, nil
}
// NewMessage temporary for #69
func (m *MockDataStore) NewMessage(mailbox string) (StoreMessage, error) {
args := m.Called(mailbox)
return args.Get(0).(StoreMessage), args.Error(1)
}
// VisitMailboxes accepts a function that will be called with the messages in each mailbox while it
// continues to return true.
func (m *MockDataStore) VisitMailboxes(f func([]StoreMessage) (cont bool)) error {
return nil
}
// MockMessage is a shared mock for unit testing
type MockMessage struct {
mock.Mock
}
// Mailbox mock function
func (m *MockMessage) Mailbox() string {
args := m.Called()
return args.String(0)
}
// ID mock function
func (m *MockMessage) ID() string {
args := m.Called()
return args.String(0)
}
// From mock function
func (m *MockMessage) From() *mail.Address {
args := m.Called()
return args.Get(0).(*mail.Address)
}
// To mock function
func (m *MockMessage) To() []*mail.Address {
args := m.Called()
return args.Get(0).([]*mail.Address)
}
// Date mock function
func (m *MockMessage) Date() time.Time {
args := m.Called()
return args.Get(0).(time.Time)
}
// Subject mock function
func (m *MockMessage) Subject() string {
args := m.Called()
return args.String(0)
}
// ReadHeader mock function
func (m *MockMessage) ReadHeader() (msg *mail.Message, err error) {
args := m.Called()
return args.Get(0).(*mail.Message), args.Error(1)
}
// ReadBody mock function
func (m *MockMessage) ReadBody() (body *enmime.Envelope, err error) {
args := m.Called()
return args.Get(0).(*enmime.Envelope), args.Error(1)
}
// ReadRaw mock function
func (m *MockMessage) ReadRaw() (raw *string, err error) {
args := m.Called()
return args.Get(0).(*string), args.Error(1)
}
// RawReader mock function
func (m *MockMessage) RawReader() (reader io.ReadCloser, err error) {
args := m.Called()
return args.Get(0).(io.ReadCloser), args.Error(1)
}
// Size mock function
func (m *MockMessage) Size() int64 {
args := m.Called()
return int64(args.Int(0))
}
// Append mock function
func (m *MockMessage) Append(data []byte) error {
// []byte arg seems to mess up testify/mock
return nil
}
// Close mock function
func (m *MockMessage) Close() error {
args := m.Called()
return args.Error(0)
}
// String mock function
func (m *MockMessage) String() string {
args := m.Called()
return args.String(0)
}