github.com/status-im/status-go@v1.1.0/services/wallet/currency/currency_db_test.go (about) 1 package currency 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 8 "github.com/status-im/status-go/t/helpers" 9 "github.com/status-im/status-go/walletdatabase" 10 ) 11 12 func setupTestCurrencyDB(t *testing.T) (*DB, func()) { 13 db, err := helpers.SetupTestMemorySQLDB(walletdatabase.DbInitializer{}) 14 require.NoError(t, err) 15 return NewCurrencyDB(db), func() { 16 require.NoError(t, db.Close()) 17 } 18 } 19 20 func TestCurrencyFormats(t *testing.T) { 21 db, stop := setupTestCurrencyDB(t) 22 defer stop() 23 24 rst, err := db.GetCachedFormats() 25 require.NoError(t, err) 26 require.Empty(t, rst) 27 28 pr1 := FormatPerSymbol{ 29 "A": { 30 Symbol: "A", 31 DisplayDecimals: 1, 32 StripTrailingZeroes: false, 33 }, 34 "B": { 35 Symbol: "B", 36 DisplayDecimals: 2, 37 StripTrailingZeroes: true, 38 }, 39 } 40 41 err = db.UpdateCachedFormats(pr1) 42 require.NoError(t, err) 43 44 rst, err = db.GetCachedFormats() 45 require.NoError(t, err) 46 require.Equal(t, rst, pr1) 47 48 pr2 := FormatPerSymbol{ 49 "B": { 50 Symbol: "B", 51 DisplayDecimals: 3, 52 StripTrailingZeroes: true, 53 }, 54 "C": { 55 Symbol: "C", 56 DisplayDecimals: 4, 57 StripTrailingZeroes: false, 58 }, 59 } 60 61 err = db.UpdateCachedFormats(pr2) 62 require.NoError(t, err) 63 64 rst, err = db.GetCachedFormats() 65 require.NoError(t, err) 66 67 expected := FormatPerSymbol{ 68 "A": { 69 Symbol: "A", 70 DisplayDecimals: 1, 71 StripTrailingZeroes: false, 72 }, 73 "B": { 74 Symbol: "B", 75 DisplayDecimals: 3, 76 StripTrailingZeroes: true, 77 }, 78 "C": { 79 Symbol: "C", 80 DisplayDecimals: 4, 81 StripTrailingZeroes: false, 82 }, 83 } 84 85 require.Equal(t, rst, expected) 86 }