Add test for CreatePastes

master
Nick Krichevsky 2019-03-31 09:53:41 -04:00
parent f91320814d
commit b477aed8a1
1 changed files with 64 additions and 0 deletions

View File

@ -1,18 +1,24 @@
package paste
import (
"fmt"
"testing"
"github.com/google/uuid"
"github.com/ollien/updown/repository"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
type MockPasteRepository struct {
mock.Mock
}
type MockClumpRepository struct {
mock.Mock
}
func (repo *MockPasteRepository) GetPaste(handle uuid.UUID) (repository.Paste, error) {
args := repo.Called(handle)
@ -31,10 +37,25 @@ func (repo *MockPasteRepository) UpdatePaste(paste repository.Paste) error {
return args.Error(0)
}
func (repo *MockClumpRepository) GetClump(handle uuid.UUID) (repository.Clump, error) {
args := repo.Called(handle)
return args.Get(0).(repository.Clump), args.Error(1)
}
func (repo *MockClumpRepository) PutClump(title string, pastes ...repository.Paste) (repository.Clump, error) {
args := repo.Called(title, pastes)
return args.Get(0).(repository.Clump), args.Error(1)
}
func setupService() *Service {
mockPasteRepo := &MockPasteRepository{}
mockClumpRepo := &MockClumpRepository{}
return &Service{
pasteRepo: mockPasteRepo,
clumpRepo: mockClumpRepo,
pasteDir: "/tmp/pastes/",
}
}
@ -73,3 +94,46 @@ func TestGetPaste(t *testing.T) {
assert.Equal(t, testPaste, paster.Paste)
assert.Equal(t, service.pasteDir, paster.pasteDir)
}
func TestCreatePastes(t *testing.T) {
service := setupService()
mockPasteRepo := service.pasteRepo.(*MockPasteRepository)
mockClumpRepo := service.clumpRepo.(*MockClumpRepository)
pastes := make([]repository.Paste, 5)
pasteNames := make([]string, 5)
for i := range pastes {
pasteName := fmt.Sprintf("Awesome Paste #%d.txt", i)
pasteNames[i] = pasteName
pastes[i] = repository.Paste{
ID: 23 + i,
Filename: pasteName,
Handle: uuid.New(),
}
}
putPasteCall := mockPasteRepo.On("PutPaste", mock.Anything).Times(5)
pasteCursor := 0
putPasteCall.Run(func(args mock.Arguments) {
putPasteCall.Return(pastes[pasteCursor], nil)
pasteCursor++
}).Times(5)
clumpTitle := "Clumpy McClumpFace"
mockClumpRepo.On("PutClump", clumpTitle, pastes).Return(repository.Clump{
ID: 1,
Title: clumpTitle,
Handle: uuid.New(),
Pastes: pastes,
}, nil).Once()
clumper, err := service.CreatePastes(clumpTitle, pasteNames)
assert.Nil(t, err)
assert.Equal(t, clumpTitle, clumper.Clump.Title)
// The lengths must be the same, otherwise we will panic when looping over pastes
require.Len(t, clumper.Pasters, len(pastes))
for i := range pastes {
assert.Equal(t, pastes[i], clumper.Pasters[i].Paste)
}
}