def test_should_add_constraint_text_for_numeric_field_with_min(self): type = DataDictType(Mock(DatabaseManager), name="age type") constraint = NumericConstraint(min=10) field = IntegerField(name="What's in the age?", code="nam", label="naam", ddtype=type, range=constraint) preview = helper.get_preview_for_field(field) self.assertEqual("Minimum 10", preview["constraint"]) self.assertEqual("integer", preview["type"])
def test_should_create_integer_field_type_for_default_english_language_with_range( self): expected_json = { "label": { "eng": "What is your age" }, "name": "Age", "code": "Q2", "range": { "min": 15, "max": 120 }, "ddtype": self.DDTYPE_JSON, "type": "integer", "instruction": "test_instruction" } field = IntegerField(name="Age", code="Q2", label="What is your age", language="eng", range=NumericConstraint(min=15, max=120), ddtype=self.ddtype, instruction="test_instruction") actual_json = field._to_json() self.assertEqual(actual_json, expected_json)
def _get_integer_field(code, ddtype, dictionary, label, name, instruction): range_dict = dictionary.get("range") range = NumericConstraint(min=range_dict.get(ConstraintAttributes.MIN), max=range_dict.get(ConstraintAttributes.MAX)) return IntegerField(name=name, code=code, label=label, range=range, ddtype=ddtype, instruction=instruction)
def test_should_return_error_for_integer_range_validation(self): field = IntegerField(name="Age", code="Q2", label="What is your age", language="eng", range=NumericConstraint(min=15, max=120), ddtype=self.ddtype) 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_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", language="eng", range=NumericConstraint(min=15, max=120), ddtype=self.ddtype) field.validate("asas") self.assertEqual(e.exception.message, "Answer asas for question Q2 is of the wrong type.")
def setUp(self): self.dbm = Mock(spec=DatabaseManager) self.datadict_patcher = patch( "mangrove.form_model.form_model.get_or_create_data_dict") self.datadict_mock = self.datadict_patcher.start() self.ddtype_mock = Mock(spec=DataDictType) self.datadict_mock.return_value = self.ddtype_mock q1 = TextField(name="entity_question", code="ID", label="What is associated entity", language="eng", entity_question_flag=True, ddtype=self.ddtype_mock) q2 = TextField(name="question1_Name", code="Q1", label="What is your name", defaultValue="some default value", language="eng", length=TextConstraint(5, 10), ddtype=self.ddtype_mock) q3 = IntegerField(name="Father's age", code="Q2", label="What is your Father's Age", range=NumericConstraint(min=15, max=120), ddtype=self.ddtype_mock) q4 = SelectField(name="Color", code="Q3", label="What is your favourite color", options=[("RED", 1), ("YELLOW", 2)], ddtype=self.ddtype_mock) q5 = TextField(name="Desc", code="Q4", label="Description", ddtype=self.ddtype_mock) self.form_model = FormModel( self.dbm, entity_type=["XYZ"], name="aids", label="Aids form_model", form_code="1", type='survey', fields=[ # expected_short_code = "dog3" # self.assertEqual(response.short_code, expected_short_code) # b = get_by_short_code(self.dbm, expected_short_code, ["dog"]) # self.assertEqual(b.short_code, expected_short_code) q1, q2, q3, q4, q5 ])
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", language="eng", range=NumericConstraint(min=15, max=120), ddtype=self.ddtype) 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_should_save_submitted_sms_for_activity_report(self): question1 = TextField(name="entity_question", code="EID", label="What is associated entity", language="eng", entity_question_flag=True, ddtype=self.entity_id_type) question2 = TextField(name="Name", code="NAME", label="Clinic Name", defaultValue="some default value", language="eng", length=TextConstraint(4, 15), ddtype=self.name_type) question3 = IntegerField(name="Arv stock", code="ARV", label="ARV Stock", range=NumericConstraint(min=15, max=120), ddtype=self.stock_type) activity_report = FormModel(self.dbm, entity_type=["reporter"], name="report", label="reporting form_model", form_code="acp", type='survey', fields=[question1, question2, question3]) activity_report.save() text = "acp +name tester +ARV 50 " response = self.send_sms(text) self.assertTrue(response.success) self.assertEqual(u"Test_reporter", response.reporters[0].get(NAME_FIELD)) data_record_id = response.datarecord_id data_record = self.dbm._load_document( id=data_record_id, document_class=DataRecordDocument) self.assertEqual(self.name_type.slug, data_record.data["Name"]["type"]["slug"]) self.assertEqual(self.stock_type.slug, data_record.data["Arv stock"]["type"]["slug"]) self.assertEqual("acp", data_record.submission['form_code']) data = self.reporter.values({"Name": "latest", "Arv stock": "latest"}) self.assertEquals(data["Arv stock"], 50) self.assertEquals(data["Name"], "tester") submission_log = self.dbm._load_document(response.submission_id, SubmissionLogDocument) self.assertEquals(data_record_id, submission_log.data_record_id)
def _create_integer_question(post_dict, ddtype): 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 = NumericConstraint(min=min_range, max=max_range) return IntegerField(name=post_dict["title"], code=post_dict["code"].strip(), label="default", range=range, ddtype=ddtype, instruction=post_dict.get("instruction"))
def setUp(self): self.dbm = Mock(spec=DatabaseManager) self.ddtype1 = Mock(spec=DataDictType) self.ddtype2 = Mock(spec=DataDictType) self.ddtype3 = Mock(spec=DataDictType) self.ddtype4 = Mock(spec=DataDictType) question1 = TextField(name="entity_question", code="ID", label="What is associated entity", language="eng", entity_question_flag=True, ddtype=self.ddtype1) question2 = TextField(name="Name", code="Q1", label="What is your name", defaultValue="some default value", language="eng", ddtype=self.ddtype2) question3 = IntegerField(name="Father's age", code="Q2", label="What is your Father's Age", range=NumericConstraint(min=15, max=120), ddtype=self.ddtype3) question4 = SelectField(name="Color", code="Q3", label="What is your favourite color", options=[("RED", 1), ("YELLOW", 2)], ddtype=self.ddtype4) self.form_model = FormModel(self.dbm, entity_type=["Clinic"], name="aids", label="Aids form_model", form_code="AIDS", type='survey', fields=[question1, question2, question3, question4])
def __init__(self, name, code, label, ddtype, range=None, instruction=None, language=field_attributes.DEFAULT_LANGUAGE): Field.__init__(self, type=field_attributes.INTEGER_FIELD, name=name, code=code, label=label, language=language, ddtype=ddtype, instruction=instruction) self.constraint = range if range is not None else NumericConstraint() self._dict[self.RANGE] = self.constraint._to_json()
def _create_form_model(self): self.entity_type = ["HealthFacility", "Clinic"] define_type(self.dbm, ["HealthFacility", "Clinic"]) self.default_ddtype = DataDictType(self.dbm, name='Default String Datadict Type', slug='string_default', primitive_type='string') self.default_ddtype.save() question1 = TextField(name="entity_question", code="ID", label="What is associated entity", language="eng", entity_question_flag=True, ddtype=self.default_ddtype) question2 = TextField(name="question1_Name", code="Q1", label="What is your name", defaultValue="some default value", language="eng", length=TextConstraint(5, 10), ddtype=self.default_ddtype) question3 = IntegerField(name="Father's age", code="Q2", label="What is your Father's Age", range=NumericConstraint(min=15, max=120), ddtype=self.default_ddtype) question4 = SelectField(name="Color", code="Q3", label="What is your favourite color", options=[("RED", 1), ("YELLOW", 2)], ddtype=self.default_ddtype) self.form_model = FormModel( self.dbm, entity_type=self.entity_type, name="aids", label="Aids form_model", form_code="1", type='survey', fields=[question1, question2, question3, question4]) self.form_model__id = self.form_model.save()
def test_should_create_field_with_datadict_type(self): nameType = Mock(spec=DataDictType) field1 = TextField(name="Name", code="Q1", label="What is your Name", language="eng", length=TextConstraint(min=4, max=15), ddtype=nameType) self.assertEqual(nameType, field1.ddtype) ageType = Mock(spec=DataDictType) field2 = IntegerField(name="Age", code="Q2", label="What is your age", language="eng", range=NumericConstraint(min=4, max=15), ddtype=ageType) self.assertEqual(ageType, field2.ddtype) selectType = Mock(spec=DataDictType) field3 = SelectField(name="clinic type", code="Q1", label="What type of clinic is it?", language="eng", options=["village", "urban"], ddtype=selectType) self.assertEqual(selectType, field3.ddtype) dateType = Mock(spec=DataDictType) field4 = DateField(name="Age", code="Q2", label="What is your birth date", language="eng", date_format="%m.%d.%Y", ddtype=dateType) self.assertEqual(dateType, field4.ddtype)
def test_should_validate_range(self): constraint = NumericConstraint(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_return_empty_dict_for_empty_integer_constraint(self): constraint = NumericConstraint() actual_dict = constraint._to_json() self.assertTrue(is_empty(actual_dict))
def test_should_return_min_as_dictionary(self): expected_dict = {"min": 1} constraint = NumericConstraint(min=1) actual_dict = constraint._to_json() self.assertEqual(expected_dict, actual_dict)
def test_should_return_max_as_dictionary(self): expected_dict = {"max": 20} constraint = NumericConstraint(min=None, max=20) actual_dict = constraint._to_json() self.assertEqual(expected_dict, actual_dict)
def test_should_return_min_max_as_dictionary_for_integer(self): expected_dict = {"min": 10, "max": 20} constraint = NumericConstraint(min=10, max=20) actual_dict = constraint._to_json() self.assertEqual(expected_dict, actual_dict)
def test_should_create_a_questionnaire_from_dictionary(self): fields = [{ "name": "What are you reporting on?", "defaultValue": "", "label": { "eng": "Entity being reported on" }, "entity_question_flag": True, "type": "text", "ddtype": self.default_ddtype.to_json(), "code": "eid", "length": { "min": 1, "max": 10 }, }, { "range": { "max": 10, "min": 0 }, "label": { "eng": "" }, "type": "integer", "ddtype": self.default_ddtype.to_json(), "name": "What is your age?", "code": "AGE" }, { "choices": [{ "text": { "eng": "Pune" } }, { "text": { "eng": "Bangalore" } }], "label": { "eng": "" }, "type": "select", "ddtype": self.default_ddtype.to_json(), "name": "Where do you live?", "code": "PLC" }] document = FormModelDocument() document.json_fields = fields document.entity_type = ["Reporter"] document.document_type = "FormModel" document.form_code = "F1" document.name = "New Project" document.type = "survey" document.type = "survey" entityQ = TextField(name="What are you reporting on?", code="eid", label="Entity being reported on", entity_question_flag=True, length=TextConstraint(min=1, max=10), ddtype=self.default_ddtype) ageQ = IntegerField(name="What is your age?", code="AGE", label="", range=NumericConstraint(min=0, max=10), ddtype=self.default_ddtype) placeQ = SelectField(name="Where do you live?", code="PLC", label="", options=[{ "text": { "eng": "Pune" } }, { "text": { "eng": "Bangalore" } }], single_select_flag=False, ddtype=self.default_ddtype) questions = [entityQ, ageQ, placeQ] questionnaire = FormModel.new_from_doc(self.dbm, document) self.maxDiff = None self.assertListEqual(questionnaire.entity_type, ["Reporter"]) self.assertEqual(questionnaire.name, "New Project") self.assertEqual(questionnaire.type, "survey") for i in range(len(questions)): self.assertEqual(questionnaire.fields[i]._to_json(), questions[i]._to_json())
def create_clinic_projects(CLINIC_ENTITY_TYPE, manager): name_type = create_data_dict(manager, name='Name', slug='Name', primitive_type='string') # Entity id is a default type in the system. entity_id_type = get_datadict_type_by_slug(manager, slug='entity_id') age_type = create_data_dict(manager, name='Age Type', slug='age', primitive_type='integer') date_type = create_data_dict(manager, name='Report Date', slug='date', primitive_type='date') select_type = create_data_dict(manager, name='Choice Type', slug='choice', primitive_type='select') geo_code_type = create_data_dict(manager, name='GeoCode Type', slug='geo_code', primitive_type='geocode') question1 = TextField(label="entity_question", code="EID", name="What is associated entity?", language="eng", entity_question_flag=True, ddtype=entity_id_type, length=TextConstraint(min=1, max=12)) question2 = TextField(label="Name", code="NA", name="What is your name?", length=TextConstraint(min=1, max=10), defaultValue="some default value", language="eng", ddtype=name_type) question3 = IntegerField(label="Father age", code="FA", name="What is age of father?", range=NumericConstraint(min=18, max=100), ddtype=age_type) question4 = DateField(label="Report date", code="RD", name="What is reporting date?", date_format="dd.mm.yyyy", ddtype=date_type) question5 = SelectField(label="Blood Group", code="BG", name="What is your blood group?", options=[("O+", "a"), ("O-", "b"), ("AB", "c"), ("B+", "d")], single_select_flag=True, ddtype=select_type) question6 = SelectField(label="Symptoms", code="SY", name="What are symptoms?", options=[("Rapid weight loss", "a"), ("Dry cough", "b"), ("Pneumonia", "c"), ("Memory loss", "d"), ("Neurological disorders ", "e")], single_select_flag=False, ddtype=select_type) question7 = GeoCodeField(name="What is the GPS code for clinic", code="GPS", label="What is the GPS code for clinic?", language="eng", ddtype=geo_code_type) form_model = FormModel(manager, name="AIDS", label="Aids form_model", form_code="cli001", type='survey', fields=[ question1, question2, question3, question4, question5, question6, question7 ], entity_type=CLINIC_ENTITY_TYPE) try: qid = form_model.save() except DataObjectAlreadyExists as e: get_form_model_by_code(manager, "cli001").delete() qid = form_model.save() project = Project(name="Clinic Test Project", goals="This project is for automation", project_type="survey", entity_type=CLINIC_ENTITY_TYPE[-1], devices=["sms"], activity_report='no', sender_group="close") project.qid = qid project.state = ProjectState.ACTIVE try: project.save(manager) except Exception: pass form_model2 = FormModel(manager, name="AIDS", label="Aids form_model", form_code="cli002", type='survey', fields=[ question1, question2, question3, question4, question5, question6, question7 ], entity_type=CLINIC_ENTITY_TYPE) try: qid2 = form_model2.save() except DataObjectAlreadyExists as e: get_form_model_by_code(manager, "cli002").delete() qid2 = form_model2.save() project2 = Project(name="Clinic2 Test Project", goals="This project is for automation", project_type="survey", entity_type=CLINIC_ENTITY_TYPE[-1], devices=["sms"], activity_report='no', sender_group="close") project2.qid = qid2 project2.state = ProjectState.ACTIVE try: project2.save(manager) except Exception: pass
def setUp(self): self.dbm = get_db_manager(database='mangrove-test') initializer.run(self.dbm) define_type(self.dbm, ["dog"]) self.entity_type = ["healthfacility", "clinic"] define_type(self.dbm, self.entity_type) self.name_type = DataDictType(self.dbm, name='Name', slug='name', primitive_type='string') self.telephone_number_type = DataDictType(self.dbm, name='telephone_number', slug='telephone_number', primitive_type='string') self.entity_id_type = DataDictType(self.dbm, name='Entity Id Type', slug='entity_id', primitive_type='string') self.stock_type = DataDictType(self.dbm, name='Stock Type', slug='stock', primitive_type='integer') self.color_type = DataDictType(self.dbm, name='Color Type', slug='color', primitive_type='string') self.name_type.save() self.telephone_number_type.save() self.stock_type.save() self.color_type.save() self.entity = create_entity( self.dbm, 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", self.name_type)], submission=dict(submission_id="1")) self.reporter = create_entity( self.dbm, entity_type=["reporter"], location=["India", "Pune"], aggregation_paths=None, short_code="rep1", ) self.reporter.add_data(data=[ (MOBILE_NUMBER_FIELD, '1234', self.telephone_number_type), (NAME_FIELD, "Test_reporter", self.name_type) ], submission=dict(submission_id="2")) question1 = TextField(name="entity_question", code="EID", label="What is associated entity", language="eng", entity_question_flag=True, ddtype=self.entity_id_type) question2 = TextField(name="Name", code="NAME", label="Clinic Name", defaultValue="some default value", language="eng", length=TextConstraint(4, 15), ddtype=self.name_type) question3 = IntegerField(name="Arv stock", code="ARV", label="ARV Stock", range=NumericConstraint(min=15, max=120), ddtype=self.stock_type) question4 = SelectField(name="Color", code="COL", label="Color", options=[("RED", 1), ("YELLOW", 2)], ddtype=self.color_type) self.form_model = FormModel(self.dbm, entity_type=self.entity_type, name="aids", label="Aids form_model", form_code="clinic", type='survey', fields=[question1, question2, question3]) self.form_model.add_field(question4) self.form_model__id = self.form_model.save() self.submission_handler = SubmissionHandler(self.dbm) self.sms_player = SMSPlayer(self.dbm, self.submission_handler, LocationTree())
def test_should_raise_exception_for_integer_below_range(self): with self.assertRaises(VdtValueTooSmallError): constraint = NumericConstraint(min=10, max=20) constraint.validate(1)
def test_should_raise_exception_for_non_integer_value(self): with self.assertRaises(VdtTypeError): constraint = NumericConstraint(min=10, max=20) constraint.validate("asasd")