Backend: Add unit tests for internal/form
This commit is contained in:
parent
c71c232107
commit
f58ca6dcda
3 changed files with 173 additions and 1 deletions
110
internal/form/folder_search_test.go
Normal file
110
internal/form/folder_search_test.go
Normal file
|
@ -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())
|
||||
})
|
||||
}
|
62
internal/form/folder_test.go
Normal file
62
internal/form/folder_test.go
Normal file
|
@ -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)
|
||||
})
|
||||
}
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue