learned about mocking and made mocks work

added new tests for the images part of the app
added go generate info to help generate mocks for tests
This commit is contained in:
Philipp Häfelfinger 2019-03-22 00:14:34 +01:00
parent 4fdcf5a090
commit c9b6a7e6db
6 changed files with 185 additions and 51 deletions

View File

@ -1,6 +1,6 @@
package app
//go:generate mockgen -destination=../pkg/mocks/mock_app_datastore.go -package=mocks git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/app ImageMetadataProvider
//go:generate mockgen -destination=./datastore_mock_test.go -package=app git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/app ImageMetadataProvider
import (
"database/sql"

View File

@ -1,11 +1,10 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/app (interfaces: ImageMetadataProvider)
// Package mocks is a generated GoMock package.
package mocks
// Package app is a generated GoMock package.
package app
import (
app "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/app"
gomock "github.com/golang/mock/gomock"
reflect "reflect"
)
@ -34,10 +33,10 @@ func (m *MockImageMetadataProvider) EXPECT() *MockImageMetadataProviderMockRecor
}
// ImageMetadata mocks base method
func (m *MockImageMetadataProvider) ImageMetadata(arg0 string) (app.ImageMetaData, error) {
func (m *MockImageMetadataProvider) ImageMetadata(arg0 string) (ImageMetaData, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ImageMetadata", arg0)
ret0, _ := ret[0].(app.ImageMetaData)
ret0, _ := ret[0].(ImageMetaData)
ret1, _ := ret[1].(error)
return ret0, ret1
}
@ -49,10 +48,10 @@ func (mr *MockImageMetadataProviderMockRecorder) ImageMetadata(arg0 interface{})
}
// ImageMetadataToUpload mocks base method
func (m *MockImageMetadataProvider) ImageMetadataToUpload() ([]app.ImageMetaData, error) {
func (m *MockImageMetadataProvider) ImageMetadataToUpload() ([]ImageMetaData, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ImageMetadataToUpload")
ret0, _ := ret[0].([]app.ImageMetaData)
ret0, _ := ret[0].([]ImageMetaData)
ret1, _ := ret[1].(error)
return ret0, ret1
}
@ -64,7 +63,7 @@ func (mr *MockImageMetadataProviderMockRecorder) ImageMetadataToUpload() *gomock
}
// SaveImageMetadata mocks base method
func (m *MockImageMetadataProvider) SaveImageMetadata(arg0 app.ImageMetaData) error {
func (m *MockImageMetadataProvider) SaveImageMetadata(arg0 ImageMetaData) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "SaveImageMetadata", arg0)
ret0, _ := ret[0].(error)

View File

@ -184,6 +184,11 @@ func updatePiwigoIdIfAlreadyUploaded(provider ImageMetadataProvider, piwigoCtx p
}
}
if len(files) == 0 {
logrus.Info("There are no images without piwigo id to check for modification on the server.")
return nil
}
missingResults, err := piwigoCtx.ImagesExistOnPiwigo(files)
if err != nil {
return err
@ -195,5 +200,6 @@ func updatePiwigoIdIfAlreadyUploaded(provider ImageMetadataProvider, piwigoCtx p
logrus.Warnf("Could not save piwigo id %d for file %s", piwigoId, md5sum)
}
}
return nil
}

View File

@ -1,14 +1,17 @@
package app
//go:generate mockgen -destination=./piwigo_mock_test.go -package=app git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo PiwigoApi,PiwigoCategoryApi,PiwigoImageApi
import (
"errors"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure"
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
"github.com/golang/mock/gomock"
"testing"
"time"
)
func TestSynchronizeLocalImageMetadataShouldDoNothingIfEmpty(t *testing.T) {
func Test_synchronize_local_image_metadata_should_fo_nothing_if_empty(t *testing.T) {
categories := make(map[string]*piwigo.PiwigoCategory)
categories["2019/shooting1"] = &piwigo.PiwigoCategory{Id: 1}
@ -25,7 +28,7 @@ func TestSynchronizeLocalImageMetadataShouldDoNothingIfEmpty(t *testing.T) {
}
}
func TestSynchronizeLocalImageMetadataShouldAddNewMetadata(t *testing.T) {
func Test_synchronize_local_image_metadata_should_add_new_metadata(t *testing.T) {
categories := make(map[string]*piwigo.PiwigoCategory)
categories["2019/shooting1"] = &piwigo.PiwigoCategory{Id: 1}
@ -70,7 +73,7 @@ func TestSynchronizeLocalImageMetadataShouldAddNewMetadata(t *testing.T) {
}
}
func TestSynchronizeLocalImageMetadataShouldMarkChangedEntriesAsUploads(t *testing.T) {
func Test_synchronize_local_image_metadata_should_mark_changed_entries_as_uploads(t *testing.T) {
categories := make(map[string]*piwigo.PiwigoCategory)
categories["2019/shooting1"] = &piwigo.PiwigoCategory{Id: 1}
@ -113,7 +116,7 @@ func TestSynchronizeLocalImageMetadataShouldMarkChangedEntriesAsUploads(t *testi
}
}
func TestSynchronizeLocalImageMetadataShouldNotMarkUnchangedFilesToUpload(t *testing.T) {
func Test_synchronize_local_image_metadata_should_not_mark_unchanged_files_to_upload(t *testing.T) {
db := NewtestStore()
categories := make(map[string]*piwigo.PiwigoCategory)
@ -153,7 +156,7 @@ func TestSynchronizeLocalImageMetadataShouldNotMarkUnchangedFilesToUpload(t *tes
}
}
func TestSynchronizeLocalImageMetadataShouldNotProcessDirectories(t *testing.T) {
func Test_synchronize_local_image_metadata_should_not_process_directories(t *testing.T) {
categories := make(map[string]*piwigo.PiwigoCategory)
categories["2019/shooting1"] = &piwigo.PiwigoCategory{Id: 1}
@ -180,22 +183,166 @@ func TestSynchronizeLocalImageMetadataShouldNotProcessDirectories(t *testing.T)
}
}
func TestSynchronizePiwigoMetadata(t *testing.T) {
db := NewtestStore()
db.savedMetadata["2019/shooting1/abc.jpg"] = ImageMetaData{
Md5Sum: "2019/shooting1/abc.jpg",
FullImagePath: "2019/shooting1/abc.jpg",
UploadRequired: false,
LastChange: time.Date(2019, 01, 01, 00, 0, 0, 0, time.UTC),
Filename: "abc.jpg",
}
func Test_checkPiwigoForChangedImages_none_with_piwigoId(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
// execute the sync metadata based on the file system results
//err := synchronizeLocalImageMetadata( db)
//if err != nil {
// t.Error(err)
//}
t.Skip("Not yet implemented!")
img := ImageMetaData{ImageId: 1, UploadRequired: true}
images := []ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
piwigomock := NewMockPiwigoImageApi(mockCtrl)
piwigomock.EXPECT().ImagesExistOnPiwigo(gomock.Any()).Times(0)
piwigomock.EXPECT().ImageCheckFile(gomock.Any(), gomock.Any()).Times(0)
err := checkPiwigoForChangedImages(dbmock, piwigomock)
if err != nil {
t.Error(err)
}
}
func Test_checkPiwigoForChangedImages_with_empty_list(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
images := []ImageMetaData{}
dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
piwigomock := NewMockPiwigoImageApi(mockCtrl)
piwigomock.EXPECT().ImagesExistOnPiwigo(gomock.Any()).Times(0)
piwigomock.EXPECT().ImageCheckFile(gomock.Any(), gomock.Any()).Times(0)
err := checkPiwigoForChangedImages(dbmock, piwigomock)
if err != nil {
t.Error(err)
}
}
func Test_checkPiwigoForChangedImages_should_call_piwigo_set_uploadRequired_to_false(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
img := ImageMetaData{
ImageId: 1,
PiwigoId: 1,
UploadRequired: true,
Md5Sum: "1234",
}
images := []ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
imgExpected := img
imgExpected.UploadRequired = false
dbmock.EXPECT().SaveImageMetadata(imgExpected).Times(1)
piwigomock := NewMockPiwigoImageApi(mockCtrl)
piwigomock.EXPECT().ImageCheckFile(1, "1234").Return(piwigo.ImageStateUptodate, nil)
err := checkPiwigoForChangedImages(dbmock, piwigomock)
if err != nil {
t.Error(err)
}
}
func Test_checkPiwigoForChangedImages_return_image_differs(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
img := ImageMetaData{
ImageId: 1,
PiwigoId: 1,
UploadRequired: true,
Md5Sum: "1234",
}
images := []ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
dbmock.EXPECT().SaveImageMetadata(gomock.Any()).Times(0)
piwigomock := NewMockPiwigoImageApi(mockCtrl)
piwigomock.EXPECT().ImageCheckFile(1, "1234").Return(piwigo.ImageStateDifferent, nil)
err := checkPiwigoForChangedImages(dbmock, piwigomock)
if err != nil {
t.Error(err)
}
}
func Test_updatePiwigoIdIfAlreadyUploaded_without_images_to_upload(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
images := []ImageMetaData{}
dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
dbmock.EXPECT().SavePiwigoIdAndUpdateUploadFlag(gomock.Any(), gomock.Any()).Times(0)
piwigomock := NewMockPiwigoImageApi(mockCtrl)
piwigomock.EXPECT().ImagesExistOnPiwigo(gomock.Any()).Times(0)
err := updatePiwigoIdIfAlreadyUploaded(dbmock, piwigomock)
if err != nil {
t.Error(err)
}
}
func Test_updatePiwigoIdIfAlreadyUploaded_without_image_to_check(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
img := ImageMetaData{
ImageId: 1,
PiwigoId: 1,
UploadRequired: true,
Md5Sum: "1234",
}
images := []ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
dbmock.EXPECT().SavePiwigoIdAndUpdateUploadFlag(gomock.Any(), gomock.Any()).Times(0)
piwigomock := NewMockPiwigoImageApi(mockCtrl)
piwigomock.EXPECT().ImagesExistOnPiwigo(gomock.Any()).Times(0)
err := updatePiwigoIdIfAlreadyUploaded(dbmock, piwigomock)
if err != nil {
t.Error(err)
}
}
func Test_updatePiwigoIdIfAlreadyUploaded_with_image_to_check(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
img := ImageMetaData{
ImageId: 1,
PiwigoId: 0,
UploadRequired: true,
Md5Sum: "1234",
}
images := []ImageMetaData{img}
dbmock := NewMockImageMetadataProvider(mockCtrl)
dbmock.EXPECT().ImageMetadataToUpload().Return(images, nil)
dbmock.EXPECT().SavePiwigoIdAndUpdateUploadFlag("1234", 1).Times(1)
piwigoResponose := make(map[string]int)
piwigoResponose["1234"] = 1
piwigomock := NewMockPiwigoImageApi(mockCtrl)
piwigomock.EXPECT().ImagesExistOnPiwigo(gomock.Any()).Times(1).Return(piwigoResponose, nil)
err := updatePiwigoIdIfAlreadyUploaded(dbmock, piwigomock)
if err != nil {
t.Error(err)
}
}
// test metadata store to store save the metadat and simulate the database

View File

@ -1,8 +1,8 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo (interfaces: PiwigoApi,PiwigoCategoryApi,PiwigoImageApi)
// Package mocks is a generated GoMock package.
package mocks
// Package app is a generated GoMock package.
package app
import (
piwigo "git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo"
@ -33,21 +33,6 @@ func (m *MockPiwigoApi) EXPECT() *MockPiwigoApiMockRecorder {
return m.recorder
}
// GetStatus mocks base method
func (m *MockPiwigoApi) GetStatus() (*piwigo.getStatusResponse, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetStatus")
ret0, _ := ret[0].(*piwigo.getStatusResponse)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetStatus indicates an expected call of GetStatus
func (mr *MockPiwigoApiMockRecorder) GetStatus() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetStatus", reflect.TypeOf((*MockPiwigoApi)(nil).GetStatus))
}
// Initialize mocks base method
func (m *MockPiwigoApi) Initialize(arg0, arg1, arg2 string, arg3 int) error {
m.ctrl.T.Helper()

View File

@ -1,7 +1,5 @@
package piwigo
//go:generate mockgen -destination=../mocks/mock_piwigo_context.go -package=mocks git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo PiwigoApi,PiwigoCategoryApi,PiwigoImageApi
import (
"encoding/json"
"errors"
@ -19,7 +17,6 @@ type PiwigoApi interface {
Initialize(baseUrl string, username string, password string, chunkSizeInKB int) error
Login() error
Logout() error
GetStatus() (*getStatusResponse, error)
}
type PiwigoCategoryApi interface {
@ -104,7 +101,7 @@ func (context *PiwigoContext) Logout() error {
return nil
}
func (context *PiwigoContext) GetStatus() (*getStatusResponse, error) {
func (context *PiwigoContext) getStatus() (*getStatusResponse, error) {
logrus.Debugln("Getting current login state...")
formData := url.Values{}
@ -255,7 +252,7 @@ func (context *PiwigoContext) initializeCookieJarIfRequired() {
}
func (context *PiwigoContext) initializeUploadChunkSize() error {
userStatus, err := context.GetStatus()
userStatus, err := context.getStatus()
if err != nil {
return err
}