def fake_questionnaire_store_no_submitted_at(): storage = Mock() storage.get_user_data = Mock(return_value=("{}", "ce_sid", 1, None)) questionnaire_store = QuestionnaireStore(storage) questionnaire_store.submitted_at = None questionnaire_store.metadata = {} questionnaire_store.answer_store = AnswerStore() return questionnaire_store
def fake_questionnaire_store(): storage = Mock() storage.get_user_data = Mock(return_value=("{}", "ce_sid", 1, None)) questionnaire_store = QuestionnaireStore(storage) questionnaire_store.submitted_at = SUBMITTED_AT questionnaire_store.metadata = {"tx_id": "123456789", "ru_name": "Apple"} questionnaire_store.answer_store = AnswerStore( [ Answer("name-answer", "John Smith", None).to_dict(), Answer("address-answer", "NP10 8XG", None).to_dict(), ] ) return questionnaire_store
def test_questionnaire_store_updates_storage(questionnaire_store, basic_input): # Given store = QuestionnaireStore(questionnaire_store.storage) store.set_metadata(basic_input["METADATA"]) store.answer_store = AnswerStore(basic_input["ANSWERS"]) store.response_metadata = basic_input["RESPONSE_METADATA"] store.progress_store = ProgressStore(basic_input["PROGRESS"]) # When store.save() # Then assert basic_input == json_loads(questionnaire_store.output_data)
def test_questionnaire_store_updates_storage(self): # Given expected = get_basic_input() store = QuestionnaireStore(self.storage) store.set_metadata(expected["METADATA"]) store.answer_store = AnswerStore(expected["ANSWERS"]) store.collection_metadata = expected["COLLECTION_METADATA"] store.progress_store = ProgressStore(expected["PROGRESS"]) # When store.save() # See setUp - populates self.output_data # Then self.assertEqual(expected, json.loads(self.output_data))
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_questionnaire_store_errors_on_invalid_object(self): # Given class NotSerializable: pass non_serializable_metadata = {"test": NotSerializable()} expected = get_basic_input() store = QuestionnaireStore(self.storage) store.set_metadata(non_serializable_metadata) store.collection_metadata = expected["COLLECTION_METADATA"] store.answer_store = AnswerStore(expected["ANSWERS"]) store.progress_store = ProgressStore(expected["PROGRESS"]) # When / Then self.assertRaises(TypeError, store.save)
def test_questionnaire_store_deletes(questionnaire_store, basic_input): # Given store = QuestionnaireStore(questionnaire_store.storage) store.set_metadata(basic_input["METADATA"]) store.response_metadata = basic_input["RESPONSE_METADATA"] store.answer_store = AnswerStore(basic_input["ANSWERS"]) store.progress_store = ProgressStore(basic_input["PROGRESS"]) # When store.delete() # Then assert "a-test-section" not in store.progress_store assert store.metadata.copy() == {} assert len(store.answer_store) == 0 assert store.response_metadata == {}
def test_questionnaire_store_deletes(self): # Given expected = get_basic_input() store = QuestionnaireStore(self.storage) store.set_metadata(expected["METADATA"]) store.collection_metadata = expected["COLLECTION_METADATA"] store.answer_store = AnswerStore(expected["ANSWERS"]) store.progress_store = ProgressStore(expected["PROGRESS"]) # When store.delete() # See setUp - populates self.output_data # Then self.assertNotIn("a-test-section", store.progress_store) self.assertEqual(store.metadata.copy(), {}) self.assertEqual(len(store.answer_store), 0) self.assertEqual(store.collection_metadata, {})
def test_questionnaire_store_errors_on_invalid_object(questionnaire_store, basic_input): # Given class NotSerializable: pass non_serializable_metadata = {"test": NotSerializable()} store = QuestionnaireStore(questionnaire_store.storage) store.set_metadata(non_serializable_metadata) store.response_metadata = basic_input["RESPONSE_METADATA"] store.answer_store = AnswerStore(basic_input["ANSWERS"]) store.progress_store = ProgressStore(basic_input["PROGRESS"]) # When / Then with pytest.raises(TypeError): store.save()