From 4f4725a630fccaa6b4582b78a94dd11601b5c7b7 Mon Sep 17 00:00:00 2001 From: Khushboo-dev-cpp <60327365+Khushboo-dev-cpp@users.noreply.github.com> Date: Wed, 2 Feb 2022 10:36:52 +0100 Subject: [PATCH] feat: add missing test cases for gif service (#2514) --- services/gif/gif_test.go | 139 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 services/gif/gif_test.go diff --git a/services/gif/gif_test.go b/services/gif/gif_test.go new file mode 100644 index 000000000..edc038991 --- /dev/null +++ b/services/gif/gif_test.go @@ -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)) +}