From 8efd309a9c4b7f27e1d0d52de758555cee9ceac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Sat, 23 Mar 2019 21:35:16 +0100 Subject: [PATCH] added some tests to the image code --- internal/app/images_test.go | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/internal/app/images_test.go b/internal/app/images_test.go index 49efda0..1b4d552 100644 --- a/internal/app/images_test.go +++ b/internal/app/images_test.go @@ -345,6 +345,68 @@ func Test_updatePiwigoIdIfAlreadyUploaded_with_image_to_check(t *testing.T) { } } +func Test_uploadImages_saves_new_id_to_db(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + + img := ImageMetaData{ + ImageId: 1, + PiwigoId: 0, + FullImagePath: "/nonexisting/file.jpg", + UploadRequired: true, + Md5Sum: "1234", + CategoryId: 2, + } + images := []ImageMetaData{img} + + imgToSave := img + imgToSave.PiwigoId = 5 + imgToSave.UploadRequired = false + + dbmock := NewMockImageMetadataProvider(mockCtrl) + dbmock.EXPECT().ImageMetadataToUpload().Times(1).Return(images, nil) + dbmock.EXPECT().SaveImageMetadata(imgToSave).Times(1) + + piwigomock := NewMockPiwigoImageApi(mockCtrl) + piwigomock.EXPECT().UploadImage(0, "/nonexisting/file.jpg", "1234", 2).Times(1).Return(5, nil) + + err := uploadImages(piwigomock, dbmock) + if err != nil { + t.Error(err) + } +} + +func Test_uploadImages_saves_same_id_to_db(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + + img := ImageMetaData{ + ImageId: 1, + PiwigoId: 5, + FullImagePath: "/nonexisting/file.jpg", + UploadRequired: true, + Md5Sum: "1234", + CategoryId: 2, + } + images := []ImageMetaData{img} + + imgToSave := img + imgToSave.UploadRequired = false + + dbmock := NewMockImageMetadataProvider(mockCtrl) + dbmock.EXPECT().ImageMetadataToUpload().Times(1).Return(images, nil) + dbmock.EXPECT().SaveImageMetadata(imgToSave).Times(1) + + piwigomock := NewMockPiwigoImageApi(mockCtrl) + piwigomock.EXPECT().UploadImage(5, "/nonexisting/file.jpg", "1234", 2).Times(1).Return(5, nil) + + err := uploadImages(piwigomock, dbmock) + if err != nil { + t.Error(err) + } +} + + // test metadata store to store save the metadat and simulate the database type testStore struct { savedMetadata map[string]ImageMetaData