#include "test_treeoperations.h" #include #include "app/events.h" #include "connections-tree/items/databaseitem.h" #include "connections-tree/model.h" #include "models/connectionconf.h" #include "models/treeoperations.h" void TestTreeOperations::testCreation() { // given auto events = QSharedPointer(new Events()); auto config = getDummyConfig(); // when TreeOperations operations(config, events); // then // all ok Q_UNUSED(operations); } void TestTreeOperations::testGetDatabases() { // given auto events = QSharedPointer(new Events()); QString infoResp = getBulkStringReply( "# CPU\n" "used_cpu_sys:17.89\n" "used_cpu_user:24.70\n" "used_cpu_sys_children:0.06\n" "used_cpu_user_children:0.33\n\n" "# Keyspace\n" "db0:keys=3495,expires=0,avg_ttl=0\n" "db9:keys=1,expires=0,avg_ttl=0\n"); QStringList expectedResponses{infoResp, infoResp, "+OK\r\n", "+OK\r\n", "+OK\r\n", "-ERROR\r\n"}; auto connection = getFakeConnection(); connection->setFakeResponses(expectedResponses); connection->setClone(connection); bool callbackCalled = false; RedisClient::DatabaseList result; // when qDebug() << "testGetDatabases - start execution"; TreeOperations operations(getDummyConfig(), events); operations.setConnection(connection); operations.getDatabases( [&callbackCalled, &result](const RedisClient::DatabaseList& r, const QString& err) { callbackCalled = true; result = r; }); // then wait(100); QCOMPARE(callbackCalled, true); QCOMPARE(result.size(), 13); } void TestTreeOperations::testLoadNamespaceItems() { // given QFETCH(uint, runCommandCalled); QFETCH(uint, retrieveCollectionCalled); QFETCH(QList, expectedScanResponses); QFETCH(QStringList, expectedResponses); // Setup dummy connection with on/off lua loading auto connection = getFakeConnection(expectedScanResponses, expectedResponses); ServerConfig conf; connection->setConnectionConfig(conf); auto events = QSharedPointer(new Events()); QSharedPointer operations( new TreeOperations(getDummyConfig(), events)); operations->setConnection(connection); ConnectionsTree::Model dummyModel; QSharedPointer item( new ConnectionsTree::DatabaseItem( 0, 1, operations, QWeakPointer(), dummyModel)); // when bool callbackCalled = false; operations->loadNamespaceItems( qSharedPointerDynamicCast(item), QString(), [&callbackCalled](const QString& err) { // then - part 2 callbackCalled = true; QVERIFY2(err.isEmpty(), qPrintable(err)); }, QSet()); // then - part 1 wait(5); QCOMPARE(callbackCalled, true); QCOMPARE(connection->runCommandCalled, runCommandCalled); QCOMPARE(connection->retrieveCollectionCalled, retrieveCollectionCalled); } void TestTreeOperations::testLoadNamespaceItems_data() { QTest::addColumn("runCommandCalled"); QTest::addColumn("retrieveCollectionCalled"); QTest::addColumn >("expectedScanResponses"); QTest::addColumn("expectedResponses"); QTest::newRow("SCAN execution") << false << 0u << 1u << (QList() << QVariant(QVariantList() << QString("test") << QString("test2"))) << (QStringList() << ""); } void TestTreeOperations::testFlushDb() { // given auto events = QSharedPointer(new Events()); auto connection = getFakeConnection(QList() << QVariant(), QStringList() << "+OK"); // when bool callbackCalled = false; TreeOperations operations(getDummyConfig(), events); operations.setConnection(connection); operations.flushDb(0, [&callbackCalled](const QString&) { // then - part 2 callbackCalled = true; }); // then - part 1 wait(5); QCOMPARE(callbackCalled, true); QCOMPARE(connection->runCommandCalled, 1u); QCOMPARE(connection->executedCommands[0].getPartAsString(0), QString("FLUSHDB")); } void TestTreeOperations::testFlushDbCommandError() { // given auto events = QSharedPointer(new Events()); auto connection = getFakeConnection(); connection->returnErrorOnCmdRun = true; // when bool callbackCalledWithError = false; TreeOperations operations(getDummyConfig(), events); operations.setConnection(connection); operations.flushDb(0, [&callbackCalledWithError](const QString& e) { // then - part 2 callbackCalledWithError = !e.isEmpty(); }); // then - part 1 wait(5); QCOMPARE(callbackCalledWithError, true); }