def test_setting_reserved_words(self): """Test setting any of the existing attribute names.""" with self.assertRaises(ValueError): cad = models.CustomAttributeDefinition() cad.definition_type = "requirement" cad.title = "title" with self.assertRaises(ValueError): cad = models.CustomAttributeDefinition() cad.title = "title" cad.definition_type = "requirement" with self.assertRaises(ValueError): models.CustomAttributeDefinition( title="title", definition_type="Assessment", ) with self.assertRaises(ValueError): models.CustomAttributeDefinition( title="TITLE", definition_type="program", ) cad = models.CustomAttributeDefinition( title="non existing title", definition_type="program", ) self.assertEqual(cad.title, "non existing title")
def relate_ca(assessment, template): """Generates custom attribute list and relates it to Assessment objects Args: assessment (model instance): Assessment model template: Assessment Temaplte instance (may be None) """ if not template: return ca_definitions = all_models.CustomAttributeDefinition.query.options( orm.undefer_group('CustomAttributeDefinition_complete'), ).filter_by( definition_id=template.id, definition_type="assessment_template", ).order_by( all_models.CustomAttributeDefinition.id ) for definition in ca_definitions: cad = all_models.CustomAttributeDefinition( title=definition.title, definition=assessment, attribute_type=definition.attribute_type, multi_choice_options=definition.multi_choice_options, multi_choice_mandatory=definition.multi_choice_mandatory, mandatory=definition.mandatory, helptext=definition.helptext, placeholder=definition.placeholder, ) db.session.add(cad)
def test_role_attribute(self): """Test access control role collisions with attributes & CADs""" db.session.add( models.CustomAttributeDefinition( title="my custom attribute title", definition_type="requirement", attribute_type="Text", )) db.session.commit() with self.assertRaises(ValueError): db.session.add( models.AccessControlRole( name="title", object_type="Market", )) db.session.commit() with self.assertRaises(ValueError): db.session.add( models.AccessControlRole( name="my custom attribute title", object_type="Requirement", )) db.session.commit()
def setUp(self): # pylint: disable=protected-access self.cad = all_models.CustomAttributeDefinition() self.cad._get_reserved_names = MagicMock( return_value=frozenset({'title'})) self.cad._get_global_cad_names = MagicMock(return_value={'reg url': 1}) acr.get_custom_roles_for = MagicMock(return_value=dict())
def relate_ca(assessment, related): """Generates custom attribute list and relates it to Assessment objects Args: assessment (model instance): Assessment model related (dict): Dict containing model instances related to assessment - template - obj - audit """ if not related["template"]: return ca_query = all_models.CustomAttributeDefinition.eager_query() ca_definitions = ca_query.filter_by( definition_id=related["template"].id, definition_type="assessment_template", ).order_by( all_models.CustomAttributeDefinition.id ) for definition in ca_definitions: cad = all_models.CustomAttributeDefinition( title=definition.title, definition=assessment, attribute_type=definition.attribute_type, multi_choice_options=definition.multi_choice_options, multi_choice_mandatory=definition.multi_choice_mandatory, mandatory=definition.mandatory, helptext=definition.helptext, placeholder=definition.placeholder, ) db.session.add(cad)
def relate_ca(assessment, template): """Generates custom attribute list and relates it to Assessment objects Args: assessment (model instance): Assessment model template: Assessment Temaplte instance (may be None) """ if not template: return None created_cads = [] for definition in template.custom_attribute_definitions: cad = all_models.CustomAttributeDefinition( title=definition.title, definition=assessment, attribute_type=definition.attribute_type, multi_choice_options=definition.multi_choice_options, multi_choice_mandatory=definition.multi_choice_mandatory, mandatory=definition.mandatory, helptext=definition.helptext, placeholder=definition.placeholder, ) db.session.add(cad) created_cads.append(cad) return created_cads
def test_different_models(self): """Test unique names over on different models.""" db.session.add(models.CustomAttributeDefinition( title="my custom attribute title", definition_type="section", attribute_type="Text", )) db.session.commit() db.session.add(models.AccessControlRole( name="my custom attribute title", object_type="Contract", )) db.session.commit() cad = models.CustomAttributeDefinition( title="my custom attribute title", definition_type="program", attribute_type="Text", ) self.assertEqual(cad.title, "my custom attribute title")
def test_setting_global_cad_names(self): """Test duplicates with global attribute names.""" db.session.add( models.CustomAttributeDefinition( title="global cad", definition_type="requirement", attribute_type="Text", )) db.session.add( models.CustomAttributeDefinition( title="non existing title", definition_type="requirement", definition_id=1, attribute_type="Text", )) db.session.add( models.CustomAttributeDefinition( title="non existing title", definition_type="requirement", definition_id=2, attribute_type="Text", )) db.session.add( models.AccessControlRole( name="a name for a role", object_type="Requirement", )) db.session.commit() with self.assertRaises(IntegrityError): db.session.add( models.CustomAttributeDefinition( title="non existing title", definition_type="requirement", definition_id=2, attribute_type="Text", )) db.session.commit() db.session.rollback() with self.assertRaises(ValueError): db.session.add( models.CustomAttributeDefinition( title="global cad", definition_type="requirement", definition_id=2, attribute_type="Text", )) db.session.commit() with self.assertRaises(ValueError): db.session.add( models.CustomAttributeDefinition( title="a name for a role", definition_type="requirement", definition_id=2, attribute_type="Text", )) db.session.commit()
def test_export_assessments_with_filters_and_conflicting_ca_names(self): """Test exporting assessments with conflicting custom attribute names.""" self.import_file("assessment_template_no_warnings.csv") self.import_file("assessment_with_templates.csv") # also create an object level custom attribute with a name that clashes # with a name of a "regular" attribute assessment = all_models.Assessment.query.filter( all_models.Assessment.slug == u"A 2").first() cad = all_models.CustomAttributeDefinition( attribute_type=u"Text", title=u"ca title", definition_type=u"assessment", definition_id=assessment.id) db.session.add(cad) db.session.commit() data = [{ "object_name": "Assessment", "fields": ["slug", "title", "description", "status"], "filters": { "expression": { "left": { "left": "code", "op": { "name": "~" }, "right": "A 2" }, "op": { "name": "AND" }, "right": { "left": "title", "op": { "name": "~" }, "right": "no template Assessment" } }, "keys": ["code", "title", "status"], "order_by": { "keys": [], "order": "", "compare": None } } }] response = self.export_csv(data) self.assertIn(u"No template Assessment 2", response.data)
def test_setting_same_name(self): """Setting an already existing title should pass the validator.""" db.session.add(models.CustomAttributeDefinition( title="my custom attribute title", definition_type="section", attribute_type="Text", )) db.session.commit() cad = models.CustomAttributeDefinition.query.first() cad.title = "my custom attribute title" cad.attribute_type = "Rich Text" db.session.commit() cad = models.CustomAttributeDefinition.query.first() self.assertEqual(cad.title, "my custom attribute title") self.assertEqual(cad.attribute_type, "Rich Text")