From 6988ff0549533f129883eb955c9e35788420afe4 Mon Sep 17 00:00:00 2001 From: Marco Realacci Date: Fri, 14 Jun 2024 14:33:43 +0200 Subject: [PATCH] Add validation job --- .github/workflows/update-data.yml | 11 +++++++- .github/workflows/validate-questions.yml | 15 +++++++++++ scripts/validate_questions.py | 33 ++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/validate-questions.yml create mode 100644 scripts/validate_questions.py diff --git a/.github/workflows/update-data.yml b/.github/workflows/update-data.yml index ffb7469..32bab80 100644 --- a/.github/workflows/update-data.yml +++ b/.github/workflows/update-data.yml @@ -6,9 +6,18 @@ on: branches: ['main'] jobs: + validate-questions: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Validate questions + run: python scripts/validate_questions.py + shell: sh + update-data: runs-on: ubuntu-latest - steps: - name: Call webhook run: curl -X POST ${{secrets.DEPLOY_WEBHOOK}} \ No newline at end of file diff --git a/.github/workflows/validate-questions.yml b/.github/workflows/validate-questions.yml new file mode 100644 index 0000000..faeb7b4 --- /dev/null +++ b/.github/workflows/validate-questions.yml @@ -0,0 +1,15 @@ +name: Validate questions + +# Configures this workflow to run every time a change is pushed to the branch called `release`. +on: [pull_request] + +jobs: + validate-questions: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Validate questions + run: python scripts/validate_questions.py + shell: sh \ No newline at end of file diff --git a/scripts/validate_questions.py b/scripts/validate_questions.py new file mode 100644 index 0000000..8bcf750 --- /dev/null +++ b/scripts/validate_questions.py @@ -0,0 +1,33 @@ +import os +import json +import logging + +logging.basicConfig(level=logging.DEBUG) + +for filename in os.listdir("data/questions"): + logging.info("Analyzing " + filename) + with open(os.path.join("data/questions", filename), 'r') as f: + text = f.read() + try: + data = json.loads(text) + + for q in data: + if q["quest"] == "" and q["image"] == "": + raise Exception(str(data.index(q)) + ") Question's text and image cannot both be empty.") + if q["quest"] is None or q["image"] is None or q["answers"] is None or q["correct"] is None: + raise Exception(str(data.index(q)) + ") Some parameters are null or missing.") + + if len(q["answers"]) == 0: + raise Exception(str(data.index(q)) + ") Question has no answers.") + + for a in q["answers"]: + if a["text"] == "" and a["image"] == "": + raise Exception(str(data.index(q)) + ") Answer's text and image cannot both be empty.") + if a["text"] is None or a["image"] is None: + raise Exception(str(data.index(q)) + ") Some answer's parameters are null or missing.") + + except Exception as e: + logging.error(getattr(e, 'message', repr(e))) + logging.fatal(filename + " is invalid. Aborting.") + +logging.info("Parsing successful!") \ No newline at end of file