From e1ddaa7c0b74c7a14c5217f17d44a8530564722d Mon Sep 17 00:00:00 2001 From: graciousgrey Date: Tue, 27 Jun 2023 18:41:36 +0200 Subject: [PATCH] Tests: Add unit tests --- pkg/fs/blacklist_test.go | 50 ++++++++++++++++++++++++++++++++++++++++ pkg/fs/file_type_test.go | 16 +++++++++++++ pkg/fs/fileinfo_test.go | 14 +++++++++++ pkg/fs/fs_test.go | 14 ++++++++--- 4 files changed, 91 insertions(+), 3 deletions(-) diff --git a/pkg/fs/blacklist_test.go b/pkg/fs/blacklist_test.go index 4cb63e5c3..4e9f36860 100644 --- a/pkg/fs/blacklist_test.go +++ b/pkg/fs/blacklist_test.go @@ -47,3 +47,53 @@ func TestBlacklist_Ok(t *testing.T) { assert.True(t, list.Allow("raw")) }) } + +func TestBlacklist_Contains(t *testing.T) { + t.Run("DNG", func(t *testing.T) { + list := NewBlacklist("dng") + assert.True(t, list.Contains("dng")) + assert.False(t, list.Contains("cr2")) + }) + t.Run("Empty", func(t *testing.T) { + list := NewBlacklist("") + assert.False(t, list.Contains("")) + }) +} + +func TestBlacklist_Set(t *testing.T) { + t.Run("DNG, CR2", func(t *testing.T) { + list := NewBlacklist("dng") + assert.True(t, list.Contains("dng")) + assert.False(t, list.Contains("cr2")) + list.Set("cr2") + assert.True(t, list.Contains("dng")) + assert.True(t, list.Contains("cr2")) + }) + t.Run("DNG", func(t *testing.T) { + list := NewBlacklist("dng") + assert.True(t, list.Contains("dng")) + assert.False(t, list.Contains("cr2")) + list.Set("") + assert.True(t, list.Contains("dng")) + assert.False(t, list.Contains("cr2")) + }) +} + +func TestBlacklist_Add(t *testing.T) { + t.Run("DNG, CR2", func(t *testing.T) { + list := NewBlacklist("dng") + assert.True(t, list.Contains("dng")) + assert.False(t, list.Contains("cr2")) + list.Add("cr2") + assert.True(t, list.Contains("dng")) + assert.True(t, list.Contains("cr2")) + }) + t.Run("DNG", func(t *testing.T) { + list := NewBlacklist("dng") + assert.True(t, list.Contains("dng")) + assert.False(t, list.Contains("cr2")) + list.Add("") + assert.True(t, list.Contains("dng")) + assert.False(t, list.Contains("cr2")) + }) +} diff --git a/pkg/fs/file_type_test.go b/pkg/fs/file_type_test.go index 5fec44e8e..399aa81c7 100644 --- a/pkg/fs/file_type_test.go +++ b/pkg/fs/file_type_test.go @@ -18,6 +18,22 @@ func TestType_Equal(t *testing.T) { }) } +func TestType_NotEqual(t *testing.T) { + t.Run("jpg", func(t *testing.T) { + assert.False(t, ImageJPEG.NotEqual("JPG")) + assert.True(t, ImageJPEG.NotEqual("xmp")) + }) +} + +func TestType_DefaultExt(t *testing.T) { + t.Run("jpg", func(t *testing.T) { + assert.Equal(t, ".jpg", ImageJPEG.DefaultExt()) + }) + t.Run("avif", func(t *testing.T) { + assert.Equal(t, ".avif", ImageAVIF.DefaultExt()) + }) +} + func TestToType(t *testing.T) { t.Run("jpg", func(t *testing.T) { assert.Equal(t, "jpg", NewType("JPG").String()) diff --git a/pkg/fs/fileinfo_test.go b/pkg/fs/fileinfo_test.go index d35470ff3..0d9584db4 100644 --- a/pkg/fs/fileinfo_test.go +++ b/pkg/fs/fileinfo_test.go @@ -1,6 +1,7 @@ package fs import ( + "github.com/emersion/go-webdav" "os" "testing" "time" @@ -72,6 +73,19 @@ func TestNewFileInfos(t *testing.T) { } } +func TestWebFileInfo(t *testing.T) { + + infos := webdav.FileInfo{Path: "testdata", Size: 3, ModTime: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), IsDir: true, MIMEType: "image"} + + result := WebFileInfo(infos, PathSeparator) + + assert.Equal(t, "testdata", result.Name) + assert.Equal(t, "/testdata", result.Abs) + assert.Equal(t, int64(3), result.Size) + assert.Equal(t, "2020-01-01 00:00:00 +0000 UTC", result.Date.String()) + assert.Equal(t, true, result.Dir) +} + func TestFileInfos_Less(t *testing.T) { infos := FileInfos{ {Name: "test.jpg", Abs: "/test.jpg", Size: 10990, Dir: false}, diff --git a/pkg/fs/fs_test.go b/pkg/fs/fs_test.go index 5238f5e40..e886282a7 100644 --- a/pkg/fs/fs_test.go +++ b/pkg/fs/fs_test.go @@ -41,12 +41,20 @@ func TestPathExists(t *testing.T) { assert.True(t, PathExists("./testdata")) assert.False(t, PathExists("./testdata/test.jpg")) assert.False(t, PathExists("./testdata3ggdtgdg")) + assert.False(t, PathExists("")) } func TestPathWritable(t *testing.T) { - assert.True(t, PathExists("./testdata")) - assert.False(t, PathExists("./testdata/test.jpg")) - assert.False(t, PathExists("./testdata3ggdtgdg")) + assert.True(t, PathWritable("./testdata")) + assert.False(t, PathWritable("./testdata/test.jpg")) + assert.False(t, PathWritable("./testdata3ggdtgdg")) + assert.False(t, PathWritable("")) +} + +func TestWritable(t *testing.T) { + assert.True(t, Writable("./testdata")) + assert.False(t, Writable("./testdata3ggdtgdg")) + assert.False(t, Writable("")) } func TestOverwrite(t *testing.T) {