def test_should_add_min_max_constraint_if_numeric_constraint(self): self.assertEquals( ". >= 10 and . <= 20", NumericRangeConstraint(min=10, max=20).xform_constraint()) self.assertEquals(". >= 10", NumericRangeConstraint(min=10).xform_constraint()) self.assertEquals(". <= 10", NumericRangeConstraint(max=10).xform_constraint()) self.assertEquals("", NumericRangeConstraint().xform_constraint())
def _create_summary_form_model(self): question1 = TextField( name="question1_Name", code="Q1", label="What is your name", defaultValue="some default value", constraints=[TextLengthConstraint(5, 10), RegexConstraint("\w+")], ddtype=self.default_ddtype) question2 = IntegerField( name="Father's age", code="Q2", label="What is your Father's Age", constraints=[NumericRangeConstraint(min=15, max=120)], ddtype=self.default_ddtype) question3 = SelectField(name="Color", code="Q3", label="What is your favourite color", options=[("RED", 1), ("YELLOW", 2)], ddtype=self.default_ddtype) self.summary_form_model = FormModel( self.manager, entity_type=["reporter"], name="aids", label="Aids form_model", form_code=FORM_CODE_2, type="survey", fields=[question1, question2, question3]) self.summary_form_model__id = self.summary_form_model.save()
def test_should_create_integer_field_type_with_range(self): expected_json = { "label": "What is your age", "name": "Age", "code": "Q2", "constraints": [('range', { "min": 15, "max": 120 })], "parent_field_code": None, "type": "integer", "required": True, "instruction": "test_instruction", "appearance": None, "constraint_message": None, "default": None, "hint": None, "xform_constraint": None, "relevant": None } field = IntegerField( name="Age", code="Q2", label="What is your age", constraints=[NumericRangeConstraint(min=15, max=120)], instruction="test_instruction") actual_json = field._to_json() self.assertEqual(actual_json, expected_json)
def _create_form_model(self): self.entity_type = ["HealthFacility", "Clinic"] question1 = UniqueIdField('clinic', name="entity_question", code="ID", label="What is associated entity") question2 = TextField( name="question1_Name", code="Q1", label="What is your name", defaultValue="some default value", constraints=[TextLengthConstraint(5, 10), RegexConstraint("\w+")]) question3 = IntegerField( name="Father's age", code="Q2", label="What is your Father's Age", constraints=[NumericRangeConstraint(min=15, max=120)]) question4 = SelectField(name="Color", code="Q3", label="What is your favourite color", options=[("RED", 'a'), ("YELLOW", 'b')]) self.form_model = FormModelBuilder( self.manager, self.entity_type, "1").label("Aids form_model").name("aids").add_fields( question1, question2, question3, question4).build() self.form_model_id = self.form_model.id
def test_should_create_a_questionnaire_from_dictionary(self): #entityQ = UniqueIdField('reporter', name="What are you reporting on?", code="eid", # label="Entity being reported on", ) ageQ = IntegerField( name="What is your age?", code="AGE", label="", constraints=[NumericRangeConstraint(min=0, max=10)], required=False) placeQ = SelectField(name="Where do you live?", code="PLC", label="", options=[{ "text": "Pune" }, { "text": "Bangalore" }], single_select_flag=False, required=False) questions = [ageQ, placeQ] document = self.get_form_model_doc() questionnaire = FormModel.new_from_doc(self.manager, document) self.maxDiff = None self.assertListEqual(questionnaire.entity_type, []) self.assertEqual(questionnaire.name, "New Project") for i in range(len(questions)): self.assertEqual(questionnaire.fields[i]._to_json(), questions[i]._to_json())
def form_model(self): question1 = UniqueIdField(unique_id_type='clinic', name="entity_question", code="q1", label="What is associated entity", ) question2 = IntegerField(name="question1_Name", code="q2", label="What is your name", constraints=[NumericRangeConstraint(min=10, max=100)]) return FormModel(self.dbm, name="aids", label="Aids form_model", form_code="aids", fields=[question1, question2])
def test_should_add_constraint_text_for_numeric_field_with_max(self): constraint = NumericRangeConstraint(max=100) field = IntegerField(name="What's in the age?", code="nam", label="naam", constraints=[constraint]) preview = helper.get_preview_for_field(field) self.assertEqual("Upto 100", preview["constraints"])
def _get_integer_field(self): integer_field = IntegerField( name=self.field_name, code='ag', label=self.field_name, ddtype=Mock(spec=DataDictType), constraints=[NumericRangeConstraint(min=18, max=100)]) return integer_field
def _create_integer_question(self, post_dict, code): max_range_from_post = post_dict.get("range_max") min_range_from_post = post_dict.get("range_min") max_range = max_range_from_post if not is_empty(max_range_from_post) else None min_range = min_range_from_post if not is_empty(min_range_from_post) else None range = NumericRangeConstraint(min=min_range, max=max_range) return IntegerField(name=self._get_name(post_dict), code=code, label=post_dict["title"], constraints=[range], instruction=post_dict.get("instruction"), required=post_dict.get("required"), parent_field_code=post_dict.get('parent_field_code'))
def test_should_return_error_for_integer_range_validation(self): field = IntegerField( name="Age", code="Q2", label="What is your age", constraints=[NumericRangeConstraint(min=15, max=120)]) valid_value = field.validate("120") self.assertEqual(valid_value, 120) valid_value = field.validate("25.5") self.assertEqual(valid_value, 25.5)
def test_should_add_constraint_text_for_numeric_field_with_max(self): type = DataDictType(Mock(DatabaseManager), name="age type") constraint = NumericRangeConstraint(max=100) field = IntegerField(name="What's in the age?", code="nam", label="naam", ddtype=type, constraints=[constraint]) preview = helper.get_preview_for_field(field) self.assertEqual("Upto 100", preview["constraints"])
def test_should_return_error_for_wrong_type_for_integer(self): with self.assertRaises(AnswerWrongType) as e: field = IntegerField( name="Age", code="Q2", label="What is your age", constraints=[NumericRangeConstraint(min=15, max=120)]) field.validate("asas") self.assertEqual(e.exception.message, "Answer asas for question Q2 is of the wrong type.")
def test_should_return_error_for_integer_range_validation_for_min_value( self): with self.assertRaises(AnswerTooSmallException) as e: field = IntegerField( name="Age", code="Q2", label="What is your age", constraints=[NumericRangeConstraint(min=15, max=120)]) valid_value = field.validate(11) self.assertFalse(valid_value) self.assertEqual(e.exception.message, "Answer 11 for question Q2 is smaller than allowed.")
def test_integer_field_for_min_number(self): int_field = IntegerField( "age", 'age', 'age', constraints=[NumericRangeConstraint(min='100')]) field = FormField().create(int_field) self.assertTrue(isinstance(field.widget, TextInputForFloat)) self.assertEquals(field.widget.attrs['watermark'], 'Minimum 100') self.assertEqual(field.min_value, 100) self.assertEqual(field.max_value, None)
def test_integer_field_for_max_number(self): int_field = IntegerField( "age", 'age', 'age', "Answer must be a number. The maximum is 100.", constraints=[NumericRangeConstraint(max='100')]) field = FormField().create(int_field) self.assertTrue(isinstance(field.widget, TextInputForFloat)) self.assertEquals(field.widget.attrs['watermark'], 'Upto 100') self.assertEqual(field.max_value, 100) self.assertEqual(field.min_value, None) self.assertEqual(field.help_text, "Answer must be a number. The maximum is 100.")
def _create_sample_questionnaire(self): entity_type = [] question3 = IntegerField( name="Father's age", code="Q2", label="What is your Father's Age", constraints=[NumericRangeConstraint(min=15, max=120)]) form_model = FormModel(self.manager, name='New survey', label='Survey122', form_code='S122', fields=[question3], is_registration_model=False) form_model_id = form_model.save() return form_model_id
def setUp(self): self.dbm = Mock(spec=DatabaseManager) q1 = UniqueIdField('clinic', name="entity_question", code="ID", label="What is associated entity") q2 = TextField(name="question1_Name", code="Q1", label="What is your name", defaultValue="some default value", constraints=[TextLengthConstraint(5, 10)], required=False) q3 = IntegerField( name="Father's age", code="Q2", label="What is your Father's Age", constraints=[NumericRangeConstraint(min=15, max=120)], required=False) q4 = SelectField(name="Color", code="Q3", label="What is your favourite color", options=[("RED", 'a'), ("YELLOW", 'b')], required=False) q5 = TextField(name="Desc", code="Q4", label="Description", required=False) self.event_time_field_code = "Q6" q6 = DateField(name="Event time", code=self.event_time_field_code, label="Event time field", date_format="%m.%d.%Y", required=False) q7 = GeoCodeField(name="My Location", code="loc", label="Geo Location Field", required=False) self.form_model = FormModel(self.dbm, name="aids", label="Aids form_model", form_code="1", fields=[q1, q2, q3, q4, q5, q6, q7]) self.form_model_patch = patch( 'mangrove.form_model.form_model.FormModel') self.form_model_document_patch = patch( 'mangrove.form_model.form_model.FormModelDocument')
def test_should_get_questionnaires_for_user_excluding_deleted_questionnaires( self): entity_type = [] question3 = IntegerField( name="Father's age", code="Q2", label="What is your Father's Age", constraints=[NumericRangeConstraint(min=15, max=120)]) form_model = FormModel(self.manager, name='New survey', label='Survey122', form_code='S122', fields=[question3], is_registration_model=False) form_model.void(True) form_model_id = form_model.save() user_permission = UserPermission(self.manager, 1, [form_model_id]) user_permission.save() questionnaires = get_questionnaires_for_user(1, self.manager) self.assertEqual(len(questionnaires), 0)
def test_should_validate_range(self): constraint = NumericRangeConstraint(min=10, max=20) valid_data = constraint.validate(15) self.assertEqual(valid_data, 15) valid_data = constraint.validate("15") self.assertEqual(valid_data, 15)
def test_should_raise_exception_for_non_integer_value(self): with self.assertRaises(VdtTypeError): constraint = NumericRangeConstraint(min=10, max=20) constraint.validate("asasd")
def setUp(self): MangroveTestCase.setUp(self) initializer.run(self.manager) define_type(self.manager, ["dog"]) self.entity_type = ["clinic"] define_type(self.manager, self.entity_type) self.entity = create_entity( self.manager, entity_type=self.entity_type, location=["India", "Pune"], aggregation_paths=None, short_code="cli1", ) self.data_record_id = self.entity.add_data( data=[("Name", "Ruby")], submission=dict(submission_id="1")) self.reporter = create_entity( self.manager, entity_type=["reporter"], location=["India", "Pune"], aggregation_paths=None, short_code="rep1", ) self.reporter.add_data(data=[(MOBILE_NUMBER_FIELD, '1234'), (NAME_FIELD, "Test_reporter")], submission=dict(submission_id="2")) #Web submission Form Model question1 = UniqueIdField(unique_id_type='clinic', name="entity_question", code="EID", label="What is associated entity") question2 = TextField(name="Name", code="NAME", label="Clinic Name", defaultValue="some default value", constraints=[TextLengthConstraint(4, 15)], required=False) question3 = IntegerField( name="Arv stock", code="ARV", label="ARV Stock", constraints=[NumericRangeConstraint(min=15, max=120)], required=False) question4 = SelectField(name="Color", code="COL", label="Color", options=[("RED", 1), ("YELLOW", 2)], required=False) self.form_model = FormModel( self.manager, name="aids", label="Aids form_model", form_code="clinic", fields=[question1, question2, question3, question4]) self.form_model.save() #Activity Report Form Model question1 = UniqueIdField(unique_id_type='reporter', name="entity_question", code="EID", label="What is associated entity") question2 = TextField(name="Name", code="NAME", label="Clinic Name", defaultValue="some default value", constraints=[TextLengthConstraint(4, 15)]) question3 = IntegerField( name="Arv stock", code="ARV", label="ARV Stock", constraints=[NumericRangeConstraint(min=15, max=120)]) activity_report = FormModel(self.manager, name="report", label="reporting form_model", form_code="acp", fields=[question1, question2, question3]) activity_report.save() self.web_player = WebPlayerV2(self.manager)
def setUpClass(cls): cls.dbm = create_db(uniq('mangrove-test')) initializer.initial_data_setup(cls.dbm) cls.entity_type = ["healthfacility", "clinic"] safe_define_type(cls.dbm, cls.entity_type) cls.entity_short_code = "cli" + str(int(random.random() * 10000)) cls.entity = create_entity( cls.dbm, entity_type=cls.entity_type, location=["India", "Pune"], aggregation_paths=None, short_code=cls.entity_short_code, ) cls.entity.save() cls.reporter_id = "rep" + str(int(random.random() * 10000)) cls.reporter = create_contact(cls.dbm, location=["India", "Pune"], aggregation_paths=None, short_code=cls.reporter_id) cls.reporter.save() cls.phone_number = str(int(random.random() * 10000000)) cls.reporter.add_data(data=[(MOBILE_NUMBER_FIELD, cls.phone_number), (NAME_FIELD, "Test_reporter")], submission=dict(submission_id="2")) question1 = ShortCodeField( name="entity_question", code="EID", label="What is associated entity", constraints=[TextLengthConstraint(min=1, max=20)]) question2 = TextField(name="Name", code="NAME", label="Clinic Name", defaultValue="some default value", constraints=[TextLengthConstraint(4, 15)], required=False) question3 = IntegerField( name="Arv stock", code="ARV", label="ARV Stock", constraints=[NumericRangeConstraint(min=15, max=120)], required=False) question4 = SelectField(name="Color", code="COL", label="Color", options=[("RED", 'a'), ("YELLOW", 'a')], required=False) try: cls.form_model = get_form_model_by_code(cls.dbm, "clinic") except FormModelDoesNotExistsException: cls.form_model = EntityFormModel( cls.dbm, entity_type=cls.entity_type, name="aids", label="Aids form_model", form_code="clinic", fields=[question1, question2, question3], is_registration_model=True) cls.form_model.add_field(question4) cls.form_model.save() cls.sms_player = SMSPlayer(cls.dbm, LocationTree()) cls.sms_ordered_message_player = SMSPlayer(cls.dbm, LocationTree())
def test_should_return_max_as_dictionary(self): expected_value = ('range', {"max": 20}) constraint = NumericRangeConstraint(min=None, max=20) actual_value = constraint._to_json() self.assertEqual(expected_value, actual_value)
def _create_project(self): registration_form = construct_global_registration_form(self.manager) registration_form.save() self.check_uniqueness_patch = patch.object( Project, '_check_if_project_name_unique') self.check_uniqueness_patch.start() clinic_form_model = EntityFormModel( self.manager, name='clinic', label='Entity Form Model', form_code='clin', fields=[TextField('name', 'code', 'label')], language="en", is_registration_model=True, enforce_unique_labels=True, entity_type=['clinic']) clinic_form_model.save() hf_form_model = EntityFormModel(self.manager, name='health', label='Entity Form Model', form_code='hf1', fields=[ TextField('name', 'code', 'label'), TextField('location', 'loc', 'Where is it?') ], language="en", is_registration_model=True, enforce_unique_labels=True, entity_type=['healthfacility']) hf_form_model.save() entity_type = ["HealthFacility", "Clinic"] question1 = UniqueIdField('clinic', name="entity_question", code="ID", label="What is associated Clinic") question2 = TextField( name="question1_Name", code="Q1", label="What is your name", defaultValue="some default value", constraints=[TextLengthConstraint(5, 10), RegexConstraint("\w+")]) question3 = IntegerField( name="Father's age", code="Q2", label="What is your Father's Age", constraints=[NumericRangeConstraint(min=15, max=120)]) question4 = SelectField(name="Color", code="Q3", label="What is your favourite color", options=[("RED", 'a'), ("YELLOW", 'b')]) question5 = UniqueIdField('healthfacility', name="health facility", code="hf", label="For which HF is it?") fields = [question1, question2, question3, question4, question5] project = Project(self.manager, name='test project', form_code='test01', fields=fields) project.save() return project
def test_should_return_min_as_dictionary(self): expected_value = ('range', {"min": 1}) constraint = NumericRangeConstraint(min=1) actual_value = constraint._to_json() self.assertEqual(expected_value, actual_value)
def test_should_raise_exception_for_integer_below_range(self): with self.assertRaises(VdtValueTooSmallError): constraint = NumericRangeConstraint(min=10, max=20) constraint.validate(1)
def test_should_return_empty_dict_for_empty_integer_constraint(self): constraint = NumericRangeConstraint( ) #First off there should not be an empty NumericConstraint actual_value = constraint._to_json() self.assertTrue(is_empty(actual_value[1]))