Add test to ensure correct operation of DataGridWidget
This commit is contained in:
parent
c181fe99fa
commit
82cc9f14bc
6 changed files with 424 additions and 0 deletions
72
tests/integration/DataGridTest.h
Normal file
72
tests/integration/DataGridTest.h
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#pragma once
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "DbTest.h"
|
||||
#include "ConnectionConfig.h"
|
||||
#include "OpenDatabase.h"
|
||||
#include "crud/CrudModel.h"
|
||||
#include "crud/DataGridWidget.h"
|
||||
#include "Pgsql_Connection.h"
|
||||
|
||||
#include <QSortFilterProxyModel>
|
||||
#include <QUrl>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class DataGridTest : public pgtest::DbTest {
|
||||
protected:
|
||||
std::shared_ptr<OpenDatabase> openDb()
|
||||
{
|
||||
QUrl url(dbUrl());
|
||||
ConnectionConfig cfg;
|
||||
cfg.setHost(url.host());
|
||||
cfg.setPort(static_cast<unsigned short>(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<Pgsql::Result>(conn().query(sql));
|
||||
model = std::make_unique<CrudModel>();
|
||||
widget = std::make_unique<DataGridWidget>();
|
||||
widget->setSourceModel(model.get());
|
||||
model->setResult(db, result);
|
||||
}
|
||||
|
||||
/// Editable flag of every result column, as seen through the widget's proxy
|
||||
std::vector<bool> editableColumns()
|
||||
{
|
||||
auto proxy = widget->proxy();
|
||||
std::vector<bool> 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<std::string> 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<OpenDatabase> db;
|
||||
std::unique_ptr<CrudModel> model;
|
||||
std::unique_ptr<DataGridWidget> widget;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue