File: t_kraftdoc.cpp

package info (click to toggle)
kraft 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,112 kB
  • sloc: cpp: 28,535; sql: 1,248; perl: 396; xml: 330; python: 101; sh: 92; makefile: 10
file content (268 lines) | stat: -rw-r--r-- 8,971 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <QTest>
#include <QSql>
#include <QSqlDatabase>
#include <QSqlQuery>

#include "testconfig.h"
#include "kraftdb.h"
#include "kraftdoc.h"
#include "attribute.h"
#include "docposition.h"
#include "format.h"
#include "kraftsettings.h"
#include "format.h"

void init_test_db()
{
    const QString dbName("__test.db");

    QDir sourceDir(TESTS_PATH);
    sourceDir.cdUp();
    const QByteArray ba {sourceDir.absolutePath().toLatin1()};
    qputenv("KRAFT_HOME", ba);

    QFile::remove(dbName);

    KraftDB::self()->dbConnect("QSQLITE", dbName, QString(), QString(), QString());

    // create the tagTemplate table which is required by DocPosition::hasTag
    SqlCommandList sqls = KraftDB::self()->parseCommandFile("10_dbmigrate.sql");
    QVERIFY(sqls.size() > 0);
    KraftDB::self()->processSqlCommands(sqls);

#if 0
    sqls = KraftDB::self()->parseCommandFile("10_dbmigrate.sql");
    KraftDB::self()->processSqlCommands(sqls);

    sqls = KraftDB::self()->parseCommandFile("11_dbmigrate.sql");
    KraftDB::self()->processSqlCommands(sqls);
#endif
}

namespace {

DocPositionList buildPosList() {
    DocPositionList positions;

    // Attention: only use tags here that exist in the database, as
    // defined in the 10_migrate.sql
    DocPosition *dp1 = new DocPosition;
    dp1->setAmount(2.0);
    dp1->setUnitPrice(Geld(6.50));
    dp1->setText("Position1");
    dp1->setTags(QStringList{"Work"});

    positions.append(dp1);

    DocPosition *dp2 = new DocPosition;
    dp2->setAmount(4.0);
    dp2->setUnitPrice(Geld(12.50));
    dp2->setText("Position2");
    dp2->setTags(QStringList{"Work"});

    positions.append(dp2);

    DocPosition *dp3 = new DocPosition;
    dp3->setAmount(4.0);
    dp3->setUnitPrice(Geld(1.50));
    dp3->setText("Position3");
    dp3->setTags(QStringList{"Material"});

    positions.append(dp3);

    return positions;
}
}

class T_KraftDoc: public QObject {
    Q_OBJECT
private Q_SLOTS:
    void initTestCase()
    {
        QLocale::setDefault(QLocale::German);
        KraftSettings::self()->setDateFormat(Format::DateFormatGerman);
        init_test_db();
    }

    void cleanupTestCase() {
        const QString dbName("__test.db");

        QDir sourceDir(TESTS_PATH);
        sourceDir.cdUp();
        Q_ASSERT(QFile::remove(dbName));
    }

    void checkTablesExist() {
        const QStringList attribCols {"sortkey", "name", "description", "color"};
        QVERIFY(KraftDB::self()->checkTableExistsSqlite("tagTemplates", attribCols));
    }

    void sumPerTag() {
        KraftDoc *kraftdoc = &kDoc;

        DocPositionList positions = buildPosList();
        double fullTax = 19.0;
        double redTax = 7.0;

        const QString tmpl{"This is a template test with NETTO_SUM_PER_TAG(Work)"};

        const QString expanded = kraftdoc->resolveMacros(tmpl, positions, _date, fullTax, redTax);

        qDebug() << "Expanded text is" << expanded;
        QString shouldBe{tmpl};
        // 0xA0 is a non splitable space, no idea how to hardcode it.
        shouldBe.replace("NETTO_SUM_PER_TAG(Work)", "63,00" + QChar(0xA0) + "€");
        QCOMPARE(expanded, shouldBe);
    }

    void sumPerTagBrutto() {
        KraftDoc *kraftdoc = &kDoc;

        DocPositionList positions = buildPosList();
        double fullTax = 19.0;
        double redTax = 7.0;

        const QString tmpl{"This is a template test with NETTO_SUM_PER_TAG(Work) and BRUTTO_SUM_PER_TAG(Work) and VAT_SUM_PER_TAG(Work)"};

        const QString expanded = kraftdoc->resolveMacros(tmpl, positions, _date, fullTax, redTax);

        qDebug() << "Expanded text is" << expanded;
        QString shouldBe{tmpl};
        // 0xA0 is a non splitable space, no idea how to hardcode it.
        shouldBe.replace("NETTO_SUM_PER_TAG(Work)", "63,00" + QChar(0xA0) + "€");
        shouldBe.replace("BRUTTO_SUM_PER_TAG(Work)", "74,97" + QChar(0xA0) + "€");
        shouldBe.replace("VAT_SUM_PER_TAG(Work)", "11,97" + QChar(0xA0) + "€");
        QCOMPARE(expanded, shouldBe);
    }

    void sumPerTagBruttoNull() {
        KraftDoc *kraftdoc = &kDoc;

        DocPositionList positions = buildPosList();
        double fullTax = 19.0;
        double redTax = 7.0;

        const QString tmpl{"This is a template test with NETTO_SUM_PER_TAG(NoWork) and BRUTTO_SUM_PER_TAG(NoWork) and VAT_SUM_PER_TAG(NoWork)"};

        const QString expanded = kraftdoc->resolveMacros(tmpl, positions, _date, fullTax, redTax);

        qDebug() << "Expanded text is" << expanded;
        QString shouldBe{tmpl};
        // 0xA0 is a non splitable space, no idea how to hardcode it.
        shouldBe.replace("NETTO_SUM_PER_TAG(NoWork)", "0,00" + QChar(0xA0) + "€");
        shouldBe.replace("BRUTTO_SUM_PER_TAG(NoWork)", "0,00" + QChar(0xA0) + "€");
        shouldBe.replace("VAT_SUM_PER_TAG(NoWork)", "0,00" + QChar(0xA0) + "€");
        QCOMPARE(expanded, shouldBe);
    }

    // Verify that the sum is zero for a pos list where no pos has that tag
    void sumPerTagNoMatch() {
        KraftDoc *kraftdoc = &kDoc;

        DocPositionList positions = buildPosList();
        double fullTax = 19.0;
        double redTax = 7.0;

        const QString tmpl{"This is a template test with NETTO_SUM_PER_TAG(Plants)"};
        const QString expanded = kraftdoc->resolveMacros(tmpl, positions, _date, fullTax, redTax);

        QString shouldBe{tmpl};
        // 0xA0 is a non splitable space, no idea how to hardcode it.
        shouldBe.replace("NETTO_SUM_PER_TAG(Plants)", "0,00" + QChar(0xA0) + "€");
        QCOMPARE(expanded, shouldBe);
    }

    void ifHasTag() {
        KraftDoc *kraftdoc = &kDoc;

        DocPositionList positions = buildPosList();
        double fullTax = 19.0;
        double redTax = 7.0;

        const QString tmpl{"This template IF_ANY_HAS_TAG(Work) has the tag Work END_HAS_TAG"};
        const QString expanded = kraftdoc->resolveMacros(tmpl, positions, _date, fullTax, redTax);

        QString shouldBe{"This template has the tag Work"};
        // 0xA0 is a non splitable space, no idea how to hardcode it.
        QCOMPARE(expanded, shouldBe);
    }

    void ifHasTagNoEnd() {
        KraftDoc *kraftdoc = &kDoc;

        DocPositionList positions = buildPosList();
        double fullTax = 19.0;
        double redTax = 7.0;

        const QString tmpl{"This template IF_ANY_HAS_TAG(Work) has the tag Work without an end"};
        const QString expanded = kraftdoc->resolveMacros(tmpl, positions, _date, fullTax, redTax);

        QString shouldBe{"This template has the tag Work without an end"};
        // 0xA0 is a non splitable space, no idea how to hardcode it.
        QCOMPARE(expanded, shouldBe);
    }

    void ifHasTagDoesNotHaveTheTag() {
        KraftDoc *kraftdoc = &kDoc;

        DocPositionList positions = buildPosList();
        double fullTax = 19.0;
        double redTax = 7.0;

        const QString tmpl{"This template IF_ANY_HAS_TAG(Plants) has the tag Work END_HAS_TAG"};
        const QString expanded = kraftdoc->resolveMacros(tmpl, positions, _date, fullTax, redTax);

        QString shouldBe{"This template"};
        // 0xA0 is a non splitable space, no idea how to hardcode it.
        QCOMPARE(expanded, shouldBe);
    }

    void amountOfTag() {
        KraftDoc *kraftdoc = &kDoc;

        DocPositionList positions = buildPosList();
        double fullTax = 19.0;
        double redTax = 7.0;

        const QString tmpl{"This template has ITEM_COUNT_WITH_TAG(Work) work items"};
        const QString expanded = kraftdoc->resolveMacros(tmpl, positions, _date, fullTax, redTax);

        QString shouldBe{"This template has 2 work items"};
        // 0xA0 is a non splitable space, no idea how to hardcode it.
        QCOMPARE(expanded, shouldBe);
    }

    void dateAddDay() {
        KraftDoc *kraftdoc = &kDoc;

        DocPositionList positions = buildPosList();
        QDate d{2020, 1, 24};
        double fullTax = 19.0;
        double redTax = 7.0;

        QString tmpl{"This is 12 days later than 24.01.2020: DATE_ADD_DAYS(12)"};
        QString expanded = kraftdoc->resolveMacros(tmpl, positions, d, fullTax, redTax, Format::DateFormatGerman);

        QString shouldBe{"This is 12 days later than 24.01.2020: 05.02.2020"};
        QCOMPARE(expanded, shouldBe);

        tmpl = "This is 0 days later than 24.01.2020: DATE_ADD_DAYS(0)";
        expanded = kraftdoc->resolveMacros(tmpl, positions, d, fullTax, redTax, Format::DateFormatGerman);
        shouldBe = "This is 0 days later than 24.01.2020: 24.01.2020";
        QCOMPARE(expanded, shouldBe);

        tmpl = "This is -5 days later than 24.01.2020: DATE_ADD_DAYS(-5)";
        expanded = kraftdoc->resolveMacros(tmpl, positions, d, fullTax, redTax, Format::DateFormatGerman);
        shouldBe = "This is -5 days later than 24.01.2020: 19.01.2020";
        QCOMPARE(expanded, shouldBe);
    }

private:
    KraftDoc kDoc;
    QDate _date;

};

QTEST_MAIN(T_KraftDoc)
#include "t_kraftdoc.moc"