def test_should_create_select_field_with_multiple_labels(self): LABEL = "select type field" field_json = { "name": "q3", "code": "qc3", "type": "select", "choices": [{ "text": "option 1", "value": "c1" }, { "text": "option 1", "value": "c2" }], "required": False, "entity_field_flag": False, "label": LABEL } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, SelectField) self.assertEqual(LABEL, created_field.label) self.assertEqual(created_field.single_select_flag, False) self.assertFalse(created_field.is_required())
def test_should_create_integer_field_with_multiple_labels(self): LABEL = "What is your age" field_json = { "defaultValue": "", "label": LABEL, "name": "field1_age", "code": "Q1", "type": "integer", 'constraints': [("range", { "min": 0, "max": 100 })], "required": True, "entity_field_flag": False } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, IntegerField) self.assertEqual(created_field._dict["constraints"][0][1], { "min": 0, "max": 100 }) self.assertIsInstance(created_field.constraints[0], NumericRangeConstraint) self.assertEqual(LABEL, created_field.label) self.assertEqual(created_field.constraints[0].max, 100) self.assertEqual(created_field.constraints[0].min, 0) self.assertTrue(created_field.is_required())
def _set_document(self, document): DataObject._set_document(self, document) # make form_model level fields for any json fields in to for json_field in document.json_fields: f = field.create_question_from(json_field, self._dbm) self._form_fields.append(f)
def _set_document(self, document): DataObject._set_document(self, document) # make form_model level fields for any json fields in to for json_field in document.json_fields: f = field.create_question_from(json_field, self._dbm) self._form_fields.append(f)
def test_should_create_select_field_with_options(self): self.ddtype_module.create_from_json.return_value = self.ddtype field_json = { "name": "q3", "code": "qc3", "type": "select", "ddtype": self.DDTYPE_JSON, "choices": [{ "text": { "eng": "option 1" }, "value": "c1" }, { "text": { "eng": "option 1" }, "value": "c2" }], "entity_field_flag": False } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, SelectField) self.assertEqual(created_field.SINGLE_SELECT_FLAG, False) self.assertEqual(created_field.ddtype, self.ddtype)
def test_should_create_text_field_from_dictionary(self): self.ddtype_module.create_from_json.return_value = self.ddtype field_json = { "defaultValue": "", "label": { "eng": "What is your name" }, "name": "field1_Name", "code": "Q1", "type": "text", "length": { "min": 1, "max": 10 }, "entity_field_flag": True, "ddtype": self.ddtype, "instruction": "some instruction" } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, TextField) self.assertIsInstance(created_field.constraint, TextConstraint) self.assertEqual(created_field.constraint.max, 10) self.assertEqual(created_field.constraint.min, 1) self.assertEqual(created_field.ddtype, self.ddtype) self.assertEqual(created_field.label, {"eng": "What is your name"}) self.assertEqual(created_field.instruction, "some instruction")
def test_should_create_select_field_with_single_select_options(self): field_json = { "name": "q3", "code": "qc3", "type": "select1", "choices": [{ "text": "hello", "value": "c1" }, { "text": "world", "value": "c2" }], "required": False, "entity_field_flag": False, "label": "select1 type question" } expected_option_list = [{ "text": "hello", "value": "c1" }, { "text": "world", "value": "c2" }] created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, SelectField) self.assertEqual(created_field.single_select_flag, True) self.assertEqual(created_field.options, expected_option_list) self.assertFalse(created_field.is_required())
def test_should_create_telephone_number_field_from_dictionary(self): LABEL = "test" field_json = { "defaultValue": "", "label": LABEL, "name": "field1_Name", "code": "Q1", "type": "telephone_number", "constraints": [("length", { "min": 1, "max": 10 }), ('regex', '^[0-9]+$')], "instruction": "some instruction", "required": False } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, TelephoneNumberField) self.assertIsInstance(created_field.constraints[0], TextLengthConstraint) self.assertIsInstance(created_field.constraints[1], RegexConstraint) self.assertEqual(created_field.constraints[0].max, 10) self.assertEqual(created_field.constraints[0].min, 1) self.assertEqual(created_field.label, LABEL) self.assertEqual(created_field.instruction, "some instruction") self.assertFalse(created_field.is_required())
def test_should_create_text_field_from_dictionary_with_multiple_labels( self): field_json = { "defaultValue": "", "label": "What is your name", "name": "field1_Name", "code": "Q1", "type": "text", "constraints": [("length", { "min": 1, "max": 10 })], "entity_field_flag": True, "required": False, "instruction": "some instruction" } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, TextField) self.assertIsInstance(created_field.constraints[0], TextLengthConstraint) self.assertEqual(created_field.constraints[0].max, 10) self.assertEqual(created_field.constraints[0].min, 1) self.assertEqual(created_field.label, "What is your name") self.assertEqual(created_field.instruction, "some instruction") self.assertFalse(created_field.is_required())
def _set_document(self, document): DataObject._set_document(self, document) # make form_model level fields for any json fields in to for json_field in document.json_fields: f = field.create_question_from(json_field, self._dbm) self._form_fields.append(f) for validator_json in document.validators: validator = validator_factory(validator_json) if validator not in self.validators: self.validators.append(validator) if hasattr(document, 'snapshots'): for key, value in document.snapshots.items(): self._snapshots[key] = [] for each in value: f = field.create_question_from(each, self._dbm) self._snapshots[key].append(f)
def test_should_create_geo_code_field(self): field_json = { "label": "What is your location", "name": "Birth_place", "code": "LOC", "type": "geocode", "required": True, } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, GeoCodeField) self.assertTrue(created_field.is_required())
def test_should_create_geo_code_field(self): self.ddtype_module.create_from_json.return_value = self.ddtype field_json = { "label": {"eng": "What is your location"}, "name": "Birth_place", "code": "LOC", "type": "geocode", "ddtype": self.DDTYPE_JSON, } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, GeoCodeField) self.assertEqual(created_field.ddtype, self.ddtype)
def test_should_create_telephone_number_field_with_multiple_labels(self): LABEL = "telephone number field" field_json = { "name": "q3", "code": "qc3", "type": "telephone_number", "required": False, "label": LABEL } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, TelephoneNumberField) self.assertEqual(LABEL, created_field.label) self.assertFalse(created_field.is_required())
def test_should_create_geo_code_field_with_multiple_labels(self): LABEL = "What is your location" field_json = { "label": LABEL, "name": "Birth_place", "code": "LOC", "type": "geocode", "required": True, } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, GeoCodeField) self.assertEqual(LABEL, created_field.label) self.assertTrue(created_field.is_required())
def test_should_create_date_field(self): field_json = { "defaultValue": "", "label": "What is your birth date", "name": "Birth_date", "code": "Q1", "type": "date", "date_format": "%m.%Y", "required": False } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, DateField) self.assertEqual(created_field.date_format, "%m.%Y") self.assertFalse(created_field.is_required())
def test_should_create_geo_code_field(self): self.ddtype_module.create_from_json.return_value = self.ddtype field_json = { "label": { "eng": "What is your location" }, "name": "Birth_place", "code": "LOC", "type": "geocode", "ddtype": self.DDTYPE_JSON, } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, GeoCodeField) self.assertEqual(created_field.ddtype, self.ddtype)
def test_should_create_hierarchy_field_with_multiple_labels(self): LABEL = "hierarchy type field" field_json = { "name": "q3", "code": "qc3", "type": "list", "required": False, "entity_field_flag": False, "label": LABEL } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, HierarchyField) self.assertEqual(LABEL, created_field.label) self.assertFalse(created_field.is_required())
def test_should_create_select_field_with_options(self): self.ddtype_module.create_from_json.return_value = self.ddtype field_json = { "name": "q3", "code": "qc3", "type": "select", "ddtype": self.DDTYPE_JSON, "choices": [{"text": {"eng": "option 1"}, "value": "c1"}, {"text": {"eng": "option 1"}, "value": "c2"}], "entity_field_flag": False} created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, SelectField) self.assertEqual(created_field.SINGLE_SELECT_FLAG, False) self.assertEqual(created_field.ddtype, self.ddtype)
def test_should_create_date_field(self): self.ddtype_module.create_from_json.return_value = self.ddtype field_json = { "defaultValue": "", "label": {"eng": "What is your birth date"}, "name": "Birth_date", "code": "Q1", "type": "date", "date_format": "%m.%Y", "ddtype": self.DDTYPE_JSON, } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, DateField) self.assertEqual(created_field.date_format, "%m.%Y") self.assertEqual(created_field.ddtype, self.ddtype)
def test_should_create_date_field(self): self.ddtype_module.create_from_json.return_value = self.ddtype field_json = { "defaultValue": "", "label": { "eng": "What is your birth date" }, "name": "Birth_date", "code": "Q1", "type": "date", "date_format": "%m.%Y", "ddtype": self.DDTYPE_JSON, } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, DateField) self.assertEqual(created_field.date_format, "%m.%Y") self.assertEqual(created_field.ddtype, self.ddtype)
def test_should_create_select_field_with_single_select_options(self): self.ddtype_module.create_from_json.return_value = self.ddtype field_json = { "name": "q3", "code": "qc3", "type": "select1", "ddtype": self.DDTYPE_JSON, "choices": [{"text": {"eng": "hello", "fr": "bonjour"}, "value": "c1"}, {"text": {"eng": "world"}, "value": "c2"}], "entity_field_flag": False} expected_option_list = [{"text": {"eng": "hello", "fr": "bonjour"}, "value": "c1"}, {"text": {"eng": "world"}, "value": "c2"}] created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, SelectField) self.assertEqual(created_field.SINGLE_SELECT_FLAG, True) self.assertEqual(created_field.options, expected_option_list) self.assertEqual(created_field.ddtype, self.ddtype)
def test_should_create_select_field_with_single_select_options(self): self.ddtype_module.create_from_json.return_value = self.ddtype field_json = { "name": "q3", "code": "qc3", "type": "select1", "ddtype": self.DDTYPE_JSON, "choices": [{ "text": { "eng": "hello", "fr": "bonjour" }, "value": "c1" }, { "text": { "eng": "world" }, "value": "c2" }], "entity_field_flag": False } expected_option_list = [{ "text": { "eng": "hello", "fr": "bonjour" }, "value": "c1" }, { "text": { "eng": "world" }, "value": "c2" }] created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, SelectField) self.assertEqual(created_field.SINGLE_SELECT_FLAG, True) self.assertEqual(created_field.options, expected_option_list) self.assertEqual(created_field.ddtype, self.ddtype)
def test_should_create_integer_field_with_validations(self): self.ddtype_module.create_from_json.return_value = self.ddtype field_json = { "defaultValue": "", "label": {"eng": "What is your age"}, "name": "field1_age", "code": "Q1", "type": "integer", "ddtype": self.DDTYPE_JSON, "range": {"min": 0, "max": 100}, "entity_field_flag": False } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, IntegerField) self.assertEqual(created_field._dict["range"], {"min": 0, "max": 100}) self.assertIsInstance(created_field.constraint, NumericConstraint) self.assertEqual(created_field.constraint.max, 100) self.assertEqual(created_field.constraint.min, 0) self.assertEqual(created_field.ddtype, self.ddtype)
def test_should_create_text_field_from_dictionary(self): self.ddtype_module.create_from_json.return_value = self.ddtype field_json = { "defaultValue": "", "label": {"eng": "What is your name"}, "name": "field1_Name", "code": "Q1", "type": "text", "length": {"min": 1, "max": 10}, "entity_field_flag": True, "ddtype": self.ddtype, "instruction":"some instruction" } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, TextField) self.assertIsInstance(created_field.constraint, TextConstraint) self.assertEqual(created_field.constraint.max, 10) self.assertEqual(created_field.constraint.min, 1) self.assertEqual(created_field.ddtype, self.ddtype) self.assertEqual(created_field.label, {"eng": "What is your name"}) self.assertEqual(created_field.instruction, "some instruction")
def test_should_create_integer_field_with_validations(self): self.ddtype_module.create_from_json.return_value = self.ddtype field_json = { "defaultValue": "", "label": { "eng": "What is your age" }, "name": "field1_age", "code": "Q1", "type": "integer", "ddtype": self.DDTYPE_JSON, "range": { "min": 0, "max": 100 }, "entity_field_flag": False } created_field = field.create_question_from(field_json, self.dbm) self.assertIsInstance(created_field, IntegerField) self.assertEqual(created_field._dict["range"], {"min": 0, "max": 100}) self.assertIsInstance(created_field.constraint, NumericConstraint) self.assertEqual(created_field.constraint.max, 100) self.assertEqual(created_field.constraint.min, 0) self.assertEqual(created_field.ddtype, self.ddtype)