File indexing completed on 2024-04-28 16:13:23

0001 /*
0002     SPDX-FileCopyrightText: 2018 Thomas Baumgart tbaumgart @kde.org
0003 
0004     This file is part of libalkimia.
0005 
0006     SPDX-License-Identifier: LGPL-2.1-or-later
0007 */
0008 
0009 #include "alkonlinequotesourcetest.h"
0010 
0011 #include "alkonlinequotesource.h"
0012 #include "test.h"
0013 
0014 QTEST_GUILESS_MAIN(AlkOnlineQuoteSourceTest)
0015 
0016 void AlkOnlineQuoteSourceTest::init()
0017 {
0018 }
0019 
0020 void AlkOnlineQuoteSourceTest::cleanup()
0021 {
0022 }
0023 
0024 void AlkOnlineQuoteSourceTest::emptyCtor()
0025 {
0026     const QString emptyString;
0027     AlkOnlineQuoteSource *m = new AlkOnlineQuoteSource();
0028     QCOMPARE(m->name(), emptyString);
0029     QCOMPARE(m->isValid(), false);
0030     QCOMPARE(m->url(), emptyString);
0031     QCOMPARE(m->sym(), emptyString);
0032     QCOMPARE(m->price(), emptyString);
0033     QCOMPARE(m->date(), emptyString);
0034     QCOMPARE(m->dateformat(), emptyString);
0035     QCOMPARE(m->skipStripping(), false);
0036     QCOMPARE(m->isGHNS(), false);
0037     delete m;
0038 }
0039 
0040 void AlkOnlineQuoteSourceTest::copyCtor()
0041 {
0042     AlkOnlineQuoteSource m0;
0043     m0.setName(QLatin1String("MyName"));
0044     m0.setUrl(QLatin1String("MyUrl"));
0045 
0046     AlkOnlineQuoteSource m1(m0);
0047     QCOMPARE(&m1 != &m0, true);
0048     QCOMPARE(m1.name(), QLatin1String("MyName"));
0049     QCOMPARE(m1.url(), QLatin1String("MyUrl"));
0050 
0051     m1.setName(QLatin1String("YourName"));
0052     QCOMPARE(m0.name(), QLatin1String("MyName"));
0053     QCOMPARE(m1.name(), QLatin1String("YourName"));
0054 }
0055 
0056 void AlkOnlineQuoteSourceTest::assignOperator()
0057 {
0058     AlkOnlineQuoteSource m1;
0059     AlkOnlineQuoteSource m2;
0060 
0061     m1.setName(QLatin1String("MyName"));
0062     m2 = m1;
0063     m1.setName(QLatin1String("MyOtherName"));
0064     QCOMPARE(m1.name(), QLatin1String("MyOtherName"));
0065     QCOMPARE(m2.name(), QLatin1String("MyName"));
0066 }
0067 
0068 void AlkOnlineQuoteSourceTest::testReadWriteRemove()
0069 {
0070     AlkOnlineQuotesProfile profile("test", AlkOnlineQuotesProfile::Type::Alkimia4);
0071     AlkOnlineQuoteSource m1(
0072         "test-currency",
0073         "https://fx-rate.net/%1/%2",
0074         QString(), // symbolregexp
0075         "1[ a-zA-Z]+=</span><br */?> *(\\d+\\.\\d+)",
0076         "updated\\s\\d+:\\d+:\\d+\\(\\w+\\)\\s+(\\d{1,2}/\\d{2}/\\d{4})",
0077         "%d/%m/%y",
0078         true // skip HTML stripping
0079     );
0080     m1.setProfile(&profile);
0081     m1.write();
0082 
0083     AlkOnlineQuoteSource m2("test-currency", &profile);
0084     QCOMPARE(m1.name(),          m2.name());
0085     QCOMPARE(m1.isValid(),       m2.isValid());
0086     QCOMPARE(m1.url(),           m2.url());
0087     QCOMPARE(m1.sym(),           m2.sym());
0088     QCOMPARE(m1.price(),         m2.price());
0089     QCOMPARE(m1.date(),          m2.date());
0090     QCOMPARE(m1.dateformat(),    m2.dateformat());
0091     QCOMPARE(m1.skipStripping(), m2.skipStripping());
0092     QCOMPARE(m1.isGHNS(),        m2.isGHNS());
0093 
0094     m2.remove();
0095     AlkOnlineQuoteSource m3("test-currency", &profile);
0096     QVERIFY(m3.url().isEmpty());
0097 }
0098 
0099 void AlkOnlineQuoteSourceTest::testRename()
0100 {
0101     AlkOnlineQuotesProfile profile("test", AlkOnlineQuotesProfile::Type::Alkimia4);
0102     AlkOnlineQuoteSource m1(
0103         "test-currency",
0104         "https://fx-rate.net/%1/%2",
0105         QString(), // symbolregexp
0106         "1[ a-zA-Z]+=</span><br */?> *(\\d+\\.\\d+)",
0107         "updated\\s\\d+:\\d+:\\d+\\(\\w+\\)\\s+(\\d{1,2}/\\d{2}/\\d{4})",
0108         "%d/%m/%y",
0109         true // skip HTML stripping
0110     );
0111     m1.setProfile(&profile);
0112     m1.write();
0113     m1.rename("test-currency.new");
0114 
0115     // should be the same
0116     AlkOnlineQuoteSource m2("test-currency.new", &profile);
0117     QCOMPARE(m1.name(),          m2.name());
0118     QCOMPARE(m1.isValid(),       m2.isValid());
0119     QCOMPARE(m1.url(),           m2.url());
0120     QCOMPARE(m1.sym(),           m2.sym());
0121     QCOMPARE(m1.price(),         m2.price());
0122     QCOMPARE(m1.date(),          m2.date());
0123     QCOMPARE(m1.dateformat(),    m2.dateformat());
0124     QCOMPARE(m1.skipStripping(), m2.skipStripping());
0125     QCOMPARE(m1.isGHNS(),        m2.isGHNS());
0126 
0127     // should be empty
0128     AlkOnlineQuoteSource m3("test-currency", &profile);
0129     QVERIFY(m3.url().isEmpty());
0130 
0131     // cleanup
0132     m1.remove();
0133 }