def test_should_create_text_field_type_for_default_english_language(self): expected_json = { "defaultValue": "some default value", "label": { "eng": "What is your name" }, "name": "field1_Name", "instruction": "Answer is word or phrase", "code": "Q1", "length": { "min": 1, "max": 20 }, "type": "text", "ddtype": self.DDTYPE_JSON, } field = TextField(name="field1_Name", code="Q1", label="What is your name", defaultValue="some default value", length=TextConstraint(1, 20), language="eng", ddtype=self.ddtype, instruction="Answer is word or phrase") actual_json = field._to_json() self.assertEqual(actual_json, expected_json)
def __init__(self, name, code, label, ddtype, length=None, defaultValue=None, instruction=None, language=field_attributes.DEFAULT_LANGUAGE, entity_question_flag=False): Field.__init__(self, type=field_attributes.TEXT_FIELD, name=name, code=code, label=label, language=language, ddtype=ddtype, instruction=instruction) self._dict[ self. DEFAULT_VALUE] = defaultValue if defaultValue is not None else "" self.constraint = length if length is not None else TextConstraint() self._dict[self.LENGTH] = self.constraint._to_json() if entity_question_flag: self._dict[self.ENTITY_QUESTION_FLAG] = entity_question_flag
def _create_form_model_for_project(self, project): ddtype = DataDictType(self.dbm, name='Default String Datadict Type', slug='string_default', primitive_type='string') question1 = TextField(name="entity_question", code="ID", label="What is associated entity", language="eng", entity_question_flag=True, ddtype=ddtype) question2 = TextField(name="question1_Name", code="Q1", label="What is your name", defaultValue="some default value", language="eng", length=TextConstraint(5, 10), ddtype=ddtype) self.form_model = FormModel(self.dbm, name=self.project1.name, form_code="abc", fields=[question1, question2], entity_type=["Clinic"], state=attributes.INACTIVE_STATE) qid = self.form_model.save() project.qid = qid project.save(self.dbm)
def test_should_throw_exception_if_field_created_with_none_datadict_type( self): with self.assertRaises(AssertionError): TextField(name="Name", code="Q1", label="What is your Name", language="eng", length=TextConstraint(min=4, max=15), ddtype=None)
def _get_text_field(code, ddtype, dictionary, is_entity_question, label, name, instruction): length_dict = dictionary.get("length") length = TextConstraint(min=length_dict.get(ConstraintAttributes.MIN), max=length_dict.get(ConstraintAttributes.MAX)) return TextField(name=name, code=code, label=label, entity_question_flag=is_entity_question, length=length, ddtype=ddtype, instruction=instruction)
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_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_entity_id_question(dbm): entity_data_dict_type = get_or_create_data_dict(dbm=dbm, name="eid", slug="entity_id", primitive_type="string", description="Entity ID") name = "Which subject are you reporting on?" entity_id_question = TextField(name=name, code=ENTITY_QUESTION_DISPLAY_CODE, label="Entity being reported on", entity_question_flag=True, ddtype=entity_data_dict_type, length=TextConstraint(min=1, max=12)) return entity_id_question
def test_should_return_error_for_text_length_validation_for_min_value( self): with self.assertRaises(AnswerTooShortException) as e: field = TextField(name="Age", code="Q2", label="What is your age", language="eng", length=TextConstraint(min=15, max=120), ddtype=self.ddtype) valid_value = field.validate("short") self.assertFalse(valid_value) self.assertEqual( e.exception.message, "Answer short for question Q2 is shorter than allowed.")
def _create_text_question(post_dict, ddtype): max_length_from_post = post_dict.get("max_length") min_length_from_post = post_dict.get("min_length") max_length = max_length_from_post if not is_empty( max_length_from_post) else None min_length = min_length_from_post if not is_empty( min_length_from_post) else None length = TextConstraint(min=min_length, max=max_length) return TextField(name=post_dict["title"], code=post_dict["code"].strip(), label="default", entity_question_flag=post_dict.get("is_entity_question"), length=length, ddtype=ddtype, instruction=post_dict.get("instruction"))
def test_successful_text_length_validation(self): field = TextField(name="Name", code="Q2", label="What is your Name", language="eng", length=TextConstraint(min=4, max=15), ddtype=self.ddtype) field1 = TextField(name="Name", code="Q2", label="What is your Name", language="eng", ddtype=self.ddtype) valid_value = field.validate("valid") self.assertEqual(valid_value, "valid") valid_value = field1.validate("valid") self.assertEqual(valid_value, "valid")
def test_should_assert_activity_report(self): question1 = 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) activity_report = FormModel(self.dbm, entity_type=["reporter"], name="aids", label="Aids form_model", form_code="1", type='survey', fields=[question1]) self.assertTrue(activity_report.entity_defaults_to_reporter())
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_and_strip_text(self): constraint = TextConstraint(min=10, max=20) valid_data = constraint.validate("valid_string ") self.assertEqual(valid_data, "valid_string")
def test_should_raise_exception_for_integer_below_range(self): with self.assertRaises(VdtValueTooShortError): constraint = TextConstraint(min=10, max=20) constraint.validate("small")
def test_should_raise_exception_for_integer_above_range(self): with self.assertRaises(VdtValueTooLongError): constraint = TextConstraint(min=1, max=4) constraint.validate("invalid_string")
def test_should_return_empty_dict_for_empty_text_constraint(self): constraint = TextConstraint() actual_dict = constraint._to_json() self.assertTrue(is_empty(actual_dict))
def test_should_return_min_as_dictionary(self): expected_dict = {"min": 1} constraint = TextConstraint(min=1) actual_dict = constraint._to_json() self.assertEqual(expected_dict, actual_dict)
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 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 test_should_return_max_as_dictionary(self): expected_dict = {"max": 20} constraint = TextConstraint(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 = TextConstraint(min=10, max=20) actual_dict = constraint._to_json() self.assertEqual(expected_dict, actual_dict)
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_add_constraint_text_for_text_field_with_max_and_min(self): type = DataDictType(Mock(DatabaseManager), name="Name type") constraint = TextConstraint(min=10, max=100) field = TextField(name="What's in a name?", code="nam", label="naam", ddtype=type, length=constraint) preview = helper.get_preview_for_field(field) self.assertEqual("Between 10 - 100 characters", preview["constraint"])