feat(StatusQ/Tests): Added utility for persistent indexes validation
This commit is contained in:
parent
12569d795f
commit
6d502427e5
|
@ -21,6 +21,8 @@ add_library(StatusQTestLib
|
|||
src/TestHelpers/modelsignalsspy.h
|
||||
src/TestHelpers/modeltestutils.cpp
|
||||
src/TestHelpers/modeltestutils.h
|
||||
src/TestHelpers/persistentindexestester.cpp
|
||||
src/TestHelpers/persistentindexestester.h
|
||||
src/TestHelpers/snapshotmodel.cpp
|
||||
src/TestHelpers/snapshotmodel.h
|
||||
src/TestHelpers/testmodel.cpp
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
#include "persistentindexestester.h"
|
||||
|
||||
#include <TestHelpers/snapshotmodel.h>
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
PersistentIndexesTester::PersistentIndexesTester(QAbstractItemModel* model)
|
||||
: m_model(model)
|
||||
{
|
||||
storeIndexesAndData();
|
||||
}
|
||||
|
||||
void PersistentIndexesTester::storeIndexesAndData()
|
||||
{
|
||||
if (m_model == nullptr) {
|
||||
qWarning() << "PersistentIndexesTester: model cannot be null.";
|
||||
return;
|
||||
}
|
||||
|
||||
m_snapshot = std::make_unique<SnapshotModel>(*m_model);
|
||||
|
||||
const int count = m_model->rowCount();
|
||||
m_persistentIndexes.clear();
|
||||
m_persistentIndexes.reserve(count);
|
||||
|
||||
for (auto i = 0; i < count; i++)
|
||||
m_persistentIndexes << m_model->index(i, 0);
|
||||
}
|
||||
|
||||
bool PersistentIndexesTester::compare()
|
||||
{
|
||||
if (m_model == nullptr) {
|
||||
qWarning() << "PersistentIndexesTester: model cannot be null.";
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto count = m_model->rowCount();
|
||||
const auto roles = m_model->roleNames().keys();
|
||||
|
||||
for (auto i = 0; i < count; i++) {
|
||||
|
||||
const auto idx = m_persistentIndexes[i];
|
||||
|
||||
for (auto role : roles) {
|
||||
if (idx.data(role) != m_snapshot->data(i, role)) {
|
||||
auto roleName = QString::fromUtf8(m_model->roleNames()
|
||||
.value(role));
|
||||
|
||||
qWarning() << QString("Mismatch detected. Persistent index "
|
||||
"data: %1, snapshot data: %2, idx: %3, "
|
||||
"role: %4")
|
||||
.arg(idx.data(role).toString(),
|
||||
m_snapshot->data(i, role).toString(),
|
||||
QString::number(i), roleName);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
#pragma once
|
||||
|
||||
#include <QAbstractItemModel>
|
||||
#include <QPointer>
|
||||
|
||||
#include <memory>
|
||||
|
||||
class SnapshotModel;
|
||||
|
||||
/**
|
||||
* @brief The PersistentIndexesTester class is a simple utility for persistent
|
||||
* indexes validation.
|
||||
*
|
||||
* It stores persistent indexes for all items and snapshot of the model's data.
|
||||
* Using compare() method it can be checked if data indicated by persistent
|
||||
* indexes match the snapshot.
|
||||
*/
|
||||
class PersistentIndexesTester
|
||||
{
|
||||
public:
|
||||
explicit PersistentIndexesTester(QAbstractItemModel* model);
|
||||
|
||||
void storeIndexesAndData();
|
||||
bool compare();
|
||||
|
||||
private:
|
||||
QPointer<QAbstractItemModel> m_model;
|
||||
QList<QPersistentModelIndex> m_persistentIndexes;
|
||||
std::unique_ptr<SnapshotModel> m_snapshot;
|
||||
};
|
Loading…
Reference in New Issue