Skip to content

Commit

Permalink
tests enhanced connections handling
Browse files Browse the repository at this point in the history
If DB driver for any connection is not available then skip all tests
for this connection.

 - also added DB::drivers()/isDriverAvailable()/...
  • Loading branch information
silverqx committed Jun 24, 2022
1 parent 8898b5e commit de2c642
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 39 deletions.
10 changes: 8 additions & 2 deletions include/orm/databasemanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,20 @@ namespace Query
to be called before querying a database. */
QSqlDatabase connectEagerly(const QString &name = "");

/*! Get all of the support drivers. */
QStringList supportedDrivers() const;
/*! Returns a list containing the names of all connections. */
QStringList connectionNames() const;
/*! Returns a list containing the names of opened connections. */
QStringList openedConnectionNames() const;
/*! Get the number of registered connections. */
std::size_t connectionsSize() const;
/*! Get all of the support drivers. */
QStringList supportedDrivers() const;
/*! Get all of the available drivers (loadable). */
QStringList drivers() const;
/*! Is the given driver name available? */
bool isDriverAvailable(const QString &driverName) const;
/*! Is a driver for the given connection available? */
bool isConnectionDriverAvailable(const QString &connectionName);

/*! Get the default connection name. */
const QString &getDefaultConnection() const final;
Expand Down
10 changes: 8 additions & 2 deletions include/orm/db.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,20 @@ namespace Orm
to be called before querying a database. */
static QSqlDatabase connectEagerly(const QString &name = "");

/*! Get all of the support drivers. */
static QStringList supportedDrivers();
/*! Returns a list containing the names of all connections. */
static QStringList connectionNames();
/*! Returns a list containing the names of opened connections. */
static QStringList openedConnectionNames();
/*! Get the number of registered connections. */
static std::size_t connectionsSize();
/*! Get all of the support drivers. */
static QStringList supportedDrivers();
/*! Get all of the available drivers (loadable). */
static QStringList drivers();
/*! Is the given driver name available? */
static bool isDriverAvailable(const QString &driverName);
/*! Is a driver for the given connection available? */
static bool isConnectionDriverAvailable(const QString &connectionName);

/*! Get the default connection name. */
static const QString &getDefaultConnection();
Expand Down
1 change: 0 additions & 1 deletion include/orm/tiny/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,6 @@ TINYORM_END_COMMON_NAMESPACE
// CUR study how to use acquire/release memory order for m_queryLogId atomic silverqx
// FUTURE divide Query Builder and TinyOrm to own packages (dlls)? think about it 🤔 silverqx
// BUG clang on mingw inline static initialization with another static in the same class defined line before, all other compilers (on linux too) works silverqx
// CUR cmake when MYSQL_PING is on and QMYSQL driver is not build ctest fails, fail configure? I don't knwo how I will solve this for now, also fix qmake silverqx
// BUG docs many-to-many examples contain queries with created_at in pivot table, but pivot doesn't contain timestamp columns silverqx
// CUR docs mdx syntax highlight prism Treeview https://prismjs.com/plugins/treeview/ silverqx
// CUR docs IdealImage silverqx
Expand Down
36 changes: 29 additions & 7 deletions src/orm/databasemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,6 @@ QSqlDatabase DatabaseManager::connectEagerly(const QString &name)
return connection(name).connectEagerly();
}

QStringList DatabaseManager::supportedDrivers() const
{
// FUTURE add method to not only supported drivers, but also check if driver is available/loadable by qsqldatabase silverqx
// aaaaaaaaaaaaaachjo 🤔😁 -- 4 months later, looks much better, right?
return {QMYSQL, QPSQL, QSQLITE};
}

QStringList DatabaseManager::connectionNames() const
{
return (*m_configuration).keys();
Expand All @@ -391,6 +384,35 @@ std::size_t DatabaseManager::connectionsSize() const
return (*m_connections).size();
}

QStringList DatabaseManager::supportedDrivers() const
{
// aaaaaaaaaaaaaachjo 🤔😁 -- 4 months later, looks much better, right?
return {QMYSQL, QPSQL, QSQLITE};
}

QStringList DatabaseManager::drivers() const
{
return QSqlDatabase::drivers();
}

bool DatabaseManager::isDriverAvailable(const QString &driverName) const
{
return QSqlDatabase::isDriverAvailable(driverName);
}

bool DatabaseManager::isConnectionDriverAvailable(const QString &connectionName)
{
const auto driverName = configuration(connectionName)[driver_].value<QString>();

if (!supportedDrivers().contains(driverName))
throw Exceptions::LogicError(
QStringLiteral("An unsupported driver name '%1' has been defined for "
"the '%2' connection.")
.arg(driverName, connectionName));

return QSqlDatabase::isDriverAvailable(driverName);
}

const QString &
DatabaseManager::getDefaultConnection() const
{
Expand Down
25 changes: 20 additions & 5 deletions src/orm/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ QSqlDatabase DB::connectEagerly(const QString &name)
return manager().connectEagerly(name);
}

QStringList DB::supportedDrivers()
{
return manager().supportedDrivers();
}

QStringList DB::connectionNames()
{
return manager().connectionNames();
Expand All @@ -102,6 +97,26 @@ std::size_t DB::connectionsSize()
return manager().connectionsSize();
}

QStringList DB::supportedDrivers()
{
return manager().supportedDrivers();
}

QStringList DB::drivers()
{
return manager().drivers();
}

bool Orm::DB::isDriverAvailable(const QString &driverName)
{
return manager().isDriverAvailable(driverName);
}

bool DB::isConnectionDriverAvailable(const QString &connectionName)
{
return manager().isConnectionDriverAvailable(connectionName);
}

const QString &DB::getDefaultConnection()
{
return manager().getDefaultConnection();
Expand Down
34 changes: 16 additions & 18 deletions tests/TinyUtils/src/databases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ const QStringList &Databases::createConnections(const QStringList &connections)
throwIfConnectionsInitialized();

// Ownership of a shared_ptr()
static const auto manager = *db = DB::create();

/* The default connection is empty for tests, there is no default connection
because it can produce hard to find bugs, I have to be explicit about
the connection which will be used. */
static const auto manager = *db = DB::create(getConfigurations(connections), EMPTY);
manager->addConnections(createConfigurationsHash(connections), EMPTY);

static const auto cachedConnectionNames = manager->connectionNames();

Expand Down Expand Up @@ -117,37 +119,33 @@ const std::shared_ptr<Orm::DatabaseManager> &Databases::manager()

/* private */

const Databases::ConfigurationsType &
Databases::getConfigurations(const QStringList &connections)
{
static auto configurations = createConfigurationsHash(connections);

// When connections variable is empty, then return all configurations
if (connections.isEmpty())
return configurations;

return configurations;
}

const Databases::ConfigurationsType &
Databases::createConfigurationsHash(const QStringList &connections)
{
static ConfigurationsType configurations;

const auto shouldCreateConnection = [&connections](const auto &connection)
const auto shouldCreateConnection = [&connections]
(const auto &connection, auto &&driver)
{
return connections.isEmpty() || connections.contains(connection);
const auto isAvailable = DB::isDriverAvailable(driver);

if (!isAvailable)
qWarning("%s driver not available, all tests for this database will be "
"skipped.", driver.toLatin1().constData());

return isAvailable &&
(connections.isEmpty() || connections.contains(connection));
};

if (shouldCreateConnection(MYSQL))
if (shouldCreateConnection(MYSQL, QMYSQL))
if (auto [config, envDefined] = mysqlConfiguration(); envDefined)
configurations[MYSQL] = std::move(config);

if (shouldCreateConnection(SQLITE))
if (shouldCreateConnection(SQLITE, QSQLITE))
if (auto [config, envDefined] = sqliteConfiguration(); envDefined)
configurations[SQLITE] = std::move(config);

if (shouldCreateConnection(POSTGRESQL))
if (shouldCreateConnection(POSTGRESQL, QPSQL))
if (auto [config, envDefined] = postgresConfiguration(); envDefined)
configurations[POSTGRESQL] = std::move(config);

Expand Down
4 changes: 0 additions & 4 deletions tests/TinyUtils/src/databases.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ namespace TestUtils
static const std::shared_ptr<Orm::DatabaseManager> &manager();

private:
/*! Obtain configurations for the given connection names. */
static const ConfigurationsType &
getConfigurations(const QStringList &connections = {});

/*! Create database configurations hash. */
static const ConfigurationsType &
createConfigurationsHash(const QStringList &connections);
Expand Down

0 comments on commit de2c642

Please sign in to comment.