2019-03-15 22:50:48 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var databaseFile = "./metadatatest.db"
|
|
|
|
var dbinitOk bool
|
|
|
|
|
|
|
|
func TestDataStoreInitialize(t *testing.T) {
|
|
|
|
_ = setupDatabase(t)
|
|
|
|
cleanupDatabase(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveAndLoadMetadata(t *testing.T) {
|
|
|
|
if !dbinitOk {
|
|
|
|
t.Skip("Skipping test as TestDataStoreInitialize failed!")
|
|
|
|
}
|
|
|
|
dataStore := setupDatabase(t)
|
|
|
|
|
|
|
|
filePath := "blah/foo/bar.jpg"
|
|
|
|
img := getExampleImageMetadata(filePath)
|
|
|
|
|
|
|
|
saveImageShouldNotFail("insert", dataStore, img, t)
|
|
|
|
img.ImageId = 1
|
|
|
|
|
|
|
|
imgLoad := loadMetadataShouldNotFail("insert", dataStore, filePath, t)
|
|
|
|
EnsureMetadataAreEqual("insert", img, imgLoad, t)
|
|
|
|
|
|
|
|
// updated the image again
|
|
|
|
img.Md5Sum = "123456"
|
|
|
|
saveImageShouldNotFail("update", dataStore, img, t)
|
|
|
|
|
|
|
|
imgLoad = loadMetadataShouldNotFail("update", dataStore, filePath, t)
|
|
|
|
EnsureMetadataAreEqual("update", img, imgLoad, t)
|
|
|
|
|
|
|
|
cleanupDatabase(t)
|
|
|
|
}
|
|
|
|
|
2019-03-17 23:36:40 +01:00
|
|
|
func TestSaveAndQueryForUploadRecords(t *testing.T) {
|
|
|
|
if !dbinitOk {
|
|
|
|
t.Skip("Skipping test as TestDataStoreInitialize failed!")
|
|
|
|
}
|
|
|
|
dataStore := setupDatabase(t)
|
|
|
|
|
|
|
|
filePath := "blah/foo/bar.jpg"
|
|
|
|
img := getExampleImageMetadata(filePath)
|
|
|
|
|
|
|
|
saveImageShouldNotFail("toupload", dataStore, img, t)
|
|
|
|
img.ImageId = 1
|
|
|
|
|
2019-03-19 21:19:38 +01:00
|
|
|
images, err := dataStore.ImageMetadataToUpload()
|
2019-03-17 23:36:40 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not query images to upload! %s", err)
|
|
|
|
}
|
|
|
|
|
2019-03-20 00:14:10 +01:00
|
|
|
if len(images) < 1 {
|
2019-03-17 23:36:40 +01:00
|
|
|
t.Fatal("Did not get any saved images to upload!")
|
|
|
|
}
|
|
|
|
|
|
|
|
imgLoad := images[0]
|
2019-03-20 00:14:10 +01:00
|
|
|
EnsureMetadataAreEqual("toupload", img, imgLoad, t)
|
2019-03-17 23:36:40 +01:00
|
|
|
|
|
|
|
cleanupDatabase(t)
|
|
|
|
}
|
|
|
|
|
2019-03-15 22:50:48 +01:00
|
|
|
func TestLoadMetadataNotFound(t *testing.T) {
|
|
|
|
if !dbinitOk {
|
|
|
|
t.Skip("Skipping test as TestDataStoreInitialize failed!")
|
|
|
|
}
|
|
|
|
dataStore := setupDatabase(t)
|
|
|
|
|
|
|
|
filePath := "blah/foo/bar.jpg"
|
2019-03-19 21:19:38 +01:00
|
|
|
imgLoad, err := dataStore.ImageMetadata(filePath)
|
2019-03-15 22:50:48 +01:00
|
|
|
if err != ErrorRecordNotFound {
|
|
|
|
t.Errorf("Unexpected error on loading non existing file %s: %s", filePath, err)
|
|
|
|
}
|
|
|
|
if imgLoad.ImageId > 0 {
|
|
|
|
t.Error("Found an image metadata that should not exist on an emtpy database.")
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanupDatabase(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUniqueIndexOnRelativeFilePath(t *testing.T) {
|
|
|
|
if !dbinitOk {
|
|
|
|
t.Skip("Skipping test as TestDataStoreInitialize failed!")
|
|
|
|
}
|
|
|
|
dataStore := setupDatabase(t)
|
|
|
|
|
|
|
|
filePath := "blah/foo/bar.jpg"
|
|
|
|
img := getExampleImageMetadata(filePath)
|
|
|
|
|
|
|
|
saveImageShouldNotFail("insert", dataStore, img, t)
|
|
|
|
|
|
|
|
err := dataStore.SaveImageMetadata(img)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Could save duplicated image metadata. Expected error but got none!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the error contains the expected column as name. If not, this indicates another problem than
|
|
|
|
// the expected duplicated insert error.
|
2019-03-20 23:15:41 +01:00
|
|
|
if !strings.Contains(err.Error(), "fullImagePath") {
|
2019-03-15 22:50:48 +01:00
|
|
|
t.Errorf("Got a unexpected error on saving duplicate records: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanupDatabase(t)
|
|
|
|
}
|
|
|
|
|
2019-03-19 23:07:55 +01:00
|
|
|
func TestUpdatePiwigoIdByChecksum(t *testing.T) {
|
|
|
|
if !dbinitOk {
|
|
|
|
t.Skip("Skipping test as TestDataStoreInitialize failed!")
|
|
|
|
}
|
|
|
|
dataStore := setupDatabase(t)
|
|
|
|
|
|
|
|
filePath := "blah/foo/bar.jpg"
|
|
|
|
img := getExampleImageMetadata(filePath)
|
|
|
|
|
2019-03-20 00:33:53 +01:00
|
|
|
saveImageShouldNotFail("SavePiwigoIdAndUpdateUploadFlag", dataStore, img, t)
|
2019-03-19 23:07:55 +01:00
|
|
|
img.ImageId = 1
|
|
|
|
img.PiwigoId = 1234
|
2019-03-20 00:33:53 +01:00
|
|
|
img.UploadRequired = false
|
2019-03-19 23:07:55 +01:00
|
|
|
|
|
|
|
err := dataStore.SavePiwigoIdAndUpdateUploadFlag(img.Md5Sum, img.PiwigoId)
|
|
|
|
if err != nil {
|
2019-03-20 00:33:53 +01:00
|
|
|
t.Errorf("SavePiwigoIdAndUpdateUploadFlag: Could not update piwigo id: %s", err)
|
2019-03-19 23:07:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
imgLoad := loadMetadataShouldNotFail("update", dataStore, filePath, t)
|
2019-03-20 00:33:53 +01:00
|
|
|
EnsureMetadataAreEqual("SavePiwigoIdAndUpdateUploadFlag", img, imgLoad, t)
|
|
|
|
|
|
|
|
cleanupDatabase(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdatePiwigoIdByChecksumFoundNoImage(t *testing.T) {
|
|
|
|
if !dbinitOk {
|
|
|
|
t.Skip("Skipping test as TestDataStoreInitialize failed!")
|
|
|
|
}
|
|
|
|
dataStore := setupDatabase(t)
|
|
|
|
|
|
|
|
filePath := "blah/foo/bar.jpg"
|
|
|
|
img := getExampleImageMetadata(filePath)
|
|
|
|
|
|
|
|
saveImageShouldNotFail("SavePiwigoIdAndUpdateUploadFlag", dataStore, img, t)
|
|
|
|
img.ImageId = 1
|
|
|
|
img.PiwigoId = 0
|
|
|
|
img.UploadRequired = true
|
|
|
|
|
|
|
|
err := dataStore.SavePiwigoIdAndUpdateUploadFlag(img.Md5Sum, img.PiwigoId)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("SavePiwigoIdAndUpdateUploadFlag: Could not update piwigo id: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
imgLoad := loadMetadataShouldNotFail("update", dataStore, filePath, t)
|
|
|
|
EnsureMetadataAreEqual("SavePiwigoIdAndUpdateUploadFlag", img, imgLoad, t)
|
2019-03-19 23:07:55 +01:00
|
|
|
|
|
|
|
cleanupDatabase(t)
|
|
|
|
}
|
|
|
|
|
2019-03-15 22:50:48 +01:00
|
|
|
func saveImageShouldNotFail(action string, dataStore *localDataStore, img ImageMetaData, t *testing.T) {
|
|
|
|
err := dataStore.SaveImageMetadata(img)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: Could not save Metadata: %s", action, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadMetadataShouldNotFail(action string, dataStore *localDataStore, filePath string, t *testing.T) ImageMetaData {
|
2019-03-19 21:19:38 +01:00
|
|
|
imgLoad, err := dataStore.ImageMetadata(filePath)
|
2019-03-15 22:50:48 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: Could not load saved Metadata: %s - %s", action, filePath, err)
|
|
|
|
}
|
|
|
|
return imgLoad
|
|
|
|
}
|
|
|
|
|
|
|
|
func EnsureMetadataAreEqual(action string, img ImageMetaData, imgLoad ImageMetaData, t *testing.T) {
|
|
|
|
// check if both instances serialize to the same string representation
|
|
|
|
if img.String() != imgLoad.String() {
|
|
|
|
t.Errorf("%s: Invalid image loaded! expected (ignore ImageId) %s but got %s", action, img.String(), imgLoad.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getExampleImageMetadata(filePath string) ImageMetaData {
|
|
|
|
return ImageMetaData{
|
2019-03-20 23:15:41 +01:00
|
|
|
FullImagePath: filePath,
|
|
|
|
PiwigoId: 1,
|
|
|
|
Md5Sum: "aabbccddeeff",
|
|
|
|
LastChange: time.Now().UTC(),
|
|
|
|
Filename: "bar.jpg",
|
|
|
|
CategoryPath: "blah/foo",
|
|
|
|
CategoryId: 100,
|
|
|
|
UploadRequired: true,
|
2019-03-15 22:50:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanupDatabase(t *testing.T) {
|
|
|
|
err := os.Remove(databaseFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed remove test database %s: %s", databaseFile, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupDatabase(t *testing.T) *localDataStore {
|
|
|
|
dataStore := &localDataStore{}
|
|
|
|
err := dataStore.Initialize(databaseFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to init datastore: %s", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dbinitOk = true
|
|
|
|
return dataStore
|
|
|
|
}
|