#pragma once #include #include "DbTest.h" #include "ConnectionConfig.h" #include "OpenDatabase.h" #include "crud/CrudModel.h" #include "crud/DataGridWidget.h" #include "Pgsql_Connection.h" #include #include #include #include "catalog/PgClassContainer.h" #include "catalog/PgDatabaseCatalog.h" #include #include #include #include #include class DataGridTest : public pgtest::DbTest { protected: std::shared_ptr openDb() { QUrl url(dbUrl()); ConnectionConfig cfg; cfg.setHost(url.host()); cfg.setPort(static_cast(url.port(5432))); cfg.setUser(url.userName()); cfg.setPassword(url.password()); cfg.setDbname(url.path().mid(1)); return OpenDatabase::createOpenDatabase(cfg); } /// Runs the query and shows its result in a grid void showQuery(const char *sql) { db = openDb(); auto result = std::make_shared(conn().query(sql)); model = std::make_unique(); widget = std::make_unique(); widget->setSourceModel(model.get()); model->setResult(db, result); } /// Shows a whole table (CRUD mode) and waits for the asynchronous load to finish bool showTable(const QString &name) { db = openDb(); auto table = db->catalog()->classes()->findIf([&name](const PgClass &c) { return c.fullyQualifiedQuotedObjectName().endsWith("." + name); }); if (!table) return false; model = std::make_unique(); widget = std::make_unique(); widget->setSourceModel(model.get()); bool reset = false; QObject::connect(model.get(), &QAbstractItemModel::modelReset, model.get(), [&reset] { reset = true; }); model->setConfig(db, *table); QDeadlineTimer deadline(10000); while (!reset && !deadline.hasExpired()) QCoreApplication::processEvents(QEventLoop::AllEvents, 50); return reset; } /// Editable flag of every result column, as seen through the widget's proxy std::vector editableColumns() { auto proxy = widget->proxy(); std::vector r; for (int c = 1; c < proxy->columnCount(); ++c) r.push_back(proxy->flags(proxy->index(0, c)) & Qt::ItemIsEditable); return r; } QString columnName(int resultCol) { return widget->proxy()->headerData(resultCol + 1, Qt::Horizontal).toString().section('\n', 0, 0); } /// Reads a single cell with the test's own connection std::optional dbValue(const char *sql) { auto res = conn().query(sql); auto v = res.get(0, 0); if (v.null()) return std::nullopt; return std::string(v.c_str()); } std::shared_ptr db; std::unique_ptr model; std::unique_ptr widget; };