def test_form_wizard_next_in_post(rf, law_case_factory, document_factory, questionaire_factory): class TestWizardView(FormWizardView): def get_lawcase(self): return LawCase.objects.all().first() d = document_factory() lc = law_case_factory(allow_download=True, save_answers=True, document=d) qn1 = questionaire_factory(success_message='Success qn1', law_case=lc) qn2 = questionaire_factory(success_message='Success qn1', law_case=lc) q1 = Question.add_root(**get_single_option_question(questionaire=qn1)) q2 = Question.add_root(**get_single_option_question(questionaire=qn2)) data = {'next': q2.id} request = rf.post('/', data) middleware = SessionMiddleware(dummy_get_response) middleware.process_request(request) praefix = 'legal_advice_builder_{}'.format(lc.id) request.session[praefix] = json.dumps( { 'current_question': q1.id, 'answers': [{ 'question': q1.id, 'option': 'yes' }] }, cls=DjangoJSONEncoder) request.session.save() resp = TestWizardView.as_view()(request) assert resp.context_data.get('question') == q2
def test_placeholders_for_template_with_short_titles(law_case_factory, questionaire_factory): law_case = law_case_factory() qn_1 = questionaire_factory(law_case=law_case, order=1, short_title='qn_1') qn_2 = questionaire_factory(law_case=law_case, order=2, short_title='qn_2') q1_1 = Question.add_root( **(get_single_option_question(short_title='q1_1', questionaire=qn_1))) q1_2 = q1_1.add_child(**(get_single_option_question(questionaire=qn_1))) q2_1 = Question.add_root(**(get_date_question(questionaire=qn_2))) q2_2 = q2_1.add_child(**(get_text_question(questionaire=qn_2))) variables = law_case.placeholders_for_template q1_1_string = 'qn_1_q1_1' q1_2_string = 'qn_1_question_{}'.format(q1_2.id) q2_1_string = 'qn_2_question_{}'.format(q2_1.id) q2_2_string = 'qn_2_question_{}'.format(q2_2.id) assert q1_1_string in variables.keys() assert variables[q1_1_string] == Question.objects.get(id=q1_1.id).text assert q1_2_string in variables.keys() assert variables[q1_2_string] == Question.objects.get(id=q1_2.id).text assert q2_1_string in variables.keys() assert variables[q2_1_string] == Question.objects.get(id=q2_1.id).text assert q2_2_string in variables.keys() assert variables[q2_2_string] == Question.objects.get(id=q2_2.id).text assert len(variables.keys()) == 4
def test_evaluate_date(condition_factory): q1 = Question.add_root(**(get_date_question())) q2 = Question.add_root(**(get_date_question())) condition_1 = condition_factory(if_option='deadline_running', if_value='months_+3', question=q1) condition_2 = condition_factory(if_option='deadline_expired', if_value='months_+3', question=q2) condition_3 = condition_factory(if_option='is', if_value='yes', then_value='success', question=q1) with freeze_time('2020-05-10'): date_1 = datetime.date(2020, 4, 10) assert condition_1.evaluate_date(date_1) date_2 = datetime.date(2020, 1, 10) assert condition_2.evaluate_date(date_2) assert not condition_3.evaluate_date(date_2)
def test_prepare_for_delete(): q1 = Question.add_root(**(get_text_question())) q1.add_child(**(get_text_question())) q1.prepare_for_delete() assert not q1.get_children() q2 = Question.add_root(**(get_text_question())) q3 = q2.add_child(**(get_text_question())) q4 = q3.add_child(**(get_text_question())) q3.prepare_for_delete() assert not q3.get_children() assert q2.get_children().last() == q4 assert Question.objects.get(id=q4.id).get_parent() == q2
def test_get_dict_key(questionaire_factory): qn = questionaire_factory(short_title='qn') q1 = Question.add_root(**get_single_option_question(questionaire=qn)) assert q1.get_dict_key(option='yes') == ('qn_question_1', 'Yes') q1.field_type = q1.MULTIPLE_OPTIONS q1.save() assert q1.get_dict_key(option=['yes', 'maybe']) == ('qn_question_1', ['Yes', 'Maybe']) q2 = Question.add_root(**get_date_question(questionaire=qn)) assert q2.get_dict_key(date='2021-05-10') == ('qn_question_2', '2021-05-10')
def test_question_createForm_form(): question = Question.add_root(**get_text_question()) assert QuestionCreateForm( parent_question=question).fields['parent_question'].initial == question assert QuestionCreateForm().fields['parent_question'].initial is None
def test_form_wizard_render_done(rf, law_case_factory, create_user, document_factory, questionaire_factory): class TestWizardView(FormWizardView): def get_lawcase(self): return LawCase.objects.all().first() d = document_factory() lc = law_case_factory(allow_download=True, save_answers=True, document=d) qn1 = questionaire_factory(success_message='Success qn1', law_case=lc) q1 = Question.add_root(**get_single_option_question(questionaire=qn1)) data = {'question': q1.id, 'option': 'yes'} request = rf.post('/', data) user = create_user(email='*****@*****.**', password='******', username='******') request.user = user middleware = SessionMiddleware(dummy_get_response) middleware.process_request(request) praefix = 'legal_advice_builder_{}'.format(lc.id) request.session[praefix] = json.dumps( { 'current_question': q1.id, 'answers': [] }, cls=DjangoJSONEncoder) request.session.save() assert Answer.objects.all().count() == 0 TestWizardView.as_view()(request) assert Answer.objects.all().count() == 1
def test_law_case_preview_view(rf, law_case_factory, questionaire_factory): law_case = law_case_factory() qn_1 = questionaire_factory(law_case=law_case, order=1) Question.add_root(**(get_single_option_question(questionaire=qn_1))) request = rf.get('/') middleware = SessionMiddleware(dummy_get_response) middleware.process_request(request) request.session.save() request = rf.get('/') middleware = SessionMiddleware(dummy_get_response) middleware.process_request(request) request.session.save() view_response = LawCasePreview.as_view()(request, pk=law_case.id) assert view_response.context_data.get('law_case') == law_case
def test_form_wizard_returns_first_question_form(rf, law_case_factory, questionaire_factory): class TestWizardView(FormWizardView): def get_lawcase(self): return LawCase.objects.all().first() law_case = law_case_factory() qn_1 = questionaire_factory(law_case=law_case, order=1) q1 = Question.add_root(**(get_single_option_question(questionaire=qn_1))) request = rf.get('/') middleware = SessionMiddleware(dummy_get_response) middleware.process_request(request) request.session.save() response = TestWizardView.as_view()(request) assert 'legal_advice_builder/form_wizard.html' in response.template_name assert response.context_data.get( 'form').fields['question'].initial == q1.id dict_as_choices = [(k, v) for k, v in q1.options.items()] assert response.context_data.get( 'form').fields['option'].choices == dict_as_choices praefix = 'legal_advice_builder_{}'.format(law_case.id) assert json.loads(response._request.session.get(praefix)).get( 'current_questionaire') == qn_1.id assert json.loads(response._request.session.get(praefix)).get( 'current_question') == q1.id assert json.loads( response._request.session.get(praefix)).get('answers') == []
def test_has_error(questionaire_factory): qn = questionaire_factory() q1 = Question.add_root(**get_single_option_question(questionaire=qn)) q1.options = {} q1.save() assert qn.has_error
def test_string(): q1 = Question.add_root(**get_single_option_question()) q1.text = 'test' q1.save() assert str(q1) == 'test' q1.short_title = 'question' q1.field_type = q1.TEXT q1.save() assert str(q1) == 'test'
def test_stra(condition_factory): q1 = Question.add_root(**(get_text_question())) condition_1 = condition_factory(if_option='is', if_value='yes', then_value='success', question=q1) assert str(condition_1) == 'if answer is "yes" then success'
def test_get_if_text_by_type(): q1 = Question.add_root(**get_single_option_question()) for type in q1.FIELD_TYPES: q1.field_type = type[0] q1.save() assert not q1.get_if_text_by_type() == '' q1.field_type = 'test' q1.save() assert q1.get_if_text_by_type() == ''
def test_get_options_by_type(): q1 = Question.add_root(**get_single_option_question()) for type in q1.FIELD_TYPES: q1.field_type = type[0] q1.save() assert q1.get_options_by_type() is not None for field_type in [q1.SINGLE_OPTION, q1.MULTIPLE_OPTIONS, q1.YES_NO]: q1.field_type = field_type q1.save() assert list(q1.get_options_by_type().keys()) == ['is']
def test_question_update_form(): question = Question.add_root(**get_text_question()) assert 'options' not in QuestionUpdateForm(instance=question).fields assert QuestionUpdateForm( instance=question).fields['text'].widget.attrs == { 'class': 'form-control' } question_2 = Question.add_root(**get_single_option_question()) Condition.objects.create(question=question_2, if_option='is', if_value='yes', then_value='success') Condition.objects.create(question=question_2, if_option='is', if_value='no', then_value='failure') assert question_2.conditions.count() == 2 data = { 'text': question_2.text, 'field_type': question_2.field_type, 'information': 'Information', 'options': json.dumps({ 'options_1': 'option 1', 'options_2': 'option 2' }) } assert 'options' in QuestionUpdateForm(instance=question_2).fields assert QuestionUpdateForm( instance=question_2).fields['options'].widget.attrs == {} form = QuestionUpdateForm(instance=question_2, data=data) form.is_valid() form.save() assert question_2.conditions.count() == 0
def test_conditions_widget(questionaire_factory): qn = questionaire_factory() q_1 = Question.add_root(**get_text_question(questionaire=qn)) q_2 = q_1.add_child(**get_text_question(questionaire=qn)) cw = ConditionsWidget(question=q_1) assert len(cw.get_other_questions()) == 1 assert cw.create_conditions_dict() == [] assert not cw.get_if_options() == '' assert list( cw.get_then_options().keys()) == ['failure', 'question', 'success'] assert list(cw.get_period_options().keys()) == ['days', 'months', 'years'] assert cw.get_default_next() == q_2.id qn = questionaire_factory() q_1 = Question.add_root(**get_text_question(questionaire=qn)) cw = ConditionsWidget(question=q_1) assert cw.get_default_next() == '' assert 'initial' in cw.get_context('conditions', None, {'id': 'id_conditions'})
def test_clean_up_conditions(): q1 = Question.add_root(**get_single_option_question()) Condition.objects.create(question=q1, if_option='is', if_value='maybe', then_value='success') assert q1.conditions.count() == 1 q1.options = { 'yes': 'yes', 'no': 'no', } q1.save() q1.clean_up_conditions(list(q1.options.keys())) assert q1.conditions.count() == 0
def test_get_status( law_case_factory, questionaire_factory, ): law_case = law_case_factory() qn_1 = questionaire_factory(law_case=law_case, success_message='Success') question = Question.add_root(**(get_single_option_question( questionaire=qn_1))) assert question.get_status(option='yes') == { 'success': True, 'message': 'Success', 'next': None }
def test_question_conditions_date_deadline_expired(): question = Question.add_root(**(get_date_question())) Condition.objects.create(question=question, if_option='deadline_expired', if_value='months_+3', then_value='failure') with freeze_time('2020-05-10'): inserted_date = datetime.date(2020, 4, 10) assert not question.is_status_by_conditions('failure', date=inserted_date) inserted_date = datetime.date(2020, 1, 21) assert question.is_status_by_conditions('failure', date=inserted_date)
def test_question_conditions_date_unit(): question = Question.add_root(**(get_date_question())) Condition.objects.create(question=question, if_option='deadline_running', if_value='days_+10', then_value='failure') with freeze_time('2020-05-16'): inserted_date = datetime.date(2020, 5, 13) assert question.is_status_by_conditions('failure', date=inserted_date) inserted_date = datetime.date(2020, 5, 1) assert not question.is_status_by_conditions('failure', date=inserted_date)
def test_add_new_after_question(questionaire_factory): qn = questionaire_factory() q1 = Question.add_root(**get_text_question(questionaire=qn)) q2 = q1.add_child(**get_text_question(questionaire=qn)) data = get_text_question(questionaire=qn) qn.add_new_after_question(data, parent_question=q1.id) assert q1.get_children().count() == 1 child = q1.get_children().first() assert child.get_children().first() == q2 qn2 = questionaire_factory() data = get_text_question(questionaire=qn2) qn.add_new_after_question(data) assert qn2.questions.count() == 1
def test_question_form(): question = Question.add_root(**get_question()) initial = {'question': question.id} question.field_type = question.MULTIPLE_OPTIONS question.save() assert 'option' in QuestionForm(initial=initial).fields assert QuestionForm( initial=initial).fields['option'].widget.input_type == 'checkbox' question.field_type = question.SINGLE_OPTION question.save() assert 'option' in QuestionForm(initial=initial).fields assert QuestionForm( initial=initial).fields['option'].widget.input_type == 'radio' question.field_type = question.YES_NO question.save() assert 'option' in QuestionForm(initial=initial).fields assert QuestionForm( initial=initial).fields['option'].widget.input_type == 'radio' question.field_type = question.SINGLE_LINE question.save() assert 'text' in QuestionForm(initial=initial).fields assert QuestionForm( initial=initial).fields['text'].widget.input_type == 'text' question.field_type = question.TEXT question.save() assert 'text' in QuestionForm(initial=initial).fields assert QuestionForm( initial=initial).fields['text'].widget.__class__.__name__ == 'Textarea' question.field_type = question.DATE question.save() assert 'date' in QuestionForm(initial=initial).fields assert QuestionForm( initial=initial).fields['date'].widget.input_type == 'date' data = {'date': '2021-10-10', 'question': question.id} form = QuestionForm(initial=initial, data=data) form.is_valid() assert form.cleaned_data.get('date') == '10.10.2021'
def test_document_preview_view(rf, document_factory, law_case_factory, text_block_factory, questionaire_factory): document = document_factory() text_block_factory(document=document, content='<p>{{ qn_q1 }}</p>') text_block_factory(document=document, content='<p>{{ qn_q2 }}</p>') law_case = law_case_factory(document=document) qn = questionaire_factory(law_case=law_case, short_title='qn') q1 = Question.add_root( **get_text_question(questionaire=qn, short_title='q1')) q2 = q1.add_child(**get_text_question(questionaire=qn, short_title='q2')) document.sample_answer = [{'question': q1.id, 'text': 'test'}] document.save() request = rf.get('/') resp = DocumentPreviewView.as_view()(request, pk=law_case.id) assert resp.context_data.get('document') == document assert resp.context_data.get('lawcase') == law_case assert len(resp.context_data.get('questions_formset').forms) == 2 data = { 'form-TOTAL_FORMS': 2, 'form-INITIAL_FORMS': 0, 'form-0-question': q1.id, 'form-0-text': 'text', 'form-1-question': q2.id, 'form-1-text': 'text 2' } request = rf.post('/', data) DocumentPreviewView.as_view()(request, pk=law_case.id) assert len(Document.objects.get(id=document.id).sample_answers) == 2 assert Document.objects.get( id=document.id).sample_answers[0].get('question') == str(q1.id) assert Document.objects.get( id=document.id).sample_answers[0].get('text') == 'text' assert Document.objects.get( id=document.id).sample_answers[1].get('question') == str(q2.id) assert Document.objects.get( id=document.id).sample_answers[1].get('text') == 'text 2'
def test_get_initial_questions_dict(law_case_factory, questionaire_factory, text_block_factory, document_factory): document = document_factory() law_case = law_case_factory(document=document) questionaire_1 = questionaire_factory(short_title='qn_1', law_case=law_case, order=1) q1 = question = Question.add_root(**(get_text_question( short_title='first_name', questionaire=questionaire_1))) q2 = question.add_child(**(get_text_question(short_title='last_name', questionaire=questionaire_1))) q2.add_child( **(get_text_question(short_title='city', questionaire=questionaire_1))) document.sample_answers = [{ "question": str(q1.id), "text": "Mickey" }, { "question": str(q2.id), "text": "Mouse" }] document.save() tb = text_block_factory(document=document, content='{{ qn_1_first_name }}', order=0) assert str(tb) == '{{ qn_1_first_name }}' text_block_factory(document=document, content='{{ qn_1_last_name }} {{ qn_1_city }}', order=1) assert len(document.get_initial_questions_dict()) == 3 assert document.get_initial_questions_dict()[0].get('question') == str( q1.id) assert document.get_initial_questions_dict()[0].get('text') == 'Mickey' assert document.get_initial_questions_dict()[1].get('question') == str( q2.id) assert document.get_initial_questions_dict()[1].get('text') == 'Mouse'
def test_template_with_answers(law_case_factory, questionaire_factory, text_block_factory, document_factory, answer_factory): document = document_factory() law_case = law_case_factory(document=document) questionaire_1 = questionaire_factory(short_title='qn_1', law_case=law_case, order=1) q1 = question = Question.add_root(**(get_text_question( short_title='first_name', questionaire=questionaire_1))) q2 = question.add_child(**(get_text_question(short_title='last_name', questionaire=questionaire_1))) answer = answer_factory(law_case=law_case, answers=[{ "question": str(q1.id), "text": "Mickey" }, { "question": str(q2.id), "text": "Mouse" }]) text_block_factory(document=document, content='<p>{{ answers.qn_1_first_name }}</p>', order=1) text_block_factory(document=document, content='{{ answers.qn_1_last_name }}', order=2) document.sample_answers = [{ "question": str(q1.id), "text": "Donald" }, { "question": str(q2.id), "text": "Duck" }] document.save() assert document.template_with_answers( answer.answers) == '<p>Mickey</p> Mouse' assert document.template_with_sample_answers == '<p>Donald</p> Duck'
def test_form_wizard_return_next_question_by_option(rf, law_case_factory, questionaire_factory): class TestWizardView(FormWizardView): def get_lawcase(self): return LawCase.objects.all().first() law_case = law_case_factory() qn_1 = questionaire_factory(law_case=law_case, order=1) q1 = Question.add_root(**(get_single_option_question(questionaire=qn_1))) q2 = q1.add_child(**(get_single_option_question(questionaire=qn_1))) request = rf.get('/') middleware = SessionMiddleware(dummy_get_response) middleware.process_request(request) request.session.save() data = {'question': q1.id, 'option': 'yes'} request = rf.get('/') middleware = SessionMiddleware(dummy_get_response) middleware.process_request(request) request.session.save() resp = TestWizardView.as_view()(request) praefix = 'legal_advice_builder_{}'.format(law_case.id) request = rf.post('/', data) middleware = SessionMiddleware(dummy_get_response) middleware.process_request(request) request.session.save() session_data = resp._request.session.get(praefix) request.session[praefix] = session_data resp = TestWizardView.as_view()(request) assert resp.context_data.get('form').fields['question'].initial == q2.id assert json.loads(resp._request.session.get(praefix)).get( 'current_questionaire') == qn_1.id assert json.loads( resp._request.session.get(praefix)).get('current_question') == q2.id assert json.loads(resp._request.session.get(praefix)).get('answers') == [{ 'option': 'yes', 'question': '1' }]
def test_question_delete_view(rf, questionaire_factory): qn = questionaire_factory() q1 = Question.add_root(**get_text_question(questionaire=qn)) q2 = q1.add_child(**get_text_question(questionaire=qn)) q3 = q2.add_child(**get_text_question(questionaire=qn)) assert Question.objects.all().count() == 3 request = rf.post('/', {}) setattr(request, 'session', 'session') messages = FallbackStorage(request) setattr(request, '_messages', messages) resp = QuestionDelete.as_view()(request, pk=q2.id) assert resp.status_code == 302 assert Question.objects.all().count() == 2 assert q3.get_parent() == q2 request = rf.get('/') setattr(request, 'session', 'session') messages = FallbackStorage(request) setattr(request, '_messages', messages) resp = QuestionDelete.as_view()(request, pk=qn.id) assert resp.status_code == 302
def test_form_wizard_test_invalid_form(rf, law_case_factory, questionaire_factory): class TestWizardView(FormWizardView): def get_lawcase(self): return LawCase.objects.all().first() lc = law_case_factory() qn = questionaire_factory(law_case=lc) q1 = Question.add_root(**get_single_option_question(questionaire=qn)) data = {'question': q1.id, 'text': 'yes'} request = rf.post('/', data) middleware = SessionMiddleware(dummy_get_response) middleware.process_request(request) praefix = 'legal_advice_builder_{}'.format(lc.id) request.session[praefix] = json.dumps( { 'current_question': q1.id, 'answers': [] }, cls=DjangoJSONEncoder) request.session.save() resp = TestWizardView.as_view()(request) assert 'option' in resp.context_data.get('form').errors
def test_template(law_case_factory, questionaire_factory, text_block_factory, document_factory, answer_factory): document = document_factory() law_case = law_case_factory(document=document) questionaire_1 = questionaire_factory(short_title='qn_1', law_case=law_case, order=1) q1 = question = Question.add_root(**(get_text_question( short_title='first_name', questionaire=questionaire_1))) q2 = question.add_child(**(get_text_question(short_title='last_name', questionaire=questionaire_1))) answer = answer_factory(law_case=law_case, answers=[{ "question": str(q1.id), "text": "Mickey" }, { "question": str(q2.id), "text": "Mouse" }]) text_block_factory(document=document, content='{{ answers.qn_1_first_name }}', order=1) text_block_factory(document=document, content='{{ answers.qn_1_last_name }}', order=2) assert answer.template == 'Mickey Mouse' answer.save_rendered_document() assert answer.rendered_document == 'Mickey Mouse'
def test_form_wizard_initial_options(rf, law_case_factory, questionaire_factory): class TestWizardView(FormWizardView): def get_lawcase(self): return LawCase.objects.all().first() def get_initial_dict(self): return { 'qn_1': { 'q1': { 'options': { 'option1': 'option1', 'option2': 'option2' } } } } law_case = law_case_factory() qn_1 = questionaire_factory(law_case=law_case, order=1) qn_1.short_title = 'qn_1' qn_1.save() q1 = Question.add_root(**(get_single_option_question(questionaire=qn_1))) q1.short_title = 'q1' q1.save() request = rf.get('/') middleware = SessionMiddleware(dummy_get_response) middleware.process_request(request) request.session.save() resp = TestWizardView.as_view()(request) choices = [('option1', 'option1'), ('option2', 'option2')] assert resp.context_data.get('form').fields['option'].choices == choices