fix history sort
This commit is contained in:
4 files changed
+298
-35
No files matched your search
+175
-4
@@ -93,7 +93,7 @@ private slots:
|
||||
HistoryModel model(registry);
|
||||
|
||||
QSignalSpy countSpy(&model, &HistoryModel::countChanged);
|
||||
model.addOrUpdateBook(QStringLiteral("/path/to/book.cbz"), 0.42, QStringLiteral("2026-01-01"));
|
||||
model.addOrUpdateBook(QStringLiteral("/path/to/book.cbz"), 0.42, QStringLiteral("2026-01-01T12:00:00"));
|
||||
|
||||
QCOMPARE(countSpy.count(), 1);
|
||||
QCOMPARE(model.rowCount(), 1);
|
||||
@@ -101,8 +101,9 @@ private slots:
|
||||
const QModelIndex idx = model.index(0, 0);
|
||||
QCOMPARE(model.data(idx, HistoryModel::FilePathRole).toString(), QStringLiteral("/path/to/book.cbz"));
|
||||
QCOMPARE(model.data(idx, HistoryModel::ProgressRole).toDouble(), 0.42);
|
||||
QCOMPARE(model.data(idx, HistoryModel::LastOpenedRole).toString(), QStringLiteral("2026-01-01"));
|
||||
QCOMPARE(model.data(idx, HistoryModel::LastOpenedRole).toString(), QStringLiteral("2026-01-01T12:00:00"));
|
||||
QCOMPARE(model.data(idx, HistoryModel::FormatRole).toString(), QStringLiteral("CBZ"));
|
||||
QCOMPARE(model.lastOpened(0), QDateTime::fromString(QStringLiteral("2026-01-01T12:00:00"), Qt::ISODate));
|
||||
}
|
||||
|
||||
void loadMetadataPopulatesTitleAndCoverUrl()
|
||||
@@ -155,7 +156,7 @@ private slots:
|
||||
|
||||
{
|
||||
HistoryModel model(registry);
|
||||
model.addOrUpdateBook(QStringLiteral("/path/to/book.cbz"), 0.75, QStringLiteral("2026-10-25"));
|
||||
model.addOrUpdateBook(QStringLiteral("/path/to/book.cbz"), 0.75, QStringLiteral("2026-10-25T14:00:00"));
|
||||
model.save(appState);
|
||||
}
|
||||
|
||||
@@ -165,9 +166,179 @@ private slots:
|
||||
QCOMPARE(model2.rowCount(), 1);
|
||||
QCOMPARE(model2.filePath(0), QStringLiteral("/path/to/book.cbz"));
|
||||
QCOMPARE(model2.progress(0), 0.75);
|
||||
QCOMPARE(model2.lastOpened(0), QStringLiteral("2026-10-25"));
|
||||
QCOMPARE(model2.lastOpened(0), QDateTime::fromString(QStringLiteral("2026-10-25T14:00:00"), Qt::ISODate));
|
||||
}
|
||||
}
|
||||
|
||||
void bogusDateDroppedOnLoad()
|
||||
{
|
||||
QTemporaryFile f;
|
||||
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/hist-bogus-XXXXXX.ini"));
|
||||
(void)f.open(); f.close();
|
||||
QSettings s(f.fileName(), QSettings::IniFormat);
|
||||
AppState appState(s);
|
||||
|
||||
appState.saveArray(QStringLiteral("history"), {
|
||||
{
|
||||
{QStringLiteral("filePath"), QStringLiteral("/path/valid1.cbz")},
|
||||
{QStringLiteral("progress"), 0.1},
|
||||
{QStringLiteral("lastOpened"), QStringLiteral("2026-10-20T10:00:00")},
|
||||
},
|
||||
{
|
||||
{QStringLiteral("filePath"), QStringLiteral("/path/bogus_empty.cbz")},
|
||||
{QStringLiteral("progress"), 0.2},
|
||||
{QStringLiteral("lastOpened"), QStringLiteral("")},
|
||||
},
|
||||
{
|
||||
{QStringLiteral("filePath"), QStringLiteral("/path/valid2.cbz")},
|
||||
{QStringLiteral("progress"), 0.3},
|
||||
{QStringLiteral("lastOpened"), QStringLiteral("2026-10-25T15:30:00")},
|
||||
},
|
||||
{
|
||||
{QStringLiteral("filePath"), QStringLiteral("/path/bogus_text.cbz")},
|
||||
{QStringLiteral("progress"), 0.4},
|
||||
{QStringLiteral("lastOpened"), QStringLiteral("not-a-valid-date")},
|
||||
},
|
||||
{
|
||||
{QStringLiteral("filePath"), QStringLiteral("/path/bogus_invalid_date.cbz")},
|
||||
{QStringLiteral("progress"), 0.5},
|
||||
{QStringLiteral("lastOpened"), QStringLiteral("2026-02-31T00:00:00")},
|
||||
},
|
||||
{
|
||||
{QStringLiteral("filePath"), QStringLiteral("")},
|
||||
{QStringLiteral("progress"), 0.6},
|
||||
{QStringLiteral("lastOpened"), QStringLiteral("2026-10-30T10:00:00")},
|
||||
},
|
||||
});
|
||||
|
||||
FormatRegistry registry = makeRegistry();
|
||||
HistoryModel model(registry);
|
||||
model.load(appState);
|
||||
|
||||
QCOMPARE(model.rowCount(), 2);
|
||||
QCOMPARE(model.filePath(0), QStringLiteral("/path/valid2.cbz"));
|
||||
QCOMPARE(model.filePath(1), QStringLiteral("/path/valid1.cbz"));
|
||||
}
|
||||
|
||||
void bogusDateDroppedOnAddOrUpdate()
|
||||
{
|
||||
FormatRegistry registry = makeRegistry();
|
||||
HistoryModel model(registry);
|
||||
|
||||
model.addOrUpdateBook(QStringLiteral("/path/bad1.cbz"), 0.5, QDateTime());
|
||||
QCOMPARE(model.rowCount(), 0);
|
||||
|
||||
model.addOrUpdateBook(QStringLiteral("/path/bad2.cbz"), 0.5, QStringLiteral("corrupted-date"));
|
||||
QCOMPARE(model.rowCount(), 0);
|
||||
|
||||
model.addOrUpdateBook(QString(), 0.5, QDateTime::currentDateTime());
|
||||
QCOMPARE(model.rowCount(), 0);
|
||||
|
||||
const QDateTime dt = QDateTime::fromString(QStringLiteral("2026-10-25T12:00:00"), Qt::ISODate);
|
||||
model.addOrUpdateBook(QStringLiteral("/path/good.cbz"), 0.5, dt);
|
||||
QCOMPARE(model.rowCount(), 1);
|
||||
|
||||
model.addOrUpdateBook(QStringLiteral("/path/good.cbz"), 0.8, QStringLiteral("garbage"));
|
||||
QCOMPARE(model.lastOpened(0), dt);
|
||||
QCOMPARE(model.progress(0), 0.5);
|
||||
}
|
||||
|
||||
void loadSortsEntriesByLastOpenedDesc()
|
||||
{
|
||||
QTemporaryFile f;
|
||||
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/hist-sort-XXXXXX.ini"));
|
||||
(void)f.open(); f.close();
|
||||
QSettings s(f.fileName(), QSettings::IniFormat);
|
||||
AppState appState(s);
|
||||
|
||||
appState.saveArray(QStringLiteral("history"), {
|
||||
{
|
||||
{QStringLiteral("filePath"), QStringLiteral("/path/oldest.cbz")},
|
||||
{QStringLiteral("progress"), 0.1},
|
||||
{QStringLiteral("lastOpened"), QStringLiteral("2026-01-01T10:00:00")},
|
||||
},
|
||||
{
|
||||
{QStringLiteral("filePath"), QStringLiteral("/path/newest.cbz")},
|
||||
{QStringLiteral("progress"), 0.5},
|
||||
{QStringLiteral("lastOpened"), QStringLiteral("2026-10-30T10:00:00")},
|
||||
},
|
||||
{
|
||||
{QStringLiteral("filePath"), QStringLiteral("/path/middle.cbz")},
|
||||
{QStringLiteral("progress"), 0.3},
|
||||
{QStringLiteral("lastOpened"), QStringLiteral("2026-06-15T10:00:00")},
|
||||
},
|
||||
});
|
||||
|
||||
FormatRegistry registry = makeRegistry();
|
||||
HistoryModel model(registry);
|
||||
model.load(appState);
|
||||
|
||||
QCOMPARE(model.rowCount(), 3);
|
||||
QCOMPARE(model.filePath(0), QStringLiteral("/path/newest.cbz"));
|
||||
QCOMPARE(model.filePath(1), QStringLiteral("/path/middle.cbz"));
|
||||
QCOMPARE(model.filePath(2), QStringLiteral("/path/oldest.cbz"));
|
||||
}
|
||||
|
||||
void saveAndLoadPreservesChronologicalOrder()
|
||||
{
|
||||
QTemporaryFile f;
|
||||
f.setFileTemplate(QDir::tempPath() + QStringLiteral("/hist-pres-XXXXXX.ini"));
|
||||
(void)f.open(); f.close();
|
||||
QSettings s(f.fileName(), QSettings::IniFormat);
|
||||
AppState appState(s);
|
||||
|
||||
FormatRegistry registry = makeRegistry();
|
||||
|
||||
{
|
||||
HistoryModel model(registry);
|
||||
model.addOrUpdateBook(QStringLiteral("/path/a.cbz"), 0.1, QStringLiteral("2026-10-10T10:00:00"));
|
||||
model.addOrUpdateBook(QStringLiteral("/path/b.cbz"), 0.2, QStringLiteral("2026-10-20T10:00:00"));
|
||||
model.addOrUpdateBook(QStringLiteral("/path/c.cbz"), 0.3, QStringLiteral("2026-10-30T10:00:00"));
|
||||
|
||||
QCOMPARE(model.filePath(0), QStringLiteral("/path/c.cbz"));
|
||||
QCOMPARE(model.filePath(1), QStringLiteral("/path/b.cbz"));
|
||||
QCOMPARE(model.filePath(2), QStringLiteral("/path/a.cbz"));
|
||||
|
||||
model.save(appState);
|
||||
}
|
||||
|
||||
{
|
||||
HistoryModel model2(registry);
|
||||
model2.load(appState);
|
||||
|
||||
QCOMPARE(model2.rowCount(), 3);
|
||||
QCOMPARE(model2.filePath(0), QStringLiteral("/path/c.cbz"));
|
||||
QCOMPARE(model2.filePath(1), QStringLiteral("/path/b.cbz"));
|
||||
QCOMPARE(model2.filePath(2), QStringLiteral("/path/a.cbz"));
|
||||
}
|
||||
}
|
||||
|
||||
void addOrUpdateMaintainsChronologicalOrder()
|
||||
{
|
||||
FormatRegistry registry = makeRegistry();
|
||||
HistoryModel model(registry);
|
||||
|
||||
model.addOrUpdateBook(QStringLiteral("/path/1.cbz"), 0.1, QStringLiteral("2026-05-01T12:00:00"));
|
||||
model.addOrUpdateBook(QStringLiteral("/path/2.cbz"), 0.2, QStringLiteral("2026-01-01T12:00:00"));
|
||||
model.addOrUpdateBook(QStringLiteral("/path/3.cbz"), 0.3, QStringLiteral("2026-03-01T12:00:00"));
|
||||
|
||||
QCOMPARE(model.rowCount(), 3);
|
||||
QCOMPARE(model.filePath(0), QStringLiteral("/path/1.cbz"));
|
||||
QCOMPARE(model.filePath(1), QStringLiteral("/path/3.cbz"));
|
||||
QCOMPARE(model.filePath(2), QStringLiteral("/path/2.cbz"));
|
||||
|
||||
// Update book 2 (Jan) to June (newest) -> moves to index 0
|
||||
model.addOrUpdateBook(QStringLiteral("/path/2.cbz"), 0.5, QStringLiteral("2026-06-01T12:00:00"));
|
||||
QCOMPARE(model.filePath(0), QStringLiteral("/path/2.cbz"));
|
||||
QCOMPARE(model.filePath(1), QStringLiteral("/path/1.cbz"));
|
||||
QCOMPARE(model.filePath(2), QStringLiteral("/path/3.cbz"));
|
||||
|
||||
// Update book 2 (now index 0) to February (between Jan and Mar) -> moves to index 2
|
||||
model.addOrUpdateBook(QStringLiteral("/path/2.cbz"), 0.6, QStringLiteral("2026-02-01T12:00:00"));
|
||||
QCOMPARE(model.filePath(0), QStringLiteral("/path/1.cbz"));
|
||||
QCOMPARE(model.filePath(1), QStringLiteral("/path/3.cbz"));
|
||||
QCOMPARE(model.filePath(2), QStringLiteral("/path/2.cbz"));
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_MAIN(HistoryModelTest)
|
||||
|
||||
Reference in new issue
Block a user