From f58ca6dcdab74badff131677af2e369334d5875c Mon Sep 17 00:00:00 2001 From: Theresa Gresch Date: Tue, 7 Jul 2020 18:03:20 +0200 Subject: [PATCH] Backend: Add unit tests for internal/form --- internal/form/folder_search_test.go | 110 ++++++++++++++++++++++++++++ internal/form/folder_test.go | 62 ++++++++++++++++ pkg/txt/convert_test.go | 2 +- 3 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 internal/form/folder_search_test.go create mode 100644 internal/form/folder_test.go diff --git a/internal/form/folder_search_test.go b/internal/form/folder_search_test.go new file mode 100644 index 000000000..5e8cbb07f --- /dev/null +++ b/internal/form/folder_search_test.go @@ -0,0 +1,110 @@ +package form + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFolderSearchForm(t *testing.T) { + form := &FolderSearch{} + + assert.IsType(t, new(FolderSearch), form) +} + +func TestFolderSearch_GetQuery(t *testing.T) { + form := &FolderSearch{Query: "test"} + + assert.Equal(t, "test", form.GetQuery()) +} + +func TestFolderSearch_SetQuery(t *testing.T) { + form := &FolderSearch{Query: "test"} + form.SetQuery("new query") + + assert.Equal(t, "new query", form.GetQuery()) +} + +func TestFolderSearch_Serialize(t *testing.T) { + form := &FolderSearch{Query: "test", Files: true} + + assert.Equal(t, "q:test files:true", form.Serialize()) +} + +func TestFolderSearch_SerializeAll(t *testing.T) { + form := &FolderSearch{Query: "test", Files: true} + + assert.Equal(t, "q:test files:true", form.SerializeAll()) +} + +func TestParseQueryStringFolder(t *testing.T) { + + t.Run("valid query", func(t *testing.T) { + form := &FolderSearch{Query: "uncached:false files:true recursive:true count:10 offset:1"} + + err := form.ParseQueryString() + + log.Debugf("%+v\n", form) + + if err != nil { + t.Fatal("err should be nil") + } + + assert.Equal(t, false, form.Uncached) + assert.Equal(t, true, form.Recursive) + assert.Equal(t, true, form.Files) + assert.Equal(t, 10, form.Count) + assert.Equal(t, 1, form.Offset) + + }) + t.Run("valid query with umlauts", func(t *testing.T) { + form := &FolderSearch{Query: "query:\"tübingen\""} + + err := form.ParseQueryString() + + log.Debugf("%+v\n", form) + + if err != nil { + t.Fatal("err should be nil") + } + + assert.Equal(t, "tübingen", form.Query) + }) + t.Run("query for invalid filter", func(t *testing.T) { + form := &FolderSearch{Query: "xxx:false"} + + err := form.ParseQueryString() + + if err == nil { + t.FailNow() + } + + log.Debugf("%+v\n", form) + + assert.Equal(t, "unknown filter: Xxx", err.Error()) + }) + t.Run("query for recursive with uncommon bool value", func(t *testing.T) { + form := &FolderSearch{Query: "recursive:cat"} + + err := form.ParseQueryString() + + if err != nil { + t.Fatal("err should be nil") + } + + assert.True(t, form.Recursive) + }) + t.Run("query for count with invalid type", func(t *testing.T) { + form := &FolderSearch{Query: "count:cat"} + + err := form.ParseQueryString() + + if err == nil { + t.FailNow() + } + + log.Debugf("%+v\n", form) + + assert.Equal(t, "strconv.Atoi: parsing \"cat\": invalid syntax", err.Error()) + }) +} diff --git a/internal/form/folder_test.go b/internal/form/folder_test.go new file mode 100644 index 000000000..5814b23ff --- /dev/null +++ b/internal/form/folder_test.go @@ -0,0 +1,62 @@ +package form + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestNewFolder(t *testing.T) { + t.Run("success", func(t *testing.T) { + var folder = struct { + Path string + Root string + FolderType string + FolderTitle string + FolderCategory string + FolderDescription string + FolderOrder string + FolderCountry string + FolderYear int + FolderMonth int + FolderFavorite bool + FolderPrivate bool + FolderIgnore bool + FolderWatch bool + }{Path: "HD/2011/11-WG-Party", + Root: "", + FolderType: "", + FolderTitle: "testTitle", + FolderCategory: "family", + FolderDescription: "", + FolderOrder: "name", + FolderCountry: "de", + FolderYear: 2020, + FolderMonth: 07, + FolderFavorite: false, + FolderPrivate: false, + FolderIgnore: false, + FolderWatch: false, + } + + r, err := NewFolder(folder) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, "HD/2011/11-WG-Party", r.Path) + assert.Equal(t, "", r.Root) + assert.Equal(t, "", r.FolderType) + assert.Equal(t, "testTitle", r.FolderTitle) + assert.Equal(t, "family", r.FolderCategory) + assert.Equal(t, "", r.FolderDescription) + assert.Equal(t, "name", r.FolderOrder) + assert.Equal(t, "de", r.FolderCountry) + assert.Equal(t, 2020, r.FolderYear) + assert.Equal(t, false, r.FolderPrivate) + assert.Equal(t, 07, r.FolderMonth) + assert.Equal(t, false, r.FolderFavorite) + assert.Equal(t, false, r.FolderIgnore) + assert.Equal(t, false, r.FolderWatch) + }) +} diff --git a/pkg/txt/convert_test.go b/pkg/txt/convert_test.go index 2949581d8..4a3696f39 100644 --- a/pkg/txt/convert_test.go +++ b/pkg/txt/convert_test.go @@ -179,7 +179,7 @@ func TestTime(t *testing.T) { assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", result.String()) }) t.Run("hour > hourmax", func(t *testing.T) { - result := Time("1020-01-30_25-57-18.jpg") + result := Time("2020-01-30_25-57-18.jpg") assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", result.String()) }) t.Run("invalid days", func(t *testing.T) {