From 560f5057ac2bf88728891c6ea6a345a063c81233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Sat, 23 Mar 2019 23:21:35 +0100 Subject: [PATCH] added ImageMetadataToDelete to the datastore added test if it selectes the records to delete added todo to refactor some image tests to use the generated mocks --- internal/app/datastore.go | 30 ++++++++++++++++++++++++++++ internal/app/datastore_mock_test.go | 15 ++++++++++++++ internal/app/datastore_test.go | 31 +++++++++++++++++++++++++++++ internal/app/images_test.go | 5 +++++ 4 files changed, 81 insertions(+) diff --git a/internal/app/datastore.go b/internal/app/datastore.go index 4520788..adcb4ff 100644 --- a/internal/app/datastore.go +++ b/internal/app/datastore.go @@ -38,6 +38,7 @@ func (img *ImageMetaData) String() string { type ImageMetadataProvider interface { ImageMetadata(fullImagePath string) (ImageMetaData, error) ImageMetadataToUpload() ([]ImageMetaData, error) + ImageMetadataToDelete() ([]ImageMetaData, error) SaveImageMetadata(m ImageMetaData) error SavePiwigoIdAndUpdateUploadFlag(md5Sum string, piwigoId int) error } @@ -98,6 +99,35 @@ func (d *localDataStore) ImageMetadata(fullImagePath string) (ImageMetaData, err return img, err } +func (d *localDataStore) ImageMetadataToDelete() ([]ImageMetaData, error) { + logrus.Tracef("Query all image metadata that represent files queued to delete") + + db, err := d.openDatabase() + if err != nil { + return nil, err + } + defer db.Close() + + rows, err := db.Query("SELECT imageId, piwigoId, fullImagePath, fileName, md5sum, lastChanged, categoryPath, categoryId, uploadRequired, deleteRequired FROM image WHERE deleteRequired = 1") + if err != nil { + return nil, err + } + defer rows.Close() + + images := []ImageMetaData{} + for rows.Next() { + img := &ImageMetaData{} + err = ReadImageMetadataFromRow(rows, img) + if err != nil { + return nil, err + } + images = append(images, *img) + } + err = rows.Err() + + return images, err +} + func (d *localDataStore) ImageMetadataToUpload() ([]ImageMetaData, error) { logrus.Tracef("Query all image metadata that represent files queued to upload") diff --git a/internal/app/datastore_mock_test.go b/internal/app/datastore_mock_test.go index 09e14a2..db3725e 100644 --- a/internal/app/datastore_mock_test.go +++ b/internal/app/datastore_mock_test.go @@ -47,6 +47,21 @@ func (mr *MockImageMetadataProviderMockRecorder) ImageMetadata(arg0 interface{}) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ImageMetadata", reflect.TypeOf((*MockImageMetadataProvider)(nil).ImageMetadata), arg0) } +// ImageMetadataToDelete mocks base method +func (m *MockImageMetadataProvider) ImageMetadataToDelete() ([]ImageMetaData, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ImageMetadataToDelete") + ret0, _ := ret[0].([]ImageMetaData) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ImageMetadataToDelete indicates an expected call of ImageMetadataToDelete +func (mr *MockImageMetadataProviderMockRecorder) ImageMetadataToDelete() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ImageMetadataToDelete", reflect.TypeOf((*MockImageMetadataProvider)(nil).ImageMetadataToDelete)) +} + // ImageMetadataToUpload mocks base method func (m *MockImageMetadataProvider) ImageMetadataToUpload() ([]ImageMetaData, error) { m.ctrl.T.Helper() diff --git a/internal/app/datastore_test.go b/internal/app/datastore_test.go index 0acf182..87ab1dd 100644 --- a/internal/app/datastore_test.go +++ b/internal/app/datastore_test.go @@ -105,6 +105,37 @@ func Test_save_and_query_for_upload_records_do_not_contain_images_to_delete(t *t EnsureMetadataAreEqual("toupload", img1, imgLoad, t) } +func Test_save_and_query_for_deleted_records_do_contain_images(t *testing.T) { + if !dbinitOk { + t.Skip("Skipping test as TestDataStoreInitialize failed!") + } + dataStore := setupDatabase(t) + defer cleanupDatabase(t) + + img1 := getExampleImageMetadata("blah/foo/bar.jpg") + img1.UploadRequired = false + img1.DeleteRequired = true + + saveImageShouldNotFail("todelete", dataStore, img1, t) + img1.ImageId = 1 + + images, err := dataStore.ImageMetadataToDelete() + if err != nil { + t.Fatalf("Could not query images to delete! %s", err) + } + + if len(images) > 1 { + t.Fatal("Got more than one image to delete but only one is expected") + } + + if len(images) < 1 { + t.Fatal("Got no image to delete but one is expected!") + } + + imgLoad := images[0] + EnsureMetadataAreEqual("todelete", img1, imgLoad, t) +} + func Test_load_metadata_not_found(t *testing.T) { if !dbinitOk { t.Skip("Skipping test as TestDataStoreInitialize failed!") diff --git a/internal/app/images_test.go b/internal/app/images_test.go index f405ed4..0817e72 100644 --- a/internal/app/images_test.go +++ b/internal/app/images_test.go @@ -412,6 +412,7 @@ func Test_uploadImages_saves_same_id_to_db(t *testing.T) { } // test metadata store to store save the metadat and simulate the database +//TODO: refactor to use generated test implementation type testStore struct { savedMetadata map[string]ImageMetaData } @@ -437,6 +438,10 @@ func (d *testStore) ImageMetadataToUpload() ([]ImageMetaData, error) { return nil, errors.New("N/A") } +func (d *testStore) ImageMetadataToDelete() ([]ImageMetaData, error) { + return nil, errors.New("N/A") +} + func (d *testStore) SavePiwigoIdAndUpdateUploadFlag(md5Sum string, piwigoId int) error { return errors.New("N/A") }