Added IdentityImage JSON marshalling tests
This commit is contained in:
parent
0a5ce9d489
commit
01657b4665
|
@ -52,7 +52,7 @@ func (i IdentityImage) GetDataURI() (string, error) {
|
|||
|
||||
b64 := base64.StdEncoding.EncodeToString(i.Payload)
|
||||
|
||||
return "data:image/" + mt + ";base64," + b64 + "\\", nil
|
||||
return "data:image/" + mt + ";base64," + b64, nil
|
||||
}
|
||||
|
||||
func (d *Database) StoreIdentityImages() {
|
||||
|
|
|
@ -1,13 +1,70 @@
|
|||
package images
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIdentityImage_GetDataURI(t *testing.T) {
|
||||
cs := []struct{
|
||||
II IdentityImage
|
||||
URI string
|
||||
Error error
|
||||
}{
|
||||
{
|
||||
IdentityImage{Payload: testJpegBytes},
|
||||
"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=",
|
||||
nil,
|
||||
},
|
||||
{
|
||||
IdentityImage{Payload: testPngBytes},
|
||||
"data:image/png;base64,iVBORw0KGgoAAAANSUg=",
|
||||
nil,
|
||||
},
|
||||
{
|
||||
IdentityImage{Payload: testGifBytes},
|
||||
"data:image/gif;base64,R0lGODlhAAEAAYQfAP8=",
|
||||
nil,
|
||||
},
|
||||
{
|
||||
IdentityImage{Payload: testWebpBytes},
|
||||
"data:image/webp;base64,UklGRpBJAABXRUJQVlA=",
|
||||
nil,
|
||||
},
|
||||
{
|
||||
IdentityImage{Payload: testAacBytes},
|
||||
"",
|
||||
errors.New("image format not supported"),
|
||||
},
|
||||
}
|
||||
|
||||
// TODO
|
||||
for _, c := range cs {
|
||||
u, err := c.II.GetDataURI()
|
||||
|
||||
if c.Error == nil {
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
require.EqualError(t, err, c.Error.Error())
|
||||
}
|
||||
|
||||
require.Exactly(t, c.URI, u)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIdentityImage_MarshalJSON(t *testing.T) {
|
||||
// TODO
|
||||
ii := IdentityImage{
|
||||
Type: "thumbnail",
|
||||
Payload: testJpegBytes,
|
||||
Width: 80,
|
||||
Height: 80,
|
||||
FileSize: 256,
|
||||
ResizeTarget: 80,
|
||||
}
|
||||
expected := `{"type":"thumbnail","uri":"data:image/jpeg;base64,/9j/2wCEAFA3PEY8MlA=","width":80,"height":80,"file_size":256,"resize_target":80}`
|
||||
|
||||
js, err := json.Marshal(ii)
|
||||
require.NoError(t, err)
|
||||
require.Exactly(t, expected, string(js))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue