2019-03-17 23:05:17 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2019-03-17 23:57:28 +01:00
|
|
|
"errors"
|
2019-03-17 23:05:17 +01:00
|
|
|
"git.haefelfinger.net/piwigo/PiwigoDirectoryUploader/internal/pkg/localFileStructure"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSynchronizeLocalImageMetadataShouldDoNothingIfEmpty(t *testing.T) {
|
|
|
|
db := NewtestStore()
|
|
|
|
fileSystemNodes := map[string]*localFileStructure.FilesystemNode{}
|
|
|
|
|
|
|
|
err := synchronizeLocalImageMetadata(db, fileSystemNodes, testChecksumCalculator)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(db.savedMetadata) > 0 {
|
|
|
|
t.Error("There were metadata records saved but non expected!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSynchronizeLocalImageMetadataShouldAddNewMetadata(t *testing.T) {
|
|
|
|
db := NewtestStore()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// execute the sync metadata based on the file system results
|
|
|
|
err := synchronizeLocalImageMetadata(db, fileSystemNodes, testChecksumCalculator)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if data are saved
|
|
|
|
savedData, exist := db.savedMetadata[testFileSystemNode.Key]
|
|
|
|
if !exist {
|
|
|
|
t.Fatal("Could not find correct metadata!")
|
|
|
|
}
|
|
|
|
if savedData.RelativeImagePath != testFileSystemNode.Key {
|
|
|
|
t.Errorf("relativeImagePath %s on db image metadata is not set to %s!", savedData.RelativeImagePath, testFileSystemNode.Key)
|
|
|
|
}
|
|
|
|
if savedData.LastChange != testFileSystemNode.ModTime {
|
|
|
|
t.Error("lastChange on db image metadata is not set to the right date!")
|
|
|
|
}
|
|
|
|
if savedData.Filename != "abc.jpg" {
|
|
|
|
t.Error("filename on db image metadata is not set to abc.jpg!")
|
|
|
|
}
|
|
|
|
if savedData.Md5Sum != testFileSystemNode.Key {
|
|
|
|
t.Errorf("md5sum %s on db image metadata is not set to %s!", savedData.Md5Sum, testFileSystemNode.Key)
|
|
|
|
}
|
|
|
|
if savedData.UploadRequired != true {
|
|
|
|
t.Errorf("uploadRequired on db image metadata is not set to true!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSynchronizeLocalImageMetadataShouldMarkChangedEntriesAsUploads(t *testing.T) {
|
|
|
|
db := NewtestStore()
|
|
|
|
db.savedMetadata["2019/shooting1/abc.jpg"] = ImageMetaData{
|
|
|
|
Md5Sum: "2019/shooting1/abc.jpg",
|
|
|
|
RelativeImagePath: "2019/shooting1/abc.jpg",
|
|
|
|
UploadRequired: false,
|
|
|
|
LastChange: time.Date(2019, 01, 01, 00, 0, 0, 0, time.UTC),
|
|
|
|
Filename: "abc.jpg",
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// execute the sync metadata based on the file system results
|
|
|
|
err := synchronizeLocalImageMetadata(db, fileSystemNodes, testChecksumCalculator)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if data are saved
|
|
|
|
savedData, exist := db.savedMetadata[testFileSystemNode.Key]
|
|
|
|
if !exist {
|
|
|
|
t.Fatal("Could not find correct metadata!")
|
|
|
|
}
|
|
|
|
if savedData.LastChange != testFileSystemNode.ModTime {
|
|
|
|
t.Error("lastChange on db image metadata is not set to the right date!")
|
|
|
|
}
|
|
|
|
if savedData.UploadRequired != true {
|
|
|
|
t.Errorf("uploadRequired on db image metadata is not set to true!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSynchronizeLocalImageMetadataShouldNotMarkUnchangedFilesToUpload(t *testing.T) {
|
|
|
|
db := NewtestStore()
|
|
|
|
db.savedMetadata["2019/shooting1/abc.jpg"] = ImageMetaData{
|
|
|
|
Md5Sum: "2019/shooting1/abc.jpg",
|
|
|
|
RelativeImagePath: "2019/shooting1/abc.jpg",
|
|
|
|
UploadRequired: false,
|
|
|
|
LastChange: time.Date(2019, 01, 01, 01, 0, 0, 0, time.UTC),
|
|
|
|
Filename: "abc.jpg",
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// execute the sync metadata based on the file system results
|
|
|
|
err := synchronizeLocalImageMetadata(db, fileSystemNodes, testChecksumCalculator)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if data are saved
|
|
|
|
savedData, exist := db.savedMetadata[testFileSystemNode.Key]
|
|
|
|
if !exist {
|
|
|
|
t.Fatal("Could not find correct metadata!")
|
|
|
|
}
|
|
|
|
if savedData.UploadRequired {
|
|
|
|
t.Errorf("uploadRequired on db image metadata is set to true, but should not be on unchanged items!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSynchronizeLocalImageMetadataShouldNotProcessDirectories(t *testing.T) {
|
|
|
|
db := NewtestStore()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// execute the sync metadata based on the file system results
|
|
|
|
err := synchronizeLocalImageMetadata(db, fileSystemNodes, testChecksumCalculator)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(db.savedMetadata) > 0 {
|
|
|
|
t.Error("There were metadata records saved but non expected!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:57:28 +01:00
|
|
|
func TestSynchronizePiwigoMetadata(t *testing.T) {
|
|
|
|
db := NewtestStore()
|
|
|
|
db.savedMetadata["2019/shooting1/abc.jpg"] = ImageMetaData{
|
|
|
|
Md5Sum: "2019/shooting1/abc.jpg",
|
|
|
|
RelativeImagePath: "2019/shooting1/abc.jpg",
|
|
|
|
UploadRequired: false,
|
|
|
|
LastChange: time.Date(2019, 01, 01, 00, 0, 0, 0, time.UTC),
|
|
|
|
Filename: "abc.jpg",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// execute the sync metadata based on the file system results
|
|
|
|
//err := synchronizeLocalImageMetadata( db)
|
|
|
|
//if err != nil {
|
|
|
|
// t.Error(err)
|
|
|
|
//}
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:05:17 +01:00
|
|
|
// test metadata store to store save the metadat and simulate the database
|
|
|
|
type testStore struct {
|
|
|
|
savedMetadata map[string]ImageMetaData
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewtestStore() *testStore {
|
|
|
|
return &testStore{savedMetadata: make(map[string]ImageMetaData)}
|
|
|
|
}
|
|
|
|
|
2019-03-19 21:19:38 +01:00
|
|
|
func (s *testStore) ImageMetadata(relativePath string) (ImageMetaData, error) {
|
2019-03-17 23:05:17 +01:00
|
|
|
metadata, exist := s.savedMetadata[relativePath]
|
|
|
|
if !exist {
|
|
|
|
return ImageMetaData{}, ErrorRecordNotFound
|
|
|
|
}
|
|
|
|
return metadata, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *testStore) SaveImageMetadata(m ImageMetaData) error {
|
|
|
|
s.savedMetadata[m.RelativeImagePath] = m
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-19 21:19:38 +01:00
|
|
|
func (d *testStore) ImageMetadataToUpload() ([]*ImageMetaData, error) {
|
2019-03-17 23:57:28 +01:00
|
|
|
return nil, errors.New("N/A")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|