Files
photoview/api/database/migrations/exif_invalid_gps_test.go
Kostiantyn df9af39a16 Validate incoming GPS data (#951)
* Validate incoming GPS data and throw an error if it is incorrect, storing Null values

* Extracted GPS data processing to function in external parser; optimized IF in internal parser; removed unnecessary comments; set exact values for positive test

* Add the migration for removing existing invalid GPS data and its test; added better errors to asserts in the GPS validation test

* Install FFmpeg and ExifTool on the API unit-test environment

* Fix 'stripped.jpg', 'IncorrectGPS.jpg', and 'CorrectGPS.jpg' tests for the external parser

* Optimized data validation in the external parser, returned error by the internal parser for invalid data, updated test to expect errors and handle them

* Switched from error to log entry in case of incorrect GPS data, as error handling is not so transparent in the internal parser

---------

Co-authored-by: Konstantin Koval
2024-06-29 10:32:48 +03:00

75 lines
2.3 KiB
Go

package migrations_test
import (
"bufio"
"math"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/photoview/photoview/api/database/migrations"
"github.com/photoview/photoview/api/graphql/models"
"github.com/photoview/photoview/api/test_utils"
)
func TestExifMigration(t *testing.T) {
envFile, err := os.Open("/home/runner/work/photoview/photoview/api/testing.env")
if err != nil {
t.Fatalf("Failed to open environment file: %v", err)
}
defer envFile.Close()
scanner := bufio.NewScanner(envFile)
for scanner.Scan() {
line := scanner.Text()
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
t.Fatalf("Invalid line in environment file: %s", line)
}
key, value := parts[0], strings.Trim(parts[1], "'")
os.Setenv(key, value)
}
db := test_utils.DatabaseTest(t)
defer db.Exec("DELETE FROM media_exif") // Clean up after test
// Create test data
exifEntries := []models.MediaEXIF{
{GPSLatitude: floatPtr(90.1), GPSLongitude: floatPtr(90.0)}, // Invalid GPSLatitude
{GPSLatitude: floatPtr(-90.1), GPSLongitude: floatPtr(-90.0)}, // Invalid GPSLatitude
{GPSLatitude: floatPtr(90.0), GPSLongitude: floatPtr(90.1)}, // Invalid GPSLongitude
{GPSLatitude: floatPtr(-90.0), GPSLongitude: floatPtr(-90.1)}, // Invalid GPSLongitude
{GPSLatitude: floatPtr(90.0), GPSLongitude: floatPtr(90.0)}, // Valid GPS data
{GPSLatitude: floatPtr(-90.0), GPSLongitude: floatPtr(-90.0)}, // Valid GPS data
{GPSLatitude: floatPtr(90.1), GPSLongitude: floatPtr(90.1)}, // Invalid GPSLatitude and GPSLongitude
{GPSLatitude: floatPtr(-90.1), GPSLongitude: floatPtr(-90.1)}, // Invalid GPSLatitude and GPSLongitude
}
// Insert test data
for _, entry := range exifEntries {
assert.NoError(t, db.Create(&entry).Error)
}
// Run migration
assert.NoError(t, migrations.MigrateForExifGPSCorrection(db))
// Validate the results
var results []models.MediaEXIF
assert.NoError(t, db.Find(&results).Error)
for _, entry := range results {
if entry.GPSLatitude != nil {
assert.LessOrEqual(t, math.Abs(*entry.GPSLatitude), 90.0, "GPSLatitude should be within [-90, 90]: %+v", entry)
}
if entry.GPSLongitude != nil {
assert.LessOrEqual(t, math.Abs(*entry.GPSLongitude), 90.0, "GPSLongitude should be within [-90, 90]: %+v", entry)
}
}
}
func floatPtr(f float64) *float64 {
return &f
}