Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Unify backend environment for Unit & Integration tests #210

Merged
merged 20 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2eb6fb0
Update cadence dependency to latest master commit
m-Peter Sep 29, 2023
c0f4116
Update flow-emulator dependency to latest master commit
m-Peter Sep 29, 2023
898e17d
Run go mod tidy
m-Peter Sep 29, 2023
1742d39
Unify backend environment for Unit & Integration tests
m-Peter Sep 19, 2023
831bc2e
Setup emulator blockchain with same runtime as the script environment
m-Peter Sep 20, 2023
46840cf
Allow unit tests to make use of self.account in contracts
m-Peter Sep 21, 2023
053513c
Change number of initial accounts to 10
m-Peter Sep 27, 2023
7a1a6b9
Rename TestFramework's NewEmulatorBackend method to EmulatorBackend
m-Peter Sep 27, 2023
51c46cb
Add test case with usage of undeployed contract
m-Peter Sep 27, 2023
01e13cf
Remove custom InjectedCompositeFieldsHandler and use the default one …
m-Peter Sep 27, 2023
24f170f
Reuse the replaceImports method of EmulatorBackend
m-Peter Sep 27, 2023
97e8e1e
Remove the emulator.WithRuntime() config
m-Peter Sep 27, 2023
05feb43
Refactor logic for parseCheckAndInterpret method
m-Peter Sep 29, 2023
3db6e2e
Fix contract loading error for unit tests with nested imports
m-Peter Oct 4, 2023
fda31d8
Add blockchain helpers for script/transaction execution
m-Peter Oct 4, 2023
c1e14d2
Add test case with multiple imports in the same test file
m-Peter Oct 4, 2023
b3d0a35
Make sure to update the interpreter's storage to reflect deployment c…
m-Peter Oct 6, 2023
0cce6b2
Bump onflow/cadence dependency to v0.42.0
m-Peter Oct 9, 2023
7c52b63
Update onflow/flow-go module to latest master commit with cadence v0.…
m-Peter Oct 11, 2023
285769e
Add todo for the tech-debt
SupunS Oct 12, 2023
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
158 changes: 93 additions & 65 deletions test/blockchain_helpers.cdc
Original file line number Diff line number Diff line change
@@ -1,80 +1,108 @@
import Test

pub struct BlockchainHelpers {
pub let blockchain: Test.Blockchain
/// Returns the current block height of the blockchain.
///
access(all)
fun getCurrentBlockHeight(): UInt64 {
let script = readFile("get_current_block_height.cdc")
let scriptResult = Test.executeScript(script, [])

init(blockchain: Test.Blockchain) {
self.blockchain = blockchain
if scriptResult.status == Test.ResultStatus.failed {
panic(scriptResult.error!.message)
}
return scriptResult.returnValue! as! UInt64
}

/// Returns the current block height of the blockchain.
///
pub fun getCurrentBlockHeight(): UInt64 {
let script = self.readFile("get_current_block_height.cdc")
let scriptResult = self.blockchain.executeScript(script, [])
/// Returns the Flow token balance for the given account.
///
access(all)
fun getFlowBalance(for account: Test.Account): UFix64 {
let script = readFile("get_flow_balance.cdc")
let scriptResult = Test.executeScript(script, [account.address])

if scriptResult.status == Test.ResultStatus.failed {
panic(scriptResult.error!.message)
}
return scriptResult.returnValue! as! UInt64
if scriptResult.status == Test.ResultStatus.failed {
panic(scriptResult.error!.message)
}
return scriptResult.returnValue! as! UFix64
}

/// Returns the Flow token balance for the given account.
///
pub fun getFlowBalance(for account: Test.Account): UFix64 {
let script = self.readFile("get_flow_balance.cdc")
let scriptResult = self.blockchain.executeScript(script, [account.address])
/// Mints the given amount of Flow tokens to a specified test account.
/// The transaction is authorized and signed by the service account.
/// Returns the result of the transaction.
///
access(all)
fun mintFlow(
to receiver: Test.Account,
amount: UFix64
): Test.TransactionResult {
let code = readFile("mint_flow.cdc")
let tx = Test.Transaction(
code: code,
authorizers: [Test.serviceAccount().address],
signers: [],
arguments: [receiver.address, amount]
)

if scriptResult.status == Test.ResultStatus.failed {
panic(scriptResult.error!.message)
}
return scriptResult.returnValue! as! UFix64
}
return Test.executeTransaction(tx)
}

/// Mints the given amount of Flow tokens to a specified test account.
/// The transaction is authorized and signed by the service account.
/// Returns the result of the transaction.
///
pub fun mintFlow(
to receiver: Test.Account,
amount: UFix64
): Test.TransactionResult {
let code = self.readFile("mint_flow.cdc")
let tx = Test.Transaction(
code: code,
authorizers: [self.blockchain.serviceAccount().address],
signers: [],
arguments: [receiver.address, amount]
)
/// Burns the specified amount of Flow tokens for the given
/// test account. Returns the result of the transaction.
///
access(all)
fun burnFlow(
from account: Test.Account,
amount: UFix64
): Test.TransactionResult {
let code = readFile("burn_flow.cdc")
let tx = Test.Transaction(
code: code,
authorizers: [account.address],
signers: [account],
arguments: [amount]
)

return self.blockchain.executeTransaction(tx)
}
return Test.executeTransaction(tx)
}

/// Burns the specified amount of Flow tokens for the given
/// test account. Returns the result of the transaction.
///
pub fun burnFlow(
from account: Test.Account,
amount: UFix64
): Test.TransactionResult {
let code = self.readFile("burn_flow.cdc")
let tx = Test.Transaction(
code: code,
authorizers: [account.address],
signers: [account],
arguments: [amount]
)
/// Executes a script and returns the script return value and the status.
/// `returnValue` field of the result will be `nil` if the script failed.
///
access(all)
fun executeScript(
_ path: String,
_ arguments: [AnyStruct]
): Test.ScriptResult {
let script = Test.readFile(path)
return Test.executeScript(script, arguments)
}

return self.blockchain.executeTransaction(tx)
}
/// Executes a given transaction, commits the current block
/// and returns the result of transaction execution.
///
access(all)
fun executeTransaction(
_ path: String,
_ arguments: [AnyStruct],
_ account: Test.Account
): Test.TransactionResult {
let code = Test.readFile(path)
let tx = Test.Transaction(
code: code,
authorizers: [account.address],
signers: [account],
arguments: arguments
)

/// Reads the code for the script/transaction with the given
/// file name and returns its content as a String.
///
access(self)
fun readFile(_ name: String): String {
// The "\u{0}helper/" prefix is used in order to prevent
// conflicts with user-defined scripts/transactions.
return Test.readFile("\u{0}helper/".concat(name))
}
return Test.executeTransaction(tx)
}

/// Reads the code for the script/transaction with the given
/// file name and returns its content as a String.
///
access(self)
fun readFile(_ name: String): String {
// The "\u{0}helper/" prefix is used in order to prevent
// conflicts with user-defined scripts/transactions.
return Test.readFile("\u{0}helper/".concat(name))
}
Loading
Loading