Skip to content

Commit

Permalink
Added tests for core/assets (smartcontractkit#6801)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Smirnov authored Jun 16, 2022
1 parent 873f63d commit 89f9a5e
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 0 deletions.
1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ ignore:
- 'contracts/src/v0.5'
- 'contracts/src/v0.8' # Disabled due to solidity-coverage not reporting coverage
- 'core/internal'
- 'core/scripts'
143 changes: 143 additions & 0 deletions core/assets/currencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package assets_test

import (
"encoding/json"
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/smartcontractkit/chainlink/core/assets"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -145,3 +147,144 @@ func TestAssets_Eth_UnmarshalJsonError(t *testing.T) {
err = json.Unmarshal([]byte(`1`), &eth)
assert.Equal(t, assets.ErrNoQuotesForCurrency, err)
}

func TestAssets_LinkToInt(t *testing.T) {
t.Parallel()

link := assets.NewLinkFromJuels(0)
assert.Equal(t, big.NewInt(0), link.ToInt())

link = assets.NewLinkFromJuels(123)
assert.Equal(t, big.NewInt(123), link.ToInt())
}

func TestAssets_LinkToHash(t *testing.T) {
t.Parallel()

link := assets.NewLinkFromJuels(123)
expected := common.BigToHash((*big.Int)(link))
assert.Equal(t, expected, link.ToHash())
}

func TestAssets_LinkSetLink(t *testing.T) {
t.Parallel()

link1 := assets.NewLinkFromJuels(123)
link2 := assets.NewLinkFromJuels(321)
link3 := link1.Set(link2)
assert.Equal(t, link3, link2)
}

func TestAssets_LinkCmpLink(t *testing.T) {
t.Parallel()

link1 := assets.NewLinkFromJuels(123)
link2 := assets.NewLinkFromJuels(321)
assert.NotZero(t, link1.Cmp(link2))

link3 := assets.NewLinkFromJuels(321)
assert.Zero(t, link3.Cmp(link2))
}

func TestAssets_LinkAddLink(t *testing.T) {
t.Parallel()

link1 := assets.NewLinkFromJuels(123)
link2 := assets.NewLinkFromJuels(321)
sum := assets.NewLinkFromJuels(123 + 321)
assert.Equal(t, sum, link1.Add(link1, link2))
}

func TestAssets_LinkText(t *testing.T) {
t.Parallel()

link := assets.NewLinkFromJuels(123)
assert.Equal(t, "123", link.Text(10))
assert.Equal(t, "7b", link.Text(16))
}

func TestAssets_LinkIsZero(t *testing.T) {
t.Parallel()

link := assets.NewLinkFromJuels(123)
assert.False(t, link.IsZero())

link = assets.NewLinkFromJuels(0)
assert.True(t, link.IsZero())
}

func TestAssets_LinkSymbol(t *testing.T) {
t.Parallel()

link := assets.NewLinkFromJuels(123)
assert.Equal(t, "LINK", link.Symbol())
}

func TestAssets_LinkScanValue(t *testing.T) {
t.Parallel()

link := assets.NewLinkFromJuels(123)
v, err := link.Value()
assert.NoError(t, err)

link2 := assets.NewLinkFromJuels(0)
err = link2.Scan(v)
assert.NoError(t, err)
assert.Equal(t, link2, link)

err = link2.Scan("123")
assert.NoError(t, err)
assert.Equal(t, link2, link)

err = link2.Scan([]uint8{'1', '2', '3'})
assert.NoError(t, err)
assert.Equal(t, link2, link)

assert.ErrorContains(t, link2.Scan([]uint8{'x'}), "unable to set string")
assert.ErrorContains(t, link2.Scan("123.56"), "unable to set string")
assert.ErrorContains(t, link2.Scan(1.5), "unable to convert")
assert.ErrorContains(t, link2.Scan(int64(123)), "unable to convert")
}

func TestAssets_NewEth(t *testing.T) {
t.Parallel()

ethRef := assets.NewEth(123)
ethVal := assets.NewEthValue(123)
ethStr, err := assets.NewEthValueS(ethRef.String())
assert.NoError(t, err)
assert.Equal(t, *ethRef, ethVal)
assert.Equal(t, *ethRef, ethStr)
}

func TestAssets_EthSymbol(t *testing.T) {
t.Parallel()

eth := assets.NewEth(123)
assert.Equal(t, "ETH", eth.Symbol())
}

func TestAssets_EthScanValue(t *testing.T) {
t.Parallel()

eth := assets.NewEth(123)
v, err := eth.Value()
assert.NoError(t, err)

eth2 := assets.NewEth(0)
err = eth2.Scan(v)
assert.NoError(t, err)

assert.Equal(t, eth, eth2)
}

func TestAssets_EthCmpEth(t *testing.T) {
t.Parallel()

eth1 := assets.NewEth(123)
eth2 := assets.NewEth(321)
assert.NotZero(t, eth1.Cmp(eth2))

eth3 := assets.NewEth(321)
assert.Zero(t, eth3.Cmp(eth2))
}
44 changes: 44 additions & 0 deletions core/assets/units_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package assets_test

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/params"
"github.com/smartcontractkit/chainlink/core/assets"
"github.com/stretchr/testify/assert"
)

func TestAssets_Units(t *testing.T) {
t.Parallel()

tests := []struct {
name string
fn func(int64) *big.Int
factor *big.Int
}{
{name: "Wei", fn: assets.Wei, factor: big.NewInt(params.Wei)},
{name: "GWei", fn: assets.GWei, factor: big.NewInt(params.GWei)},
{name: "UEther", fn: assets.UEther, factor: big.NewInt(params.GWei * 1000)},
{name: "Ether", fn: assets.Ether, factor: big.NewInt(params.Ether)},
}

for _, test := range tests {
test := test

t.Run(test.name, func(t *testing.T) {
t.Parallel()

expected := big.NewInt(0)
assert.Equal(t, expected, test.fn(0))

expected = big.NewInt(100)
expected = new(big.Int).Mul(expected, test.factor)
assert.Equal(t, expected, test.fn(100))

expected = big.NewInt(-100)
expected = new(big.Int).Mul(expected, test.factor)
assert.Equal(t, expected, test.fn(-100))
})
}
}

0 comments on commit 89f9a5e

Please sign in to comment.