mirror of
https://github.com/status-im/status-go.git
synced 2025-01-27 23:18:40 +00:00
1097b14a7f
* Migrations in place, how to run them? * Remove down migrations and touch database.go * Database and Database Test package in place, added functions to get and store app metrics * make generate output * Minor bug fix on app metrics insert and select * Add a validation layer to restrict what can be saved in the database * Make validation more terse, throw error if schema doesn't exist, expose appmetrics service * service updates * Compute all errors before sending them out * Trying to bring a closjure to appmetrics go * Expose appmetrics via an api, skip fancy * Address value as Jason Dawt Rawmasage to ease parsing * Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function. * Lint issues * Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel * Bump migration number * Fix API factory usage * Add comment re:json.RawMessage instead of strings * Get rid of test vars, throw save error inside the loop * Update version Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package appmetrics
|
|
|
|
import (
|
|
"encoding/json"
|
|
"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("", "appmetrics-tests-")
|
|
require.NoError(t, err)
|
|
db, err := appdatabase.InitializeDB(tmpfile.Name(), "appmetrics-tests")
|
|
require.NoError(t, err)
|
|
|
|
return NewDB(db), func() {
|
|
require.NoError(t, db.Close())
|
|
require.NoError(t, os.Remove(tmpfile.Name()))
|
|
}
|
|
}
|
|
|
|
func TestSaveAppMetrics(t *testing.T) {
|
|
db, stop := setupTestDB(t)
|
|
defer stop()
|
|
|
|
// we need backticks (``) for value because it is expected by gojsonschema
|
|
// it considers text inside tics to be stringified json
|
|
appMetrics := []AppMetric{
|
|
{Event: NavigationNavigateToCofx, Value: json.RawMessage(`{"view_id": "some-view-id", "params": {"screen": "allowed-screen-name"}}`), OS: "android", AppVersion: "1.11"},
|
|
}
|
|
|
|
err := db.SaveAppMetrics(appMetrics)
|
|
require.NoError(t, err)
|
|
|
|
res, err := db.GetAppMetrics(10, 0)
|
|
require.NoError(t, err)
|
|
require.Equal(t, appMetrics, res)
|
|
}
|