Add tests for paster
parent
af658da26b
commit
5c7d1c7da7
1
go.mod
1
go.mod
|
@ -18,6 +18,7 @@ require (
|
|||
github.com/pressly/goose v2.4.5+incompatible // indirect
|
||||
github.com/rogpeppe/go-internal v1.2.2 // indirect
|
||||
github.com/sirupsen/logrus v1.3.0
|
||||
github.com/stretchr/testify v1.3.0
|
||||
github.com/ziutek/mymysql v1.5.4 // indirect
|
||||
golang.org/x/crypto v0.0.0-20190222235706-ffb98f73852f // indirect
|
||||
golang.org/x/sys v0.0.0-20190222171317-cd391775e71e // indirect
|
||||
|
|
1
go.sum
1
go.sum
|
@ -419,6 +419,7 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
|
|||
github.com/spf13/viper v1.2.1/go.mod h1:P4AexN0a+C9tGAnUFNwDMYYZv3pjFuvmeiMyKRaNVlI=
|
||||
github.com/spf13/viper v1.3.1/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
|
|
|
@ -0,0 +1,93 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/ollien/updown/repository"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type MockFile struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (file *MockFile) Read(buffer []byte) (int, error) {
|
||||
args := file.Called(buffer)
|
||||
|
||||
return args.Int(0), args.Error(1)
|
||||
}
|
||||
|
||||
func (file *MockFile) Write(buffer []byte) (int, error) {
|
||||
args := file.Called(buffer)
|
||||
|
||||
return args.Int(0), args.Error(1)
|
||||
}
|
||||
|
||||
func (file *MockFile) Seek(offset int64, whence int) (int64, error) {
|
||||
args := file.Called(offset, whence)
|
||||
|
||||
return args.Get(0).(int64), args.Error(1)
|
||||
}
|
||||
|
||||
func (file *MockFile) Close() error {
|
||||
args := file.Called()
|
||||
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func setupPaster() Paster {
|
||||
paste := repository.Paste{
|
||||
ID: 23,
|
||||
Handle: uuid.Nil,
|
||||
Title: "Test post",
|
||||
}
|
||||
|
||||
return Paster{
|
||||
Paste: paste,
|
||||
file: &MockFile{},
|
||||
}
|
||||
}
|
||||
|
||||
func TestRead(t *testing.T) {
|
||||
paster := setupPaster()
|
||||
mockFile := paster.file.(*MockFile)
|
||||
|
||||
buffer := make([]byte, 5)
|
||||
mockFile.On("Read", buffer).Return(len(buffer), nil).Once()
|
||||
// Ensure we seek to the start of the file
|
||||
mockFile.On("Seek", int64(0), 0).Return(int64(0), nil).Once()
|
||||
mockFile.AssertNotCalled(t, "Close")
|
||||
mockFile.AssertNotCalled(t, "Write")
|
||||
|
||||
paster.Read(buffer)
|
||||
mockFile.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestWrite(t *testing.T) {
|
||||
paster := setupPaster()
|
||||
mockFile := paster.file.(*MockFile)
|
||||
|
||||
testPaste := []byte("hello world")
|
||||
mockFile.On("Write", testPaste).Return(len(testPaste), nil).Once()
|
||||
// Ensure we seek to the start of the file
|
||||
mockFile.On("Seek", int64(0), 0).Return(int64(0), nil).Once()
|
||||
mockFile.AssertNotCalled(t, "Close")
|
||||
|
||||
paster.Write(testPaste)
|
||||
mockFile.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestClose(t *testing.T) {
|
||||
paster := setupPaster()
|
||||
mockFile := paster.file.(*MockFile)
|
||||
|
||||
mockFile.On("Close").Return(nil).Once()
|
||||
mockFile.AssertNotCalled(t, "Close")
|
||||
mockFile.AssertNotCalled(t, "Seek")
|
||||
mockFile.AssertNotCalled(t, "Read")
|
||||
// If we write to the file on close, that isn't the worst thing in the world and we won't enforce it being a problem
|
||||
|
||||
paster.Close()
|
||||
mockFile.AssertExpectations(t)
|
||||
}
|
Loading…
Reference in New Issue