From 19699c49c0519b913b6642a7bcaedc386beb6fe2 Mon Sep 17 00:00:00 2001 From: graciousgrey Date: Wed, 28 Jun 2023 16:07:06 +0200 Subject: [PATCH] Tests: Add unit tests --- internal/form/search_photos_geo_test.go | 66 +++++++++++++++++++++++++ internal/form/search_photos_test.go | 25 ++++++++++ internal/form/user_login_test.go | 11 +++++ internal/form/user_test.go | 35 +++++++++++++ 4 files changed, 137 insertions(+) diff --git a/internal/form/search_photos_geo_test.go b/internal/form/search_photos_geo_test.go index 99704cd56..eb708bc4d 100644 --- a/internal/form/search_photos_geo_test.go +++ b/internal/form/search_photos_geo_test.go @@ -19,6 +19,18 @@ func TestSearchPhotosGeo(t *testing.T) { assert.Equal(t, "Jens Mander", form.Subjects) }) + t.Run("subject", func(t *testing.T) { + form := &SearchPhotosGeo{Query: "subject:\"Jens\""} + + err := form.ParseQueryString() + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, "Jens", form.Subject) + assert.Equal(t, "", form.Person) + }) t.Run("id", func(t *testing.T) { form := &SearchPhotosGeo{Query: "id:\"ii3e4567-e89b-hdgtr\""} @@ -56,6 +68,32 @@ func TestSearchPhotosGeo(t *testing.T) { assert.Equal(t, "Foo Bar", form.Keywords) }) + t.Run("path", func(t *testing.T) { + form := &SearchPhotosGeo{Query: "path:123abc/,EFG"} + + err := form.ParseQueryString() + + // log.Debugf("%+v\n", form) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, "123abc/,EFG", form.Path) + }) + t.Run("name", func(t *testing.T) { + form := &SearchPhotosGeo{Query: "name:filename.jpg"} + + err := form.ParseQueryString() + + // log.Debugf("%+v\n", form) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, "filename", form.Name) + }) t.Run("valid query", func(t *testing.T) { form := &SearchPhotosGeo{Query: "q:\"fooBar baz\" before:2019-01-15 dist:25000 lat:33.45343166666667"} @@ -90,6 +128,21 @@ func TestSearchPhotosGeo(t *testing.T) { assert.Equal(t, uint(0x61a8), form.Dist) assert.Equal(t, float32(33.45343), form.Lat) }) + t.Run("valid query with filter", func(t *testing.T) { + form := &SearchPhotosGeo{Query: "keywords:cat title:\"fooBar baz\"", Filter: "keywords:dog"} + + err := form.ParseQueryString() + + // log.Debugf("%+v\n", form) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, "dog", form.Keywords) + assert.Equal(t, "keywords:dog", form.Filter) + assert.Equal(t, "fooBar baz", form.Title) + }) t.Run("PortraitLandscapeSquare", func(t *testing.T) { form := &SearchPhotosGeo{Query: "portrait:true landscape:yes square:jo"} @@ -204,3 +257,16 @@ func TestNewSearchPhotosGeo(t *testing.T) { r := NewSearchPhotosGeo("Berlin") assert.IsType(t, SearchPhotosGeo{}, r) } + +func TestSearchPhotosGeo_FindUidOnly(t *testing.T) { + t.Run("true", func(t *testing.T) { + f := &SearchPhotosGeo{UID: "priqwb43p5dh7777"} + + assert.True(t, f.FindUidOnly()) + }) + t.Run("false", func(t *testing.T) { + f := &SearchPhotosGeo{Query: "label:cat", UID: "priqwb43p5dh7777"} + + assert.False(t, f.FindUidOnly()) + }) +} diff --git a/internal/form/search_photos_test.go b/internal/form/search_photos_test.go index 13613abba..767ea4e59 100644 --- a/internal/form/search_photos_test.go +++ b/internal/form/search_photos_test.go @@ -25,6 +25,18 @@ func TestParseQueryString(t *testing.T) { assert.Equal(t, "Jens & Mander", form.Subjects) }) + t.Run("subject", func(t *testing.T) { + form := &SearchPhotos{Query: "subject:\"Jens\""} + + err := form.ParseQueryString() + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, "Jens", form.Subject) + assert.Equal(t, "", form.Person) + }) t.Run("aliases", func(t *testing.T) { form := &SearchPhotos{Query: "people:\"Jens & Mander\" folder:Foo person:Bar"} @@ -786,3 +798,16 @@ func TestSearchPhotos_Unserialize(t *testing.T) { assert.Equal(t, "ariqwb43p5dh5555", f.Album) }) } + +func TestSearchPhotos_FindUidOnly(t *testing.T) { + t.Run("true", func(t *testing.T) { + f := &SearchPhotos{UID: "priqwb43p5dh7777"} + + assert.True(t, f.FindUidOnly()) + }) + t.Run("false", func(t *testing.T) { + f := &SearchPhotos{Query: "label:cat", UID: "priqwb43p5dh7777"} + + assert.False(t, f.FindUidOnly()) + }) +} diff --git a/internal/form/user_login_test.go b/internal/form/user_login_test.go index 49deb1e17..33e43d7b4 100644 --- a/internal/form/user_login_test.go +++ b/internal/form/user_login_test.go @@ -6,6 +6,17 @@ import ( "github.com/stretchr/testify/assert" ) +func TestLogin_Email(t *testing.T) { + t.Run("Empty", func(t *testing.T) { + form := &Login{UserEmail: "", Password: "passwd", AuthToken: ""} + assert.Equal(t, "", form.Email()) + }) + t.Run("valid", func(t *testing.T) { + form := &Login{UserEmail: "test@test.com", UserName: "John", Password: "passwd", AuthToken: "123"} + assert.Equal(t, "test@test.com", form.Email()) + }) +} + func TestLogin_HasToken(t *testing.T) { t.Run("false", func(t *testing.T) { form := &Login{UserEmail: "test@test.com", UserName: "John", Password: "passwd", AuthToken: ""} diff --git a/internal/form/user_test.go b/internal/form/user_test.go index 8e53d0786..80dbc85a0 100644 --- a/internal/form/user_test.go +++ b/internal/form/user_test.go @@ -3,6 +3,8 @@ package form import ( "testing" + "github.com/photoprism/photoprism/pkg/authn" + "github.com/stretchr/testify/assert" ) @@ -29,6 +31,17 @@ func TestUser_Username(t *testing.T) { }) } +func TestUser_Provider(t *testing.T) { + t.Run("Empty", func(t *testing.T) { + form := &User{UserName: "", UserEmail: "test@test.com", Password: "passwd", AuthProvider: ""} + assert.Equal(t, authn.ProviderDefault, form.Provider()) + }) + t.Run("Valid", func(t *testing.T) { + form := &User{UserName: "John", UserEmail: "test@test.com", Password: "passwd", AuthProvider: "local"} + assert.Equal(t, authn.ProviderLocal, form.Provider()) + }) +} + func TestUser_Email(t *testing.T) { t.Run("Empty", func(t *testing.T) { form := &User{UserName: "foobar", UserEmail: "", Password: "passwd"} @@ -43,3 +56,25 @@ func TestUser_Email(t *testing.T) { assert.Equal(t, "", form.Email()) }) } + +func TestUser_Role(t *testing.T) { + t.Run("Empty", func(t *testing.T) { + form := &User{UserName: "", UserEmail: "test@test.com", Password: "passwd", UserRole: ""} + assert.Equal(t, "", form.Role()) + }) + t.Run("Valid", func(t *testing.T) { + form := &User{UserName: "John", UserEmail: "test@test.com", Password: "passwd", UserRole: "admin"} + assert.Equal(t, "admin", form.Role()) + }) + t.Run("Invalid", func(t *testing.T) { + form := &User{UserName: "John", UserEmail: "test@test.com", Password: "passwd", UserRole: "ad&*min"} + assert.Equal(t, "admin", form.Role()) + }) +} + +func TestUser_Attr(t *testing.T) { + t.Run("Empty", func(t *testing.T) { + form := &User{UserName: "", UserEmail: "test@test.com", Password: "passwd", UserAttr: ""} + assert.Equal(t, "", form.Attr()) + }) +}