Added IdentityImage Database store and get tests

This commit is contained in:
Samuel Hawksby-Robinson 2020-10-27 13:57:28 +00:00 committed by Andrea Maria Piana
parent df55822e29
commit aad761b24b
2 changed files with 53 additions and 2 deletions

View File

@ -67,7 +67,7 @@ func (d *Database) StoreIdentityImages(iis []IdentityImage) (err error) {
defer stmt.Close()
for _, ii := range iis {
_, err = stmt.Exec(ii)
_, err = stmt.Exec(ii.Type, ii.Payload, ii.Width, ii.Height, ii.FileSize, ii.ResizeTarget)
if err != nil {
return
}

View File

@ -3,10 +3,61 @@ package images
import (
"encoding/json"
"errors"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"testing"
"github.com/status-im/status-go/appdatabase"
"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 TestDatabase_GetIdentityImages(t *testing.T) {
db, stop := setupTestDB(t)
defer stop()
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,
},
}
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}]`
err := db.StoreIdentityImages(iis)
require.NoError(t, err)
oiis, err := db.GetIdentityImages()
require.NoError(t, err)
joiis, err := json.Marshal(oiis)
require.NoError(t, err)
require.Exactly(t, expected, string(joiis))
}
func TestIdentityImage_GetDataURI(t *testing.T) {
cs := []struct{
II IdentityImage