From d3fb4201853afedf6fda80ae3111617586bbe304 Mon Sep 17 00:00:00 2001 From: theresa Date: Tue, 14 Feb 2023 19:14:03 +0100 Subject: [PATCH] Tests: Add unit tests for new search filter #3035 --- internal/form/search_photos_geo_test.go | 11 + internal/form/search_photos_test.go | 11 + internal/search/photos_filter_id_test.go | 588 +++++++++++++++++++++++ 3 files changed, 610 insertions(+) create mode 100644 internal/search/photos_filter_id_test.go diff --git a/internal/form/search_photos_geo_test.go b/internal/form/search_photos_geo_test.go index 1f5ba7558..99704cd56 100644 --- a/internal/form/search_photos_geo_test.go +++ b/internal/form/search_photos_geo_test.go @@ -19,6 +19,17 @@ func TestSearchPhotosGeo(t *testing.T) { assert.Equal(t, "Jens Mander", form.Subjects) }) + t.Run("id", func(t *testing.T) { + form := &SearchPhotosGeo{Query: "id:\"ii3e4567-e89b-hdgtr\""} + + err := form.ParseQueryString() + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, "ii3e4567-e89b-hdgtr", form.ID) + }) t.Run("aliases", func(t *testing.T) { form := &SearchPhotosGeo{Query: "people:\"Jens & Mander\" folder:Foo person:Bar"} diff --git a/internal/form/search_photos_test.go b/internal/form/search_photos_test.go index 5c636482a..13613abba 100644 --- a/internal/form/search_photos_test.go +++ b/internal/form/search_photos_test.go @@ -662,6 +662,17 @@ func TestParseQueryString(t *testing.T) { assert.Equal(t, "Could not find format for \"cat\"", err.Error()) }) + t.Run("id", func(t *testing.T) { + form := &SearchPhotos{Query: "id:\"ii3e4567-e89b-hdgtr\""} + + err := form.ParseQueryString() + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, "ii3e4567-e89b-hdgtr", form.ID) + }) } func TestNewSearchPhotos(t *testing.T) { diff --git a/internal/search/photos_filter_id_test.go b/internal/search/photos_filter_id_test.go new file mode 100644 index 000000000..14a66e7bb --- /dev/null +++ b/internal/search/photos_filter_id_test.go @@ -0,0 +1,588 @@ +package search + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/photoprism/photoprism/internal/form" +) + +func TestPhotosFilterId(t *testing.T) { + t.Run("a698ac56-6e7e-42b9-9c3e-a79ec96087uy", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "a698ac56-6e7e-42b9-9c3e-a79ec96087uy" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + assert.Equal(t, len(photos), 1) + }) + t.Run("a698ac56-6e7e-42b9-9c3e*", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "a698ac56-6e7e-42b9-9c3e*" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("StartsWithPercent", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "%gold" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("CenterPercent", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "I love % dog" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("EndsWithPercent", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "sale%" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("StartsWithAmpersand", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "&IlikeFood" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("CenterAmpersand", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "Pets & Dogs" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("EndsWithAmpersand", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "Light&" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("StartsWithSingleQuote", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "'Family" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("CenterSingleQuote", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "Father's uid" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("EndsWithSingleQuote", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "Ice Cream'" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("StartsWithAsterisk", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "*Forrest" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("CenterAsterisk", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "My*Kids" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("EndsWithAsterisk", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "Yoga***" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("StartsWithPipe", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "|Banana" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("CenterPipe", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "Red|Green" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("EndsWithPipe", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "Blue|" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("StartsWithNumber", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "345 Shirt" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("CenterNumber", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "uid555 Blue" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("EndsWithNumber", func(t *testing.T) { + var f form.SearchPhotos + + f.ID = "Route 66" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) +} + +func TestPhotosQueryId(t *testing.T) { + t.Run("PhotoUID", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:a698ac56-6e7e-42b9-9c3e-a79ec96087uy" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 1) + }) + t.Run("QuotedPhotoUID", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"a698ac56-6e7e-42b9-9c3e-a79ec96087uy\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 1) + }) + t.Run("pt9jtdre2lvl0yh*", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"a698ac56-6e7e-42b9-9c3e-a7*\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("StartsWithPercent", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"%gold\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("CenterPercent", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"I love % dog\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("EndsWithPercent", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"sale%\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("StartsWithAmpersand", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"&IlikeFood\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("CenterAmpersand", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"Pets & Dogs\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("EndsWithAmpersand", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"Light&\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("StartsWithSingleQuote", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"'Family\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("CenterSingleQuote", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"Father's uid\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("EndsWithSingleQuote", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"Ice Cream'\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("StartsWithAsterisk", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"*Forrest\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("CenterAsterisk", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"My*Kids\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("EndsWithAsterisk", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"Yoga***\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("StartsWithPipe", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"|Banana\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("CenterPipe", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"Red|Green\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("EndsWithPipe", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"Blue|\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("StartsWithNumber", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"345 Shirt\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("CenterNumber", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"uid555 Blue\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) + t.Run("EndsWithNumber", func(t *testing.T) { + var f form.SearchPhotos + + f.Query = "id:\"Route 66\"" + f.Merged = true + + photos, _, err := Photos(f) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, len(photos), 0) + }) +}