images package tidy up

This commit is contained in:
Samuel Hawksby-Robinson 2020-10-27 14:42:42 +00:00 committed by Andrea Maria Piana
parent 7ea153bae1
commit a7358fcd3f
9 changed files with 123 additions and 123 deletions

View File

@ -12,12 +12,12 @@ type Database struct {
}
type IdentityImage struct {
Type string `json:"type"`
Type string
Payload []byte
Width int `json:"width"`
Height int `json:"height"`
FileSize int `json:"file_size"`
ResizeTarget int `json:"resize_target"`
Width int
Height int
FileSize int
ResizeTarget int
}
func NewDatabase(db *sql.DB) Database {

View File

@ -12,100 +12,10 @@ import (
"github.com/stretchr/testify/require"
)
func setupTestDB(t *testing.T) (Database, func()) {
tmpfile, err := ioutil.TempFile("", "images-tests-")
require.NoError(t, err)
db, err := appdatabase.InitializeDB(tmpfile.Name(), "images-tests")
require.NoError(t, err)
return NewDatabase(db), func() {
require.NoError(t, db.Close())
require.NoError(t, os.Remove(tmpfile.Name()))
}
}
func seedTestDB(t *testing.T, db Database) {
iis := []IdentityImage{
{
Type: "thumbnail",
Payload: testJpegBytes,
Width: 80,
Height: 80,
FileSize: 256,
ResizeTarget: 80,
},
{
Type: "large",
Payload: testPngBytes,
Width: 240,
Height: 300,
FileSize: 1024,
ResizeTarget: 240,
},
}
require.NoError(t, db.StoreIdentityImages(iis))
}
func TestDatabase_GetIdentityImages(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
seedTestDB(t, db)
expected := `[{"type":"large","uri":"data:image/png;base64,iVBORw0KGgoAAAANSUg=","width":240,"height":300,"file_size":1024,"resize_target":240},{"type":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"file_size":256,"resize_target":80}]`
oiis, err := db.GetIdentityImages()
require.NoError(t, err)
joiis, err := json.Marshal(oiis)
require.NoError(t, err)
require.Exactly(t, expected, string(joiis))
}
func TestDatabase_GetIdentityImage(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
seedTestDB(t, db)
cs := []struct{
Name string
Expected string
}{
{
"thumbnail",
`{"type":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"file_size":256,"resize_target":80}`,
},
{
"large",
`{"type":"large","uri":"data:image/png;base64,iVBORw0KGgoAAAANSUg=","width":240,"height":300,"file_size":1024,"resize_target":240}`,
},
}
for _, c := range cs {
oii, err := db.GetIdentityImage(c.Name)
require.NoError(t, err)
joii, err := json.Marshal(oii)
require.NoError(t, err)
require.Exactly(t, c.Expected, string(joii))
}
}
func TestDatabase_DeleteIdentityImage(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
seedTestDB(t, db)
require.NoError(t, db.DeleteIdentityImage("thumbnail"))
oii, err := db.GetIdentityImage("thumbnail")
require.NoError(t, err)
require.Empty(t, oii)
}
func TestIdentityImage_GetDataURI(t *testing.T) {
cs := []struct{
II IdentityImage
URI string
cs := []struct {
II IdentityImage
URI string
Error error
}{
{
@ -163,3 +73,93 @@ func TestIdentityImage_MarshalJSON(t *testing.T) {
require.NoError(t, err)
require.Exactly(t, expected, string(js))
}
func setupTestDB(t *testing.T) (Database, func()) {
tmpfile, err := ioutil.TempFile("", "images-tests-")
require.NoError(t, err)
db, err := appdatabase.InitializeDB(tmpfile.Name(), "images-tests")
require.NoError(t, err)
return NewDatabase(db), func() {
require.NoError(t, db.Close())
require.NoError(t, os.Remove(tmpfile.Name()))
}
}
func seedTestDB(t *testing.T, db Database) {
iis := []IdentityImage{
{
Type: "thumbnail",
Payload: testJpegBytes,
Width: 80,
Height: 80,
FileSize: 256,
ResizeTarget: 80,
},
{
Type: "large",
Payload: testPngBytes,
Width: 240,
Height: 300,
FileSize: 1024,
ResizeTarget: 240,
},
}
require.NoError(t, db.StoreIdentityImages(iis))
}
func TestDatabase_GetIdentityImages(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
seedTestDB(t, db)
expected := `[{"type":"large","uri":"data:image/png;base64,iVBORw0KGgoAAAANSUg=","width":240,"height":300,"file_size":1024,"resize_target":240},{"type":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"file_size":256,"resize_target":80}]`
oiis, err := db.GetIdentityImages()
require.NoError(t, err)
joiis, err := json.Marshal(oiis)
require.NoError(t, err)
require.Exactly(t, expected, string(joiis))
}
func TestDatabase_GetIdentityImage(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
seedTestDB(t, db)
cs := []struct {
Name string
Expected string
}{
{
"thumbnail",
`{"type":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"file_size":256,"resize_target":80}`,
},
{
"large",
`{"type":"large","uri":"data:image/png;base64,iVBORw0KGgoAAAANSUg=","width":240,"height":300,"file_size":1024,"resize_target":240}`,
},
}
for _, c := range cs {
oii, err := db.GetIdentityImage(c.Name)
require.NoError(t, err)
joii, err := json.Marshal(oii)
require.NoError(t, err)
require.Exactly(t, c.Expected, string(joii))
}
}
func TestDatabase_DeleteIdentityImage(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
seedTestDB(t, db)
require.NoError(t, db.DeleteIdentityImage("thumbnail"))
oii, err := db.GetIdentityImage("thumbnail")
require.NoError(t, err)
require.Empty(t, oii)
}

View File

@ -93,8 +93,8 @@ func TestDecode(t *testing.T) {
}
func TestGetType(t *testing.T) {
cs := []struct{
Buf []byte
cs := []struct {
Buf []byte
Value ImageType
}{
{testJpegBytes, JPEG},
@ -110,8 +110,8 @@ func TestGetType(t *testing.T) {
}
func TestGetMimeType(t *testing.T) {
cs := []struct{
Buf []byte
cs := []struct {
Buf []byte
Value string
Error error
}{

View File

@ -9,7 +9,7 @@ import (
)
type EncodeConfig struct {
Quality int
Quality int
}
func Encode(w io.Writer, img image.Image, config EncodeConfig) error {
@ -38,7 +38,7 @@ func EncodeToBestSize(bb *bytes.Buffer, img image.Image, size ResizeDimension) e
}
if q == MinJpegQuality {
if DimensionSizeLimit[size].Max > bb.Len(){
if DimensionSizeLimit[size].Max > bb.Len() {
return nil
} else {
return fmt.Errorf(

View File

@ -50,7 +50,7 @@ func TestEncodeToBestSize(t *testing.T) {
cs := []struct {
FileName string
RenderSize int
Error error
Error error
}{
{
"elephant.jpg",

View File

@ -12,8 +12,8 @@ func GenerateProfileImages(filepath string, aX, aY, bX, bY int) ([][]byte, error
}
cropRect := image.Rectangle{
Min: image.Point{X: aX, Y: aY},
Max: image.Point{X: bX, Y: bY},
Min: image.Point{X: aX, Y: aY},
Max: image.Point{X: bX, Y: bY},
}
cImg, err := Crop(img, cropRect)
if err != nil {
@ -33,4 +33,4 @@ func GenerateProfileImages(filepath string, aX, aY, bX, bY int) ([][]byte, error
}
return imgs, nil
}
}

View File

@ -11,7 +11,7 @@ import (
func Resize(size ResizeDimension, img image.Image) image.Image {
var width, height uint
switch{
switch {
case img.Bounds().Max.X == img.Bounds().Max.Y:
width, height = uint(size), uint(size)
case img.Bounds().Max.X > img.Bounds().Max.Y:
@ -25,7 +25,7 @@ func Resize(size ResizeDimension, img image.Image) image.Image {
func Crop(img image.Image, rect image.Rectangle) (image.Image, error) {
if img.Bounds().Max.X < rect.Max.X || img.Bounds().Max.Y < rect.Max.Y{
if img.Bounds().Max.X < rect.Max.X || img.Bounds().Max.Y < rect.Max.Y {
return nil, fmt.Errorf(
"crop dimensions out of bounds of image, image width '%dpx' & height '%dpx'; crop bottom right coordinate at X '%dpx' Y '%dpx'",
img.Bounds().Max.X, img.Bounds().Max.Y,

View File

@ -12,14 +12,14 @@ import (
func TestResize(t *testing.T) {
sizes := []ResizeDimension{80, 240, 1000}
cs := []struct{
cs := []struct {
Filename string
Bounds map[ResizeDimension]image.Rectangle
Bounds map[ResizeDimension]image.Rectangle
}{
{
"elephant.jpg",
map[ResizeDimension]image.Rectangle{
80: {
80: {
Min: image.Point{X: 0, Y: 0},
Max: image.Point{X: 80, Y: 80},
},
@ -36,7 +36,7 @@ func TestResize(t *testing.T) {
{
"rose.webp",
map[ResizeDimension]image.Rectangle{
80: {
80: {
Min: image.Point{X: 0, Y: 0},
Max: image.Point{X: 107, Y: 80},
},
@ -53,7 +53,7 @@ func TestResize(t *testing.T) {
{
"spin.gif",
map[ResizeDimension]image.Rectangle{
80: {
80: {
Min: image.Point{X: 0, Y: 0},
Max: image.Point{X: 80, Y: 80},
},
@ -70,7 +70,7 @@ func TestResize(t *testing.T) {
{
"status.png",
map[ResizeDimension]image.Rectangle{
80: {
80: {
Min: image.Point{X: 0, Y: 0},
Max: image.Point{X: 80, Y: 80},
},
@ -98,11 +98,11 @@ func TestResize(t *testing.T) {
}
func TestCrop(t *testing.T) {
type params struct{
Rectangle image.Rectangle
type params struct {
Rectangle image.Rectangle
OutputBound image.Rectangle
OutputSize int
CropError error
OutputSize int
CropError error
}
topLeftSquare := image.Rectangle{
@ -110,7 +110,7 @@ func TestCrop(t *testing.T) {
Max: image.Point{X: 80, Y: 80},
}
offsetSquare := image.Rectangle{
Min: image.Point{X: 80, Y: 80},
Min: image.Point{X: 80, Y: 80},
Max: image.Point{X: 160, Y: 160},
}
outOfBoundsSquare := image.Rectangle{
@ -122,7 +122,7 @@ func TestCrop(t *testing.T) {
Quality: 70,
}
cs := []struct{
cs := []struct {
Filename string
Params []params
}{

View File

@ -16,7 +16,7 @@ const (
)
var (
ResizeDimensions = []ResizeDimension{80, 240}
ResizeDimensions = []ResizeDimension{80, 240}
// DimensionSizeLimit the size limits imposed on each resize dimension
// Figures are based on the following sample data https://github.com/status-im/status-react/issues/11047#issuecomment-694970473