/
waterstone
/
TaskList_Demo
Обзор
Документация
Войти
/
waterstone
/
TaskList_Demo
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
QPixmapCache
test/src/tst_tasklisttablemodeltest.cpp
403 строки
13 KB
Peter Sakhno
Model code review
21 янв 2026, 16:07
21 янв 2026, 16:07
a4bb872
Код
Авторство
О чём код?
#include "tst_tasklisttablemodeltest.h" #include <QThread> #include <QDateTime> #include <QCoreApplication> #include <QtTest> TaskListTableModelTest::TaskListTableModelTest() { } TaskListTableModelTest::~TaskListTableModelTest() { } void TaskListTableModelTest::test_init() { TaskListTableModelTestAdapterQP model = new TaskListTableModelTestAdapter(this); QVERIFY(model); QCOMPARE(model->rowCount(), 0); QCOMPARE(model->columnCount(), TaskListTableModel::ColumnEnd); QCOMPARE(model->GetTaskIdCounterForTest(), 1); model->deleteLater(); } void TaskListTableModelTest::test_one_insertRow_setData() { TaskListTableModelTestAdapterQP model = new TaskListTableModelTestAdapter(this); QVERIFY(model); const auto prev_counter = model->GetTaskIdCounterForTest(); const auto prev_row_count = model->rowCount(); const QModelIndex prev_index = model->index(0, TaskListTableModel::CaptionColumn); QVERIFY(!prev_index.isValid()); const QString test_caption = QString("New task #%1").arg(1); const QString caption = model->GenerateTaskName(); QCOMPARE(caption, test_caption); const auto res_insert_row = model->insertRows(prev_row_count, 1); QVERIFY(res_insert_row); const auto after_counter = model->GetTaskIdCounterForTest(); QCOMPARE_GT(after_counter, prev_counter); const auto after_row_count = model->rowCount(); QCOMPARE_GT(after_row_count, prev_row_count); QCOMPARE(after_row_count, prev_row_count + 1); const QModelIndex after_insert_index = model->index(after_row_count - 1, TaskListTableModel::CaptionColumn); QVERIFY(after_insert_index.isValid()); const auto res_set_data = model->setData(after_insert_index, caption); QVERIFY(res_set_data); auto internal_data = model->GetTaskListDataForTest(); QVERIFY(!internal_data.empty()); QCOMPARE(internal_data.size(), 1); auto cit = internal_data.cbegin(); QVERIFY(cit != internal_data.cend()); QCOMPARE(cit->id, prev_counter); QVERIFY(cit->timestamp != 0); QDateTime dt = QDateTime::fromMSecsSinceEpoch(cit->timestamp); QVERIFY(dt.isValid()); const QString timestamp_display_str = dt.date().toString( QLocale::system().dateFormat(QLocale::ShortFormat)) + QStringLiteral(" ") + dt.time().toString(QLocale::system().timeFormat(QLocale::LongFormat)); #if 0 qDebug() << timestamp_display_str; #endif // 0 QVERIFY(0 <= cit->duration); QVERIFY(cit->duration <= DurationRange::max()); QCOMPARE(cit->caption, caption); QVERIFY(!cit->active); QCOMPARE(cit->progress, ProgressRange::bad()); for (int c = TaskListTableModel::ColumnBegin; c < TaskListTableModel::ColumnEnd; c++) { const QModelIndex index = model->index(model->rowCount() - 1, c); QVERIFY(index.isValid()); // Qt::UserRole { const QVariant var = index.data(Qt::UserRole); switch (c) { case TaskListTableModel::TimestampColumn: QVERIFY(var.isValid()); QCOMPARE(var.userType(), QMetaType::LongLong); QCOMPARE(var.toLongLong(), cit->timestamp); break; case TaskListTableModel::CaptionColumn: QVERIFY(var.isValid()); QCOMPARE(var.userType(), QMetaType::QString); QCOMPARE(var.toString(), cit->caption); break; case TaskListTableModel::ProgressColumn: QVERIFY(!var.isValid()); break; case TaskListTableModel::ActionColumn: QVERIFY(var.isValid()); QCOMPARE(var.userType(), QMetaType::Bool); QCOMPARE(var.toBool(), cit->active); break; } } // Qt::DisplayRole { const QVariant var = index.data(Qt::DisplayRole); switch (c) { case TaskListTableModel::TimestampColumn: QVERIFY(var.isValid()); QCOMPARE(var.userType(), QMetaType::QString); QCOMPARE(var.toString(), timestamp_display_str); break; case TaskListTableModel::CaptionColumn: QVERIFY(var.isValid()); QCOMPARE(var.userType(), QMetaType::QString); QCOMPARE(var.toString(), caption); break; case TaskListTableModel::ProgressColumn: QVERIFY(!var.isValid()); break; case TaskListTableModel::ActionColumn: QVERIFY(var.isValid()); QCOMPARE(var.userType(), QMetaType::QString); QCOMPARE(var.toString(), tr("Start (%1)").arg(cit->duration)); break; } } } model->deleteLater(); } void TaskListTableModelTest::test_many_insertRow_setData() { const int kEnd = 2000; struct Steps { public: ~Steps() { if (row != kEnd or data != kEnd) qDebug() << "Add row step: " << row << " Compare data step: " << data; } int row{ 0 }; int data{ 0 }; } steps; TaskListTableModelTestAdapterQP model = new TaskListTableModelTestAdapter(this); QVERIFY(model); for (steps.row = 0; steps.row < kEnd; steps.row++) { const auto prev_data = model->GetTaskListDataForTest(); QCOMPARE(prev_data.size(), steps.row); const QString test_caption = QString("New task #%1").arg(steps.row + 1); const QString caption = model->GenerateTaskName(); QCOMPARE(caption, test_caption); const auto res_insert = model->insertRows(model->rowCount(), 1); QVERIFY(res_insert); const QModelIndex index = model->index(model->rowCount() - 1, TaskListTableModel::CaptionColumn); QVERIFY(index.isValid()); const auto res_set_cap = model->setData(index, caption); QVERIFY(res_set_cap); const auto after_data = model->GetTaskListDataForTest(); QCOMPARE_GT(after_data.size(), prev_data.size()); QCOMPARE(after_data.size(), prev_data.size() + 1); if (!prev_data.empty()) { for (steps.data = 0; steps.data < after_data.size(); steps.data++) { auto after_cit = std::next(after_data.cbegin(), steps.data); QCOMPARE_NE(after_cit, after_data.cend()); auto prev_cit = std::next(prev_data.cbegin(), steps.data); if (prev_cit == prev_data.cend()) { const uint64_t test_id = model->GetTaskIdCounterForTest(); QCOMPARE(after_cit->id, test_id - 1); QCOMPARE(after_cit->caption, caption); continue; } QCOMPARE(prev_cit->id, after_cit->id); QCOMPARE(prev_cit->timestamp, after_cit->timestamp); QCOMPARE(prev_cit->duration, after_cit->duration); QCOMPARE(prev_cit->caption, after_cit->caption); QCOMPARE(prev_cit->active, after_cit->active); QCOMPARE(prev_cit->progress, after_cit->progress ); } } // QThread::msleep(2); // to make gap between timestamps of adjacent rows } model->deleteLater(); } void TaskListTableModelTest::test_one_AddTask() { TaskListTableModelTestAdapterQP model = new TaskListTableModelTestAdapter(this); QVERIFY(model); const auto prev_counter = model->GetTaskIdCounterForTest(); const auto prev_row_count = model->rowCount(); const QModelIndex prev_index = model->index(0, TaskListTableModel::CaptionColumn); QVERIFY(!prev_index.isValid()); const QString test_caption = QString("New task #%1").arg(1); const QString caption = model->GenerateTaskName(); QCOMPARE(caption, test_caption); model->AddTask(caption); const auto after_counter = model->GetTaskIdCounterForTest(); QCOMPARE_GT(after_counter, prev_counter); const auto after_row_count = model->rowCount(); QCOMPARE_GT(after_row_count, prev_row_count); QCOMPARE(after_row_count, prev_row_count + 1); const QModelIndex after_insert_index = model->index(after_row_count - 1, TaskListTableModel::CaptionColumn); QVERIFY(after_insert_index.isValid()); auto internal_data = model->GetTaskListDataForTest(); QVERIFY(!internal_data.empty()); QCOMPARE(internal_data.size(), 1); auto cit = internal_data.cbegin(); QVERIFY(cit != internal_data.cend()); QCOMPARE(cit->id, prev_counter); QVERIFY(cit->timestamp != 0); QDateTime dt = QDateTime::fromMSecsSinceEpoch(cit->timestamp); QVERIFY(dt.isValid()); const QString timestamp_display_str = dt.date().toString( QLocale::system().dateFormat(QLocale::ShortFormat)) + QStringLiteral(" ") + dt.time().toString(QLocale::system().timeFormat(QLocale::LongFormat)); #if 0 qDebug() << timestamp_display_str; #endif // 0 QVERIFY(0 <= cit->duration); QVERIFY(cit->duration <= DurationRange::max()); QCOMPARE(cit->caption, caption); QVERIFY(!cit->active); QCOMPARE(cit->progress, ProgressRange::bad()); for (int c = TaskListTableModel::ColumnBegin; c < TaskListTableModel::ColumnEnd; c++) { const QModelIndex index = model->index(model->rowCount() - 1, c); QVERIFY(index.isValid()); // Qt::UserRole { const QVariant var = index.data(Qt::UserRole); switch (c) { case TaskListTableModel::TimestampColumn: QVERIFY(var.isValid()); QCOMPARE(var.userType(), QMetaType::LongLong); QCOMPARE(var.toLongLong(), cit->timestamp); break; case TaskListTableModel::CaptionColumn: QVERIFY(var.isValid()); QCOMPARE(var.userType(), QMetaType::QString); QCOMPARE(var.toString(), cit->caption); break; case TaskListTableModel::ProgressColumn: QVERIFY(!var.isValid()); break; case TaskListTableModel::ActionColumn: QVERIFY(var.isValid()); QCOMPARE(var.userType(), QMetaType::Bool); QCOMPARE(var.toBool(), cit->active); break; } } // Qt::DisplayRole { const QVariant var = index.data(Qt::DisplayRole); switch (c) { case TaskListTableModel::TimestampColumn: QVERIFY(var.isValid()); QCOMPARE(var.userType(), QMetaType::QString); QCOMPARE(var.toString(), timestamp_display_str); break; case TaskListTableModel::CaptionColumn: QVERIFY(var.isValid()); QCOMPARE(var.userType(), QMetaType::QString); QCOMPARE(var.toString(), caption); break; case TaskListTableModel::ProgressColumn: QVERIFY(!var.isValid()); break; case TaskListTableModel::ActionColumn: QVERIFY(var.isValid()); QCOMPARE(var.userType(), QMetaType::QString); QCOMPARE(var.toString(), tr("Start (%1)").arg(cit->duration)); break; } } } model->deleteLater(); } void TaskListTableModelTest::test_many_AddTask() { const int kEnd = 2000; struct Steps { public: ~Steps() { if (row != kEnd or data != kEnd) qDebug() << "Add row step: " << row << " Compare data step: " << data; } int row{ 0 }; int data{ 0 }; } steps; TaskListTableModelTestAdapterQP model = new TaskListTableModelTestAdapter(this); QVERIFY(model); for (steps.row = 0; steps.row < kEnd; steps.row++) { const auto prev_data = model->GetTaskListDataForTest(); QCOMPARE(prev_data.size(), steps.row); const QString test_caption = QString("New task #%1").arg(steps.row + 1); const QString caption = model->GenerateTaskName(); QCOMPARE(caption, test_caption); model->AddTask(caption); const QModelIndex index = model->index(model->rowCount() - 1, TaskListTableModel::CaptionColumn); QVERIFY(index.isValid()); const auto after_data = model->GetTaskListDataForTest(); QCOMPARE_GT(after_data.size(), prev_data.size()); QCOMPARE(after_data.size(), prev_data.size() + 1); if (!prev_data.empty()) { for (steps.data = 0; steps.data < after_data.size(); steps.data++) { auto after_cit = std::next(after_data.cbegin(), steps.data); QCOMPARE_NE(after_cit, after_data.cend()); auto prev_cit = std::next(prev_data.cbegin(), steps.data); if (prev_cit == prev_data.cend()) { QCOMPARE(after_cit->id, model->GetTaskIdCounterForTest() - 1); QCOMPARE(after_cit->caption, caption); continue; } QCOMPARE(prev_cit->id, after_cit->id); QCOMPARE(prev_cit->timestamp, after_cit->timestamp); QCOMPARE(prev_cit->duration, after_cit->duration); QCOMPARE(prev_cit->caption, after_cit->caption); QCOMPARE(prev_cit->active, after_cit->active); QCOMPARE(prev_cit->progress, after_cit->progress); } } // QThread::msleep(2); // to make gap between timestamps of adjacent rows } model->deleteLater(); } QTEST_MAIN (TaskListTableModelTest)