/
DemienMedich
/
BodyweightBase
Обзор
Документация
Войти
/
DemienMedich
/
BodyweightBase
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
tests/state_importer_tests.cpp
114 строк
5 KB
DemienMedich
Add adaptive training and health analytics core
09 июл 2026, 11:27
09 июл 2026, 11:27
487c3ec
Код
Авторство
О чём код?
#include "bodyweight/state_importer.h" #include "bodyweight/legacy_migrator.h" #include <QtTest> class StateImporterTests : public QObject { Q_OBJECT private slots: void importsCurrentPascalCaseFormat(); void rejectsInvalidJson(); void reportsCompatibilityWarnings(); void migratesLegacyRootToNativeSchema(); }; void StateImporterTests::importsCurrentPascalCaseFormat() { const QByteArray json = R"({ "LibraryVersion": "2026.1", "SelectedPlanId": "starter", "SelectedPlanVariantId": "full", "SelectedRoundCount": 2, "CurrentBodyWeightKg": 81.5, "WorkoutPlans": [{ "Id": "starter", "Name": "Starter", "RoundCount": 2, "Steps": [{ "ExerciseId": "squat", "TargetOverride": 12, "RestSecondsOverride": null, "CoachNote": "steady" }] }], "SessionHistory": [{}, {}], "PendingSessionDraft": null })"; const bodyweight::ImportResult result = bodyweight::importState(json); QVERIFY2(result.ok, qPrintable(result.error)); QCOMPARE(result.state.workoutPlans.size(), 1); QCOMPARE(result.state.workoutPlans.first().steps.first().exerciseId, QString("squat")); QCOMPARE(result.state.workoutPlans.first().steps.first().targetOverride, std::optional<int>(12)); QCOMPARE(result.state.sessionCount, 2); QVERIFY(!result.state.hasPendingSessionDraft); QVERIFY(result.warnings.isEmpty()); } void StateImporterTests::rejectsInvalidJson() { const bodyweight::ImportResult result = bodyweight::importState("{ invalid"); QVERIFY(!result.ok); QVERIFY(!result.error.isEmpty()); } void StateImporterTests::reportsCompatibilityWarnings() { const bodyweight::ImportResult result = bodyweight::importState(R"({ "SelectedPlanId": "missing", "SelectedRoundCount": 9, "WorkoutPlans": [], "SessionHistory": [] })"); QVERIFY(result.ok); QCOMPARE(result.warnings.size(), 2); } void StateImporterTests::migratesLegacyRootToNativeSchema() { const QJsonObject native = bodyweight::migrateLegacyRoot(QJsonObject{ {"LibraryVersion", "legacy"}, {"SelectedPlanId", "p"}, {"NutritionPresetId", "cheap"}, {"BodyweightHistory", QJsonArray{QJsonObject{{"LoggedAtLocal", "2026-06-16T08:00:00"}, {"WeightKg", 76.2}}}}, {"BodyMeasurementHistory", QJsonArray{QJsonObject{ {"LoggedAtLocal", "2026-06-16T08:05:00"}, {"WaistCm", 80.0}, {"ShouldersCm", 116.0}, {"ChestCm", 101.0}, {"ArmCm", 34.0} }}}, {"NutritionAdherenceHistory", QJsonArray{QJsonObject{{"DateLocal", "2026-06-16T00:00:00"}, {"StatusId", "good"}}}}, {"PhotoProgressHistory", QJsonArray{QJsonObject{{"LoggedAtLocal", "2026-06-16T08:10:00"}, {"ImagePath", "photo.jpg"}}}}, {"RecoveryCheckInHistory", QJsonArray{QJsonObject{ {"PlanName", "Plan"}, {"LoggedAtLocal", "2026-06-16T09:00:00"}, {"EnergyScore", 4}, {"SleepScore", 3}, {"JointScore", 5}, {"Note", "ok"} }}}, {"WorkoutPlans", QJsonArray{QJsonObject{ {"Id", "p"}, {"Name", "Пт • Plan"}, {"Steps", QJsonArray{QJsonObject{{"ExerciseId", "squat"}, {"TargetOverride", 10}}}} }}}, {"SessionHistory", QJsonArray{QJsonObject{ {"SessionId", "id"}, {"PlanId", "p"}, {"PlanName", "Plan"}, {"Steps", QJsonArray{QJsonObject{{"ExerciseId", "squat"}, {"Metric", 0}, {"Completed", true}}}} }}} }); QVERIFY(native.contains("schemaVersion")); QVERIFY(!native.contains("WorkoutPlans")); QCOMPARE(native.value("nutritionPresetId").toString(), QString("cheap")); QCOMPARE(native.value("bodyweightHistory").toArray().first().toObject().value("weightKg").toDouble(), 76.2); QCOMPARE(native.value("bodyMeasurementHistory").toArray().first().toObject().value("waistCm").toDouble(), 80.0); QCOMPARE(native.value("nutritionAdherenceHistory").toArray().first().toObject().value("statusId").toString(), QString("good")); QCOMPARE(native.value("photoProgressHistory").toArray().first().toObject().value("imagePath").toString(), QString("photo.jpg")); QCOMPARE(native.value("recoveryCheckInHistory").toArray().first().toObject().value("energyScore").toInt(), 4); QCOMPARE(native.value("plans").toArray().first().toObject().value("dayOfWeek").toInt(), 5); QCOMPARE(native.value("sessions").toArray().size(), 1); const bodyweight::ImportResult imported = bodyweight::importNativeState(QJsonDocument(native).toJson()); QVERIFY2(imported.ok, qPrintable(imported.error)); QCOMPARE(imported.state.workoutPlans.size(), 1); QCOMPARE(imported.state.sessionCount, 1); } QTEST_MAIN(StateImporterTests) #include "state_importer_tests.moc"