2019-12-03 07:19:44 -08:00
|
|
|
//
|
|
|
|
// PendingTransactionRepositoryTests.swift
|
|
|
|
// ZcashLightClientKit-Unit-Tests
|
|
|
|
//
|
|
|
|
// Created by Francisco Gindre on 11/27/19.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
2022-02-28 09:03:20 -08:00
|
|
|
@testable import TestUtils
|
2019-12-03 07:19:44 -08:00
|
|
|
@testable import ZcashLightClientKit
|
2021-09-23 06:26:41 -07:00
|
|
|
|
|
|
|
// swiftlint:disable force_try force_unwrapping implicitly_unwrapped_optional
|
2019-12-03 07:19:44 -08:00
|
|
|
class PendingTransactionRepositoryTests: XCTestCase {
|
|
|
|
let dbUrl = try! TestDbBuilder.pendingTransactionsDbURL()
|
2022-09-30 06:45:51 -07:00
|
|
|
let recipient = SaplingAddress(validatedEncoding: "ztestsapling1ctuamfer5xjnnrdr3xdazenljx0mu0gutcf9u9e74tr2d3jwjnt0qllzxaplu54hgc2tyjdc2p6")
|
2021-09-23 06:26:41 -07:00
|
|
|
|
|
|
|
var pendingRepository: PendingTransactionRepository!
|
|
|
|
|
2019-12-03 07:19:44 -08:00
|
|
|
override func setUp() {
|
2021-09-23 06:26:41 -07:00
|
|
|
super.setUp()
|
2019-12-03 07:19:44 -08:00
|
|
|
cleanUpDb()
|
2022-10-18 10:44:36 -07:00
|
|
|
let pendingDbProvider = SimpleConnectionProvider(path: try! TestDbBuilder.pendingTransactionsDbURL().absoluteString)
|
|
|
|
let migrations = try! MigrationManager(cacheDbConnection: InMemoryDbProvider(), pendingDbConnection: pendingDbProvider, networkType: .testnet)
|
2022-10-19 07:29:15 -07:00
|
|
|
try! migrations.performMigration()
|
2022-10-19 13:13:18 -07:00
|
|
|
pendingRepository = PendingTransactionSQLDAO(dbProvider: pendingDbProvider)
|
2019-12-03 07:19:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tearDown() {
|
2021-09-23 06:26:41 -07:00
|
|
|
super.tearDown()
|
2019-12-03 07:19:44 -08:00
|
|
|
cleanUpDb()
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanUpDb() {
|
|
|
|
try? FileManager.default.removeItem(at: TestDbBuilder.pendingTransactionsDbURL())
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCreate() {
|
2021-09-23 06:26:41 -07:00
|
|
|
let transaction = createAndStoreMockedTransaction()
|
2019-12-03 07:19:44 -08:00
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
guard let id = transaction.id, id >= 0 else {
|
2019-12-03 07:19:44 -08:00
|
|
|
XCTFail("failed to create mocked transaction that was just inserted")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var expectedTx: PendingTransactionEntity?
|
2021-09-23 06:26:41 -07:00
|
|
|
XCTAssertNoThrow(try { expectedTx = try pendingRepository.find(by: id) }())
|
2019-12-03 07:19:44 -08:00
|
|
|
|
|
|
|
guard let expected = expectedTx else {
|
|
|
|
XCTFail("failed to retrieve mocked transaction by id \(id) that was just inserted")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
XCTAssertEqual(transaction.accountIndex, expected.accountIndex)
|
|
|
|
XCTAssertEqual(transaction.value, expected.value)
|
2022-09-30 06:45:51 -07:00
|
|
|
XCTAssertEqual(transaction.recipient, expected.recipient)
|
2019-12-03 07:19:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testFindById() {
|
2021-09-23 06:26:41 -07:00
|
|
|
let transaction = createAndStoreMockedTransaction()
|
2019-12-03 07:19:44 -08:00
|
|
|
|
|
|
|
var expected: PendingTransactionEntity?
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
guard let id = transaction.id else {
|
2019-12-03 07:19:44 -08:00
|
|
|
XCTFail("transaction with no id")
|
|
|
|
return
|
|
|
|
}
|
2021-09-23 06:26:41 -07:00
|
|
|
|
|
|
|
XCTAssertNoThrow(try { expected = try pendingRepository.find(by: id) }())
|
2019-12-03 07:19:44 -08:00
|
|
|
XCTAssertNotNil(expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCancel() {
|
2021-09-23 06:26:41 -07:00
|
|
|
let transaction = createAndStoreMockedTransaction()
|
|
|
|
|
|
|
|
guard let id = transaction.id else {
|
|
|
|
XCTFail("transaction with no id")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-03 07:19:44 -08:00
|
|
|
guard id >= 0 else {
|
|
|
|
XCTFail("failed to create mocked transaction that was just inserted")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
XCTAssertNoThrow(try pendingRepository.cancel(transaction))
|
2019-12-03 07:19:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testDelete() {
|
2021-09-23 06:26:41 -07:00
|
|
|
let transaction = createAndStoreMockedTransaction()
|
|
|
|
|
|
|
|
guard let id = transaction.id else {
|
|
|
|
XCTFail("transaction with no id")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-03 07:19:44 -08:00
|
|
|
guard id >= 0 else {
|
|
|
|
XCTFail("failed to create mocked transaction that was just inserted")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
XCTAssertNoThrow(try pendingRepository.delete(transaction))
|
2019-12-03 07:19:44 -08:00
|
|
|
|
|
|
|
var unexpectedTx: PendingTransactionEntity?
|
|
|
|
|
|
|
|
XCTAssertNoThrow(try { unexpectedTx = try pendingRepository.find(by: id) }())
|
|
|
|
XCTAssertNil(unexpectedTx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGetAll() {
|
2021-09-23 06:26:41 -07:00
|
|
|
var mockTransactions: [PendingTransactionEntity] = []
|
2019-12-03 07:19:44 -08:00
|
|
|
for _ in 1...100 {
|
|
|
|
mockTransactions.append(createAndStoreMockedTransaction())
|
|
|
|
}
|
|
|
|
|
|
|
|
var all: [PendingTransactionEntity]?
|
|
|
|
|
|
|
|
XCTAssertNoThrow(try { all = try pendingRepository.getAll() }())
|
|
|
|
|
|
|
|
guard let allTxs = all else {
|
|
|
|
XCTFail("failed to get all transactions")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertEqual(mockTransactions.count, allTxs.count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testUpdate() {
|
2021-09-23 06:26:41 -07:00
|
|
|
let transaction = createAndStoreMockedTransaction()
|
|
|
|
|
|
|
|
guard let id = transaction.id else {
|
|
|
|
XCTFail("transaction with no id")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-03 07:19:44 -08:00
|
|
|
var stored: PendingTransactionEntity?
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
XCTAssertNoThrow(try { stored = try pendingRepository.find(by: id) }())
|
2019-12-03 07:19:44 -08:00
|
|
|
|
|
|
|
guard stored != nil else {
|
|
|
|
XCTFail("failed to store tx")
|
|
|
|
return
|
|
|
|
}
|
2022-10-17 13:54:04 -07:00
|
|
|
|
|
|
|
let oldEncodeAttempts = stored!.encodeAttempts
|
|
|
|
let oldSubmitAttempts = stored!.submitAttempts
|
2019-12-03 07:19:44 -08:00
|
|
|
|
2022-10-17 13:54:04 -07:00
|
|
|
stored!.encodeAttempts += 1
|
|
|
|
stored!.submitAttempts += 5
|
2019-12-03 07:19:44 -08:00
|
|
|
|
|
|
|
XCTAssertNoThrow(try pendingRepository.update(stored!))
|
|
|
|
|
|
|
|
guard let updatedTransaction = try? pendingRepository.find(by: stored!.id!) else {
|
|
|
|
XCTFail("failed to retrieve updated transaction with id: \(stored!.id!)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-17 13:54:04 -07:00
|
|
|
XCTAssertEqual(updatedTransaction.encodeAttempts, oldEncodeAttempts + 1)
|
|
|
|
XCTAssertEqual(updatedTransaction.submitAttempts, oldSubmitAttempts + 5)
|
2022-09-30 06:45:51 -07:00
|
|
|
XCTAssertEqual(updatedTransaction.recipient, stored!.recipient)
|
2019-12-03 07:19:44 -08:00
|
|
|
}
|
|
|
|
|
2022-05-31 05:27:24 -07:00
|
|
|
func createAndStoreMockedTransaction(with value: Zatoshi = Zatoshi(1000)) -> PendingTransactionEntity {
|
|
|
|
var transaction = mockTransaction(with: value)
|
2019-12-03 07:19:44 -08:00
|
|
|
var id: Int?
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
XCTAssertNoThrow(try { id = try pendingRepository.create(transaction) }())
|
|
|
|
transaction.id = Int(id ?? -1)
|
|
|
|
return transaction
|
2019-12-03 07:19:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testPerformanceExample() {
|
|
|
|
// This is an example of a performance test case.
|
|
|
|
self.measure {
|
|
|
|
_ = try! pendingRepository.getAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 05:27:24 -07:00
|
|
|
private func mockTransaction(with value: Zatoshi = Zatoshi(1000)) -> PendingTransactionEntity {
|
2022-09-30 06:45:51 -07:00
|
|
|
PendingTransaction(value: value, recipient: .address(.sapling(recipient)), memo: .empty(), account: 0)
|
2019-12-03 07:19:44 -08:00
|
|
|
}
|
|
|
|
}
|