def test_month_year_date_answer(fake_questionnaire_store): full_routing_path = [RoutingPath(["date-block"], section_id="section-1")] answers = AnswerStore([ Answer("single-date-answer", "01-01-1990").to_dict(), Answer("month-year-answer", "01-1990").to_dict(), ]) fake_questionnaire_store.answer_store = answers questionnaire = make_schema( "0.0.3", "section-1", "date-group", "date-block", { "id": "month-year-question", "answers": [{ "id": "month-year-answer", "type": "MonthYearDate" }], }, ) answer_object = convert_answers(QuestionnaireSchema(questionnaire), fake_questionnaire_store, full_routing_path) assert len(answer_object["data"]["answers"]) == 1 assert answer_object["data"]["answers"][0].value == "01-1990"
def test_converter_checkboxes_with_q_codes_and_empty_other_value( fake_questionnaire_store): routing_path = RoutingPath(["crisps"], section_id="food") fake_questionnaire_store.answer_store = AnswerStore([ Answer("crisps-answer", ["Ready salted", "Other"]).to_dict(), Answer("other-answer-mandatory", "").to_dict(), ]) question = { "id": "crisps-question", "answers": [{ "id": "crisps-answer", "type": "Checkbox", "options": [ { "label": "Ready salted", "value": "Ready salted", "q_code": "1" }, { "label": "Sweet chilli", "value": "Sweet chilli", "q_code": "2" }, { "label": "Cheese and onion", "value": "Cheese and onion", "q_code": "3", }, { "label": "Other", "q_code": "4", "description": "Choose any other flavour", "value": "Other", "detail_answer": { "mandatory": True, "id": "other-answer-mandatory", "label": "Please specify other", "type": "TextField", }, }, ], }], } questionnaire = make_schema("0.0.1", "section-1", "favourite-food", "crisps", question) # When answer_object = convert_answers(QuestionnaireSchema(questionnaire), fake_questionnaire_store, routing_path) # Then assert len(answer_object["data"]) == 2 assert answer_object["data"]["1"] == "Ready salted" assert answer_object["data"]["4"] == "Other"
def test_percentage_answer(fake_questionnaire_store): routing_path = RoutingPath(["percentage-block"], section_id="section-1") fake_questionnaire_store.answer_store = AnswerStore( [Answer("percentage-answer", 100).to_dict()]) question = { "id": "percentage-question", "answers": [{ "id": "percentage-answer", "type": "Percentage", "q_code": "1" }], } questionnaire = make_schema("0.0.1", "section-1", "percentage-block", "percentage-block", question) # When answer_object = convert_answers(QuestionnaireSchema(questionnaire), fake_questionnaire_store, routing_path) # Then assert len(answer_object["data"]) == 1 assert answer_object["data"]["1"] == "100"
def test_get_referenced_offset_value_with_list_item_id(app, schema_mock): list_item_id = "abcde" answer_store = AnswerStore() test_answer_id = Answer(answer_id="date", value="2018-03-20", list_item_id=list_item_id) location = Location(section_id="test", list_item_id=list_item_id) answer_store.add_or_update(test_answer_id) answer = { "maximum": { "value": { "identifier": "date", "source": "answers" }, "offset_by": { "months": 1 }, } } handler = DateHandler(answer, answer_store=answer_store, location=location) maximum_date = handler.get_date_value("maximum") assert maximum_date == convert_to_datetime("2018-04-20")
def test_unit_answer(fake_questionnaire_store): full_routing_path = [RoutingPath(["unit-block"], section_id="section-1")] answers = AnswerStore([Answer("unit-answer", 10).to_dict()]) fake_questionnaire_store.answer_store = answers questionnaire = make_schema( "0.0.3", "section-1", "unit-group", "unit-block", { "id": "unit-question", "answers": [{ "id": "unit-answer", "type": "Unit" }] }, ) answer_object = convert_answers(QuestionnaireSchema(questionnaire), fake_questionnaire_store, full_routing_path) assert len(answer_object["data"]["answers"]) == 1 assert answer_object["data"]["answers"][0].value == 10
def test_textarea_answer(fake_questionnaire_store): full_routing_path = [ RoutingPath(["textarea-block"], section_id="section-1") ] answers = AnswerStore( [Answer("textarea-answer", "This is an example text!").to_dict()]) fake_questionnaire_store.answer_store = answers questionnaire = make_schema( "0.0.3", "section-1", "textarea-group", "textarea-block", { "id": "textarea-question", "answers": [{ "id": "textarea-answer", "type": "TextArea" }], }, ) answer_object = convert_answers(QuestionnaireSchema(questionnaire), fake_questionnaire_store, full_routing_path) assert len(answer_object["data"]["answers"]) == 1 assert answer_object["data"]["answers"][ 0].value == "This is an example text!"
def get_default_answer(self, answer_id): try: answer_schema = self.get_answers_by_answer_id(answer_id)[0] answer = Answer(answer_schema["id"], answer_schema["default"]) except (KeyError, TypeError): return None return answer
def test_convert_answers_to_payload_0_0_1_with_key_error( fake_questionnaire_store): fake_questionnaire_store.answer_store = AnswerStore([ Answer("ABC", "2016-01-01").to_dict(), Answer("DEF", "2016-03-30").to_dict(), Answer("GHI", "2016-05-30").to_dict(), ]) question = { "id": "question-1", "answers": [ { "id": "LMN", "type": "TextField", "q_code": "001" }, { "id": "DEF", "type": "TextField", "q_code": "002" }, { "id": "JKL", "type": "TextField", "q_code": "003" }, ], } questionnaire = make_schema("0.0.1", "section-1", "group-1", "block-1", question) routing_path = RoutingPath(["block-1"], section_id="section-1") answer_object = convert_answers_to_payload_0_0_1( fake_questionnaire_store.metadata, fake_questionnaire_store.answer_store, fake_questionnaire_store.list_store, QuestionnaireSchema(questionnaire), routing_path, ) assert answer_object["002"] == "2016-03-30" assert len(answer_object) == 1
def test_converter_q_codes_for_empty_strings(fake_questionnaire_store): routing_path = RoutingPath(["crisps"], section_id="food") fake_questionnaire_store.answer_store = AnswerStore([ Answer("crisps-answer", "").to_dict(), Answer("other-crisps-answer", "Ready salted").to_dict(), ]) question = { "id": "crisps-question", "answers": [ { "id": "crisps-answer", "type": "TextArea", "options": [], "q_code": "1" }, { "id": "other-crisps-answer", "type": "TextArea", "options": [], "q_code": "2", }, ], } questionnaire = make_schema("0.0.1", "section-1", "favourite-food", "crisps", question) # When answer_object = convert_answers(QuestionnaireSchema(questionnaire), fake_questionnaire_store, routing_path) # Then assert len(answer_object["data"]) == 1 assert answer_object["data"]["2"] == "Ready salted"
def test_get_schema_value_answer_store(): answer_schema = { "id": "test-range", "label": "", "description": "Range Test", "mandatory": False, "type": "Number", "decimal_places": 2, "maximum": { "value": { "identifier": "set-maximum", "source": "answers" } }, "minimum": { "value": { "identifier": "set-minimum", "source": "answers" } }, } mock_metadata = {"schema_name": "test_numbers", "language_code": "en"} answer_store = AnswerStore() answer_store.add_or_update(Answer(answer_id="set-maximum", value=10)) answer_store.add_or_update(Answer(answer_id="set-minimum", value=1)) number_handler = NumberHandler(answer_schema, answer_store=answer_store, metadata=mock_metadata) maximum = number_handler.get_schema_value(answer_schema["maximum"]) minimum = number_handler.get_schema_value(answer_schema["minimum"]) assert maximum == 10 assert minimum == 1
def fake_questionnaire_store(fake_metadata, fake_collection_metadata): user_answer = Answer(answer_id="GHI", value=0, list_item_id=None) storage = MagicMock() storage.get_user_data = MagicMock(return_value=("{}", 1)) storage.add_or_update = MagicMock() store = QuestionnaireStore(storage) store.answer_store = AnswerStore() store.answer_store.add_or_update(user_answer) store.metadata = fake_metadata store.collection_metadata = fake_collection_metadata return store
def test_convert_payload_0_0_3_multiple_answers(fake_questionnaire_store): full_routing_path = [RoutingPath(["crisps"], section_id="section-1")] answers = AnswerStore( [Answer("crisps-answer", ["Ready salted", "Sweet chilli"]).to_dict()]) fake_questionnaire_store.answer_store = answers questionnaire = make_schema( "0.0.3", "section-1", "favourite-food", "crisps", { "id": "crisps-question", "answers": [{ "id": "crisps-answer", "type": "Checkbox", "options": [ { "label": "Ready salted", "value": "Ready salted" }, { "label": "Sweet chilli", "value": "Sweet chilli" }, { "label": "Cheese and onion", "value": "Cheese and onion" }, ], }], }, ) # When answer_object = convert_answers(QuestionnaireSchema(questionnaire), fake_questionnaire_store, full_routing_path) # Then assert len(answer_object["data"]["answers"]) == 1 assert answer_object["data"]["answers"][0].value == [ "Ready salted", "Sweet chilli" ]
def test_dropdown_answer(fake_questionnaire_store): full_routing_path = [ RoutingPath(["dropdown-block"], section_id="section-1") ] answers = AnswerStore( [Answer("dropdown-answer", "Rugby is better!").to_dict()]) fake_questionnaire_store.answer_store = answers questionnaire = make_schema( "0.0.3", "section-1", "dropdown-group", "dropdown-block", { "id": "dropdown-question", "answers": [{ "id": "dropdown-answer", "type": "Dropdown", "options": [ { "label": "Liverpool", "value": "Liverpool" }, { "label": "Chelsea", "value": "Chelsea" }, { "label": "Rugby is better!", "value": "Rugby is better!" }, ], }], }, ) answer_object = convert_answers(QuestionnaireSchema(questionnaire), fake_questionnaire_store, full_routing_path) # Then assert len(answer_object["data"]["answers"]) == 1 assert answer_object["data"]["answers"][0].value == "Rugby is better!"
def test_dropdown_answer(fake_questionnaire_store): routing_path = RoutingPath(["dropdown-block"], section_id="section-1") fake_questionnaire_store.answer_store = AnswerStore( [Answer("dropdown-answer", "Liverpool").to_dict()]) question = { "id": "dropdown-question", "answers": [{ "id": "dropdown-answer", "type": "Dropdown", "q_code": "1", "options": [ { "label": "Liverpool", "value": "Liverpool" }, { "label": "Chelsea", "value": "Chelsea" }, { "label": "Rugby is better!", "value": "Rugby is better!" }, ], }], } questionnaire = make_schema("0.0.1", "section-1", "dropdown-block", "dropdown-block", question) # When answer_object = convert_answers(QuestionnaireSchema(questionnaire), fake_questionnaire_store, routing_path) # Then assert len(answer_object["data"]) == 1 assert answer_object["data"]["1"] == "Liverpool"
def test_get_referenced_offset_value_for_answer_id(app): answer_store = AnswerStore() test_answer_id = Answer(answer_id="date", value="2018-03-20") answer_store.add_or_update(test_answer_id) answer = { "maximum": { "value": { "identifier": "date", "source": "answers" } } } handler = DateHandler(answer, answer_store=answer_store) maximum_date = handler.get_date_value("maximum") maximum_date = handler.transform_date_by_offset(maximum_date, {"months": 1}) assert maximum_date == convert_to_datetime("2018-04-20")
def test_answer_without_qcode(fake_questionnaire_store): fake_questionnaire_store.answer_store = AnswerStore( [Answer("GHI", "String test + !").to_dict()]) question = { "id": "question-2", "answers": [{ "id": "GHI", "type": "TextField" }] } questionnaire = make_schema("0.0.1", "section-1", "group-1", "block-1", question) routing_path = RoutingPath(["block-1"], section_id="section-1") answer_object = convert_answers(QuestionnaireSchema(questionnaire), fake_questionnaire_store, routing_path) assert not answer_object["data"]
def test_answer_with_zero(fake_questionnaire_store): fake_questionnaire_store.answer_store = AnswerStore( [Answer("GHI", 0).to_dict()]) question = { "id": "question-2", "answers": [{ "id": "GHI", "type": "TextField", "q_code": "003" }], } questionnaire = make_schema("0.0.1", "section-1", "group-1", "block-1", question) routing_path = RoutingPath(["block-1"], section_id="section-1") answer_object = convert_answers(QuestionnaireSchema(questionnaire), fake_questionnaire_store, routing_path) assert answer_object["data"]["003"] == "0"
def test_radio_answer(fake_questionnaire_store): full_routing_path = [RoutingPath(["radio-block"], section_id="section-1")] answers = AnswerStore([Answer("radio-answer", "Coffee").to_dict()]) fake_questionnaire_store.answer_store = answers questionnaire = make_schema( "0.0.3", "section-1", "radio-group", "radio-block", { "id": "radio-question", "answers": [{ "type": "Radio", "id": "radio-answer", "options": [ { "label": "Coffee", "value": "Coffee" }, { "label": "Tea", "value": "Tea" }, ], }], }, ) answer_object = convert_answers(QuestionnaireSchema(questionnaire), fake_questionnaire_store, full_routing_path) assert len(answer_object["data"]["answers"]) == 1 assert answer_object["data"]["answers"][0].value == "Coffee"
def test_radio_answer(fake_questionnaire_store): routing_path = RoutingPath(["radio-block"], section_id="section-1") fake_questionnaire_store.answer_store = AnswerStore( [Answer("radio-answer", "Coffee").to_dict()]) question = { "id": "radio-question", "answers": [{ "type": "Radio", "id": "radio-answer", "q_code": "1", "options": [ { "label": "Coffee", "value": "Coffee" }, { "label": "Tea", "value": "Tea" }, ], }], } questionnaire = make_schema("0.0.1", "section-1", "radio-block", "radio-block", question) # When answer_object = convert_answers(QuestionnaireSchema(questionnaire), fake_questionnaire_store, routing_path) # Then assert len(answer_object["data"]) == 1 assert answer_object["data"]["1"] == "Coffee"
def test_minimum_and_maximum_offset_dates(app): test_metadata = {"date": "2018-02-20"} store = AnswerStore() test_answer_id = Answer(answer_id="date", value="2018-03-20") store.add_or_update(test_answer_id) answer = { "id": "date_answer", "type": "Date", "minimum": { "value": { "identifier": "date", "source": "metadata" }, "offset_by": { "days": -10 }, }, "maximum": { "value": { "identifier": "date", "source": "answers" }, "offset_by": { "years": 1 }, }, } handler = DateHandler(answer, answer_store=store, metadata=test_metadata) minimum_date = handler.get_date_value("minimum") maximum_date = handler.get_date_value("maximum") assert minimum_date == convert_to_datetime("2018-02-10") assert maximum_date == convert_to_datetime("2019-03-20")
def test_textarea_answer(fake_questionnaire_store): routing_path = RoutingPath(["textarea-block"], section_id="section-1") fake_questionnaire_store.answer_store = AnswerStore( [Answer("textarea-answer", "example text.").to_dict()]) question = { "id": "textarea-question", "answers": [{ "id": "textarea-answer", "q_code": "1", "type": "TextArea" }], } questionnaire = make_schema("0.0.1", "section-1", "textarea-block", "textarea-block", question) # When answer_object = convert_answers(QuestionnaireSchema(questionnaire), fake_questionnaire_store, routing_path) # Then assert len(answer_object["data"]) == 1 assert answer_object["data"]["1"] == "example text."
def test_currency_answer(fake_questionnaire_store): routing_path = RoutingPath(["currency-block"], section_id="section-1") fake_questionnaire_store.answer_store = AnswerStore( [Answer("currency-answer", 99.99).to_dict()]) question = { "id": "currency-question", "answers": [{ "id": "currency-answer", "type": "Currency", "q_code": "1" }], } questionnaire = make_schema("0.0.1", "section-1", "currency-block", "currency-block", question) # When answer_object = convert_answers(QuestionnaireSchema(questionnaire), fake_questionnaire_store, routing_path) # Then assert len(answer_object["data"]) == 1 assert answer_object["data"]["1"] == "99.99"
def _build_map(answers: List[Dict]): """ Builds the answer_store's data structure from a list of answer dictionaries""" return { (answer["answer_id"], answer.get("list_item_id")): Answer.from_dict(answer) for answer in answers }
def test_convert_answers_to_payload_0_0_3(fake_questionnaire_store): full_routing_path = [ RoutingPath(["about you", "where you live"], section_id="household-section") ] fake_questionnaire_store.answer_store = AnswerStore([ Answer("name", "Joe Bloggs", None).to_dict(), Answer("address", "62 Somewhere", None).to_dict(), ]) questionnaire = { "survey_id": "021", "data_version": "0.0.3", "sections": [{ "id": "household-section", "groups": [ { "id": "personal details", "blocks": [{ "id": "about you", "type": "Question", "question": { "id": "crisps-question", "answers": [{ "id": "name", "type": "TextField" }], }, }], }, { "id": "household", "blocks": [{ "id": "where you live", "type": "Question", "question": { "id": "crisps-question", "answers": [{ "id": "address", "type": "TextField" }], }, }], }, ], }], } # When answer_object = convert_answers(QuestionnaireSchema(questionnaire), fake_questionnaire_store, full_routing_path) # Then assert len(answer_object["data"]["answers"]) == 2 assert answer_object["data"]["answers"][0].value == "Joe Bloggs" assert answer_object["data"]["answers"][1].value, "62 Somewhere"