Files
prodejna/tests/shop_tests.cpp
T
2026-10-14 22:38:06 +02:00

107 lines
3.1 KiB
C++

#include <QCoreApplication>
#include <QtTest>
#include "data/voucher.h"
#ifdef PRODEJNA_EET_V4
#include "eet/eetv4adapter.h"
#endif
class ShopTests : public QObject
{
Q_OBJECT
private slots:
void defaultsMatchNewVoucherState()
{
Voucher voucher;
QCOMPARE(voucher.totalPrice().toDouble(), 0.0);
QCOMPARE(voucher.status(), Voucher::NEW);
QCOMPARE(voucher.eetStatus(), Voucher::EET_FOR_SEND);
}
void totalPriceRoundTripUsesFixedPointStorage()
{
Voucher voucher;
voucher.setTotalPrice(QDecDouble(19.9));
QVERIFY(qAbs(voucher.totalPrice().toDouble() - 19.9) < 0.0001);
}
void vatAmountIsDifferenceBetweenTotalAndBase()
{
Voucher voucher;
voucher.setTotalPriceVatHigh(QDecDouble(121));
voucher.setPriceVatHigh(QDecDouble(100));
QVERIFY(qAbs(voucher.vatAmountHigh().toDouble() - 21.0) < 0.0001);
}
void voucherSumAccumulatesPricesWithoutTruncation()
{
VoucherSum sum(0, 0);
sum.addPrice(QDecDouble(19.9));
sum.addPrice(QDecDouble(0.1));
QVERIFY(qAbs(sum.totalPrice().toDouble() - 20.0) < 0.0001);
}
#ifdef PRODEJNA_EET_V4
void eetV4AdapterBuildsRequestFromVoucher()
{
Voucher voucher;
voucher.setNumSer(QStringLiteral("SHOP-42"));
voucher.setPayDateTime(QDateTime(QDate(2026, 10, 13),
QTime(20, 30),
Qt::OffsetFromUTC,
7200));
voucher.setTotalPrice(QDecDouble(QStringLiteral("19.90").toLatin1().constData()));
EetV4ShopConfig config;
config.endpoint = QUrl(QStringLiteral("https://eet.example.test/eet"));
config.taxpayerEic = QStringLiteral("CZ123456789");
config.unitId = 3;
config.cashRegisterId = QStringLiteral("KASA1");
EetV4SaleRequest request;
QString error;
QVERIFY2(EetV4ShopAdapter::createRequest(voucher, config, &request, &error),
qPrintable(error));
QCOMPARE(request.receiptNumber, QStringLiteral("SHOP-42"));
QCOMPARE(request.totalSale.toString(), QStringLiteral("19.90"));
QCOMPARE(request.unitId, 3);
QCOMPARE(request.cashRegisterId, QStringLiteral("KASA1"));
QVERIFY(request.verificationMode);
QVERIFY(request.firstSubmission);
}
void eetV4AdapterRejectsInvalidConfiguration()
{
Voucher voucher;
voucher.setNumSer(QStringLiteral("SHOP-42"));
voucher.setPayDateTime(QDateTime::currentDateTime());
voucher.setTotalPrice(QDecDouble(QStringLiteral("1.00").toLatin1().constData()));
EetV4ShopConfig config;
config.endpoint = QUrl(QStringLiteral("http://not-tls.example.test/eet"));
EetV4SaleRequest request;
QString error;
QVERIFY(!EetV4ShopAdapter::createRequest(voucher, config, &request, &error));
QVERIFY(error.contains(QStringLiteral("HTTPS")));
}
#endif
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
ShopTests tests;
return QTest::qExec(&tests, argc, argv);
}
#include "shop_tests.moc"