def test_post_section_poll_answer_multiple_choice(john_doe_api_client, default_hearing, geojson_feature): section = default_hearing.sections.first() poll = SectionPollFactory(section=section, option_count=3, type=SectionPoll.TYPE_MULTIPLE_CHOICE) option1, option2, option3 = poll.options.all() url = '/v1/hearing/%s/sections/%s/comments/' % (default_hearing.id, section.id) data = get_comment_data() data['answers'] = [{ 'question': poll.id, 'type': SectionPoll.TYPE_MULTIPLE_CHOICE, 'answers': [option1.id, option3.id], }] response = john_doe_api_client.post(url, data=data) created_data = get_data_from_response(response, status_code=201) poll.refresh_from_db(fields=['n_answers']) option1.refresh_from_db(fields=['n_answers']) option2.refresh_from_db(fields=['n_answers']) option3.refresh_from_db(fields=['n_answers']) assert poll.n_answers == 1 assert option1.n_answers == 1 assert option2.n_answers == 0 assert option3.n_answers == 1 assert created_data['answers'][0]['type'] == data['answers'][0]['type'] assert set(created_data['answers'][0]['answers']) == set( data['answers'][0]['answers'])
def test_update_poll_having_answers(valid_hearing_json_with_poll, john_doe_api_client, john_smith_api_client): valid_hearing_json_with_poll['close_at'] = datetime.datetime.now( ) + datetime.timedelta(days=5) valid_hearing_json_with_poll['sections'][0]['commenting'] = 'open' response = john_smith_api_client.post('/v1/hearing/', data=valid_hearing_json_with_poll, format='json') data = get_data_from_response(response, status_code=201) hearing_id = data['id'] section_id = data['sections'][0]['id'] poll_id = data['sections'][0]['questions'][0]['id'] option_id = data['sections'][0]['questions'][0]['options'][0]['id'] data = get_comment_data() data['answers'] = [{ 'question': poll_id, 'type': SectionPoll.TYPE_MULTIPLE_CHOICE, 'answers': [option_id] }] comment_response = john_doe_api_client.post( '/v1/hearing/%s/sections/%s/comments/' % (hearing_id, section_id), data=data) assert comment_response.status_code == 201 # Edit question data = get_data_from_response(response, status_code=201) data['sections'][0]['questions'][0]['text']['en'] = 'Edited question' update_response = john_smith_api_client.put('/v1/hearing/%s/' % data['id'], data=data, format='json') assert update_response.status_code == 400 # Edit option data = get_data_from_response(response, status_code=201) data['sections'][0]['questions'][0]['options'][0]['text'][ 'en'] = 'Edited option' update_response = john_smith_api_client.put('/v1/hearing/%s/' % data['id'], data=data, format='json') assert update_response.status_code == 400 # Add option data = get_data_from_response(response, status_code=201) new_option = deepcopy(data['sections'][0]['questions'][0]['options'][0]) data['sections'][0]['questions'][0]['options'].append(new_option) update_response = john_smith_api_client.put('/v1/hearing/%s/' % data['id'], data=data, format='json') assert update_response.status_code == 400 # Remove option data = get_data_from_response(response, status_code=201) del data['sections'][0]['questions'][0]['options'][1] update_response = john_smith_api_client.put('/v1/hearing/%s/' % data['id'], data=data, format='json') assert update_response.status_code == 400
def test_post_section_poll_answer_unauthenticated(api_client, default_hearing, geojson_feature): section = default_hearing.sections.first() SectionPollFactory(section=section, option_count=3) url = '/v1/hearing/%s/sections/%s/comments/' % (default_hearing.id, section.id) data = get_comment_data() data['answers'] = [{}] response = api_client.post(url, data=data) assert response.status_code == 403
def test_put_section_poll_answer(john_doe_api_client, default_hearing, geojson_feature): section = default_hearing.sections.first() poll = SectionPollFactory(section=section, option_count=3, type=SectionPoll.TYPE_MULTIPLE_CHOICE) poll2 = SectionPollFactory(section=section, option_count=3, type=SectionPoll.TYPE_SINGLE_CHOICE) option1, option2, option3 = poll.options.all() optionyes, optionno, optiondunno = poll2.options.all() url = '/v1/hearing/%s/sections/%s/comments/' % (default_hearing.id, section.id) data = get_comment_data() data['answers'] = [{ 'question': poll.id, 'type': SectionPoll.TYPE_MULTIPLE_CHOICE, 'answers': [option1.id, option3.id], }, { 'question': poll2.id, 'type': SectionPoll.TYPE_SINGLE_CHOICE, 'answers': [optionyes.id], }] response = john_doe_api_client.post(url, data=data) assert response.status_code == 201 url = '/v1/hearing/%s/sections/%s/comments/%s/' % ( default_hearing.id, section.id, response.data['id']) data = response.data data['answers'] = [{ 'question': poll.id, 'type': SectionPoll.TYPE_MULTIPLE_CHOICE, 'answers': [option2.id, option3.id], }, { 'question': poll2.id, 'type': SectionPoll.TYPE_SINGLE_CHOICE, 'answers': [optionno.id], }] response = john_doe_api_client.put(url, data=data) assert response.status_code == 200 updated_data = get_data_from_response(response, status_code=200) option1.refresh_from_db(fields=['n_answers']) option2.refresh_from_db(fields=['n_answers']) option3.refresh_from_db(fields=['n_answers']) optionyes.refresh_from_db(fields=['n_answers']) optionno.refresh_from_db(fields=['n_answers']) assert option1.n_answers == 0 assert option2.n_answers == 1 assert option3.n_answers == 1 assert optionyes.n_answers == 0 assert optionno.n_answers == 1 for answer in data['answers']: assert answer in updated_data['answers']
def test_answers_appear_in_user_data(john_doe_api_client, default_hearing): section = default_hearing.sections.first() poll = SectionPollFactory(section=section, option_count=3, type=SectionPoll.TYPE_SINGLE_CHOICE) option = poll.options.all().first() data = get_comment_data() data['answers'] = [{ 'question': poll.id, 'type': SectionPoll.TYPE_SINGLE_CHOICE, 'answers': [option.id] }] john_doe_api_client.post('/v1/hearing/%s/sections/%s/comments/' % (default_hearing.id, section.id), data=data) response = john_doe_api_client.get('/v1/users/') assert poll.pk in response.data[0]['answered_questions']