From 8a361fff3c2a5aa776083a8e4538c67558fdb148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Mon, 25 Mar 2019 22:40:19 +0100 Subject: [PATCH] added test to remove unneeded updates if a file is not found on the server --- internal/app/images.go | 10 +++++++--- internal/app/images_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/internal/app/images.go b/internal/app/images.go index 3e0c892..99f19d9 100644 --- a/internal/app/images.go +++ b/internal/app/images.go @@ -275,9 +275,13 @@ func updatePiwigoIdIfAlreadyUploaded(provider ImageMetadataProvider, piwigoCtx p } for md5sum, piwigoId := range missingResults { logrus.Debugf("Setting piwigo id of %s to %d", md5sum, piwigoId) - err = provider.SavePiwigoIdAndUpdateUploadFlag(md5sum, piwigoId) - if err != nil { - logrus.Warnf("Could not save piwigo id %d for file %s", piwigoId, md5sum) + if piwigoId > 0 { + err = provider.SavePiwigoIdAndUpdateUploadFlag(md5sum, piwigoId) + if err != nil { + logrus.Warnf("Could not save piwigo id %d for file %s", piwigoId, md5sum) + } + } else { + logrus.Tracef("Image %s not found on server", md5sum) } } diff --git a/internal/app/images_test.go b/internal/app/images_test.go index dfed35c..b735906 100644 --- a/internal/app/images_test.go +++ b/internal/app/images_test.go @@ -407,6 +407,33 @@ func Test_updatePiwigoIdIfAlreadyUploaded_with_image_to_check(t *testing.T) { } } +func Test_updatePiwigoIdIfAlreadyUploaded_with_image_to_check_missing_on_server(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(0) + + piwigoResponose := make(map[string]int) + + piwigomock := NewMockPiwigoImageApi(mockCtrl) + piwigomock.EXPECT().ImagesExistOnPiwigo(gomock.Any()).Times(1).Return(piwigoResponose, nil) + + err := updatePiwigoIdIfAlreadyUploaded(dbmock, piwigomock) + if err != nil { + t.Error(err) + } +} + func Test_uploadImages_saves_new_id_to_db(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish()