Backend: Add unit tests for pkg/fs

This commit is contained in:
Theresa Gresch 2020-07-07 15:13:42 +02:00
parent bf08157ebf
commit e74de4716e
10 changed files with 118 additions and 2 deletions

View file

@ -115,6 +115,11 @@ func TestRelBase(t *testing.T) {
result := RelBase("/testdata/foo/Test (3).jpg", "/testdata", false)
assert.Equal(t, "foo/Test (3)", result)
})
t.Run("Test (3).jpg", func(t *testing.T) {
result := RelBase("/testdata/foo/Test (3).jpg", "/testdata/foo/Test (3).jpg", false)
assert.Equal(t, "Test (3)", result)
})
}
func TestBaseAbs(t *testing.T) {

View file

@ -12,10 +12,10 @@ func TestNonCanonical(t *testing.T) {
assert.Equal(t, true, NonCanonical("short"))
})
t.Run("short_", func(t *testing.T) {
assert.Equal(t, true, NonCanonical("short_"))
assert.Equal(t, true, NonCanonical("short/short/short/test1_"))
})
t.Run("short?", func(t *testing.T) {
assert.Equal(t, true, NonCanonical("short?"))
assert.Equal(t, true, NonCanonical("short/short/short/test1?"))
})
t.Run("short/test_test_test1234", func(t *testing.T) {
assert.Equal(t, false, NonCanonical("hort/test_test_test12345"))

View file

@ -31,3 +31,16 @@ func TestDirs(t *testing.T) {
assert.Equal(t, expected, result)
})
}
func TestFindDirs(t *testing.T) {
t.Run("/directory", func(t *testing.T) {
result := FindDir([]string{"/directory", "/directory/subdirectory", "/linked"})
assert.Equal(t, "", result)
})
t.Run("./testdata", func(t *testing.T) {
result := FindDir([]string{"./testdata"})
assert.Equal(t, "/go/src/github.com/photoprism/photoprism/pkg/fs/testdata", result)
})
}

View file

@ -92,3 +92,25 @@ func TestFileInfos_Swap(t *testing.T) {
assert.Equal(t, "CATYELLOW.jpg", infos[0].Name)
assert.Equal(t, "test.jpg", infos[1].Name)
}
func TestFileInfos_Len(t *testing.T) {
infos := FileInfos{
{Name: "test.jpg", Abs: "/test.jpg", Size: 10990, Dir: false},
{Name: "CATYELLOW.jpg", Abs: "/CATYELLOW.jpg", Size: 70790, Dir: false},
{Name: "directory", Abs: "/directory", Size: 256, Dir: true},
{Name: "linked", Abs: "/linked", Size: 256, Dir: true},
}
assert.Equal(t, 4, infos.Len())
}
func TestFileInfos_Abs(t *testing.T) {
infos := FileInfos{
{Name: "test.jpg", Abs: "/test.jpg", Size: 10990, Dir: false},
{Name: "CATYELLOW.jpg", Abs: "/CATYELLOW.jpg", Size: 70790, Dir: false},
{Name: "directory", Abs: "/directory", Size: 256, Dir: true},
{Name: "linked", Abs: "/linked", Size: 256, Dir: true},
}
assert.Equal(t, []string{"/test.jpg", "/CATYELLOW.jpg", "/directory", "/linked"}, infos.Abs())
}

View file

@ -11,6 +11,7 @@ import (
func TestFileExists(t *testing.T) {
assert.True(t, FileExists("./testdata/test.jpg"))
assert.False(t, FileExists("./foo.jpg"))
assert.False(t, FileExists(""))
}
func TestPathExists(t *testing.T) {

View file

@ -27,3 +27,13 @@ func TestChecksum(t *testing.T) {
assert.Equal(t, "", hash)
})
}
func TestIsHash(t *testing.T) {
t.Run("false", func(t *testing.T) {
assert.Equal(t, false, IsHash(""))
})
t.Run("true", func(t *testing.T) {
assert.Equal(t, true, IsHash("516cb1fefbfd9fa66f1db50b94503a480cee30db"))
})
}

View file

@ -51,3 +51,8 @@ func TestIsID(t *testing.T) {
assert.False(t, IsID("01 Introduction Businessmodel.pdf"))
assert.False(t, IsID("A regular file name with 121345678643 numbers"))
}
func TestIsInt(t *testing.T) {
assert.True(t, IsInt("123"))
assert.False(t, IsInt(""))
}

View file

@ -121,3 +121,49 @@ func TestIgnoreList_Ignored(t *testing.T) {
assert.Equal(t, 0, len(ignore.Ignored()))
})
}
func TestNewIgnoreItem(t *testing.T) {
t.Run("case sensitive false", func(t *testing.T) {
ignore := NewIgnoreItem("testdata/directory", "Test_", false)
assert.Equal(t, "test_", ignore.Pattern)
})
t.Run("case sensitive true", func(t *testing.T) {
ignore := NewIgnoreItem("testdata/directory", "Test_", true)
assert.Equal(t, "Test_", ignore.Pattern)
})
}
func TestIgnoreList_AppendItems(t *testing.T) {
t.Run("error", func(t *testing.T) {
ignoreList := NewIgnoreList(".xyz", false, false)
assert.Error(t, ignoreList.AppendItems("", []string{"__test_"}))
})
t.Run("success", func(t *testing.T) {
ignoreList := NewIgnoreList(".xyz", false, false)
assert.Nil(t, ignoreList.AppendItems("testdata/directory", []string{"__test_"}))
})
}
func TestIgnoreList_Dir(t *testing.T) {
t.Run("error empty directory name", func(t *testing.T) {
ignoreList := NewIgnoreList(".xyz", false, false)
assert.Error(t, ignoreList.Dir(""))
})
t.Run("error empty ignore file name", func(t *testing.T) {
ignoreList := NewIgnoreList("", false, false)
assert.Error(t, ignoreList.Dir("testdata"))
})
t.Run("no file found", func(t *testing.T) {
ignoreList := NewIgnoreList(".xyz", false, false)
assert.Error(t, ignoreList.Dir("./testdata"))
})
t.Run("nil", func(t *testing.T) {
ignoreList := NewIgnoreList(".ppignore", false, false)
assert.Nil(t, ignoreList.Dir("./testdata/directory"))
})
}

View file

@ -31,4 +31,9 @@ func TestGetMediaType(t *testing.T) {
result := GetMediaType("")
assert.Equal(t, MediaOther, result)
})
t.Run("invalid type", func(t *testing.T) {
result := GetMediaType("/IMG_4120.XXX")
assert.Equal(t, MediaOther, result)
})
}

View file

@ -27,6 +27,9 @@ func TestRel(t *testing.T) {
t.Run("/some/path/bar", func(t *testing.T) {
assert.Equal(t, "/some/path/foo/bar.baz", Rel("/some/path/foo/bar.baz", "/some/path/bar"))
})
t.Run("empty dir", func(t *testing.T) {
assert.Equal(t, "/some/path/foo/bar.baz", Rel("/some/path/foo/bar.baz", ""))
})
}
func TestFileName(t *testing.T) {
@ -57,4 +60,10 @@ func TestFileName(t *testing.T) {
assert.Equal(t, tempDir+"/Test.xmp", result)
})
t.Run("empty dir", func(t *testing.T) {
result := FileName("testdata/FOO.XMP", "", Abs("testdata"), ".jpeg", true)
assert.Equal(t, "testdata/FOO.jpeg", result)
})
}