Skip to content

Commit

Permalink
Unify backend environment for Unit & Integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Sep 19, 2023
1 parent d0d3d6f commit f6ad263
Show file tree
Hide file tree
Showing 5 changed files with 1,248 additions and 870 deletions.
130 changes: 63 additions & 67 deletions test/blockchain_helpers.cdc
Original file line number Diff line number Diff line change
@@ -1,80 +1,76 @@
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
}

/// 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, [])

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! UInt64
}

/// 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])
/// 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! UFix64
if scriptResult.status == Test.ResultStatus.failed {
panic(scriptResult.error!.message)
}
return scriptResult.returnValue! as! UFix64
}

/// 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]
)
/// 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]
)

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]
)
/// 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)
}

/// 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))
}
/// 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 f6ad263

Please sign in to comment.