From e634fd97a727bb2662afcecf6126490b6294fc17 Mon Sep 17 00:00:00 2001 From: Theresa Gresch Date: Wed, 13 May 2020 11:57:54 +0200 Subject: [PATCH] Backend: Add tests to internal/query --- internal/query/geo_result_test.go | 39 +++++++++++ internal/query/geo_test.go | 111 ++++++++++++++++++++++++++++-- internal/query/label_test.go | 31 ++++++--- internal/query/labels_test.go | 28 +++++++- internal/query/photo_test.go | 27 +++++++- 5 files changed, 216 insertions(+), 20 deletions(-) create mode 100644 internal/query/geo_result_test.go diff --git a/internal/query/geo_result_test.go b/internal/query/geo_result_test.go new file mode 100644 index 000000000..bace77a16 --- /dev/null +++ b/internal/query/geo_result_test.go @@ -0,0 +1,39 @@ +package query + +import ( + "github.com/stretchr/testify/assert" + "testing" + "time" +) + +func TestGeoResult_Lat(t *testing.T) { + geo := GeoResult{ + ID: "123", + PhotoLat: 7.775, + PhotoLng: 8.775, + PhotoUUID: "", + PhotoTitle: "", + PhotoFavorite: false, + FileHash: "", + FileWidth: 0, + FileHeight: 0, + TakenAt: time.Time{}, + } + assert.Equal(t, 7.775000095367432, geo.Lat()) +} + +func TestGeoResult_Lng(t *testing.T) { + geo := GeoResult{ + ID: "123", + PhotoLat: 7.775, + PhotoLng: 8.775, + PhotoUUID: "", + PhotoTitle: "", + PhotoFavorite: false, + FileHash: "", + FileWidth: 0, + FileHeight: 0, + TakenAt: time.Time{}, + } + assert.Equal(t, 8.774999618530273, geo.Lng()) +} diff --git a/internal/query/geo_test.go b/internal/query/geo_test.go index 73a411c1c..be9dfcb6d 100644 --- a/internal/query/geo_test.go +++ b/internal/query/geo_test.go @@ -2,6 +2,7 @@ package query import ( "testing" + "time" "github.com/photoprism/photoprism/internal/form" "github.com/stretchr/testify/assert" @@ -12,16 +13,20 @@ func TestGeo(t *testing.T) { query := form.NewGeoSearch("") result, err := Geo(query) - assert.Nil(t, err) - assert.Equal(t, 4, len(result)) - + if err != nil { + t.Fatal(err) + } + assert.LessOrEqual(t, 5, len(result)) }) t.Run("search for bridge", func(t *testing.T) { query := form.NewGeoSearch("Query:bridge Before:3006-01-02") result, err := Geo(query) - assert.Nil(t, err) + if err != nil { + t.Fatal(err) + } + assert.Equal(t, "Neckarbrücke", result[0].PhotoTitle) }) @@ -30,9 +35,103 @@ func TestGeo(t *testing.T) { query := form.NewGeoSearch("After:2014-12-02 Before:3006-01-02") result, err := Geo(query) - assert.Nil(t, err) - t.Log(result) + if err != nil { + t.Fatal(err) + } assert.Equal(t, "Reunion", result[0].PhotoTitle) }) + t.Run("search for review true, quality 0", func(t *testing.T) { + f := form.GeoSearch{ + Query: "", + Before: time.Time{}, + After: time.Time{}, + Favorite: true, + Lat: 1.234, + Lng: 4.321, + S2: "", + Olc: "", + Dist: 0, + Quality: 0, + Review: true, + } + + result, err := Geo(f) + + if err != nil { + t.Fatal(err) + } + assert.LessOrEqual(t, 1, len(result)) + assert.Equal(t, "1000017", result[0].ID) + assert.IsType(t, GeoResults{}, result) + }) + + t.Run("search for review false, quality > 0", func(t *testing.T) { + f := form.GeoSearch{ + Query: "", + Before: time.Time{}, + After: time.Time{}, + Favorite: false, + Lat: 0, + Lng: 0, + S2: "", + Olc: "", + Dist: 0, + Quality: 3, + Review: false, + } + + result, err := Geo(f) + + if err != nil { + t.Fatal(err) + } + assert.LessOrEqual(t, 4, len(result)) + assert.IsType(t, GeoResults{}, result) + }) + t.Run("search for s2", func(t *testing.T) { + f := form.GeoSearch{ + Query: "", + Before: time.Time{}, + After: time.Time{}, + Favorite: false, + Lat: 0, + Lng: 0, + S2: "85", + Olc: "", + Dist: 0, + Quality: 0, + Review: false, + } + + result, err := Geo(f) + + if err != nil { + t.Fatal(err) + } + assert.Empty(t, result) + assert.IsType(t, GeoResults{}, result) + }) + t.Run("search for Olc", func(t *testing.T) { + f := form.GeoSearch{ + Query: "", + Before: time.Time{}, + After: time.Time{}, + Favorite: false, + Lat: 0, + Lng: 0, + S2: "", + Olc: "9", + Dist: 0, + Quality: 0, + Review: false, + } + + result, err := Geo(f) + + if err != nil { + t.Fatal(err) + } + assert.IsType(t, GeoResults{}, result) + }) } diff --git a/internal/query/label_test.go b/internal/query/label_test.go index 15583ffd1..a80c14475 100644 --- a/internal/query/label_test.go +++ b/internal/query/label_test.go @@ -7,7 +7,7 @@ import ( ) func TestLabelBySlug(t *testing.T) { - t.Run("files found", func(t *testing.T) { + t.Run("file found", func(t *testing.T) { label, err := LabelBySlug("flower") if err != nil { @@ -17,7 +17,7 @@ func TestLabelBySlug(t *testing.T) { assert.Equal(t, "Flower", label.LabelName) }) - t.Run("no files found", func(t *testing.T) { + t.Run("no file found", func(t *testing.T) { label, err := LabelBySlug("111") assert.Error(t, err, "record not found") @@ -26,7 +26,7 @@ func TestLabelBySlug(t *testing.T) { } func TestLabelByUUID(t *testing.T) { - t.Run("files found", func(t *testing.T) { + t.Run("file found", func(t *testing.T) { label, err := LabelByUUID("lt9k3pw1wowuy3c5") if err != nil { @@ -36,7 +36,7 @@ func TestLabelByUUID(t *testing.T) { assert.Equal(t, "COW", label.LabelName) }) - t.Run("no files found", func(t *testing.T) { + t.Run("no file found", func(t *testing.T) { label, err := LabelByUUID("111") assert.Error(t, err, "record not found") @@ -45,7 +45,7 @@ func TestLabelByUUID(t *testing.T) { } func TestLabelThumbBySlug(t *testing.T) { - t.Run("files found", func(t *testing.T) { + t.Run("file found", func(t *testing.T) { file, err := LabelThumbBySlug("flower") if err != nil { @@ -55,7 +55,7 @@ func TestLabelThumbBySlug(t *testing.T) { assert.Equal(t, "exampleFileName.jpg", file.FileName) }) - t.Run("no files found", func(t *testing.T) { + t.Run("no file found", func(t *testing.T) { file, err := LabelThumbBySlug("cow") assert.Error(t, err, "record not found") @@ -64,7 +64,7 @@ func TestLabelThumbBySlug(t *testing.T) { } func TestLabelThumbByUUID(t *testing.T) { - t.Run("files found", func(t *testing.T) { + t.Run("file found", func(t *testing.T) { file, err := LabelThumbByUUID("lt9k3pw1wowuy3c4") if err != nil { @@ -74,10 +74,25 @@ func TestLabelThumbByUUID(t *testing.T) { assert.Equal(t, "exampleFileName.jpg", file.FileName) }) - t.Run("no files found", func(t *testing.T) { + t.Run("no file found", func(t *testing.T) { file, err := LabelThumbByUUID("14") assert.Error(t, err, "record not found") t.Log(file) }) } + +func TestPhotoLabel(t *testing.T) { + t.Run("photo label found", func(t *testing.T) { + r, err := PhotoLabel(uint(1000000), uint(1000001)) + if err != nil { + t.Fatal(err) + } + assert.Equal(t, 38, r.Uncertainty) + }) + t.Run("no photo label found", func(t *testing.T) { + r, err := PhotoLabel(uint(1000000), uint(1000003)) + assert.Equal(t, "record not found", err.Error()) + t.Log(r) + }) +} diff --git a/internal/query/labels_test.go b/internal/query/labels_test.go index 3b9ef89d6..328f6d12d 100644 --- a/internal/query/labels_test.go +++ b/internal/query/labels_test.go @@ -1,11 +1,10 @@ package query import ( - "testing" - "github.com/photoprism/photoprism/internal/entity" "github.com/photoprism/photoprism/internal/form" "github.com/stretchr/testify/assert" + "testing" ) func TestLabels(t *testing.T) { @@ -37,7 +36,7 @@ func TestLabels(t *testing.T) { }) t.Run("search for favorites", func(t *testing.T) { - query := form.NewLabelSearch("Favorites:true") + query := form.NewLabelSearch("Favorites:true Count:15") result, err := Labels(query) if err != nil { @@ -70,6 +69,7 @@ func TestLabels(t *testing.T) { t.Fatal(err) } + t.Log(result) assert.LessOrEqual(t, 3, len(result)) }) @@ -80,4 +80,26 @@ func TestLabels(t *testing.T) { assert.Error(t, err, "unknown filter") assert.Empty(t, result) }) + + t.Run("search for ID", func(t *testing.T) { + f := form.LabelSearch{ + Query: "", + ID: "lt9k3pw1wowuy3c4", + Slug: "", + Name: "", + All: false, + Favorites: false, + Count: 0, + Offset: 0, + Order: "", + } + + result, err := Labels(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, "cake", result[0].LabelSlug) + }) } diff --git a/internal/query/photo_test.go b/internal/query/photo_test.go index f76d41349..ecbcd4cf5 100644 --- a/internal/query/photo_test.go +++ b/internal/query/photo_test.go @@ -9,7 +9,9 @@ import ( func TestPhotoByID(t *testing.T) { t.Run("photo found", func(t *testing.T) { result, err := PhotoByID(1000000) - assert.Nil(t, err) + if err != nil { + t.Fatal(err) + } assert.Equal(t, 2790, result.PhotoYear) }) @@ -23,7 +25,9 @@ func TestPhotoByID(t *testing.T) { func TestPhotoByUUID(t *testing.T) { t.Run("photo found", func(t *testing.T) { result, err := PhotoByUUID("pt9jtdre2lvl0y12") - assert.Nil(t, err) + if err != nil { + t.Fatal(err) + } assert.Equal(t, "Reunion", result.PhotoTitle) }) @@ -37,7 +41,9 @@ func TestPhotoByUUID(t *testing.T) { func TestPreloadPhotoByUUID(t *testing.T) { t.Run("photo found", func(t *testing.T) { result, err := PreloadPhotoByUUID("pt9jtdre2lvl0y12") - assert.Nil(t, err) + if err != nil { + t.Fatal(err) + } assert.Equal(t, "Reunion", result.PhotoTitle) }) @@ -47,3 +53,18 @@ func TestPreloadPhotoByUUID(t *testing.T) { t.Log(result) }) } + +func TestMissingPhotos(t *testing.T) { + r, err := MissingPhotos(15, 0) + if err != nil { + t.Fatal(err) + } + assert.LessOrEqual(t, 1, len(r)) +} + +func TestResetPhotosQuality(t *testing.T) { + err := ResetPhotosQuality() + if err != nil { + t.Fatal(err) + } +}