Nostro/tests/ShouldStoreTransactions/main.cpp

100 lines
5.9 KiB
C++
Raw Normal View History

2026-11-01 18:35:23 +01:00
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// The transaction store (Nostro:Account): what a sync stores and what it drops as already stored, the queries, the
// table's file name, a version 1 table migrating on its first open, and how transactions and accounts format.
#include <stdlib.h>
#include <time.h>
import std;
import Nostro;
import Crafter.DB;
#include "../Support/Check.inc"
using namespace Nostro;
using namespace std::chrono;
using Test::Check;
using Test::Equal;
namespace {
// Transaction version 1 as nostro wrote it; the same members give the same fingerprint, so it writes a real old table.
struct OldTransaction {
std::string name;
std::string bic;
std::string iban;
std::string description;
Currency currency;
std::int64_t amount;
system_clock::time_point timePoint;
static constexpr std::uint32_t version = 1;
static constexpr auto orderedBy = &OldTransaction::timePoint;
};
const system_clock::time_point start = sys_days(year(2026) / 10 / 1) + hours(10);
Transaction Made(std::string_view name, std::int32_t minute, std::string_view id, std::int64_t amount = -100, std::string_view description = "groceries", std::string_view iban = "NL02BUNQ0000000002") {
return Transaction{std::string(name), "BUNQNL2A", std::string(iban), std::string(description), Currency::Euro, amount, start + minutes(minute), std::string(id), "BUNQ/PAYMENT"};
}
std::vector<std::string> Names(const std::vector<Transaction>& rows) {
return rows | std::views::transform(&Transaction::name) | std::ranges::to<std::vector<std::string>>();
}
}
int main() {
const Test::Directory directory("transactions");
cachePath = directory.path;
setenv("TZ", "UTC", 1);
tzset();
const Account account{"Main/Spending", "BUNQNL2A", "NL01BUNQ0000000001", Currency::Euro, -1250};
Equal(std::format("{}", account), std::string("Main/Spending\tBUNQNL2A NL01BUNQ0000000001\t-€12.50"), "an account formats as name, BIC and IBAN, balance");
const std::string key = "test/sandbox/1/2";
Check(account.GetLastTransactionTime(key) == system_clock::time_point::min(), "a new table has no last transaction");
Account store = account;
store.SaveTransactions(key, {Made("Bob", 1, "1"), Made("Alice", 0, "0")});
Check(store.GetLastTransactionTime(key) == start + minutes(1), "rows arrive in any order and are stored sorted");
Check(std::filesystem::exists(directory.path / "transactions" / key / "Main_Spending.cdb"), "a '/' in the account name becomes '_' in the file name");
// At the last stored timestamp only a new id is new; a row without an id there cannot be told apart.
store.SaveTransactions(key, {Made("Bob again", 1, "1"), Made("Carol", 1, "2", -250, "rent", "NL03BUNQ0000000003"), Made("Nobody", 1, ""), Made("Old", 0, "9"), Made("Dave", 2, "3", 5000, "salary")});
Equal(Names(store.FindTransactions(key, {})).size(), std::size_t{4}, "a sync that overlaps the last one stores only what is new");
Check(Names(store.FindTransactions(key, {})) == std::vector<std::string>{"Dave", "Carol", "Bob", "Alice"}, "newest first, and the later of two at one time first");
Check(Names(store.FindTransactions(key, {.search = "CAROL"})) == std::vector<std::string>{"Carol"}, "search matches the name, whatever its case");
Check(Names(store.FindTransactions(key, {.search = "Salary"})) == std::vector<std::string>{"Dave"}, "search matches the description");
Check(Names(store.FindTransactions(key, {.search = "nl03"})) == std::vector<std::string>{"Carol"}, "search matches the IBAN");
Check(Names(store.FindTransactions(key, {.amount = -100})) == std::vector<std::string>{"Bob", "Alice"}, "an exact amount");
Check(Names(store.FindTransactions(key, {.from = start + minutes(1), .to = start + minutes(1)})) == std::vector<std::string>{"Carol", "Bob"}, "an inclusive time range");
Check(Names(store.FindTransactions(key, {.limit = 1})) == std::vector<std::string>{"Dave"}, "at most `limit`");
Check(store.FindTransactions(key, {.search = "nothing like it"}).empty(), "a search that matches nothing");
const Transaction alice = store.FindTransactions(key, {.search = "alice"}).at(0);
Equal(alice.id, std::string("0"), "the bank's id is stored");
Equal(alice.kind, std::string("BUNQ/PAYMENT"), "and its classification");
Equal(std::format("{}", alice), std::string("2026-10-01 10:00\t-€1.00\tAlice\tNL02BUNQ0000000002\tgroceries"), "a transaction formats as one line");
// A table written by version 1 migrates when it is first opened, and its rows come back without id or kind.
const std::filesystem::path oldFile = directory.path / "transactions" / "test" / "v1" / "Old.cdb";
{
Crafter::DB::Table<OldTransaction> old(oldFile);
const std::array rows = {
OldTransaction{"First", "", "NL04", "old", Currency::Euro, 100, start},
OldTransaction{"Second", "", "NL05", "old", Currency::Euro, 200, start + minutes(5)},
};
old.Append(std::span<const OldTransaction>(rows));
old.Close();
}
Account migrated{"Old", "", "", Currency::Euro, 0};
const std::vector<Transaction> rows = migrated.FindTransactions("test/v1", {});
Check(Names(rows) == std::vector<std::string>{"Second", "First"}, "a version 1 table reads after its migration");
Check(rows.size() == 2 && rows[0].id.empty() && rows[0].kind.empty(), "migrated rows have no id or kind");
Check(std::filesystem::exists(std::format("{}.v1.bak", oldFile.string())), "the migration keeps the old file");
migrated.SaveTransactions("test/v1", {Made("Second", 5, "55"), Made("Third", 6, "66")});
Check(Names(migrated.FindTransactions("test/v1", {})) == std::vector<std::string>{"Third", "Second", "First"}, "a migrated row at the last timestamp is not stored again under an id");
return Test::Result();
}