feat(GODT-2794): Clear cached answers when report is sent.

This commit is contained in:
Romain LE JEUNE
2023-07-28 10:20:01 +02:00
committed by Romain Le Jeune
parent c4bcc38c53
commit 3d64c5f894
9 changed files with 147 additions and 48 deletions

View File

@ -22,6 +22,36 @@
using namespace bridgepp;
namespace {
const QString goodJson = "{"
" \"metadata\": {"
" \"version\": \"1.0.0\""
" },"
" \"data_v1.0.0\": {"
" \"categories\": ["
" {"
" \"id\": 0,"
" \"name\": \"I can't receive mail\","
" \"questions\": [0]"
" }"
" ],"
" \"questions\": ["
" {"
" \"id\": 0,"
" \"text\": \"What happened?\","
" \"tips\": \"Expected behavior\","
" \"type\": 1"
" }"
" ]"
" }"
"}";
}
//****************************************************************************************************************************************************
//
//****************************************************************************************************************************************************
@ -84,28 +114,7 @@ TEST_F(BugReportFlowFixture, emptyFile) {
//
//****************************************************************************************************************************************************
TEST_F(BugReportFlowFixture, validFile) {
feedTempFile("{"
" \"metadata\": {"
" \"version\": \"1.0.0\""
" },"
" \"data_v1.0.0\": {"
" \"categories\": ["
" {"
" \"id\": 0,"
" \"name\": \"I can't receive mail\","
" \"questions\": [0]"
" }"
" ],"
" \"questions\": ["
" {"
" \"id\": 0,"
" \"text\": \"What happened?\","
" \"tips\": \"Expected behavior\","
" \"type\": 1"
" }"
" ]"
" }"
"}");
feedTempFile(goodJson);
EXPECT_TRUE(flow_.parse(file_.fileName()));
QStringList categories = flow_.categories();
@ -131,6 +140,8 @@ TEST_F(BugReportFlowFixture, validFile) {
qDebug() << flow_.collectAnswers(0);
EXPECT_EQ(flow_.collectAnswers(0), "Category: I can't receive mail\n\r - What happened?\n\rpwet\n\r");
EXPECT_EQ(flow_.collectAnswers(1), "");
EXPECT_EQ(flow_.getAnswer(0), "pwet");
EXPECT_EQ(flow_.getAnswer(1), "");
flow_.clearAnswers();
EXPECT_EQ(flow_.collectAnswers(0), "Category: I can't receive mail\n\r");
}

View File

@ -91,6 +91,19 @@ bool BugReportFlow::setAnswer(quint8 questionId, QString const &answer) {
}
//****************************************************************************************************************************************************
/// \param[in] questionId The id of the question.
/// \return answer the given question.
//****************************************************************************************************************************************************
QString BugReportFlow::getAnswer(quint8 questionId) const {
QString answer;
if (questionId <= questions_.count() - 1) {
answer = answers_[questionId];
}
return answer;
}
//****************************************************************************************************************************************************
/// \param[in] categoryId The id of the question set.
/// \return concatenate answers for set of questions.
@ -103,7 +116,7 @@ QString BugReportFlow::collectAnswers(quint8 categoryId) const {
answers += "Category: " + categories_[categoryId] + "\n\r";
QVariantList sets = this->questionSet(categoryId);
for (QVariant const &var: sets) {
const QString& answer = answers_[var.toInt()];
const QString& answer = getAnswer(var.toInt());
if (answer.isEmpty())
continue;
answers += " - " + questions_[var.toInt()].toMap()["text"].toString() + "\n\r";
@ -122,15 +135,12 @@ void BugReportFlow::clearAnswers() {
//****************************************************************************************************************************************************
//
/// \return true iff parsing succeed.
//****************************************************************************************************************************************************
bool BugReportFlow::parseFile() {
categories_.clear();
questions_.clear();
questionsSet_.clear();
reset();
QJsonObject data = getJsonDataObj(getJsonRootObj());
QJsonArray categoriesJson = data.value("categories").toArray();
for (const QJsonValueRef &v : categoriesJson) {
categories_.append(v.toObject()["name"].toString());
@ -140,7 +150,22 @@ bool BugReportFlow::parseFile() {
return true;
}
QJsonObject BugReportFlow::getJsonRootObj() {
//****************************************************************************************************************************************************
//
//****************************************************************************************************************************************************
void BugReportFlow::reset() {
categories_.clear();
questions_.clear();
questionsSet_.clear();
answers_.clear();
}
//****************************************************************************************************************************************************
//
//****************************************************************************************************************************************************
QJsonObject BugReportFlow::getJsonRootObj() const {
QFile file(filepath_);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return QJsonObject();
@ -152,7 +177,10 @@ QJsonObject BugReportFlow::getJsonRootObj() {
}
QJsonObject BugReportFlow::getJsonDataObj(const QJsonObject& root) {
//****************************************************************************************************************************************************
//
//****************************************************************************************************************************************************
QJsonObject BugReportFlow::getJsonDataObj(const QJsonObject& root) const {
QString version = getJsonVersion(root);
if (version.isEmpty())
return QJsonObject();
@ -160,13 +188,15 @@ QJsonObject BugReportFlow::getJsonDataObj(const QJsonObject& root) {
QJsonValue data = root.value(QString("data_v%1").arg(version));
if (data == QJsonValue::Undefined || !data.isObject())
return QJsonObject();
QJsonObject dataObj = data.toObject();
return migrateData(dataObj, version);
return migrateData(data.toObject(), version);
}
QString BugReportFlow::getJsonVersion(const QJsonObject& root) {
//****************************************************************************************************************************************************
//
//****************************************************************************************************************************************************
QString BugReportFlow::getJsonVersion(const QJsonObject& root) const{
QJsonValue metadata = root.value("metadata");
if (metadata == QJsonValue::Undefined || !metadata.isObject()) {
return QString();
@ -179,9 +209,13 @@ QString BugReportFlow::getJsonVersion(const QJsonObject& root) {
}
QJsonObject BugReportFlow::migrateData(const QJsonObject& data, const QString& version) {
//****************************************************************************************************************************************************
//
//****************************************************************************************************************************************************
QJsonObject BugReportFlow::migrateData(const QJsonObject& data, const QString& version) const{
if (version != currentFormatVersion)
return QJsonObject();
// nothing to migrate now but migration should be done here.
return data;
}

View File

@ -33,23 +33,25 @@ public: // member functions.
BugReportFlow(BugReportFlow &&) = delete; ///< Disabled assignment copy-constructor.
~BugReportFlow() = default; ///< Destructor.
bool parse(const QString& filepath); ///< Initialize the Bug Report Flow.
[[nodiscard]] bool parse(const QString& filepath); ///< Initialize the Bug Report Flow.
QStringList categories() const; ///< Getter for the 'bugCategories' property.
QVariantList questions() const; ///< Getter for the 'bugQuestions' property.
QVariantList questionSet(quint8 categoryId) const; ///< Retrieve the set of question for a given bug category.
[[nodiscard]] QStringList categories() const; ///< Getter for the 'bugCategories' property.
[[nodiscard]] QVariantList questions() const; ///< Getter for the 'bugQuestions' property.
[[nodiscard]] QVariantList questionSet(quint8 categoryId) const; ///< Retrieve the set of question for a given bug category.
bool setAnswer(quint8 questionId, QString const &answer); ///< Feed an answer for a given question.
QString collectAnswers(quint8 categoryId) const; ///< Collect answer for a given set of questions.
[[nodiscard]] bool setAnswer(quint8 questionId, QString const &answer); ///< Feed an answer for a given question.
[[nodiscard]] QString getAnswer(quint8 questionId) const; ///< Collect answer for a given questions.
[[nodiscard]] QString collectAnswers(quint8 categoryId) const; ///< Collect answer for a given set of questions.
void clearAnswers(); ///< Clear all collected answers.
private: // member functions
bool parseFile(); ///< Parse the bug report flow description file.
QJsonObject getJsonRootObj(); ///< Extract the JSON root object.
QJsonObject getJsonDataObj(const QJsonObject& root); ///< Extract the JSON data object.
QString getJsonVersion(const QJsonObject& root); ///< Extract the JSON version of the file.
QJsonObject migrateData(const QJsonObject& data, const QString& version); ///< Migrate data if needed/possible.
void reset(); ///< Reset all data.
[[nodiscard]] QJsonObject getJsonRootObj() const; ///< Extract the JSON root object.
[[nodiscard]] QJsonObject getJsonDataObj(const QJsonObject& root) const; ///< Extract the JSON data object.
[[nodiscard]] QString getJsonVersion(const QJsonObject& root) const; ///< Extract the JSON version of the file.
[[nodiscard]] QJsonObject migrateData(const QJsonObject& data, const QString& version) const; ///< Migrate data if needed/possible.
private: // data members
QString filepath_; ///< The file path of the BugReportFlow description file.