# -*- encoding: utf-8 -*- from polls.models import Poll, PollResult, Structure from pollster.models import Pollster from utils.test import MongoTestCase class PollResultTests(MongoTestCase): """ data = "poll_id" "poll_name" "poll_type" general | monitoreo "pollster_id" "pollster_username" "result" "ID RESULT" "polled" polled data "answers" "GROUP ORDER" "fields" "FIELD ORDER" "answer" "id_option" "text" "weight" "id_option" ... ... "name" field_name ... "name" group_name ... ... """ def test_init(self): data = {'name': "poll #1"} poll = Poll(data=data) poll_id = poll.save() data = {'poll_id': poll_id} poll_result = PollResult(data) self.assertEqual(data, poll_result._data) def test_to_python(self): data = {'poll_id': 'ID'} poll_result = PollResult(data) self.assertTrue('poll_id' in poll_result.to_python().keys()) def test_merge_results(self): data = {'name': "poll #1"} poll = Poll(data=data) poll_id = poll.save() data_1 = { 'poll_id': poll_id, 'result': { "0": { "answers": {"no_empty": ""}, "polled": {} }, "1": { "answers": {"no_empty": ""}, "polled": {} } } } data_2 = { 'poll_id': poll_id, 'result': { "0": { "answers": {"no_empty": ""}, "polled": {} }, "1": { "answers": {"no_empty": ""}, "polled": {} } } } poll_result = PollResult(data=[data_1, data_2]) self.assertEqual( len(data_1['result']) + len(data_2['result']), len(poll_result._data['result']) ) def test_to_csv(self): pollster = Pollster.create(username="test", password="test") poll_data = { "name": "test", "pollsters": [pollster] } structure_data = { "groups": { "1": { "name": "Questions that admit many answers", "fields": { "1": { "widget_type": "ImageCheckBox", "dependence": {}, "name": "Q: ImageCheckBox", "options": { "1370786178813": { "img_name": "1370786178813.jpg", "type": "img_input", "weight": 2, "order": "1" }, "1370786178812": { "img_name": "1370786178812.jpg", "type": "img_input", "weight": 1, "order": "2" } } }, "0": { "widget_type": "MultipleCheckBox", "dependence": {}, "name": "Q: MultipleCheckBox", "options": { "1370786178809": { "text": "option 1", "weight": 1, "order": "1" }, "1370786178810": { "text": "option 2", "weight": 2, "order": "2" } } } } }, "10": { "name": "Questions that admit only one answer", "fields": { "1": { "widget_type": "RadioButton", "dependence": {}, "name": "Q: RadioButton con ácento", "options": { "1370786178803": { "text": "option 2", "weight": 2 }, "1370786178802": { "text": "option 1", "weight": 1 } } }, "0": { "widget_type": "TextInput", "name": "Q: TextInput con ácento", "options": {} }, "3": { "widget_type": "ImageRadioButton", "dependence": {}, "name": "Q: ImageRadioButton", "options": { "1370786178807": { "img_name": "1370786178807.jpg", "type": "img_input", "weight": 2 }, "1370786178806": { "img_name": "1370786178806.jpg", "type": "img_input", "weight": 1 } } }, "2": { "widget_type": "DropDownList", "dependence": {}, "name": "Q: DropDownList", "options": { "1370786178805": { "text": "option 2", "weight": 2 }, "1370786178804": { "text": "option 1", "weight": 1 } } } } } } } poll = Poll(data=poll_data) poll_id = poll.save() poll = Poll.get(poll_id) structure = Structure(data=structure_data, poll=poll) structure.save() result_data = { "poll_id": str(poll_id), "poll_name": "", "poll_type": "general", "pollster_id": "51b0e946421aa90df17182e9", "pollster_username": "encuestador 1", "result": { "100": { "answers": { "10": { "fields": { "0": { "answer": { "1370787034900": { "text": "some text" } }, "name": "Q: TextInput con ácento", "widget_type": "TextInput" }, "1": { "answer": { "1370786178802": { "text": "option 1", "weight": 1 } }, "name": "Q: RadioButton con ácento", "widget_type": "RadioButton" }, "2": { "answer": { "1370786178804": { "text": "option 1", "weight": 1 } }, "name": "Q: DropDownList", "widget_type": "DropDownList" }, "3": { "answer": { "1370786178806": { "type": "img_input", "weight": 1 } }, "name": "Q: ImageRadioButton", "widget_type": "ImageRadioButton" } }, "name": "Questions that admit only one answer" }, "1": { "fields": { "0": { "answer": { "1370786178809": { "text": "option 1", "weight": 1 }, "1370786178810": { "text": "option 2", "weight": 2 } }, "name": "Q: CheckBox", "widget_type": "MultipleCheckBox" }, "1": { "answer": { "1370786178812": { "type": "img_input", "weight": 1 } }, "name": "Q: ImageCheckBox", "widget_type": "ImageCheckBox" } }, "name": "Questions that admit many answers" } }, "polled": { "DEPARTAMENTO": "MONTEVIDEO", "GRADO": "2", "GRUPO": "A", "ID": "0", "NUM_ESC": "236", "RUEE": "1101236", "TIPO_GRUPO": "1" } } } } poll_result = PollResult(data=result_data) expected_header = unicode( "RUEE;DEPARTAMENTO;NUM_ESC;GRADO;GRUPO;" + "TIPO_GRUPO;option 1;option 2;1370786178813;1370786178812;" + "Q: TextInput con ácento;Q: RadioButton con ácento;" + "Q: DropDownList;Q: ImageRadioButton", 'utf-8') self.assertEqual(expected_header, poll_result.get_csv_header()) csv = poll_result.to_csv() record = "1101236;MONTEVIDEO;236;2;A;1;1;2;;1;some text;1;1;1;" self.assertTrue(expected_header in csv) self.assertTrue(record in csv) class CsvNoRepitePesoParaOpcionesConElMismoTextDescriptivo(MongoTestCase): """ issue 4430 """ def setUp(self): poll_data = {"name": "test"} question_name = "repite nombre pregunta" structure_data = { "groups": { "0": { "name": "grupo 1", "fields": { "0": { "widget_type": "MultipleCheckBox", "name": "pregunta 1", "options": { "1": { "text": "no repite 1", "weight": 1, "order": "0" }, "2": { "text": "se repite", "weight": 2, "order": "1" } } }, "1": { "widget_type": "MultipleCheckBox", "name": "pregunta 2", "options": { "1": { "text": "no repite 2", "weight": 3, "order": "0" }, "2": { "text": "se repite", "weight": 10, "order": "1" } } }, "2": { "widget_type": "TextInput", "name": question_name, "options": { "0": { "text": "texto 1", "weight": 5, "order": "0" } } }, "3": { "widget_type": "TextInput", "name": question_name, "options": { "0": { "text": "texto 2", "weight": 1, "order": "0" } } } } } } } poll = Poll(data=poll_data) poll_id = poll.save() poll = Poll.get(poll_id) structure = Structure(data=structure_data, poll=poll) structure.save() poll.status = Poll.CLOSED poll_id = poll.save() self.poll = Poll.get(poll_id) def test_get_csv_header_with_order(self): poll = self.poll poll_header = poll.get_csv_header(with_order=True) expected_header = unicode( "0_0 no repite 1;0_0 se repite;" + "0_1 no repite 2;0_1 se repite;" + "0_2 repite nombre pregunta;0_3 repite nombre pregunta", 'utf-8') self.assertEqual(expected_header, poll_header) def test_to_csv(self): poll = self.poll question_name = "repite nombre pregunta" result_data = { "poll_id": str(poll.id), "poll_name": "", "poll_type": "general", "pollster_id": "cualquiera", "pollster_username": "cualquiera", "result": { "0": { "answers": { "0": { "name": "grupo 1", "fields": { "0": { "widget_type": "MultipleCheckBox", "name": "pregunta 1", "answer": { "1": { "text": "no repite 1", "weight": 1, "order": "0" }, "2": { "text": "se repite", "weight": 2, "order": "1" } } }, "1": { "widget_type": "MultipleCheckBox", "name": "pregunta 2", "answer": { "1": { "text": "no repite 2", "weight": 3, "order": "0" } } }, "2": { "widget_type": "TextInput", "name": question_name, "answer": { "0": { "text": "texto 1", "weight": 5, "order": "0" } } }, "3": { "widget_type": "TextInput", "name": question_name, "answer": { "0": { "text": "texto 2", "weight": 1, "order": "0" } } } } } }, "polled": { "DEPARTAMENTO": "MONTEVIDEO", "GRADO": "2", "GRUPO": "A", "ID": "0", "NUM_ESC": "236", "RUEE": "1101236", "TIPO_GRUPO": "1" } } } } poll_result = PollResult(data=result_data) expected_header = unicode( "RUEE;DEPARTAMENTO;NUM_ESC;GRADO;GRUPO;TIPO_GRUPO;" + "no repite 1;se repite;no repite 2;se repite;" + question_name + ";" + question_name + ";", 'utf-8') csv = poll_result.to_csv() record = "1101236;MONTEVIDEO;236;2;A;1;1;2;3;;texto 1;texto 2;" self.assertTrue(expected_header in csv) self.assertTrue(record in csv) class PollResultShouldBeSortedByGrupoAndFields(MongoTestCase): def test_load_unsorted_and_fetch_should_be_sorted(self): field_1_1_3 = { "answer": { "1370787034900": { "text": "some text" } }, "name": "Q: TextInput con ácento", "widget_type": "TextInput" } field_1_1_1 = { "answer": { "1370786178802": { "text": "option 1", "weight": 1 } }, "name": "Q: RadioButton con ácento", "widget_type": "RadioButton" } field_1_1_0 = { "answer": { "1370786178804": { "text": "option 1", "weight": 1 } }, "name": "Q: DropDownList", "widget_type": "DropDownList" } field_1_1_2 = { "answer": { "1370786178806": { "type": "img_input", "weight": 1 } }, "name": "Q: ImageRadioButton", "widget_type": "ImageRadioButton" } field_1_0_0 = { "answer": { "1370786178809": { "text": "option 1", "weight": 1 }, "1370786178810": { "text": "option 2", "weight": 2 } }, "name": "Q: CheckBox", "widget_type": "MultipleCheckBox" } field_1_0_1 = { "answer": { "1370786178812": { "type": "img_input", "weight": 1 } }, "name": "Q: ImageCheckBox", "widget_type": "ImageCheckBox" } field_0_1_3 = field_1_1_3 field_0_0_1 = field_1_0_1 result_data = { "poll_id": "poll_id", "poll_name": "", "poll_type": "general", "pollster_id": "51b0e946421aa90df17182e9", "pollster_username": "encuestador 1", "result": { "1": { "answers": { "1": { "fields": { "3": field_1_1_3, "1": field_1_1_1, "0": field_1_1_0, "2": field_1_1_2, }, "name": "Questions that admit only one answer" }, "0": { "fields": { "0": field_1_0_0, "1": field_1_0_1 }, "name": "Questions that admit many answers" } }, "polled": { "DEPARTAMENTO": "MONTEVIDEO", "GRADO": "2", "GRUPO": "A", "ID": "0", "NUM_ESC": "236", "RUEE": "1101236", "TIPO_GRUPO": "1" } }, "0": { "answers": { "1": { "fields": { "3": field_0_1_3 }, "name": "Questions that admit only one answer" }, "0": { "fields": { "1": field_0_0_1 }, "name": "Questions that admit many answers" } }, "polled": { "DEPARTAMENTO": "MONTEVIDEO", "GRADO": "2", "GRUPO": "A", "ID": "0", "NUM_ESC": "236", "RUEE": "1101236", "TIPO_GRUPO": "1" } } } } poll_result = PollResult(data=result_data) results = poll_result.sorted() sorted_answers = [] sorted_result = [] for a_result in results: sorted_result.append(a_result.id) for answer in a_result.sorted(): sorted_answers.append(answer) expected_result = ['0', '1'] self.assertListEqual(expected_result, sorted_result) expected_answers = [ field_0_0_1, field_0_1_3, field_1_0_0, field_1_0_1, field_1_1_0, field_1_1_1, field_1_1_2, field_1_1_3, ] self.assertListEqual(expected_answers, sorted_answers)