File indexing completed on 2024-11-24 04:37:57

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 /** @file
0007  * This file is a test script.
0008  *
0009  * @author Stephane MANKOWSKI / Guillaume DE BURE
0010  */
0011 #include <klocalizedstring.h>
0012 
0013 #include "skgbankincludes.h"
0014 #include "skgimportexportmanager.h"
0015 #include "skgtestmacro.h"
0016 
0017 struct test {
0018     QString fileName;
0019     QString password;
0020     QMap<QString, double> expectedAccountAmount;
0021 };
0022 
0023 /**
0024  * The main function of the unit test
0025  * @param argc the number of arguments
0026  * @param argv the list of arguments
0027  */
0028 int main(int argc, char** argv)
0029 {
0030     Q_UNUSED(argc)
0031     Q_UNUSED(argv)
0032 
0033     // Init test
0034     SKGINITTEST(true)
0035 
0036     {
0037         // Test export ledger
0038         SKGDocumentBank document1;
0039         SKGError err;
0040         SKGTESTERROR(QStringLiteral("DOC.load"), document1.load(SKGTest::getTestPath(QStringLiteral("IN")) % "/skgtestimportskg/all_types.skg"), true)
0041 
0042         {
0043             // Scope of the transaction
0044             SKGBEGINTRANSACTION(document1, QStringLiteral("EXPORT_LEDGE"), err)
0045             SKGImportExportManager imp1(&document1, QUrl::fromLocalFile(SKGTest::getTestPath(QStringLiteral("OUT")) % "/skgtestimportledger/test-obfuscated.ledger"));
0046             SKGTESTERROR(QStringLiteral("LEDGE.exportFile"), imp1.exportFile(), true)
0047         }
0048     }
0049 
0050     QVector<test> listTests;
0051     {
0052         test t1;
0053         t1.fileName = QStringLiteral("demo.ledger");
0054 
0055         QMap<QString, double> accounts;
0056         accounts[QStringLiteral("MasterCard")] = -20;
0057         accounts[QStringLiteral("Checking")] = -4124;
0058         accounts[QStringLiteral("Savings")] = -5200;
0059         accounts[QStringLiteral("Mortgage:Principal")] = -800; //Expected : 200
0060         t1.expectedAccountAmount = accounts;
0061         listTests << t1;
0062     }
0063     {
0064         test t1;
0065         t1.fileName = QStringLiteral("drewr.ledger");
0066 
0067         QMap<QString, double> accounts;
0068         accounts[QStringLiteral("Checking")] = 1366;
0069         accounts[QStringLiteral("Checking:Business")] = 30;
0070         accounts[QStringLiteral("Savings")] = -5200;
0071         accounts[QStringLiteral("MasterCard")] = -20;
0072         accounts[QStringLiteral("Mortgage:Principal")] = -800; //Expected : 200
0073         accounts[QStringLiteral("Tithe")] = -243.6;
0074         t1.expectedAccountAmount = accounts;
0075         listTests << t1;
0076     }
0077     {
0078         test t1;
0079         t1.fileName = QStringLiteral("sample.ledger");
0080 
0081         QMap<QString, double> accounts;
0082         accounts[QStringLiteral("Bank:Checking")] = 1480;
0083         accounts[QStringLiteral("Brokerage")] = 50;
0084         accounts[QStringLiteral("MasterCard")] = -70;
0085         accounts[QStringLiteral("Taxes")] = -2;
0086         t1.expectedAccountAmount = accounts;
0087         listTests << t1;
0088     }
0089     {
0090         test t1;
0091         t1.fileName = QStringLiteral("sebastien.ledger");
0092 
0093         QMap<QString, double> accounts;
0094         accounts[QStringLiteral("CMB:Compte Cheques")] = 3313.19;
0095         accounts[QStringLiteral("CMB:LIVRET A de Mme")] = 5650.00;
0096         accounts[QStringLiteral("CMB:LIVRET A de Mr")] = 5650.00;
0097         accounts[QStringLiteral("Soldes d'Ouvertures")] = -12000.00;
0098         t1.expectedAccountAmount = accounts;
0099         listTests << t1;
0100     }
0101     {
0102         test t1;
0103         t1.fileName = QStringLiteral("transfer.ledger");
0104 
0105         QMap<QString, double> accounts;
0106         accounts[QStringLiteral("Payable:hcoop.net")] = -72355001;
0107         t1.expectedAccountAmount = accounts;
0108         listTests << t1;
0109     }
0110     {
0111         test t1;
0112         t1.fileName = QStringLiteral("wow.ledger");
0113 
0114         QMap<QString, double> accounts;
0115         accounts[QStringLiteral("Tajer")] = 1973592.662; //Expected : "Orb of Deception" 1
0116         t1.expectedAccountAmount = accounts;
0117         listTests << t1;
0118     }
0119     for (const auto& t : qAsConst(listTests)) {
0120         // Test import ledger
0121         SKGDocumentBank document1;
0122         SKGTESTERROR(QStringLiteral("document1.initialize()"), document1.initialize(), true)
0123         SKGError err;
0124         {
0125             // Scope of the transaction
0126             SKGBEGINTRANSACTION(document1, QStringLiteral("EXPORT_LEDGER"), err)
0127             SKGImportExportManager imp1(&document1, QUrl::fromLocalFile(SKGTest::getTestPath(QStringLiteral("IN")) % "/skgtestimportledger/" % t.fileName));
0128             SKGTESTERROR(t.fileName % QStringLiteral(".importFile"), imp1.importFile(), true)
0129         }
0130 
0131         // document1.dump(DUMPOPERATION | DUMPACCOUNT | DUMPUNIT | DUMPPAYEE | DUMPCATEGORY);
0132 
0133         QStringList keys = t.expectedAccountAmount.keys();
0134         for (const auto& k : qAsConst(keys)) {
0135             SKGAccountObject account;
0136             SKGTESTERROR(t.fileName % QStringLiteral(".getObjectByName(") % k % ")", SKGNamedObject::getObjectByName(&document1, QStringLiteral("v_account"), k, account), true)
0137             SKGTEST(t.fileName % "." % k % QStringLiteral(".getCurrentAmount"), SKGServices::toCurrencyString(account.getCurrentAmount(), QLatin1String(""), 2), SKGServices::toCurrencyString(t.expectedAccountAmount[k], QLatin1String(""), 2))
0138         }
0139     }
0140     // End test
0141     SKGENDTEST()
0142 }