1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-18 10:07:02 +00:00

Move datastore mocks into correct package

- Start of work for #48
- Continues #67
This commit is contained in:
James Hillyerd
2017-12-26 15:45:18 -08:00
parent 3a4fd3f093
commit 11033a5359
3 changed files with 25 additions and 26 deletions

View File

@@ -1,4 +1,4 @@
package rest package datastore
import ( import (
"io" "io"
@@ -6,7 +6,6 @@ import (
"time" "time"
"github.com/jhillyerd/enmime" "github.com/jhillyerd/enmime"
"github.com/jhillyerd/inbucket/datastore"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
) )
@@ -15,14 +14,14 @@ type MockDataStore struct {
mock.Mock mock.Mock
} }
func (m *MockDataStore) MailboxFor(name string) (datastore.Mailbox, error) { func (m *MockDataStore) MailboxFor(name string) (Mailbox, error) {
args := m.Called(name) args := m.Called(name)
return args.Get(0).(datastore.Mailbox), args.Error(1) return args.Get(0).(Mailbox), args.Error(1)
} }
func (m *MockDataStore) AllMailboxes() ([]datastore.Mailbox, error) { func (m *MockDataStore) AllMailboxes() ([]Mailbox, error) {
args := m.Called() args := m.Called()
return args.Get(0).([]datastore.Mailbox), args.Error(1) return args.Get(0).([]Mailbox), args.Error(1)
} }
// Mock Mailbox object // Mock Mailbox object
@@ -30,14 +29,14 @@ type MockMailbox struct {
mock.Mock mock.Mock
} }
func (m *MockMailbox) GetMessages() ([]datastore.Message, error) { func (m *MockMailbox) GetMessages() ([]Message, error) {
args := m.Called() args := m.Called()
return args.Get(0).([]datastore.Message), args.Error(1) return args.Get(0).([]Message), args.Error(1)
} }
func (m *MockMailbox) GetMessage(id string) (datastore.Message, error) { func (m *MockMailbox) GetMessage(id string) (Message, error) {
args := m.Called(id) args := m.Called(id)
return args.Get(0).(datastore.Message), args.Error(1) return args.Get(0).(Message), args.Error(1)
} }
func (m *MockMailbox) Purge() error { func (m *MockMailbox) Purge() error {
@@ -45,9 +44,9 @@ func (m *MockMailbox) Purge() error {
return args.Error(0) return args.Error(0)
} }
func (m *MockMailbox) NewMessage() (datastore.Message, error) { func (m *MockMailbox) NewMessage() (Message, error) {
args := m.Called() args := m.Called()
return args.Get(0).(datastore.Message), args.Error(1) return args.Get(0).(Message), args.Error(1)
} }
func (m *MockMailbox) Name() string { func (m *MockMailbox) Name() string {

View File

@@ -31,7 +31,7 @@ const (
func TestRestMailboxList(t *testing.T) { func TestRestMailboxList(t *testing.T) {
// Setup // Setup
ds := &MockDataStore{} ds := &datastore.MockDataStore{}
logbuf := setupWebServer(ds) logbuf := setupWebServer(ds)
// Test invalid mailbox name // Test invalid mailbox name
@@ -45,7 +45,7 @@ func TestRestMailboxList(t *testing.T) {
} }
// Test empty mailbox // Test empty mailbox
emptybox := &MockMailbox{} emptybox := &datastore.MockMailbox{}
ds.On("MailboxFor", "empty").Return(emptybox, nil) ds.On("MailboxFor", "empty").Return(emptybox, nil)
emptybox.On("GetMessages").Return([]datastore.Message{}, nil) emptybox.On("GetMessages").Return([]datastore.Message{}, nil)
@@ -59,7 +59,7 @@ func TestRestMailboxList(t *testing.T) {
} }
// Test MailboxFor error // Test MailboxFor error
ds.On("MailboxFor", "error").Return(&MockMailbox{}, fmt.Errorf("Internal error")) ds.On("MailboxFor", "error").Return(&datastore.MockMailbox{}, fmt.Errorf("Internal error"))
w, err = testRestGet(baseURL + "/mailbox/error") w, err = testRestGet(baseURL + "/mailbox/error")
expectCode = 500 expectCode = 500
if err != nil { if err != nil {
@@ -77,7 +77,7 @@ func TestRestMailboxList(t *testing.T) {
} }
// Test MailboxFor error // Test MailboxFor error
error2box := &MockMailbox{} error2box := &datastore.MockMailbox{}
ds.On("MailboxFor", "error2").Return(error2box, nil) ds.On("MailboxFor", "error2").Return(error2box, nil)
error2box.On("GetMessages").Return([]datastore.Message{}, fmt.Errorf("Internal error 2")) error2box.On("GetMessages").Return([]datastore.Message{}, fmt.Errorf("Internal error 2"))
@@ -107,7 +107,7 @@ func TestRestMailboxList(t *testing.T) {
Subject: "subject 2", Subject: "subject 2",
Date: time.Date(2012, 7, 1, 10, 11, 12, 253, time.FixedZone("PDT", -700)), Date: time.Date(2012, 7, 1, 10, 11, 12, 253, time.FixedZone("PDT", -700)),
} }
goodbox := &MockMailbox{} goodbox := &datastore.MockMailbox{}
ds.On("MailboxFor", "good").Return(goodbox, nil) ds.On("MailboxFor", "good").Return(goodbox, nil)
msg1 := data1.MockMessage() msg1 := data1.MockMessage()
msg2 := data2.MockMessage() msg2 := data2.MockMessage()
@@ -155,7 +155,7 @@ func TestRestMailboxList(t *testing.T) {
func TestRestMessage(t *testing.T) { func TestRestMessage(t *testing.T) {
// Setup // Setup
ds := &MockDataStore{} ds := &datastore.MockDataStore{}
logbuf := setupWebServer(ds) logbuf := setupWebServer(ds)
// Test invalid mailbox name // Test invalid mailbox name
@@ -169,9 +169,9 @@ func TestRestMessage(t *testing.T) {
} }
// Test requesting a message that does not exist // Test requesting a message that does not exist
emptybox := &MockMailbox{} emptybox := &datastore.MockMailbox{}
ds.On("MailboxFor", "empty").Return(emptybox, nil) ds.On("MailboxFor", "empty").Return(emptybox, nil)
emptybox.On("GetMessage", "0001").Return(&MockMessage{}, datastore.ErrNotExist) emptybox.On("GetMessage", "0001").Return(&datastore.MockMessage{}, datastore.ErrNotExist)
w, err = testRestGet(baseURL + "/mailbox/empty/0001") w, err = testRestGet(baseURL + "/mailbox/empty/0001")
expectCode = 404 expectCode = 404
@@ -183,7 +183,7 @@ func TestRestMessage(t *testing.T) {
} }
// Test MailboxFor error // Test MailboxFor error
ds.On("MailboxFor", "error").Return(&MockMailbox{}, fmt.Errorf("Internal error")) ds.On("MailboxFor", "error").Return(&datastore.MockMailbox{}, fmt.Errorf("Internal error"))
w, err = testRestGet(baseURL + "/mailbox/error/0001") w, err = testRestGet(baseURL + "/mailbox/error/0001")
expectCode = 500 expectCode = 500
if err != nil { if err != nil {
@@ -201,9 +201,9 @@ func TestRestMessage(t *testing.T) {
} }
// Test GetMessage error // Test GetMessage error
error2box := &MockMailbox{} error2box := &datastore.MockMailbox{}
ds.On("MailboxFor", "error2").Return(error2box, nil) ds.On("MailboxFor", "error2").Return(error2box, nil)
error2box.On("GetMessage", "0001").Return(&MockMessage{}, fmt.Errorf("Internal error 2")) error2box.On("GetMessage", "0001").Return(&datastore.MockMessage{}, fmt.Errorf("Internal error 2"))
w, err = testRestGet(baseURL + "/mailbox/error2/0001") w, err = testRestGet(baseURL + "/mailbox/error2/0001")
expectCode = 500 expectCode = 500
@@ -228,7 +228,7 @@ func TestRestMessage(t *testing.T) {
Text: "This is some text", Text: "This is some text",
HTML: "This is some HTML", HTML: "This is some HTML",
} }
goodbox := &MockMailbox{} goodbox := &datastore.MockMailbox{}
ds.On("MailboxFor", "good").Return(goodbox, nil) ds.On("MailboxFor", "good").Return(goodbox, nil)
msg1 := data1.MockMessage() msg1 := data1.MockMessage()
goodbox.On("GetMessage", "0001").Return(msg1, nil) goodbox.On("GetMessage", "0001").Return(msg1, nil)

View File

@@ -25,8 +25,8 @@ type InputMessageData struct {
HTML, Text string HTML, Text string
} }
func (d *InputMessageData) MockMessage() *MockMessage { func (d *InputMessageData) MockMessage() *datastore.MockMessage {
msg := &MockMessage{} msg := &datastore.MockMessage{}
msg.On("ID").Return(d.ID) msg.On("ID").Return(d.ID)
msg.On("From").Return(d.From) msg.On("From").Return(d.From)
msg.On("To").Return(d.To) msg.On("To").Return(d.To)