Skip to content

Commit

Permalink
Merge pull request #210 from m-Peter/unit-testing-improvements
Browse files Browse the repository at this point in the history
[test] Unify backend environment for Unit & Integration tests
  • Loading branch information
SupunS authored Oct 13, 2023
2 parents 70f75c6 + 285769e commit de381d8
Show file tree
Hide file tree
Showing 7 changed files with 1,830 additions and 1,060 deletions.
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

0 comments on commit de381d8

Please sign in to comment.