Example #1
0
 def test_should_create_text_question_with_no_max_length(self):
     post = [{"title": "q1", "code": "qc1", "type": "text", "choices": [], "is_entity_question": True,
              "min_length": 1, "max_length": ""},
             {"title": "q2", "code": "qc2", "type": "integer", "choices": [], "is_entity_question": False,
              "range_min": 0, "range_max": 100},
             {"title": "q3", "code": "qc3", "type": "select", "choices": [{"value": "c1"}, {"value": "c2"}],
              "is_entity_question": False}
     ]
     q1 = helper.create_question(post[0], self.dbm)
     self.assertEqual(q1.constraint.max, None)
Example #2
0
 def test_should_save_questionnaire_from_post(self):
     post = [{"title": "q1", "code": "qc1", "type": "text", "choices": [], "is_entity_question": True,
              "min_length": 1, "max_length": ""},
             {"title": "q2", "code": "qc2", "type": "integer", "choices": [], "is_entity_question": False,
              "range_min": 0, "range_max": 100},
             {"title": "q3", "code": "qc3", "type": "select", "choices": [{"value": "c1"}, {"value": "c2"}],
              "is_entity_question": False}
     ]
     q1 = helper.create_question(post[0], self.dbm)
     form_model = FormModel(self.dbm, "test", "test", "test", [q1], ["test"], "test")
     questionnaire = helper.update_questionnaire_with_questions(form_model, post, self.dbm)
     self.assertEqual(3, len(questionnaire.fields))
Example #3
0
 def test_creates_questions_from_dict(self):
     post = [{"title": "q1", "code": "qc1", "description": "desc1", "type": "text", "choices": [],
              "is_entity_question": True, "min_length": 1, "max_length": 15},
             {"title": "q2", "code": "qc2", "description": "desc2", "type": "integer", "choices": [],
              "is_entity_question": False, "range_min": 0, "range_max": 100},
             {"title": "q3", "code": "qc3", "description": "desc3", "type": "select",
              "choices": [{"value": "c1"}, {"value": "c2"}], "is_entity_question": False},
             {"title": "q4", "code": "qc4", "description": "desc4", "type": "select1",
              "choices": [{"value": "c1"}, {"value": "c2"}], "is_entity_question": False}
     ]
     q1 = helper.create_question(post[0], self.dbm)
     q2 = helper.create_question(post[1], self.dbm)
     q3 = helper.create_question(post[2], self.dbm)
     q4 = helper.create_question(post[3], self.dbm)
     self.assertIsInstance(q1, TextField)
     self.assertIsInstance(q2, IntegerField)
     self.assertIsInstance(q3, SelectField)
     self.assertIsInstance(q4, SelectField)
     self.assertEquals(q1._to_json_view()["length"], {"min": 1, "max": 15})
     self.assertEquals(q2._to_json_view()["range"], {"min": 0, "max": 100})
     self.assertEquals(q3._to_json_view()["type"], "select")
     self.assertEquals(q4._to_json_view()["type"], "select1")
Example #4
0
    def test_should_create_location_question_with_implicit_ddtype(self):
        CODE = "lc3"
        LABEL = "what is your location"
        SLUG = "what_is_your_location"
        TYPE = "geocode"
        post = {"title": LABEL, "code": CODE, "type": TYPE, "is_entity_question": False}

        dbm = Mock(spec=DatabaseManager)

        expected_data_dict = DataDictType(dbm, CODE, SLUG, TYPE, LABEL)
        self.create_ddtype_mock.return_value = expected_data_dict

        with patch("datawinners.project.helper.get_datadict_type_by_slug") as get_datadict_type_by_slug_mock:
            get_datadict_type_by_slug_mock.side_effect = DataObjectNotFound("", "", "")
            location_question = helper.create_question(post, dbm)

        self.create_ddtype_mock.assert_called_once_with(dbm=dbm, name=CODE, slug=SLUG,
                                                        primitive_type=TYPE, description=LABEL)
        self.assertEqual(expected_data_dict, location_question.ddtype)
Example #5
0
    def test_should_create_integer_question_with_implicit_ddtype(self):
        post = {"title": "What is your age", "code": "age", "type": "integer", "choices": [],
                "is_entity_question": False,
                "range_min": 0, "range_max": 100}

        dbm = Mock(spec=DatabaseManager)

        self.create_ddtype_mock.return_value = DataDictType(dbm, "age", "what_is_your_age", "integer",
                                                            "what is your age")

        with patch("datawinners.project.helper.get_datadict_type_by_slug") as get_datadict_type_by_slug_mock:
            get_datadict_type_by_slug_mock.side_effect = DataObjectNotFound("", "", "")
            integer_question = helper.create_question(post, dbm)

        self.create_ddtype_mock.assert_called_once_with(dbm=dbm, name="age", slug="what_is_your_age",
                                                        primitive_type="integer", description="What is your age")
        self.assertEqual('age', integer_question.ddtype.name)
        self.assertEqual("what is your age", integer_question.ddtype.description)
        self.assertEqual("what_is_your_age", integer_question.ddtype.slug)
        self.assertEqual("integer", integer_question.ddtype.primitive_type)
Example #6
0
    def test_should_create_text_question_with_implicit_ddtype(self):
        post = {"title": "what is your name", "code": "qc1", "description": "desc1", "type": "text",
                "choices": [],
                "is_entity_question": True, "min_length": 1, "max_length": 15}

        dbm = Mock(spec=DatabaseManager)

        self.create_ddtype_mock.return_value = DataDictType(dbm, "qc1", "what_is_your_name", "text",
                                                            "what is your name")

        with patch("datawinners.project.helper.get_datadict_type_by_slug") as get_datadict_type_by_slug_mock:
            get_datadict_type_by_slug_mock.side_effect = DataObjectNotFound("", "", "")
            text_question = helper.create_question(post, dbm)

        self.create_ddtype_mock.assert_called_once_with(dbm=dbm, name="qc1", slug="what_is_your_name",
                                                        primitive_type="text", description="what is your name")
        self.assertEqual('qc1', text_question.ddtype.name)
        self.assertEqual("what is your name", text_question.ddtype.description)
        self.assertEqual("what_is_your_name", text_question.ddtype.slug)
        self.assertEqual("text", text_question.ddtype.primitive_type)
Example #7
0
 def test_should_create_integer_question_with_no_max_constraint(self):
     post = [{"title": "q2", "code": "qc2", "type": "integer", "choices": [], "is_entity_question": False,
              "range_min": 0, "range_max": ""}]
     q1 = helper.create_question(post[0], self.dbm)
     self.assertEqual(q1.constraint.max, None)