Skip to content

Commit

Permalink
Allow dynamic specification of account for more transaction types (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nef10 authored Jul 13, 2024
1 parent 20ac642 commit 49a0d32
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
9 changes: 7 additions & 2 deletions Sources/SwiftBeanCountWealthsimpleMapper/LedgerLookup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,13 @@ struct LedgerLookup {
key = MetaDataKeys.rounding
}
guard let name = ledger.accounts.first(where: { accountTypes.contains($0.name.accountType) && $0.metaData[key]?.contains(account.number) ?? false })?.name else {
if case .transactionType(.paymentSpend) = type {
return WealthsimpleLedgerMapper.fallbackExpenseAccountName
if case let .transactionType(transactionType) = type {
switch transactionType {
case .onlineBillPayment, .deposit, .paymentSpend, .transferIn, .transferOut, .paymentTransferIn, .paymentTransferOut, .withdrawal:
return WealthsimpleLedgerMapper.fallbackExpenseAccountName
default:
throw WealthsimpleConversionError.missingAccount(key, account.number, accountTypes.map { $0.rawValue }.joined(separator: ", or "))
}
}
throw WealthsimpleConversionError.missingAccount(key, account.number, accountTypes.map { $0.rawValue }.joined(separator: ", or "))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public struct WealthsimpleLedgerMapper { // swiftlint:disable:this type_body_len

/// Fallback account for payments if not account with the correct meta data could be found
///
/// Only used for transaction type payment spend
/// Only used for certain transaction types
public static let fallbackExpenseAccountName = try! AccountName("Expenses:TODO") // swiftlint:disable:this force_try

/// Payee used for fee transactions
Expand Down
25 changes: 12 additions & 13 deletions Tests/SwiftBeanCountWealthsimpleMapperTests/LedgerLookupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ struct TestAccount: Wealthsimple.Account {
final class LedgerLookupTests: XCTestCase {

private let accountName = try! AccountName("Assets:Test") // swiftlint:disable:this force_try
private var ledger = Ledger()
private var ledgerLookup: LedgerLookup!

override func setUpWithError() throws {
ledger = Ledger()
ledgerLookup = LedgerLookup(ledger)
}

func testLedgerAccountCommoditySymbol() throws {
let name2 = try AccountName("Assets:Test1")
let symbol = "CAD"
let ledger = Ledger()
var ledgerLookup = LedgerLookup(ledger)

// account does not exist
XCTAssertNil(ledgerLookup.ledgerAccountCommoditySymbol(of: accountName))
Expand All @@ -38,8 +43,6 @@ final class LedgerLookupTests: XCTestCase {

func testLedgerAccountNameOf() throws {
let account = TestAccount(number: "abc")
let ledger = Ledger()
var ledgerLookup = LedgerLookup(ledger)

// not found
assert(
Expand All @@ -65,8 +68,6 @@ final class LedgerLookupTests: XCTestCase {
}

func testLedgerAccountNameFor() throws {
let ledger = Ledger()
var ledgerLookup = LedgerLookup(ledger)
var number = "abc123"

// fallback for payment spend
Expand All @@ -76,6 +77,8 @@ final class LedgerLookupTests: XCTestCase {
// not found
assert(try ledgerLookup.ledgerAccountName(for: .rounding, in: TestAccount(number: number), ofType: [.income]),
throws: WealthsimpleConversionError.missingAccount(MetaDataKeys.rounding, number, "Income"))
assert(try ledgerLookup.ledgerAccountName(for: .transactionType(.dividend), in: TestAccount(number: number), ofType: [.income]),
throws: WealthsimpleConversionError.missingAccount("\(MetaDataKeys.prefix)\(TransactionType.dividend)", number, "Income"))

// rounding
try ledger.add(Account(name: accountName, metaData: [MetaDataKeys.rounding: number]))
Expand Down Expand Up @@ -112,7 +115,6 @@ final class LedgerLookupTests: XCTestCase {
}

func testDoesTransactionExistInLedger() {
let ledger = Ledger()
var metaData = TransactionMetaData(date: Date(), metaData: [MetaDataKeys.id: "abc"])
var transaction = Transaction(metaData: metaData, postings: [])
ledger.add(transaction)
Expand Down Expand Up @@ -147,11 +149,10 @@ final class LedgerLookupTests: XCTestCase {
}

func testDoesPriceExistInLedger() throws {
let ledger = Ledger()
let date = Date()
var price = try Price(date: date, commoditySymbol: "CAD", amount: Amount(number: Decimal(1), commoditySymbol: "EUR"))
try ledger.add(price)
let ledgerLookup = LedgerLookup(ledger)
ledgerLookup = LedgerLookup(ledger)

// same price
XCTAssert(ledgerLookup.doesPriceExistInLedger(price))
Expand All @@ -170,11 +171,10 @@ final class LedgerLookupTests: XCTestCase {
}

func testDoesBalanceExistInLedger() throws {
let ledger = Ledger()
let date = Date()
var balance = Balance(date: date, accountName: accountName, amount: Amount(number: Decimal(1), commoditySymbol: "USD"))
ledger.add(balance)
let ledgerLookup = LedgerLookup(ledger)
ledgerLookup = LedgerLookup(ledger)

// same balance
XCTAssert(ledgerLookup.doesBalanceExistInLedger(balance))
Expand All @@ -197,10 +197,9 @@ final class LedgerLookupTests: XCTestCase {
}

func testCommoditySymbolForAssetSymbol() throws {
let ledger = Ledger()
var commodity = Commodity(symbol: "EUR")
try ledger.add(commodity)
var ledgerLookup = LedgerLookup(ledger)
ledgerLookup = LedgerLookup(ledger)

// not existing
assert(
Expand Down

0 comments on commit 49a0d32

Please sign in to comment.