def test_format_response_nonnumeric_answer_id():
    base = {
        "submission_date": SUBMISSION_DATE,
        "id": "1",
        "status": "Complete",
        "session_id": "1538059336_5bacec4869caa2.27680217",
        "response_time": 10,
        "survey_data": {
            "1": {
                "answer_id": "10001-other",
            },
            "2": {
                "answer_id": "fadfasdf-other",
            },
        },
    }
    res = format_responses(base, SUBMISSION_DATE)
    assert res["survey_data"][0]["answer_id"] == 10001
    assert not res["survey_data"][1].get("answer_id")
def test_insert_to_bq_options(testing_table_id):
    # Override survey data, but make sure to deep copy to prevent mutating state
    # in other tests.
    # https://apihelp.alchemer.com/help/surveyresponse-per-question-v5#textboxlist
    base = copy.deepcopy(EXAMPLE_RESPONSE["data"][0])
    base["survey_data"] = {
        "37": {
            "id": 37,
            "type": "parent",
            "question": "Textbox List Question Title",
            "section_id": 3,
            "options": {
                "10068": {
                    "id": 10068,
                    "option": "Row 1",
                    "answer": "text list answer"
                }
            },
            "shown": True,
        },
        "38": {
            "id": 38,
            "type": "parent",
            "question": "Continuous Sum Question Title",
            "section_id": 3,
            "options": {
                "10070": {
                    "id": 10070,
                    "option": "Row 1",
                    "answer": "6"
                },
                "10071": {
                    "id": 10071,
                    "option": "Row 2",
                    "answer": "7"
                },
            },
            "shown": True,
        },
    }
    transformed = [format_responses(base, SUBMISSION_DATE)]
    insert_to_bq(transformed, testing_table_id, SUBMISSION_DATE)
def test_insert_to_bq_subquestions(testing_table_id):
    # Override survey data. Note that the subquestion object is incompatible.
    # https://apihelp.alchemer.com/help/surveyresponse-per-question-v5#checkboxgrid
    base = copy.deepcopy(EXAMPLE_RESPONSE["data"][0])
    base["survey_data"] = {
        "30": {
            "id": 30,
            "type": "parent",
            "question": "Checkbox Grid Question Title",
            "subquestions": {
                "31": {
                    "10062": {
                        "id": 10062,
                        "type": "CHECKBOX",
                        "parent": 30,
                        "question": "Row 1 : Column 1",
                        "answer": "Column 1",
                        "shown": True,
                    },
                    "10063": {
                        "id": 10063,
                        "type": "CHECKBOX",
                        "parent": 30,
                        "question": "Row 1 : Column 2",
                        "answer": None,
                        "shown": True,
                    },
                },
                "32": {
                    "10062": {
                        "id": 10062,
                        "type": "CHECKBOX",
                        "parent": 30,
                        "question": "Row 2 : Column 1",
                        "answer": None,
                        "shown": True,
                    },
                    "10063": {
                        "id": 10063,
                        "type": "CHECKBOX",
                        "parent": 30,
                        "question": "Row 2 : Column 2",
                        "answer": "Column 2",
                        "shown": True,
                    },
                },
            },
            "section_id": 3,
            "shown": True,
        },
        "83": {
            "id": 83,
            "type": "parent",
            "question": "Custom Table Question Title",
            "subquestions": {
                "10001": {
                    "id": 10001,
                    "type": "RADIO",
                    "question": "Radio Button Column",
                    "section_id": 4,
                    "answer": "Option 1",
                    "answer_id": 10113,
                    "shown": True,
                },
                "10002": {
                    "id": 10002,
                    "type": "RADIO",
                    "question": "Radio Button Column",
                    "section_id": 4,
                    "answer": "Option 2",
                    "answer_id": 10114,
                    "shown": True,
                },
            },
            "section_id": 4,
            "shown": True,
        },
    }
    transformed = [format_responses(base, SUBMISSION_DATE)]
    insert_to_bq(transformed, testing_table_id, SUBMISSION_DATE)
def test_format_response():
    assert (format_responses(EXAMPLE_RESPONSE["data"][0],
                             SUBMISSION_DATE) == EXAMPLE_RESPONSE_FORMATTED_0)