Tests: Add unit tests for internal/crop
|
@ -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) {
|
||||||
|
|
96
internal/crop/image_test.go
Normal 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)
|
||||||
|
})
|
||||||
|
}
|
15
internal/crop/names_test.go
Normal 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())
|
||||||
|
})
|
||||||
|
}
|
BIN
internal/crop/testdata/b/c/c/bccfeaa526a36e19b555fd4ca5e8f767d5604289_100x100_center.jpg
vendored
Normal file
After Width: | Height: | Size: 3 KiB |
BIN
internal/crop/testdata/b/c/c/bccfeaa526a36e19b555fd4ca5e8f767d5604289_160x160_crop_0680bc31420d.jpg
vendored
Normal file
After Width: | Height: | Size: 9.9 KiB |
BIN
internal/crop/testdata/b/c/c/bccfeaa526a36e19b555fd4ca5e8f767d5604289_224x224_center.jpg
vendored
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
internal/crop/testdata/b/c/c/bccfeaa526a36e19b555fd4ca5e8f767d5604289_224x224_left.jpg
vendored
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
internal/crop/testdata/b/c/c/bccfeaa526a36e19b555fd4ca5e8f767d5604289_224x224_right.jpg
vendored
Normal file
After Width: | Height: | Size: 12 KiB |
BIN
internal/crop/testdata/b/c/c/bccfeaa526a36e19b555fd4ca5e8f767d5604289_3x3_resize.png
vendored
Normal file
After Width: | Height: | Size: 101 B |
BIN
internal/crop/testdata/b/c/c/bccfeaa526a36e19b555fd4ca5e8f767d5604289_500x500_center.jpg
vendored
Normal file
After Width: | Height: | Size: 47 KiB |
BIN
internal/crop/testdata/b/c/c/bccfeaa526a36e19b555fd4ca5e8f767d5604289_50x50_center.jpg
vendored
Normal file
After Width: | Height: | Size: 1.5 KiB |
BIN
internal/crop/testdata/b/c/c/bccfeaa526a36e19b555fd4ca5e8f767d5604289_720x720_fit.jpg
vendored
Normal file
After Width: | Height: | Size: 62 KiB |