File indexing completed on 2024-04-28 04:48:12

0001 /****************************************************************************************
0002  * Copyright (c) 2013 Konrad Zemek <konrad.zemek@gmail.com>                             *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "TestFastForwardImporter.h"
0018 
0019 #include "MetaValues.h"
0020 #include "importers/fastforward/FastForwardConfigWidget.h"
0021 #include "importers/fastforward/FastForwardProvider.h"
0022 
0023 #include <QTest>
0024 
0025 
0026 QTEST_MAIN( TestFastForwardImporter )
0027 
0028 using namespace StatSyncing;
0029 
0030 ProviderPtr
0031 TestFastForwardImporter::getProvider()
0032 {
0033     QVariantMap cfg = FastForwardConfigWidget( QVariantMap() ).config();
0034     cfg.insert( "dbDriver", "QSQLITE" );
0035     cfg.insert( "dbPath", QCoreApplication::applicationDirPath() +
0036                           "/importers_files/collection.db" );
0037 
0038     return ProviderPtr( new FastForwardProvider( cfg, nullptr ) );
0039 }
0040 
0041 ProviderPtr
0042 TestFastForwardImporter::getWritableProvider()
0043 {
0044     QDir base( QCoreApplication::applicationDirPath() );
0045     base.mkpath( "importers_tmp" );
0046 
0047     const QString dst = base.filePath( "importers_tmp/collection.db" );
0048     QFile( dst ).remove();
0049     QFile( base.filePath( "importers_files/collection.db" ) ).copy( dst );
0050 
0051     QVariantMap cfg = FastForwardConfigWidget( QVariantMap() ).config();
0052     cfg.insert( "dbDriver", "QSQLITE" );
0053     cfg.insert( "dbPath", dst );
0054 
0055     return ProviderPtr( new FastForwardProvider( cfg, nullptr ) );
0056 }
0057 
0058 qint64
0059 TestFastForwardImporter::reliableStatistics() const
0060 {
0061     return Meta::valFirstPlayed | Meta::valLastPlayed | Meta::valRating
0062             | Meta::valPlaycount | Meta::valLabel;
0063 }
0064 
0065 void
0066 TestFastForwardImporter::init()
0067 {
0068     m_cfg = FastForwardConfigWidget( QVariantMap() ).config();
0069 }
0070 
0071 void
0072 TestFastForwardImporter::configWidgetShouldOnlyShowFieldsRelevantToConnection()
0073 {
0074     FastForwardConfigWidget widget( m_cfg );
0075 
0076     const QList<QWidget*> remoteConfigWidgets = QList<QWidget*>()
0077             << widget.m_databaseName << widget.m_hostname << widget.m_port
0078             << widget.m_password << widget.m_username;
0079 
0080     widget.m_connectionType->setCurrentIndex( FastForwardConfigWidget::QSQLITE );
0081     QVERIFY( !widget.m_databaseLocation->isHidden() );
0082     foreach( QWidget *w, remoteConfigWidgets )
0083         QVERIFY( w->isHidden() );
0084 
0085     widget.m_connectionType->setCurrentIndex( FastForwardConfigWidget::QMYSQL );
0086     QVERIFY( widget.m_databaseLocation->isHidden() );
0087     foreach( QWidget *w, remoteConfigWidgets )
0088         QVERIFY( !w->isHidden() );
0089 
0090     widget.m_connectionType->setCurrentIndex( FastForwardConfigWidget::QPSQL );
0091     QVERIFY( widget.m_databaseLocation->isHidden() );
0092     foreach( QWidget *w, remoteConfigWidgets )
0093         QVERIFY( !w->isHidden() );
0094 }
0095 
0096 void
0097 TestFastForwardImporter::configWidgetShouldSetDriverNameAsConfigResult()
0098 {
0099     FastForwardConfigWidget widget( m_cfg );
0100 
0101     widget.m_connectionType->setCurrentIndex( FastForwardConfigWidget::QSQLITE );
0102     QCOMPARE( widget.config().value( "dbDriver" ).toString(),
0103               QString( "QSQLITE" ) );
0104 
0105     widget.m_connectionType->setCurrentIndex( FastForwardConfigWidget::QMYSQL );
0106     QCOMPARE( widget.config().value( "dbDriver" ).toString(),
0107               QString( "QMYSQL" ) );
0108 
0109     widget.m_connectionType->setCurrentIndex( FastForwardConfigWidget::QPSQL );
0110     QCOMPARE( widget.config().value( "dbDriver" ).toString(),
0111               QString( "QPSQL" ) );
0112 }
0113 
0114 void
0115 TestFastForwardImporter::configWidgetShouldShowSqliteAsDefault()
0116 {
0117     FastForwardConfigWidget widget( m_cfg );
0118     QCOMPARE( widget.m_connectionType->currentIndex(),
0119               static_cast<int>( FastForwardConfigWidget::QSQLITE ) );
0120 }
0121 
0122 void
0123 TestFastForwardImporter::configWidgetShouldNotBreakOnNonsenseInitialValues()
0124 {
0125     m_cfg.insert( "dbDriver", 19 );
0126     m_cfg.insert( "dbName", QColor( Qt::red ) );
0127     m_cfg.insert( "dbPort", "nonsensePort" );
0128 
0129     FastForwardConfigWidget widget( m_cfg );
0130     QVERIFY( !widget.m_databaseName->text().isEmpty() );
0131 
0132     const QList<QString> validDrivers = QList<QString>()
0133             << "QSQLITE" << "QMYSQL" << "QPSQL";
0134 
0135     QVERIFY( validDrivers.contains( widget.config().value( "dbDriver" ).toString() ) );
0136 }
0137 
0138 void
0139 TestFastForwardImporter::configWidgetShouldReadSavedConfig()
0140 {
0141     m_cfg.insert( "dbDriver", "QPSQL" );
0142     m_cfg.insert( "dbName", "MyName" );
0143     m_cfg.insert( "dbPort", 19 );
0144     m_cfg.insert( "name", "theName" );
0145 
0146     FastForwardConfigWidget widget( m_cfg );
0147 
0148     QCOMPARE( widget.m_connectionType->currentIndex(),
0149               static_cast<int>( FastForwardConfigWidget::QPSQL ) );
0150 
0151     QCOMPARE( widget.m_databaseName->text(), QString( "MyName" ) );
0152     QCOMPARE( widget.m_port->value(), 19 );
0153     QCOMPARE( widget.m_targetName->text(), QString( "theName" ) );
0154 }
0155 
0156 void
0157 TestFastForwardImporter::providerShouldHandleNonexistentDbFile()
0158 {
0159     m_cfg.insert( "dbPath", "/Im/sure/this/wont/exist" );
0160 
0161     FastForwardProvider provider( m_cfg, nullptr );
0162     QVERIFY( provider.artists().isEmpty() );
0163 }
0164 
0165 void
0166 TestFastForwardImporter::providerShouldHandleInvalidDbFile()
0167 {
0168     m_cfg.insert( "dbPath", QApplication::applicationFilePath() );
0169 
0170     FastForwardProvider provider( m_cfg, nullptr );
0171     QVERIFY( provider.artists().isEmpty() );
0172 }
0173 
0174 void
0175 TestFastForwardImporter::providerShouldHandleExternalConnectionError()
0176 {
0177     m_cfg.insert( "dbDriver", "QMYSQL" );
0178     m_cfg.insert( "dbHost", "I hope this isn't a valid hostname" );
0179 
0180     FastForwardProvider provider( m_cfg, nullptr );
0181     QVERIFY( provider.artists().isEmpty() );
0182 }
0183 
0184 void
0185 TestFastForwardImporter::providerShouldHandleErroneousConfigValues()
0186 {
0187     m_cfg.insert( "dbDriver", 19 );
0188     m_cfg.insert( "dbName", QColor( Qt::red ) );
0189     m_cfg.insert( "dbPort", "nonsensePort" );
0190 
0191     FastForwardProvider provider( m_cfg, nullptr );
0192     QVERIFY( provider.artists().isEmpty() );
0193 }
0194