------------------------------------------------------------------ Test 5: write CDF file to a new path (round-trip serialization) ------------------------------------------------------------------
| 335 | // Test 5: write CDF file to a new path (round-trip serialization) |
| 336 | // ------------------------------------------------------------------ |
| 337 | void testWriteCdf() { |
| 338 | Settings settings; |
| 339 | // Do not call loadSettings() in tests |
| 340 | |
| 341 | Contest *contest = loadContest(m_contestDir, &settings, this); |
| 342 | QVERIFY2(contest != nullptr, "Failed to load TestContest1.cdf"); |
| 343 | |
| 344 | // Serialize to a temp file |
| 345 | QTemporaryDir outDir; |
| 346 | QVERIFY(outDir.isValid()); |
| 347 | const QString outPath = outDir.path() + "/out.cdf"; |
| 348 | |
| 349 | QFile outFile(outPath); |
| 350 | QVERIFY2(outFile.open(QFile::WriteOnly), "Cannot open output CDF file for writing"); |
| 351 | |
| 352 | QJsonObject outObj; |
| 353 | contest->writeToJson(outObj); |
| 354 | outFile.write(QJsonDocument(outObj).toJson(QJsonDocument::Compact)); |
| 355 | outFile.close(); |
| 356 | |
| 357 | // Re-read the written CDF |
| 358 | QFile inFile(outPath); |
| 359 | QVERIFY2(inFile.open(QFile::ReadOnly), "Cannot open output CDF file for reading"); |
| 360 | QJsonParseError parseError; |
| 361 | QJsonObject inObj = QJsonDocument::fromJson(inFile.readAll(), &parseError).object(); |
| 362 | QVERIFY2(parseError.error == QJsonParseError::NoError, |
| 363 | qPrintable("JSON parse error: " + parseError.errorString())); |
| 364 | |
| 365 | // Load into a fresh Contest and verify round-trip |
| 366 | auto *contest2 = new Contest(this); |
| 367 | contest2->setSettings(&settings); |
| 368 | QVERIFY2(contest2->readFromJson(inObj) == 0, "Re-reading written CDF failed"); |
| 369 | |
| 370 | QCOMPARE(contest2->getContestTitle(), contest->getContestTitle()); |
| 371 | QCOMPARE(contest2->getTaskList().size(), contest->getTaskList().size()); |
| 372 | QCOMPARE(contest2->getContestantList().size(), contest->getContestantList().size()); |
| 373 | |
| 374 | for (int i = 0; i < contest->getTaskList().size(); i++) { |
| 375 | QCOMPARE(contest2->getTask(i)->getProblemTitle(), contest->getTask(i)->getProblemTitle()); |
| 376 | } |
| 377 | |
| 378 | delete contest; |
| 379 | delete contest2; |
| 380 | } |
| 381 | }; |
| 382 | |
| 383 | QTEST_GUILESS_MAIN(TestContest) |
nothing calls this directly
no test coverage detected