Skip to content
This repository has been archived by the owner on Jul 8, 2024. It is now read-only.

[WIP] Clean up Cadence tests #33

Merged
merged 1 commit into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions kitty-items-cadence/lib/go/test/go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
module github.com/onflow/kitty-items/lib/go/test
module github.com/onflow/kitty-items/kitty-items-cadence/lib/go/test

go 1.14

require (
github.com/onflow/cadence v0.12.6
github.com/onflow/flow-emulator v0.14.2
github.com/onflow/flow-ft/contracts v0.1.3 // indirect
github.com/onflow/flow-ft/lib/go/contracts v0.4.0
github.com/onflow/flow-go-sdk v0.14.3
github.com/onflow/flow-nft/lib/go/contracts v0.0.0-20201125231514-e1170127bdb6
github.com/onflow/flow-nft/lib/go/templates v0.0.0-20201125231514-e1170127bdb6
github.com/stretchr/testify v1.7.0
)
78 changes: 24 additions & 54 deletions kitty-items-cadence/lib/go/test/go.sum

Large diffs are not rendered by default.

147 changes: 78 additions & 69 deletions kitty-items-cadence/lib/go/test/kibble_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
emulator "github.com/onflow/flow-emulator"
Expand All @@ -15,19 +12,21 @@ import (
"github.com/onflow/flow-go-sdk/crypto"
"github.com/onflow/flow-go-sdk/templates"
"github.com/onflow/flow-go-sdk/test"
"github.com/stretchr/testify/assert"

ft_contracts "github.com/onflow/flow-ft/lib/go/contracts"
)

const (
kibbleRootPath = "../../../cadence/kibble"
kibbleKibblePath = kibbleRootPath + "/contracts/Kibble.cdc"
kibbleSetupAccountPath = kibbleRootPath + "/transactions/setup_account.cdc"
kibbleTransferTokensPath = kibbleRootPath + "/transactions/transfer_tokens.cdc"
kibbleMintTokensPath = kibbleRootPath + "/transactions/mint_tokens.cdc"
kibbleBurnTokensPath = kibbleRootPath + "/transactions/burn_tokens.cdc"
kibbleGetBalancePath = kibbleRootPath + "/scripts/get_balance.cdc"
kibbleGetSupplyPath = kibbleRootPath + "/scripts/get_supply.cdc"
kibbleTransactionsRootPath = "../../../transactions/kibble"
kibbleScriptsRootPath = "../../../scripts/kibble"

kibbleContractPath = "../../../contracts/Kibble.cdc"
kibbleSetupAccountPath = kibbleTransactionsRootPath + "/setup_account.cdc"
kibbleTransferTokensPath = kibbleTransactionsRootPath + "/transfer_tokens.cdc"
kibbleMintTokensPath = kibbleTransactionsRootPath + "/mint_tokens.cdc"
kibbleGetBalancePath = kibbleScriptsRootPath + "/get_balance.cdc"
kibbleGetSupplyPath = kibbleScriptsRootPath + "/get_supply.cdc"
)

func KibbleDeployContracts(b *emulator.Blockchain, t *testing.T) (flow.Address, flow.Address, crypto.Signer) {
Expand Down Expand Up @@ -117,26 +116,31 @@ func TestKibbleDeployment(t *testing.T) {

t.Run("Should have initialized Supply field correctly", func(t *testing.T) {
supply := executeScriptAndCheck(t, b, kibbleGenerateGetSupplyScript(fungibleAddr, kibbleAddr), nil)
expectedSupply, expectedSupplyErr := cadence.NewUFix64("0.0")
assert.NoError(t, expectedSupplyErr)
assert.Equal(t, expectedSupply, supply.(cadence.UFix64))
assert.EqualValues(t, CadenceUFix64("0.0"), supply)
})
}

func TestKibbleSetupAccount(t *testing.T) {
b := newEmulator()

t.Run("Should be able to create empty Vault that doesn't affect supply", func(t *testing.T) {

t.Run("Should be able to create empty vault that does not affect supply", func(t *testing.T) {
fungibleAddr, kibbleAddr, _ := KibbleDeployContracts(b, t)

userAddress, _ := KibbleCreateAccount(t, b, fungibleAddr, kibbleAddr)

balance := executeScriptAndCheck(t, b, kibbleGenerateGetBalanceScript(fungibleAddr, kibbleAddr), [][]byte{jsoncdc.MustEncode(cadence.Address(userAddress))})
assert.Equal(t, CadenceUFix64("0.0"), balance)
userBalance := executeScriptAndCheck(
t, b,
kibbleGenerateGetBalanceScript(fungibleAddr, kibbleAddr),
[][]byte{jsoncdc.MustEncode(cadence.Address(userAddress))},
)
assert.EqualValues(t, CadenceUFix64("0.0"), userBalance)

supply := executeScriptAndCheck(t, b, kibbleGenerateGetSupplyScript(fungibleAddr, kibbleAddr), nil)
assert.Equal(t, CadenceUFix64("0.0"), supply.(cadence.UFix64))
supply := executeScriptAndCheck(
t, b,
kibbleGenerateGetSupplyScript(fungibleAddr, kibbleAddr),
nil,
)
assert.EqualValues(t, CadenceUFix64("0.0"), supply)
})
}

Expand All @@ -147,25 +151,28 @@ func TestKibbleMinting(t *testing.T) {

userAddress, _ := KibbleCreateAccount(t, b, fungibleAddr, kibbleAddr)

t.Run("Shouldn't be able to mint zero tokens", func(t *testing.T) {
t.Run("Should not be able to mint zero tokens", func(t *testing.T) {
KibbleMint(t, b, fungibleAddr, kibbleAddr, kibbleSigner, userAddress, "0.0", true)
})

t.Run("Should mint tokens, deposit, and update balance and total supply", func(t *testing.T) {
t.Run("Should be able to mint tokens, deposit, update balance and total supply", func(t *testing.T) {
KibbleMint(t, b, fungibleAddr, kibbleAddr, kibbleSigner, userAddress, "50.0", false)

// Assert that the vault's balance is correct
result, err := b.ExecuteScript(kibbleGenerateGetBalanceScript(fungibleAddr, kibbleAddr), [][]byte{jsoncdc.MustEncode(cadence.Address(userAddress))})
require.NoError(t, err)
if !assert.True(t, result.Succeeded()) {
t.Log(result.Error.Error())
}
balance := result.Value
assert.Equal(t, CadenceUFix64("50.0"), balance.(cadence.UFix64))
// Assert that vault balance is correct
userBalance := executeScriptAndCheck(
t, b,
kibbleGenerateGetBalanceScript(fungibleAddr, kibbleAddr),
[][]byte{jsoncdc.MustEncode(cadence.Address(userAddress))},
)
assert.EqualValues(t, CadenceUFix64("50.0"), userBalance)

// Make sure that the total supply is correct
supply := executeScriptAndCheck(t, b, kibbleGenerateGetSupplyScript(fungibleAddr, kibbleAddr), nil)
assert.Equal(t, CadenceUFix64("50.0"), supply.(cadence.UFix64))
// Assert that total supply is correct
supply := executeScriptAndCheck(
t, b,
kibbleGenerateGetSupplyScript(fungibleAddr, kibbleAddr),
nil,
)
assert.EqualValues(t, CadenceUFix64("50.0"), supply)
})
}

Expand All @@ -178,7 +185,7 @@ func TestKibbleTransfers(t *testing.T) {

KibbleMint(t, b, fungibleAddr, kibbleAddr, kibbleSigner, kibbleAddr, "1000.0", false)

t.Run("Shouldn't be able to withdraw more than the balance of the Vault", func(t *testing.T) {
t.Run("Should not be able to withdraw more than the balance of the vault", func(t *testing.T) {
tx := flow.NewTransaction().
SetScript(kibbleGenerateTransferVaultScript(fungibleAddr, kibbleAddr)).
SetGasLimit(100).
Expand All @@ -196,22 +203,21 @@ func TestKibbleTransfers(t *testing.T) {
true,
)

// Assert that the vaults' balances are correct
result, err := b.ExecuteScript(kibbleGenerateGetBalanceScript(fungibleAddr, kibbleAddr), [][]byte{jsoncdc.MustEncode(cadence.Address(kibbleAddr))})
require.NoError(t, err)
if !assert.True(t, result.Succeeded()) {
t.Log(result.Error.Error())
}
balance := result.Value
assert.Equal(t, balance.(cadence.UFix64), CadenceUFix64("1000.0"))

result, err = b.ExecuteScript(kibbleGenerateGetBalanceScript(fungibleAddr, kibbleAddr), [][]byte{jsoncdc.MustEncode(cadence.Address(userAddress))})
require.NoError(t, err)
if !assert.True(t, result.Succeeded()) {
t.Log(result.Error.Error())
}
balance = result.Value
assert.Equal(t, balance.(cadence.UFix64), CadenceUFix64("0.0"))
// Assert that vault balances are correct

kibbleBalance := executeScriptAndCheck(
t, b,
kibbleGenerateGetBalanceScript(fungibleAddr, kibbleAddr),
[][]byte{jsoncdc.MustEncode(cadence.Address(kibbleAddr))},
)
assert.EqualValues(t, CadenceUFix64("1000.0"), kibbleBalance)

userBalance := executeScriptAndCheck(
t, b,
kibbleGenerateGetBalanceScript(fungibleAddr, kibbleAddr),
[][]byte{jsoncdc.MustEncode(cadence.Address(userAddress))},
)
assert.EqualValues(t, CadenceUFix64("0.0"), userBalance)
})

t.Run("Should be able to withdraw and deposit tokens from a vault", func(t *testing.T) {
Expand All @@ -232,25 +238,28 @@ func TestKibbleTransfers(t *testing.T) {
false,
)

// Assert that the vaults' balances are correct
result, err := b.ExecuteScript(kibbleGenerateGetBalanceScript(fungibleAddr, kibbleAddr), [][]byte{jsoncdc.MustEncode(cadence.Address(kibbleAddr))})
require.NoError(t, err)
if !assert.True(t, result.Succeeded()) {
t.Log(result.Error.Error())
}
balance := result.Value
assert.Equal(t, balance.(cadence.UFix64), CadenceUFix64("700.0"))

result, err = b.ExecuteScript(kibbleGenerateGetBalanceScript(fungibleAddr, kibbleAddr), [][]byte{jsoncdc.MustEncode(cadence.Address(userAddress))})
require.NoError(t, err)
if !assert.True(t, result.Succeeded()) {
t.Log(result.Error.Error())
}
balance = result.Value
assert.Equal(t, balance.(cadence.UFix64), CadenceUFix64("300.0"))
// Assert that vault balances are correct

supply := executeScriptAndCheck(t, b, kibbleGenerateGetSupplyScript(fungibleAddr, kibbleAddr), nil)
assert.Equal(t, supply.(cadence.UFix64), CadenceUFix64("1000.0"))
kibbleBalance := executeScriptAndCheck(
t, b,
kibbleGenerateGetBalanceScript(fungibleAddr, kibbleAddr),
[][]byte{jsoncdc.MustEncode(cadence.Address(kibbleAddr))},
)
assert.EqualValues(t, CadenceUFix64("700.0"), kibbleBalance)

userBalance := executeScriptAndCheck(
t, b,
kibbleGenerateGetBalanceScript(fungibleAddr, kibbleAddr),
[][]byte{jsoncdc.MustEncode(cadence.Address(userAddress))},
)
assert.EqualValues(t, CadenceUFix64("300.0"), userBalance)

supply := executeScriptAndCheck(
t, b,
kibbleGenerateGetSupplyScript(fungibleAddr, kibbleAddr),
nil,
)
assert.EqualValues(t, CadenceUFix64("1000.0"), supply)
})
}

Expand All @@ -270,7 +279,7 @@ func loadFungibleToken() []byte {

func loadKibble(fungibleAddr flow.Address) []byte {
return []byte(strings.ReplaceAll(
string(readFile(kibbleKibblePath)),
string(readFile(kibbleContractPath)),
ftAddressPlaceholder,
"0x"+fungibleAddr.String(),
))
Expand Down
Loading