feat: add missing test cases for gif service (#2514)

This commit is contained in:
Khushboo-dev-cpp 2022-02-02 10:36:52 +01:00 committed by GitHub
parent c0c90947ab
commit 4f4725a630
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 139 additions and 0 deletions

139
services/gif/gif_test.go Normal file
View File

@ -0,0 +1,139 @@
package gif
import (
"database/sql"
"encoding/json"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/require"
"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/multiaccounts/accounts"
)
func setupSQLTestDb(t *testing.T) (*sql.DB, func()) {
tmpfile, err := ioutil.TempFile("", "local-notifications-tests-")
require.NoError(t, err)
db, err := appdatabase.InitializeDB(tmpfile.Name(), "local-notifications-tests")
require.NoError(t, err)
return db, func() {
require.NoError(t, os.Remove(tmpfile.Name()))
}
}
func setupTestDB(t *testing.T, db *sql.DB) (*accounts.Database, func()) {
return accounts.NewDB(db), func() {
require.NoError(t, db.Close())
}
}
func TestSetTenorAPIKey(t *testing.T) {
appDB, appStop := setupSQLTestDb(t)
defer appStop()
db, stop := setupTestDB(t, appDB)
defer stop()
gifAPI := NewGifAPI(db)
require.NoError(t, gifAPI.SetTenorAPIKey("DU7DWZ27STB2"))
require.Equal(t, "DU7DWZ27STB2", tenorAPIKey)
}
func TestGetContentWithRetry(t *testing.T) {
appDB, appStop := setupSQLTestDb(t)
defer appStop()
db, stop := setupTestDB(t, appDB)
defer stop()
gifAPI := NewGifAPI(db)
require.NoError(t, gifAPI.SetTenorAPIKey(""))
require.Equal(t, "", tenorAPIKey)
gifs, err := gifAPI.GetContentWithRetry("trending?")
require.Error(t, err)
require.Equal(t, "", gifs)
require.NoError(t, gifAPI.SetTenorAPIKey("DU7DWZ27STB2"))
require.Equal(t, "DU7DWZ27STB2", tenorAPIKey)
gifs, err = gifAPI.GetContentWithRetry("trending?")
require.NoError(t, err)
require.NotEqual(t, "", gifs)
}
func TestFavoriteGifs(t *testing.T) {
appDB, appStop := setupSQLTestDb(t)
defer appStop()
db, stop := setupTestDB(t, appDB)
defer stop()
gifAPI := NewGifAPI(db)
require.NoError(t, gifAPI.SetTenorAPIKey("DU7DWZ27STB2"))
require.Equal(t, "DU7DWZ27STB2", tenorAPIKey)
recent := map[string]interface{}{
"id": "23833142",
"title": "",
"url": "https://media.tenor.com/images/b845ae14f43883e5cd6283e705f09efb/tenor.gif",
"tinyUrl": "https://media.tenor.com/images/2067bdc0375f9606dfb9fb4d2bfaafde/tenor.gif",
"height": 498,
"isFavorite": true,
}
newRecents := map[string]interface{}{
"items": recent,
}
inputJSON := map[string]interface{}{
"jsonrpc": "2.0",
"method": "gif_setTenorAPIKey",
"params": newRecents,
}
like, _ := json.Marshal(inputJSON)
source := (json.RawMessage)(like)
require.NoError(t, gifAPI.UpdateFavoriteGifs(source))
}
func TestRecentGifs(t *testing.T) {
appDB, appStop := setupSQLTestDb(t)
defer appStop()
db, stop := setupTestDB(t, appDB)
defer stop()
gifAPI := NewGifAPI(db)
require.NoError(t, gifAPI.SetTenorAPIKey("DU7DWZ27STB2"))
require.Equal(t, "DU7DWZ27STB2", tenorAPIKey)
recent := map[string]interface{}{
"id": "23833142",
"title": "",
"url": "https://media.tenor.com/images/b845ae14f43883e5cd6283e705f09efb/tenor.gif",
"tinyUrl": "https://media.tenor.com/images/2067bdc0375f9606dfb9fb4d2bfaafde/tenor.gif",
"height": 498,
"isFavorite": true,
}
newRecents := map[string]interface{}{
"items": recent,
}
inputJSON := map[string]interface{}{
"jsonrpc": "2.0",
"method": "gif_setTenorAPIKey",
"params": newRecents,
}
like, _ := json.Marshal(inputJSON)
source := (json.RawMessage)(like)
require.NoError(t, gifAPI.UpdateRecentGifs(source))
}