2019-03-23 22:41:04 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 Philipp Haefelfinger (http://www.haefelfinger.ch/). All Rights Reserved.
|
|
|
|
* This application is licensed under GPLv2. See the LICENSE file in the root directory of the project.
|
|
|
|
*/
|
|
|
|
|
2019-04-02 21:45:46 +02:00
|
|
|
package images
|
2019-03-17 23:05:17 +01:00
|
|
|
|
2019-04-09 00:05:02 +02:00
|
|
|
//go:generate mockgen -destination=./piwigo_mock_test.go -package=images git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/piwigo CategoryApi,ImageApi
|
2019-04-06 22:14:17 +02:00
|
|
|
//go:generate mockgen -destination=./datastore_mock_test.go -package=images git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore ImageMetadataProvider,CategoryProvider
|
2019-03-22 00:14:34 +01:00
|
|
|
|
2019-03-17 23:05:17 +01:00
|
|
|
import (
|
2019-04-02 21:30:39 +02:00
|
|
|
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/datastore"
|
2019-03-17 23:05:17 +01:00
|
|
|
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure"
|
2019-03-22 00:14:34 +01:00
|
|
|
"github.com/golang/mock/gomock"
|
2019-03-17 23:05:17 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-03-24 00:09:20 +01:00
|
|
|
func Test_synchronize_local_image_metadata_should_find_nothing_if_empty(t *testing.T) {
|
2019-04-06 22:14:17 +02:00
|
|
|
mockCtrl := gomock.NewController(t)
|
|
|
|
defer mockCtrl.Finish()
|
|
|
|
|
2019-04-06 22:46:06 +02:00
|
|
|
category := datastore.CategoryData{CategoryId: 1, Name: "shooting1", PiwigoId: 1, Key: "2019/shooting1"}
|
2019-04-06 22:14:17 +02:00
|
|
|
categoryMock := NewMockCategoryProvider(mockCtrl)
|
|
|
|
categoryMock.EXPECT().GetCategoryByKey(category.Key).Return(category, nil).Times(0)
|
2019-03-20 23:15:41 +01:00
|
|
|
|
2019-04-06 22:46:06 +02:00
|
|
|
db := NewMockImageMetadataProvider(mockCtrl)
|
|
|
|
db.EXPECT().ImageMetadataAll().Times(1)
|
|
|
|
db.EXPECT().SaveImageMetadata(gomock.Any()).Times(0)
|
|
|
|
|
2019-03-17 23:05:17 +01:00
|
|
|
fileSystemNodes := map[string]*localFileStructure.FilesystemNode{}
|
|
|
|
|
2019-04-06 22:14:17 +02:00
|
|
|
err := SynchronizeLocalImageMetadata(db, categoryMock, fileSystemNodes, testChecksumCalculator)
|
2019-03-17 23:05:17 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 00:14:34 +01:00
|
|
|
func Test_synchronize_local_image_metadata_should_add_new_metadata(t *testing.T) {
|
2019-04-06 22:14:17 +02:00
|
|
|
mockCtrl := gomock.NewController(t)
|
|
|
|
defer mockCtrl.Finish()
|
2019-03-20 23:15:41 +01:00
|
|
|
|
2019-04-06 22:46:06 +02:00
|
|
|
category := datastore.CategoryData{CategoryId: 1, Name: "shooting1", PiwigoId: 1, Key: "2019/shooting1"}
|
2019-04-06 22:14:17 +02:00
|
|
|
categoryMock := NewMockCategoryProvider(mockCtrl)
|
|
|
|
categoryMock.EXPECT().GetCategoryByKey(category.Key).Return(category, nil).Times(1)
|
2019-03-20 23:15:41 +01:00
|
|
|
|
2019-03-17 23:05:17 +01:00
|
|
|
testFileSystemNode := &localFileStructure.FilesystemNode{
|
|
|
|
Key: "2019/shooting1/abc.jpg",
|
|
|
|
ModTime: time.Date(2019, 01, 01, 01, 0, 0, 0, time.UTC),
|
|
|
|
Name: "abc.jpg",
|
|
|
|
Path: "2019/shooting1/abc.jpg",
|
|
|
|
IsDir: false}
|
|
|
|
|
|
|
|
fileSystemNodes := map[string]*localFileStructure.FilesystemNode{}
|
|
|
|
fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode
|
|
|
|
|
2019-04-06 22:46:06 +02:00
|
|
|
image := createImageMetaDataFromFilesystem(testFileSystemNode, 0, true, false)
|
2019-04-06 23:01:53 +02:00
|
|
|
image.CategoryPiwigoId = category.PiwigoId
|
2019-04-06 22:46:06 +02:00
|
|
|
image.CategoryPath = category.Key
|
|
|
|
|
|
|
|
db := NewMockImageMetadataProvider(mockCtrl)
|
|
|
|
db.EXPECT().ImageMetadataAll().Times(1)
|
|
|
|
db.EXPECT().ImageMetadata(testFileSystemNode.Key).Return(datastore.ImageMetaData{}, datastore.ErrorRecordNotFound).Times(1)
|
|
|
|
db.EXPECT().SaveImageMetadata(image).Times(1)
|
|
|
|
|
2019-03-17 23:05:17 +01:00
|
|
|
// execute the sync metadata based on the file system results
|
2019-04-06 22:14:17 +02:00
|
|
|
err := SynchronizeLocalImageMetadata(db, categoryMock, fileSystemNodes, testChecksumCalculator)
|
2019-03-17 23:05:17 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 00:09:56 +01:00
|
|
|
func Test_synchronize_local_image_metadata_should_mark_unchanged_entries_without_piwigoid_as_uploads_and_reset_deleted(t *testing.T) {
|
2019-04-06 22:14:17 +02:00
|
|
|
mockCtrl := gomock.NewController(t)
|
|
|
|
defer mockCtrl.Finish()
|
2019-03-25 00:09:56 +01:00
|
|
|
|
2019-04-06 22:46:06 +02:00
|
|
|
category := datastore.CategoryData{CategoryId: 1, Name: "shooting1", PiwigoId: 1, Key: "2019/shooting1"}
|
2019-04-06 22:14:17 +02:00
|
|
|
categoryMock := NewMockCategoryProvider(mockCtrl)
|
|
|
|
categoryMock.EXPECT().GetCategoryByKey(category.Key).Return(category, nil).Times(0)
|
2019-03-25 00:09:56 +01:00
|
|
|
|
|
|
|
testFileSystemNode := &localFileStructure.FilesystemNode{
|
|
|
|
Key: "2019/shooting1/abc.jpg",
|
|
|
|
ModTime: time.Date(2019, 01, 01, 00, 0, 0, 0, time.UTC),
|
|
|
|
Name: "abc.jpg",
|
|
|
|
Path: "2019/shooting1/abc.jpg",
|
|
|
|
IsDir: false}
|
|
|
|
|
|
|
|
fileSystemNodes := map[string]*localFileStructure.FilesystemNode{}
|
|
|
|
fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode
|
|
|
|
|
2019-04-06 22:46:06 +02:00
|
|
|
imageExptected := createImageMetaDataFromFilesystem(testFileSystemNode, 0, true, false)
|
|
|
|
|
|
|
|
imageStored := imageExptected
|
|
|
|
imageStored.DeleteRequired = true
|
|
|
|
|
|
|
|
db := NewMockImageMetadataProvider(mockCtrl)
|
|
|
|
db.EXPECT().ImageMetadataAll().Times(1)
|
|
|
|
db.EXPECT().ImageMetadata(testFileSystemNode.Key).Return(imageStored, nil).Times(1)
|
|
|
|
db.EXPECT().SaveImageMetadata(imageExptected).Times(1)
|
|
|
|
|
2019-03-25 00:09:56 +01:00
|
|
|
// execute the sync metadata based on the file system results
|
2019-04-06 22:14:17 +02:00
|
|
|
err := SynchronizeLocalImageMetadata(db, categoryMock, fileSystemNodes, testChecksumCalculator)
|
2019-03-25 00:09:56 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-24 23:46:23 +01:00
|
|
|
func Test_synchronize_local_image_metadata_should_mark_changed_entries_as_uploads_and_reset_deleted(t *testing.T) {
|
2019-04-06 22:14:17 +02:00
|
|
|
mockCtrl := gomock.NewController(t)
|
|
|
|
defer mockCtrl.Finish()
|
2019-03-20 23:15:41 +01:00
|
|
|
|
2019-04-06 22:46:06 +02:00
|
|
|
category := datastore.CategoryData{CategoryId: 1, Name: "shooting1", PiwigoId: 1, Key: "2019/shooting1"}
|
2019-04-06 22:14:17 +02:00
|
|
|
categoryMock := NewMockCategoryProvider(mockCtrl)
|
|
|
|
categoryMock.EXPECT().GetCategoryByKey(category.Key).Return(category, nil).Times(0)
|
2019-03-20 23:15:41 +01:00
|
|
|
|
2019-03-17 23:05:17 +01:00
|
|
|
testFileSystemNode := &localFileStructure.FilesystemNode{
|
|
|
|
Key: "2019/shooting1/abc.jpg",
|
|
|
|
ModTime: time.Date(2019, 01, 01, 01, 0, 0, 0, time.UTC),
|
|
|
|
Name: "abc.jpg",
|
|
|
|
Path: "2019/shooting1/abc.jpg",
|
|
|
|
IsDir: false}
|
|
|
|
|
|
|
|
fileSystemNodes := map[string]*localFileStructure.FilesystemNode{}
|
|
|
|
fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode
|
|
|
|
|
2019-04-06 22:46:06 +02:00
|
|
|
imageExptected := createImageMetaDataFromFilesystem(testFileSystemNode, 0, true, false)
|
|
|
|
|
|
|
|
imageStored := imageExptected
|
|
|
|
imageStored.DeleteRequired = true
|
|
|
|
imageStored.UploadRequired = false
|
|
|
|
imageStored.LastChange = time.Date(2019, 01, 01, 00, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
db := NewMockImageMetadataProvider(mockCtrl)
|
|
|
|
db.EXPECT().ImageMetadataAll().Times(1)
|
|
|
|
db.EXPECT().ImageMetadata(testFileSystemNode.Key).Return(imageStored, nil).Times(1)
|
|
|
|
db.EXPECT().SaveImageMetadata(imageExptected).Times(1)
|
|
|
|
|
2019-03-17 23:05:17 +01:00
|
|
|
// execute the sync metadata based on the file system results
|
2019-04-06 22:14:17 +02:00
|
|
|
err := SynchronizeLocalImageMetadata(db, categoryMock, fileSystemNodes, testChecksumCalculator)
|
2019-03-17 23:05:17 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-24 23:46:23 +01:00
|
|
|
func Test_synchronize_local_image_metadata_should_not_mark_unchanged_files_to_upload_and_reset_deleted(t *testing.T) {
|
2019-04-06 22:14:17 +02:00
|
|
|
mockCtrl := gomock.NewController(t)
|
|
|
|
defer mockCtrl.Finish()
|
2019-03-20 23:15:41 +01:00
|
|
|
|
2019-04-06 22:46:06 +02:00
|
|
|
category := datastore.CategoryData{CategoryId: 1, Name: "shooting1", PiwigoId: 1, Key: "2019/shooting1"}
|
2019-04-06 22:14:17 +02:00
|
|
|
categoryMock := NewMockCategoryProvider(mockCtrl)
|
|
|
|
categoryMock.EXPECT().GetCategoryByKey(category.Key).Return(category, nil).Times(0)
|
2019-03-20 23:15:41 +01:00
|
|
|
|
2019-03-17 23:05:17 +01:00
|
|
|
testFileSystemNode := &localFileStructure.FilesystemNode{
|
|
|
|
Key: "2019/shooting1/abc.jpg",
|
|
|
|
ModTime: time.Date(2019, 01, 01, 01, 0, 0, 0, time.UTC),
|
|
|
|
Name: "abc.jpg",
|
|
|
|
Path: "2019/shooting1/abc.jpg",
|
|
|
|
IsDir: false}
|
|
|
|
|
|
|
|
fileSystemNodes := map[string]*localFileStructure.FilesystemNode{}
|
|
|
|
fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode
|
|
|
|
|
2019-04-06 22:46:06 +02:00
|
|
|
imageExptected := createImageMetaDataFromFilesystem(testFileSystemNode, 5, false, false)
|
|
|
|
|
|
|
|
imageStored := imageExptected
|
|
|
|
imageStored.DeleteRequired = true
|
|
|
|
imageStored.UploadRequired = false
|
|
|
|
imageStored.LastChange = time.Date(2019, 01, 01, 01, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
db := NewMockImageMetadataProvider(mockCtrl)
|
|
|
|
db.EXPECT().ImageMetadataAll().Times(1)
|
|
|
|
db.EXPECT().ImageMetadata(testFileSystemNode.Key).Return(imageStored, nil).Times(1)
|
|
|
|
db.EXPECT().SaveImageMetadata(imageExptected).Times(1)
|
|
|
|
|
2019-03-17 23:05:17 +01:00
|
|
|
// execute the sync metadata based on the file system results
|
2019-04-06 22:14:17 +02:00
|
|
|
err := SynchronizeLocalImageMetadata(db, categoryMock, fileSystemNodes, testChecksumCalculator)
|
2019-03-17 23:05:17 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 00:14:34 +01:00
|
|
|
func Test_synchronize_local_image_metadata_should_not_process_directories(t *testing.T) {
|
2019-04-06 22:14:17 +02:00
|
|
|
mockCtrl := gomock.NewController(t)
|
|
|
|
defer mockCtrl.Finish()
|
|
|
|
|
2019-04-06 22:46:06 +02:00
|
|
|
category := datastore.CategoryData{CategoryId: 1, Name: "shooting1", PiwigoId: 1, Key: "2019/shooting1"}
|
2019-04-06 22:14:17 +02:00
|
|
|
categoryMock := NewMockCategoryProvider(mockCtrl)
|
|
|
|
categoryMock.EXPECT().GetCategoryByKey(category.Key).Return(category, nil).Times(0)
|
2019-03-20 23:15:41 +01:00
|
|
|
|
2019-03-17 23:05:17 +01:00
|
|
|
testFileSystemNode := &localFileStructure.FilesystemNode{
|
|
|
|
Key: "2019/shooting1",
|
|
|
|
ModTime: time.Date(2019, 01, 01, 01, 0, 0, 0, time.UTC),
|
|
|
|
Name: "shooting1",
|
|
|
|
Path: "2019/shooting1/",
|
|
|
|
IsDir: true}
|
|
|
|
|
|
|
|
fileSystemNodes := map[string]*localFileStructure.FilesystemNode{}
|
|
|
|
fileSystemNodes[testFileSystemNode.Key] = testFileSystemNode
|
|
|
|
|
2019-04-06 22:46:06 +02:00
|
|
|
db := NewMockImageMetadataProvider(mockCtrl)
|
|
|
|
db.EXPECT().ImageMetadataAll().Times(1)
|
|
|
|
db.EXPECT().SaveImageMetadata(gomock.Any()).Times(0)
|
|
|
|
|
2019-03-17 23:05:17 +01:00
|
|
|
// execute the sync metadata based on the file system results
|
2019-04-06 22:14:17 +02:00
|
|
|
err := SynchronizeLocalImageMetadata(db, categoryMock, fileSystemNodes, testChecksumCalculator)
|
2019-03-17 23:05:17 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-24 00:09:20 +01:00
|
|
|
func Test_synchronizeLocalImageMetadataFindFilesToDelete(t *testing.T) {
|
|
|
|
mockCtrl := gomock.NewController(t)
|
|
|
|
defer mockCtrl.Finish()
|
|
|
|
|
2019-03-24 23:46:23 +01:00
|
|
|
img := createTestImageMetaData(5)
|
2019-04-02 21:30:39 +02:00
|
|
|
images := []datastore.ImageMetaData{img}
|
2019-03-24 00:09:20 +01:00
|
|
|
|
|
|
|
imgToSave := img
|
|
|
|
imgToSave.UploadRequired = false
|
|
|
|
imgToSave.DeleteRequired = true
|
|
|
|
|
|
|
|
dbmock := NewMockImageMetadataProvider(mockCtrl)
|
|
|
|
dbmock.EXPECT().ImageMetadataAll().Times(1).Return(images, nil)
|
|
|
|
dbmock.EXPECT().SaveImageMetadata(imgToSave).Times(1)
|
|
|
|
|
|
|
|
err := synchronizeLocalImageMetadataFindFilesToDelete(dbmock)
|
2019-03-24 23:46:23 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:05:17 +01:00
|
|
|
// to make the sync testable, we pass in a simple mock that returns the filepath as checksum
|
|
|
|
func testChecksumCalculator(file string) (string, error) {
|
|
|
|
return file, nil
|
|
|
|
}
|
2019-03-24 23:46:23 +01:00
|
|
|
|
2019-04-02 21:30:39 +02:00
|
|
|
func createTestImageMetaData(piwigoId int) datastore.ImageMetaData {
|
|
|
|
img := datastore.ImageMetaData{
|
2019-04-06 23:01:53 +02:00
|
|
|
ImageId: 1,
|
|
|
|
PiwigoId: piwigoId,
|
|
|
|
FullImagePath: "/nonexisting/file.jpg",
|
|
|
|
UploadRequired: true,
|
|
|
|
Md5Sum: "1234",
|
|
|
|
CategoryPiwigoId: 2,
|
2019-03-24 23:46:23 +01:00
|
|
|
}
|
|
|
|
return img
|
|
|
|
}
|
2019-04-06 22:46:06 +02:00
|
|
|
|
|
|
|
func createImageMetaDataFromFilesystem(testFileSystemNode *localFileStructure.FilesystemNode, piwigoId int, uploadRequired bool, deleteRequired bool) datastore.ImageMetaData {
|
|
|
|
imageExptected := datastore.ImageMetaData{
|
|
|
|
Md5Sum: testFileSystemNode.Key,
|
|
|
|
FullImagePath: testFileSystemNode.Key,
|
|
|
|
PiwigoId: piwigoId,
|
|
|
|
UploadRequired: uploadRequired,
|
|
|
|
LastChange: testFileSystemNode.ModTime,
|
|
|
|
Filename: testFileSystemNode.Name,
|
|
|
|
DeleteRequired: deleteRequired,
|
|
|
|
}
|
|
|
|
return imageExptected
|
|
|
|
}
|