From a7358fcd3f14dcbb5f745543323dcf51cda28b48 Mon Sep 17 00:00:00 2001 From: Samuel Hawksby-Robinson Date: Tue, 27 Oct 2020 14:42:42 +0000 Subject: [PATCH] images package tidy up --- images/database.go | 10 +- images/database_test.go | 186 ++++++++++++++++++------------------ images/decode_test.go | 8 +- images/encode.go | 4 +- images/encode_test.go | 2 +- images/main.go | 6 +- images/manipulation.go | 4 +- images/manipulation_test.go | 24 ++--- images/meta.go | 2 +- 9 files changed, 123 insertions(+), 123 deletions(-) diff --git a/images/database.go b/images/database.go index b74579433..656f53684 100644 --- a/images/database.go +++ b/images/database.go @@ -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 { diff --git a/images/database_test.go b/images/database_test.go index 6ce479ffe..35760e625 100644 --- a/images/database_test.go +++ b/images/database_test.go @@ -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) +} diff --git a/images/decode_test.go b/images/decode_test.go index 845fac28a..346053ca4 100644 --- a/images/decode_test.go +++ b/images/decode_test.go @@ -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 }{ diff --git a/images/encode.go b/images/encode.go index 6d6539e49..03ba5d767 100644 --- a/images/encode.go +++ b/images/encode.go @@ -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( diff --git a/images/encode_test.go b/images/encode_test.go index 8be7c17b9..35bc25369 100644 --- a/images/encode_test.go +++ b/images/encode_test.go @@ -50,7 +50,7 @@ func TestEncodeToBestSize(t *testing.T) { cs := []struct { FileName string RenderSize int - Error error + Error error }{ { "elephant.jpg", diff --git a/images/main.go b/images/main.go index df3d9e1fd..e807d1ae0 100644 --- a/images/main.go +++ b/images/main.go @@ -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 -} \ No newline at end of file +} diff --git a/images/manipulation.go b/images/manipulation.go index fa34a32c8..1decb6190 100644 --- a/images/manipulation.go +++ b/images/manipulation.go @@ -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, diff --git a/images/manipulation_test.go b/images/manipulation_test.go index 9827de203..8d79565e3 100644 --- a/images/manipulation_test.go +++ b/images/manipulation_test.go @@ -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 }{ diff --git a/images/meta.go b/images/meta.go index 75d201e8b..01b747190 100644 --- a/images/meta.go +++ b/images/meta.go @@ -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