ZcashLightClientKit/Tests/TestUtils/MockTransactionRepository.s...

231 lines
6.4 KiB
Swift
Raw Normal View History

//
// MockTransactionRepository.swift
// ZcashLightClientKit-Unit-Tests
//
// Created by Francisco Gindre on 12/6/19.
//
import Foundation
@testable import ZcashLightClientKit
2021-09-23 06:26:41 -07:00
class MockTransactionRepository {
enum Kind {
case sent
case received
}
2020-10-20 07:07:42 -07:00
var unminedCount: Int
var receivedCount: Int
var sentCount: Int
var scannedHeight: BlockHeight
var reference: [Kind] = []
2021-09-23 06:26:41 -07:00
var network: ZcashNetwork
var transactionsNG: [Transaction.Overview] = []
var receivedTransactionsNG: [Transaction.Received] = []
var sentTransactionsNG: [Transaction.Sent] = []
var allCount: Int {
receivedCount + sentCount
}
2021-09-23 06:26:41 -07:00
init(
unminedCount: Int,
receivedCount: Int,
sentCount: Int,
scannedHeight: BlockHeight,
2021-09-23 06:26:41 -07:00
network: ZcashNetwork
) {
self.unminedCount = unminedCount
self.receivedCount = receivedCount
self.sentCount = sentCount
self.scannedHeight = scannedHeight
2021-07-28 09:59:10 -07:00
self.network = network
}
2021-09-23 06:26:41 -07:00
func referenceArray() -> [Kind] {
2021-09-23 06:26:41 -07:00
var template: [Kind] = []
for _ in 0 ..< sentCount {
template.append(.sent)
}
for _ in 0 ..< receivedCount {
template.append(.received)
}
2021-09-23 06:26:41 -07:00
return template.shuffled()
}
func randomBlockHeight() -> BlockHeight {
2021-09-15 05:21:29 -07:00
BlockHeight.random(in: network.constants.saplingActivationHeight ... 1_000_000)
}
2021-09-23 06:26:41 -07:00
func randomTimeInterval() -> TimeInterval {
Double.random(in: Date().timeIntervalSince1970 - 1000000.0 ... Date().timeIntervalSince1970)
}
}
extension MockTransactionRepository.Kind: Equatable {}
2021-09-23 06:26:41 -07:00
// MARK: - TransactionRepository
extension MockTransactionRepository: TransactionRepository {
func closeDBConnection() { }
2021-09-23 06:26:41 -07:00
func countAll() throws -> Int {
allCount
}
func countUnmined() throws -> Int {
unminedCount
}
func blockForHeight(_ height: BlockHeight) throws -> Block? {
nil
}
func findBy(id: Int) throws -> Transaction.Overview? {
transactionsNG.first(where: { $0.id == id })
2021-09-23 06:26:41 -07:00
}
func findBy(rawId: Data) throws -> Transaction.Overview? {
transactionsNG.first(where: { $0.rawID == rawId })
2021-09-23 06:26:41 -07:00
}
func lastScannedHeight() throws -> BlockHeight {
return scannedHeight
2021-09-23 06:26:41 -07:00
}
func isInitialized() throws -> Bool {
true
}
}
enum MockTransactionRepositoryError: Error {
case notImplemented
}
extension MockTransactionRepository {
func generateNG() {
var txArray: [Transaction.Overview] = []
reference = referenceArray()
for index in 0 ..< reference.count {
txArray.append(mockTx(index: index, kind: reference[index]))
}
transactionsNG = txArray
}
func mockTx(index: Int, kind: Kind) -> Transaction.Overview {
switch kind {
case .received:
return mockReceived(index)
case .sent:
return mockSent(index)
}
}
func mockSent(_ index: Int) -> Transaction.Overview {
return Transaction.Overview(
blocktime: randomTimeInterval(),
expiryHeight: BlockHeight.max,
fee: Zatoshi(2),
id: index,
index: index,
isWalletInternal: true,
hasChange: true,
memoCount: 0,
minedHeight: randomBlockHeight(),
raw: Data(),
rawID: Data(),
receivedNoteCount: 0,
sentNoteCount: 1,
value: Zatoshi(-Int64.random(in: 1 ... Zatoshi.Constants.oneZecInZatoshi))
)
}
func mockReceived(_ index: Int) -> Transaction.Overview {
return Transaction.Overview(
blocktime: randomTimeInterval(),
expiryHeight: BlockHeight.max,
fee: Zatoshi(2),
id: index,
index: index,
isWalletInternal: true,
hasChange: true,
memoCount: 0,
minedHeight: randomBlockHeight(),
raw: Data(),
rawID: Data(),
receivedNoteCount: 1,
sentNoteCount: 0,
value: Zatoshi(Int64.random(in: 1 ... Zatoshi.Constants.oneZecInZatoshi))
)
}
func slice(txs: [Transaction.Overview], offset: Int, limit: Int) -> [Transaction.Overview] {
guard offset < txs.count else { return [] }
return Array(txs[offset ..< min(offset + limit, txs.count - offset)])
}
func find(id: Int) throws -> Transaction.Overview {
guard let transaction = transactionsNG.first(where: { $0.id == id }) else {
throw TransactionRepositoryError.notFound
}
return transaction
}
func find(rawID: Data) throws -> Transaction.Overview {
guard let transaction = transactionsNG.first(where: { $0.rawID == rawID }) else {
throw TransactionRepositoryError.notFound
}
return transaction
}
func find(offset: Int, limit: Int, kind: TransactionKind) throws -> [ZcashLightClientKit.Transaction.Overview] {
throw MockTransactionRepositoryError.notImplemented
}
func find(in range: BlockRange, limit: Int, kind: TransactionKind) throws -> [Transaction.Overview] {
throw MockTransactionRepositoryError.notImplemented
}
func find(from: Transaction.Overview, limit: Int, kind: TransactionKind) throws -> [Transaction.Overview] {
throw MockTransactionRepositoryError.notImplemented
}
func findReceived(offset: Int, limit: Int) throws -> [Transaction.Received] {
throw MockTransactionRepositoryError.notImplemented
}
func findSent(offset: Int, limit: Int) throws -> [Transaction.Sent] {
throw MockTransactionRepositoryError.notImplemented
}
func findSent(in range: BlockRange, limit: Int) throws -> [Transaction.Sent] {
throw MockTransactionRepositoryError.notImplemented
}
func findSent(rawID: Data) throws -> Transaction.Sent {
throw MockTransactionRepositoryError.notImplemented
}
}
extension Array {
2021-09-23 06:26:41 -07:00
func indices(where function: (_ element: Element) -> Bool) -> [Int]? {
guard !self.isEmpty else { return nil }
var idx: [Int] = []
for index in 0 ..< self.count {
if function(self[index]) {
idx.append(index)
}
}
2021-09-23 06:26:41 -07:00
guard !idx.isEmpty else { return nil }
return idx
}
}