Skip to content

Commit

Permalink
test: better messages when tests fail for low balance
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed May 10, 2024
1 parent 1b0b50a commit f27bada
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ class LiveTxBuilderTest {
wallet.commit()
println("Balance: ${wallet.getBalance().total}")

assert(wallet.getBalance().total > 0uL)
assert(wallet.getBalance().total > 0uL) {
"Wallet balance must be greater than 0! Please send funds to ${wallet.revealNextAddress(KeychainKind.EXTERNAL).address.asString()} and try again."
}

val recipient: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.SIGNET)
val psbt: Psbt = TxBuilder()
Expand All @@ -59,7 +61,9 @@ class LiveTxBuilderTest {
wallet.commit()
println("Balance: ${wallet.getBalance().total}")

assert(wallet.getBalance().total > 0uL)
assert(wallet.getBalance().total > 0uL) {
"Wallet balance must be greater than 0! Please send funds to ${wallet.revealNextAddress(KeychainKind.EXTERNAL).address.asString()} and try again."
}

val recipient1: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.SIGNET)
val recipient2: Address = Address("tb1qw2c3lxufxqe2x9s4rdzh65tpf4d7fssjgh8nv6", Network.SIGNET)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class LiveWalletTest {
val balance: Balance = wallet.getBalance()
println("Balance: $balance")

assert(wallet.getBalance().total > 0uL)
assert(wallet.getBalance().total > 0uL) {
"Wallet balance must be greater than 0! Please send funds to ${wallet.revealNextAddress(KeychainKind.EXTERNAL).address.asString()} and try again."
}

println("Transactions count: ${wallet.transactions().count()}")
val transactions = wallet.transactions().take(3)
Expand All @@ -59,7 +61,6 @@ class LiveWalletTest {
wallet.applyUpdate(update)
wallet.commit()
println("Balance: ${wallet.getBalance().total}")
println("New address: ${wallet.revealNextAddress(KeychainKind.EXTERNAL).address}")

assert(wallet.getBalance().total > 0uL) {
"Wallet balance must be greater than 0! Please send funds to ${wallet.revealNextAddress(KeychainKind.EXTERNAL).address} and try again."
Expand All @@ -82,7 +83,7 @@ class LiveWalletTest {
println("Txid is: ${tx.txid()}")

val txFee: ULong = wallet.calculateFee(tx)
println("Tx fee is: ${txFee}")
println("Tx fee is: $txFee")

val feeRate: FeeRate = wallet.calculateFeeRate(tx)
println("Tx fee rate is: ${feeRate.toSatPerVbCeil()} sat/vB")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class LiveTxBuilderTest {
wallet.commit()
println("Balance: ${wallet.getBalance().total}")

assert(wallet.getBalance().total > 0uL)
assert(wallet.getBalance().total > 0uL) {
"Wallet balance must be greater than 0! Please send funds to ${wallet.revealNextAddress(KeychainKind.EXTERNAL).address.asString()} and try again."
}

val recipient: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.SIGNET)
val psbt: Psbt = TxBuilder()
Expand All @@ -58,7 +60,9 @@ class LiveTxBuilderTest {
wallet.commit()
println("Balance: ${wallet.getBalance().total}")

assert(wallet.getBalance().total > 0uL)
assert(wallet.getBalance().total > 0uL) {
"Wallet balance must be greater than 0! Please send funds to ${wallet.revealNextAddress(KeychainKind.EXTERNAL).address.asString()} and try again."
}

val recipient1: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.SIGNET)
val recipient2: Address = Address("tb1qw2c3lxufxqe2x9s4rdzh65tpf4d7fssjgh8nv6", Network.SIGNET)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class LiveWalletTest {
wallet.commit()
println("Balance: ${wallet.getBalance().total}")

assert(wallet.getBalance().total > 0uL)
assert(wallet.getBalance().total > 0uL) {
"Wallet balance must be greater than 0! Please send funds to ${wallet.revealNextAddress(KeychainKind.EXTERNAL).address.asString()} and try again."
}

println("Transactions count: ${wallet.transactions().count()}")
val transactions = wallet.transactions().take(3)
Expand All @@ -55,7 +57,6 @@ class LiveWalletTest {
wallet.applyUpdate(update)
wallet.commit()
println("Balance: ${wallet.getBalance().total}")
println("New address: ${wallet.revealNextAddress(KeychainKind.EXTERNAL).address.asString()}")

assert(wallet.getBalance().total > 0uL) {
"Wallet balance must be greater than 0! Please send funds to ${wallet.revealNextAddress(KeychainKind.EXTERNAL).address.asString()} and try again."
Expand Down
12 changes: 10 additions & 2 deletions bdk-python/tests/test_live_tx_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def test_tx_builder(self):
wallet.apply_update(update)
wallet.commit()

self.assertGreater(wallet.get_balance().total, 0)
self.assertGreater(
wallet.get_balance().total,
0,
f"Wallet balance must be greater than 0! Please send funds to {wallet.reveal_next_address(bdk.KeychainKind.EXTERNAL).address.as_string()} and try again."
)

recipient = bdk.Address(
address="tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989",
Expand Down Expand Up @@ -68,7 +72,11 @@ def complex_tx_builder(self):
wallet.apply_update(update)
wallet.commit()

self.assertGreater(wallet.get_balance().total, 0)
self.assertGreater(
wallet.get_balance().total,
0,
f"Wallet balance must be greater than 0! Please send funds to {wallet.reveal_next_address(bdk.KeychainKind.EXTERNAL).address.as_string()} and try again."
)

recipient1 = bdk.Address(
address="tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989",
Expand Down
12 changes: 10 additions & 2 deletions bdk-python/tests/test_live_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def test_synced_balance(self):
wallet.apply_update(update)
wallet.commit()

self.assertGreater(wallet.get_balance().total, 0)
self.assertGreater(
wallet.get_balance().total,
0,
f"Wallet balance must be greater than 0! Please send funds to {wallet.reveal_next_address(bdk.KeychainKind.EXTERNAL).address.as_string()} and try again."
)

print(f"Transactions count: {len(wallet.transactions())}")
transactions = wallet.transactions()[:3]
Expand Down Expand Up @@ -64,7 +68,11 @@ def test_broadcast_transaction(self):
wallet.apply_update(update)
wallet.commit()

self.assertGreater(wallet.get_balance().total, 0)
self.assertGreater(
wallet.get_balance().total,
0,
f"Wallet balance must be greater than 0! Please send funds to {wallet.reveal_next_address(bdk.KeychainKind.EXTERNAL).address.as_string()} and try again."
)

recipient = bdk.Address(
address="tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989",
Expand Down
26 changes: 18 additions & 8 deletions bdk-swift/Tests/BitcoinDevKitTests/LiveTxBuilderTests.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import XCTest
@testable import BitcoinDevKit

let SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net"
let TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud"
private let SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net"
private let TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud"

final class LiveTxBuilderTests: XCTestCase {
var dbFilePath: URL!
Expand Down Expand Up @@ -45,9 +45,14 @@ final class LiveTxBuilderTests: XCTestCase {
parallelRequests: 1
)
try wallet.applyUpdate(update: update)
try wallet.commit()
let _ = try wallet.commit()
let address = try wallet.revealNextAddress(keychain: KeychainKind.external).address.asString()

XCTAssertGreaterThan(wallet.getBalance().total, UInt64(0), "Wallet must have positive balance, please add funds")
XCTAssertGreaterThan(
wallet.getBalance().total,
UInt64(0),
"Wallet must have positive balance, please send funds to \(address)"
)

let recipient: Address = try Address(address: "tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", network: .signet)
let psbt: Psbt = try TxBuilder()
Expand Down Expand Up @@ -82,9 +87,14 @@ final class LiveTxBuilderTests: XCTestCase {
parallelRequests: 1
)
try wallet.applyUpdate(update: update)
try wallet.commit()

XCTAssertGreaterThan(wallet.getBalance().total, UInt64(0), "Wallet must have positive balance, please add funds")
let _ = try wallet.commit()
let address = try wallet.revealNextAddress(keychain: KeychainKind.external).address.asString()

XCTAssertGreaterThan(
wallet.getBalance().total,
UInt64(0),
"Wallet must have positive balance, please send funds to \(address)"
)

let recipient1: Address = try Address(address: "tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", network: .signet)
let recipient2: Address = try Address(address: "tb1qw2c3lxufxqe2x9s4rdzh65tpf4d7fssjgh8nv6", network: .signet)
Expand All @@ -100,7 +110,7 @@ final class LiveTxBuilderTests: XCTestCase {
.enableRbf()
.finish(wallet: wallet)

try! wallet.sign(psbt: psbt)
let _ = try! wallet.sign(psbt: psbt)

XCTAssertTrue(psbt.serialize().hasPrefix("cHNi"), "PSBT should start with cHNI")
}
Expand Down
25 changes: 18 additions & 7 deletions bdk-swift/Tests/BitcoinDevKitTests/LiveWalletTests.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import XCTest
@testable import BitcoinDevKit

let SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net"
let TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud"
private let SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net"
private let TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud"

final class LiveWalletTests: XCTestCase {
var dbFilePath: URL!
Expand Down Expand Up @@ -45,9 +45,14 @@ final class LiveWalletTests: XCTestCase {
parallelRequests: 1
)
try wallet.applyUpdate(update: update)
try wallet.commit()
let _ = try wallet.commit()
let address = try wallet.revealNextAddress(keychain: KeychainKind.external).address.asString()

XCTAssertGreaterThan(wallet.getBalance().total, UInt64(0))
XCTAssertGreaterThan(
wallet.getBalance().total,
UInt64(0),
"Wallet must have positive balance, please send funds to \(address)"
)

print("Transactions count: \(wallet.transactions().count)")
let transactions = wallet.transactions().prefix(3)
Expand Down Expand Up @@ -78,9 +83,15 @@ final class LiveWalletTests: XCTestCase {
parallelRequests: 1
)
try wallet.applyUpdate(update: update)
try wallet.commit()

XCTAssertGreaterThan(wallet.getBalance().total, UInt64(0), "Wallet must have positive balance, please add funds")
let _ = try wallet.commit()
let address = try wallet.revealNextAddress(keychain: KeychainKind.external).address.asString()

XCTAssertGreaterThan(
wallet.getBalance().total,
UInt64(0),
"Wallet must have positive balance, please send funds to \(address)"
)


print("Balance: \(wallet.getBalance().total)")

Expand Down

0 comments on commit f27bada

Please sign in to comment.