Tests: Add unit tests for internal/crop

This commit is contained in:
theresa 2021-11-17 17:28:26 +01:00
parent 842a6981a7
commit 44c8e68c8e
12 changed files with 124 additions and 0 deletions

View file

@ -71,6 +71,17 @@ func TestArea_Thumb(t *testing.T) {
}) })
} }
func TestArea_FileWidth(t *testing.T) {
t.Run("Tile50", func(t *testing.T) {
m := NewArea("face", 1.000, 0.33333, 0.001, 0.5)
assert.Equal(t, 49999, m.FileWidth(Size{Tile50, Tile320, "Lists", 50, 50, DefaultOptions}))
})
t.Run("Tile500", func(t *testing.T) {
m := NewArea("face", 1.000, 0.33333, 0.001, 0.5)
assert.Equal(t, 499999, m.FileWidth(Size{Tile500, "", "FaceNet", 500, 500, DefaultOptions}))
})
}
func TestAreaFromString(t *testing.T) { func TestAreaFromString(t *testing.T) {
t.Run("3e814d0011f4", func(t *testing.T) { t.Run("3e814d0011f4", func(t *testing.T) {
a := AreaFromString("3e814d0011f4") a := AreaFromString("3e814d0011f4")
@ -143,6 +154,7 @@ func TestArea_SurfaceRatio(t *testing.T) {
var a2 = Area{Name: "face", X: 0.208313, Y: 0.156914, W: 0.655556, H: 0.655556} var a2 = Area{Name: "face", X: 0.208313, Y: 0.156914, W: 0.655556, H: 0.655556}
var a3 = Area{Name: "face", X: 0.998133, Y: 0.816944, W: 0.0001, H: 0.0001} var a3 = Area{Name: "face", X: 0.998133, Y: 0.816944, W: 0.0001, H: 0.0001}
var a4 = Area{Name: "face", X: 0.298133, Y: 0.216944, W: 0.255556, H: 0.155556} var a4 = Area{Name: "face", X: 0.298133, Y: 0.216944, W: 0.255556, H: 0.155556}
var a5 = Area{Name: "face", X: 0.298133, Y: 0.216944, W: 0, H: 0}
assert.Equal(t, 99, int(a1.SurfaceRatio(a1.OverlapArea(a1))*100)) assert.Equal(t, 99, int(a1.SurfaceRatio(a1.OverlapArea(a1))*100))
assert.Equal(t, 99, int(a1.SurfaceRatio(a1.OverlapArea(a2))*100)) assert.Equal(t, 99, int(a1.SurfaceRatio(a1.OverlapArea(a2))*100))
@ -151,6 +163,7 @@ func TestArea_SurfaceRatio(t *testing.T) {
assert.Equal(t, 30, int(a1.SurfaceRatio(a1.OverlapArea(a4))*100)) assert.Equal(t, 30, int(a1.SurfaceRatio(a1.OverlapArea(a4))*100))
assert.Equal(t, 0, int(a1.SurfaceRatio(a3.OverlapArea(a1))*100)) assert.Equal(t, 0, int(a1.SurfaceRatio(a3.OverlapArea(a1))*100))
assert.Equal(t, 30, int(a1.SurfaceRatio(a4.OverlapArea(a1))*100)) assert.Equal(t, 30, int(a1.SurfaceRatio(a4.OverlapArea(a1))*100))
assert.Equal(t, 0, int(a1.SurfaceRatio(a5.OverlapArea(a1))*100))
} }
func TestArea_OverlapArea(t *testing.T) { func TestArea_OverlapArea(t *testing.T) {

View file

@ -0,0 +1,96 @@
package crop
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestThumbFileName(t *testing.T) {
t.Run("Invalid hash", func(t *testing.T) {
a := NewArea("face", 1.000, 0.33333, 0.001, 0.5)
s := Size{Tile50, Tile320, "Lists", 50, 50, DefaultOptions}
_, err := ThumbFileName("xxx", a, s, "path/b")
if err == nil {
t.Fatal(err)
}
assert.Contains(t, err.Error(), "invalid file hash")
})
t.Run("Path missing", func(t *testing.T) {
a := NewArea("face", 1.000, 0.33333, 0.001, 0.5)
s := Size{Tile50, Tile320, "Lists", 50, 50, DefaultOptions}
_, err := ThumbFileName("2105662d3f8d6e68d9e94280449fbf26ed89xxxx", a, s, "")
if err == nil {
t.Fatal(err)
}
assert.Contains(t, err.Error(), "path missing")
})
t.Run("File not found", func(t *testing.T) {
a := NewArea("face", 1.000, 0.33333, 0.001, 0.5)
s := Size{Tile50, Tile320, "Lists", 50, 50, DefaultOptions}
_, err := ThumbFileName("2105662d3f8d6e68d9e94280449fbf26ed89xxxx", a, s, "path/b")
if err == nil {
t.Fatal(err)
}
assert.Contains(t, err.Error(), "not found")
})
t.Run("File exists", func(t *testing.T) {
a := NewArea("face", 1.000, 0.33333, 0.001, 0.5)
s := Size{Tile500, "", "FaceNet", 500, 500, DefaultOptions}
r, err := ThumbFileName("bccfeaa526a36e19b555fd4ca5e8f767d5604289", a, s, "./testdata")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "testdata/b/c/c/bccfeaa526a36e19b555fd4ca5e8f767d5604289_720x720_fit.jpg", r)
})
}
func TestFileWidth(t *testing.T) {
t.Run("Tile50", func(t *testing.T) {
a := NewArea("face", 1.000, 0.33333, 0.001, 0.5)
assert.Equal(t, 49999, FileWidth(a, Size{Tile50, Tile320, "Lists", 50, 50, DefaultOptions}))
})
t.Run("Tile500", func(t *testing.T) {
a := NewArea("face", 1.000, 0.33333, 0.001, 0.5)
assert.Equal(t, 499999, FileWidth(a, Size{Tile500, "", "FaceNet", 500, 500, DefaultOptions}))
})
}
func TestThumbHash(t *testing.T) {
t.Run("valid filename", func(t *testing.T) {
assert.Equal(t, "23b05bc917a5aa61382210cedafc162dd3517dc0", thumbHash("23b05bc917a5aa61382210cedafc162dd3517dc0_2048x2048_fit.jpg"))
})
t.Run("empty filename", func(t *testing.T) {
assert.Equal(t, "", thumbHash(""))
})
}
func TestFindIdealThumbFileName(t *testing.T) {
t.Run("hash empty", func(t *testing.T) {
r := findIdealThumbFileName("", 500, "path/b")
assert.Equal(t, "", r)
})
t.Run("path empty", func(t *testing.T) {
r := findIdealThumbFileName("2105662d3f8d6e68d9e94280449fbf26ed89xxxx", 500, "")
assert.Equal(t, "", r)
})
t.Run("file does not exist", func(t *testing.T) {
r := findIdealThumbFileName("2105662d3f8d6e68d9e94280449fbf26ed89xxxx", 500, "path/b")
assert.Equal(t, "", r)
})
t.Run("width: 500", func(t *testing.T) {
r := findIdealThumbFileName("bccfeaa526a36e19b555fd4ca5e8f767d5604289", 500, "./testdata/b/c/c")
assert.Equal(t, "testdata/b/c/c/bccfeaa526a36e19b555fd4ca5e8f767d5604289_720x720_fit.jpg", r)
})
t.Run("width: 720", func(t *testing.T) {
r := findIdealThumbFileName("bccfeaa526a36e19b555fd4ca5e8f767d5604289", 720, "./testdata/b/c/c")
assert.Equal(t, "testdata/b/c/c/bccfeaa526a36e19b555fd4ca5e8f767d5604289_720x720_fit.jpg", r)
})
t.Run("width: 800", func(t *testing.T) {
r := findIdealThumbFileName("bccfeaa526a36e19b555fd4ca5e8f767d5604289", 800, "./testdata/b/c/c")
assert.Equal(t, "testdata/b/c/c/bccfeaa526a36e19b555fd4ca5e8f767d5604289_720x720_fit.jpg", r)
})
t.Run("width: 60", func(t *testing.T) {
r := findIdealThumbFileName("bccfeaa526a36e19b555fd4ca5e8f767d5604289", 60, "./testdata/b/c/c")
assert.Equal(t, "testdata/b/c/c/bccfeaa526a36e19b555fd4ca5e8f767d5604289_720x720_fit.jpg", r)
})
}

View file

@ -0,0 +1,15 @@
package crop
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestName_Jpeg(t *testing.T) {
t.Run("Tile320", func(t *testing.T) {
assert.Equal(t, "tile_320.jpg", Tile320.Jpeg())
})
t.Run("Tile50", func(t *testing.T) {
assert.Equal(t, "tile_50.jpg", Tile50.Jpeg())
})
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 101 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB