2019-11-18 11:49:36 -08:00
|
|
|
//
|
|
|
|
// TransactionRepositoryTests.swift
|
|
|
|
// ZcashLightClientKit-Unit-Tests
|
|
|
|
//
|
|
|
|
// Created by Francisco Gindre on 11/16/19.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
2022-02-28 09:03:20 -08:00
|
|
|
@testable import TestUtils
|
2019-11-18 11:49:36 -08:00
|
|
|
@testable import ZcashLightClientKit
|
2021-09-23 06:26:41 -07:00
|
|
|
|
|
|
|
// swiftlint:disable implicitly_unwrapped_optional force_unwrapping
|
2019-11-18 11:49:36 -08:00
|
|
|
class TransactionRepositoryTests: XCTestCase {
|
|
|
|
var transactionRepository: TransactionRepository!
|
|
|
|
|
|
|
|
override func setUp() {
|
2021-09-23 06:26:41 -07:00
|
|
|
super.setUp()
|
2022-10-21 14:19:08 -07:00
|
|
|
|
2022-10-20 13:22:24 -07:00
|
|
|
transactionRepository = try! TestDbBuilder.transactionRepository()
|
2019-11-18 11:49:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tearDown() {
|
2021-09-23 06:26:41 -07:00
|
|
|
super.tearDown()
|
2019-11-18 11:49:36 -08:00
|
|
|
transactionRepository = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCount() {
|
|
|
|
var count: Int?
|
2021-09-23 06:26:41 -07:00
|
|
|
XCTAssertNoThrow(try { count = try self.transactionRepository.countAll() }())
|
2019-11-18 11:49:36 -08:00
|
|
|
XCTAssertNotNil(count)
|
2022-10-21 09:54:53 -07:00
|
|
|
XCTAssertEqual(count, 21)
|
2019-11-18 11:49:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testCountUnmined() {
|
|
|
|
var count: Int?
|
2021-09-23 06:26:41 -07:00
|
|
|
XCTAssertNoThrow(try { count = try self.transactionRepository.countUnmined() }())
|
2019-11-18 11:49:36 -08:00
|
|
|
XCTAssertNotNil(count)
|
|
|
|
XCTAssertEqual(count, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFindById() {
|
2022-12-15 03:27:59 -08:00
|
|
|
var transaction: TransactionNG.Overview!
|
|
|
|
XCTAssertNoThrow(try { transaction = try self.transactionRepository.find(id: 10) }())
|
|
|
|
|
2019-11-18 11:49:36 -08:00
|
|
|
XCTAssertEqual(transaction.id, 10)
|
2022-10-21 14:19:08 -07:00
|
|
|
XCTAssertEqual(transaction.minedHeight, 663942)
|
2022-12-15 03:27:59 -08:00
|
|
|
XCTAssertEqual(transaction.index, 5)
|
2019-11-18 11:49:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testFindByTxId() {
|
2022-12-15 03:27:59 -08:00
|
|
|
var transaction: TransactionNG.Overview!
|
2022-10-21 14:19:08 -07:00
|
|
|
|
|
|
|
let id = Data(fromHexEncodedString: "01af48bcc4e9667849a073b8b5c539a0fc19de71aac775377929dc6567a36eff")!
|
2019-11-18 11:49:36 -08:00
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
XCTAssertNoThrow(
|
2022-12-15 03:27:59 -08:00
|
|
|
try { transaction = try self.transactionRepository.find(rawID: id) }()
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
|
|
|
|
2022-10-21 14:19:08 -07:00
|
|
|
XCTAssertEqual(transaction.id, 8)
|
|
|
|
XCTAssertEqual(transaction.minedHeight, 663922)
|
2022-12-15 03:27:59 -08:00
|
|
|
XCTAssertEqual(transaction.index, 1)
|
2019-11-18 11:49:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testFindAllSentTransactions() {
|
|
|
|
var transactions: [ConfirmedTransactionEntity]?
|
2019-12-16 14:25:45 -08:00
|
|
|
XCTAssertNoThrow(try { transactions = try self.transactionRepository.findAllSentTransactions(offset: 0, limit: Int.max) }())
|
2019-11-18 11:49:36 -08:00
|
|
|
guard let txs = transactions else {
|
|
|
|
XCTFail("find all sent transactions returned no transactions")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-21 14:19:08 -07:00
|
|
|
XCTAssertEqual(txs.count, 13)
|
2019-11-18 11:49:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testFindAllReceivedTransactions() {
|
|
|
|
var transactions: [ConfirmedTransactionEntity]?
|
2019-12-16 14:25:45 -08:00
|
|
|
XCTAssertNoThrow(try { transactions = try self.transactionRepository.findAllReceivedTransactions(offset: 0, limit: Int.max) }())
|
2019-11-18 11:49:36 -08:00
|
|
|
guard let txs = transactions else {
|
|
|
|
XCTFail("find all received transactions returned no transactions")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-21 14:19:08 -07:00
|
|
|
XCTAssertEqual(txs.count, 7)
|
2019-11-18 11:49:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testFindAllTransactions() {
|
|
|
|
var transactions: [ConfirmedTransactionEntity]?
|
2019-12-16 14:25:45 -08:00
|
|
|
XCTAssertNoThrow(try { transactions = try self.transactionRepository.findAll(offset: 0, limit: Int.max) }())
|
2019-11-18 11:49:36 -08:00
|
|
|
guard let txs = transactions else {
|
|
|
|
XCTFail("find all transactions returned no transactions")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-21 14:19:08 -07:00
|
|
|
XCTAssertEqual(txs.count, 20)
|
2019-11-18 11:49:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func testFindAllPerformance() {
|
|
|
|
// This is an example of a performance test case.
|
|
|
|
self.measure {
|
|
|
|
// Put the code you want to measure the time of here.
|
|
|
|
|
|
|
|
do {
|
2019-12-16 14:25:45 -08:00
|
|
|
_ = try self.transactionRepository.findAll(offset: 0, limit: Int.max)
|
2019-11-18 11:49:36 -08:00
|
|
|
} catch {
|
|
|
|
XCTFail("find all failed")
|
|
|
|
}
|
2020-10-19 17:01:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFindAllFrom() throws {
|
2021-09-23 06:26:41 -07:00
|
|
|
guard
|
|
|
|
let transactions = try self.transactionRepository.findAll(offset: 0, limit: Int.max),
|
|
|
|
let allFromNil = try self.transactionRepository.findAll(from: nil, limit: Int.max)
|
2020-10-19 17:01:46 -07:00
|
|
|
else {
|
|
|
|
return XCTFail("find all failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertEqual(transactions.count, allFromNil.count)
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
for transaction in transactions {
|
|
|
|
guard allFromNil.first(where: { $0.rawTransactionId == transaction.rawTransactionId }) != nil else {
|
2020-10-19 17:01:46 -07:00
|
|
|
XCTFail("not equal")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFindAllFromSlice() throws {
|
2022-10-21 14:19:08 -07:00
|
|
|
let limit = 4
|
|
|
|
let start = 7
|
2021-09-23 06:26:41 -07:00
|
|
|
guard
|
|
|
|
let transactions = try self.transactionRepository.findAll(offset: 0, limit: Int.max),
|
|
|
|
let allFromNil = try self.transactionRepository.findAll(from: transactions[start], limit: limit)
|
2020-10-19 17:01:46 -07:00
|
|
|
else {
|
|
|
|
return XCTFail("find all failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertEqual(limit, allFromNil.count)
|
|
|
|
|
|
|
|
let slice = transactions[start + 1 ... start + limit]
|
|
|
|
XCTAssertEqual(slice.count, allFromNil.count)
|
2021-09-23 06:26:41 -07:00
|
|
|
for transaction in slice {
|
|
|
|
guard allFromNil.first(where: { $0.rawTransactionId == transaction.rawTransactionId }) != nil else {
|
2020-10-19 17:01:46 -07:00
|
|
|
XCTFail("not equal")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFindAllFromLastSlice() throws {
|
2022-10-21 09:54:53 -07:00
|
|
|
let limit = 5
|
|
|
|
let start = 15
|
2021-09-23 06:26:41 -07:00
|
|
|
guard
|
|
|
|
let transactions = try self.transactionRepository.findAll(offset: 0, limit: Int.max),
|
|
|
|
let allFromNil = try self.transactionRepository.findAll(from: transactions[start], limit: limit)
|
2020-10-19 17:01:46 -07:00
|
|
|
else {
|
|
|
|
return XCTFail("find all failed")
|
|
|
|
}
|
2022-10-21 09:54:53 -07:00
|
|
|
|
2020-10-19 17:01:46 -07:00
|
|
|
let slice = transactions[start + 1 ..< transactions.count]
|
|
|
|
XCTAssertEqual(slice.count, allFromNil.count)
|
2021-09-23 06:26:41 -07:00
|
|
|
for transaction in slice {
|
|
|
|
guard allFromNil.first(where: { $0.rawTransactionId == transaction.rawTransactionId }) != nil else {
|
2020-10-19 17:01:46 -07:00
|
|
|
XCTFail("not equal")
|
|
|
|
return
|
|
|
|
}
|
2019-11-18 11:49:36 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 16:18:57 -07:00
|
|
|
extension Data {
|
|
|
|
init?(fromHexEncodedString string: String) {
|
|
|
|
// Convert 0 ... 9, a ... f, A ...F to their decimal value,
|
|
|
|
// return nil for all other input characters
|
2021-09-23 06:26:41 -07:00
|
|
|
func decodeNibble(bytes: UInt16) -> UInt8? {
|
|
|
|
switch bytes {
|
2020-06-03 16:18:57 -07:00
|
|
|
case 0x30 ... 0x39:
|
2021-09-23 06:26:41 -07:00
|
|
|
return UInt8(bytes - 0x30)
|
2020-06-03 16:18:57 -07:00
|
|
|
case 0x41 ... 0x46:
|
2021-09-23 06:26:41 -07:00
|
|
|
return UInt8(bytes - 0x41 + 10)
|
2020-06-03 16:18:57 -07:00
|
|
|
case 0x61 ... 0x66:
|
2021-09-23 06:26:41 -07:00
|
|
|
return UInt8(bytes - 0x61 + 10)
|
2020-06-03 16:18:57 -07:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-18 11:49:36 -08:00
|
|
|
}
|
2020-06-03 16:18:57 -07:00
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
self.init(capacity: string.utf16.count / 2)
|
2020-06-03 16:18:57 -07:00
|
|
|
var even = true
|
|
|
|
var byte: UInt8 = 0
|
2021-09-23 06:26:41 -07:00
|
|
|
for char in string.utf16 {
|
|
|
|
guard let val = decodeNibble(bytes: char) else { return nil }
|
2020-06-03 16:18:57 -07:00
|
|
|
if even {
|
|
|
|
byte = val << 4
|
|
|
|
} else {
|
|
|
|
byte += val
|
|
|
|
self.append(byte)
|
|
|
|
}
|
2021-09-23 06:26:41 -07:00
|
|
|
even.toggle()
|
2020-06-03 16:18:57 -07:00
|
|
|
}
|
|
|
|
guard even else { return nil }
|
2019-11-18 11:49:36 -08:00
|
|
|
}
|
|
|
|
}
|