File indexing completed on 2024-04-28 05:02:28

0001 /*
0002     SPDX-FileCopyrightText: 2018 Thomas Baumgart <tbaumgart@kde.org>
0003     SPDX-License-Identifier: LGPL-2.1-or-later
0004 
0005     This file is part of libalkimia.
0006 */
0007 
0008 #include "alkonlinequotesourcetest.h"
0009 
0010 #include "alkonlinequotesource.h"
0011 #include "test.h"
0012 
0013 QTEST_GUILESS_MAIN(AlkOnlineQuoteSourceTest)
0014 
0015 void AlkOnlineQuoteSourceTest::init()
0016 {
0017 }
0018 
0019 void AlkOnlineQuoteSourceTest::cleanup()
0020 {
0021 }
0022 
0023 void AlkOnlineQuoteSourceTest::emptyCtor()
0024 {
0025     const QString emptyString;
0026     AlkOnlineQuoteSource *m = new AlkOnlineQuoteSource();
0027     QCOMPARE(m->name(), emptyString);
0028     QCOMPARE(m->isValid(), false);
0029     QCOMPARE(m->url(), emptyString);
0030     QCOMPARE(m->idRegex(), emptyString);
0031     QCOMPARE(m->idSelector(), AlkOnlineQuoteSource::Symbol);
0032     QCOMPARE(m->priceRegex(), emptyString);
0033     QCOMPARE(m->dataFormat(), AlkOnlineQuoteSource::HTML);
0034     QCOMPARE(m->dateRegex(), emptyString);
0035     QCOMPARE(m->dateFormat(), emptyString);
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     m0.setIdSelector(AlkOnlineQuoteSource::IdentificationNumber);
0046 
0047     AlkOnlineQuoteSource m1(m0);
0048     QCOMPARE(&m1 != &m0, true);
0049     QCOMPARE(m1.name(), QLatin1String("MyName"));
0050     QCOMPARE(m1.url(), QLatin1String("MyUrl"));
0051     QCOMPARE(m1.idSelector(), AlkOnlineQuoteSource::IdentificationNumber);
0052 
0053     m1.setName(QLatin1String("YourName"));
0054     QCOMPARE(m0.name(), QLatin1String("MyName"));
0055     QCOMPARE(m1.name(), QLatin1String("YourName"));
0056 }
0057 
0058 void AlkOnlineQuoteSourceTest::assignOperator()
0059 {
0060     AlkOnlineQuoteSource m1;
0061     AlkOnlineQuoteSource m2;
0062 
0063     m1.setName(QLatin1String("MyName"));
0064     m1.setUrl(QLatin1String("MyUrl"));
0065     m1.setIdSelector(AlkOnlineQuoteSource::IdentificationNumber);
0066 
0067     m2 = m1;
0068     m1.setName(QLatin1String("MyOtherName"));
0069     QCOMPARE(m1.name(), QLatin1String("MyOtherName"));
0070     QCOMPARE(m2.name(), QLatin1String("MyName"));
0071     QCOMPARE(m2.url(), QLatin1String("MyUrl"));
0072     QCOMPARE(m2.idSelector(), AlkOnlineQuoteSource::IdentificationNumber);
0073 }
0074 
0075 void AlkOnlineQuoteSourceTest::testReadWriteRemove()
0076 {
0077     AlkOnlineQuotesProfile profile("test", AlkOnlineQuotesProfile::Type::Alkimia4);
0078     AlkOnlineQuoteSource m1 = AlkOnlineQuoteSource::defaultCurrencyQuoteSource("test-currency");
0079     m1.setProfile(&profile);
0080     m1.write();
0081 
0082     AlkOnlineQuoteSource m2("test-currency", &profile);
0083     QCOMPARE(m1.name(), m2.name());
0084     QCOMPARE(m1.isValid(), m2.isValid());
0085     QCOMPARE(m1.url(), m2.url());
0086     QCOMPARE(m1.idRegex(), m2.idRegex());
0087     QCOMPARE(m1.idSelector(), m2.idSelector());
0088     QCOMPARE(m1.priceRegex(), m2.priceRegex());
0089     QCOMPARE(m1.dataFormat(), m2.dataFormat());
0090     QCOMPARE(m1.dateRegex(), m2.dateRegex());
0091     QCOMPARE(m1.dateFormat(), m2.dateFormat());
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 = AlkOnlineQuoteSource::defaultCurrencyQuoteSource("test-currency");
0103     m1.setProfile(&profile);
0104     m1.write();
0105     m1.rename("test-currency.new");
0106 
0107     // should be the same
0108     AlkOnlineQuoteSource m2("test-currency.new", &profile);
0109     QCOMPARE(m1.name(), m2.name());
0110     QCOMPARE(m1.isValid(), m2.isValid());
0111     QCOMPARE(m1.url(), m2.url());
0112     QCOMPARE(m1.idRegex(), m2.idRegex());
0113     QCOMPARE(m1.idSelector(), m2.idSelector());
0114     QCOMPARE(m1.priceRegex(), m2.priceRegex());
0115     QCOMPARE(m1.dataFormat(), m2.dataFormat());
0116     QCOMPARE(m1.dateRegex(), m2.dateRegex());
0117     QCOMPARE(m1.dateFormat(), m2.dateFormat());
0118     QCOMPARE(m1.isGHNS(), m2.isGHNS());
0119 
0120     // should be empty
0121     AlkOnlineQuoteSource m3("test-currency", &profile);
0122     QVERIFY(m3.url().isEmpty());
0123 
0124     // cleanup
0125     m1.remove();
0126 }