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

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Maximilian Kossick <maximilian.kossick@googlemail.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 "SqlStorageMock.h"
0018 
0019 OrderedSqlStorageMock::OrderedSqlStorageMock( const QList<QPair<QString, QVariant> > &queries)
0020     : SqlStorage()
0021     , m_queries(queries)
0022 {
0023 }
0024 
0025 OrderedSqlStorageMock::~OrderesSqlStorageMock()
0026 {
0027 }
0028 
0029 bool
0030 OrderedSqlStorageMock::allQueriesRun() const
0031 {
0032     return m_queries.isEmpty();
0033 }
0034 
0035 QStringList
0036 OrderedSqlStorageMock::query( const QString &query )
0037 {
0038     QVERIFY( !m_queries.isEmpty() );
0039     QPair<QString, QVariant> pair = m_queries.takeFirst();
0040     QCOMPARE( query, pair.first );
0041     QVariant value = pair.second;
0042     QVERIFY(value.canConvert(QVariant::StringList));
0043     return value.toStringList();
0044 }
0045 
0046 int
0047 OrderedSqlStorageMock::insert(const QString &statement, const QString &table)
0048 {
0049     QVERIFY( !m_queries.isEmpty() );
0050     QPair<QString, QVariant> pair = m_queries.takeFirst();
0051     QCOMPARE( statement, pair.first );
0052     QVariant value = pair.second;
0053     QVERIFY(value.canConvert(QVariant::Int));
0054     return value.toInt();
0055 }
0056 
0057 RandomSqlStorageMock::RandomSqlStorageMock( const QVariantMap &queries )
0058     : SqlStorage()
0059     , m_queries()
0060 {
0061     foreach( QString key, queries.keys() )
0062     {
0063         m_queries.insert( key.toLower(), queries.value( key ) );
0064     }
0065 }
0066 
0067 RandomSqlStorageMock::~RandomSqlStorageMock()
0068 {
0069 }
0070 
0071 bool
0072 RandomSqlStorageMock::allQueriesRun() const
0073 {
0074     return m_queries.isEmpty();
0075 }
0076 
0077 QStringList
0078 RandomSqlStorageMock::query( const QString &query )
0079 {
0080     QVERIFY2( m_queries.contains( query.toLower() ), "Received an unknown query in query()" );
0081     QVariant value = m_queries.value( query );
0082     QVERIFY(value.canConvert(QVariant::StringList));
0083     return value.toStringList();
0084 }
0085 
0086 int
0087 RandomSqlStorageMock::insert(const QString &statement, const QString &table)
0088 {
0089     QVERIFY2( m_queries.contains( statement.toLower() ), "Received an unknown query in insert()" );
0090     QVariant value = m_queries.value( statement );
0091     QVERIFY(value.canConvert(QVariant::Int));
0092     return value.toInt();
0093 }