def _add_mapping_warning(self, source, destination): """Add warning if we have changes mappings """ mapping = all_models.Relationship.find_related(source, destination) src_object_type = source.__class__.__name__ dest_object_type = destination.__class__.__name__ mapping_error = None _SCOPING_MODELS = [scope_model.__name__ for scope_model in all_models.get_scope_models()] if (src_object_type in _SCOPING_MODELS and dest_object_type in ["Control", "Risk", "Regulation", "Standard"]): mapping_error = errors.MAP_UNMAP_SCOPE_ERROR elif (src_object_type == "Regulation" and dest_object_type in ["Control", "Risk"] + _SCOPING_MODELS): mapping_error = errors.MAP_UNMAP_REGULATION_ERROR elif (src_object_type == "Standard" and dest_object_type in ["Control", "Risk"] + _SCOPING_MODELS): mapping_error = errors.MAP_UNMAP_STANDARD_ERROR else: mapping_error = errors.MAP_UNMAP_SCOPE_ERROR if (self.unmap and mapping) or (not self.unmap and not mapping): self.add_warning( mapping_error, object_type=dest_object_type, action="unmap" if self.unmap else "map" )
class TestImportCommentable(TestCase): """Class with tests of importing fields of Commentable mixin.""" @ddt.data( all_models.Objective, all_models.Requirement, all_models.Issue, all_models.Regulation, all_models.Policy, all_models.Standard, all_models.Threat, all_models.Contract, ) def test_model_import(self, model): """Test import commentable model {}.""" recipients = model.VALID_RECIPIENTS model_name = model.__name__ import_data = [ ("object_type", model_name), ("Code", "{}-2".format(model_name)), ("Title", "{}-Title".format(model_name)), ("Admin", "*****@*****.**"), ("Recipients", ','.join(recipients)), ("Send by default", True), ] response = self.import_data(OrderedDict(import_data)) self._check_csv_response(response, {}) obj = model.query.first() self.assertEqual(obj.send_by_default, True) self.assertEqual(sorted(obj.recipients.split(",")), sorted(recipients)) def test_program_import(self): """Test import of program recipients.""" recipients = all_models.Program.VALID_RECIPIENTS model_name = "Program" import_data = [ ("object_type", model_name), ("Code", "{}-2".format(model_name)), ("Title", "{}-Title".format(model_name)), ("Program Managers", "*****@*****.**"), ("Recipients", ','.join(recipients)), ("Send by default", True), ] response = self.import_data(OrderedDict(import_data)) self._check_csv_response(response, {}) obj = all_models.Program.query.first() self.assertEqual(obj.send_by_default, True) self.assertEqual(sorted(obj.recipients.split(",")), sorted(recipients)) @ddt.data(*all_models.get_scope_models()) def test_scoping_import(self, model): """Test import scoping commentable object {}.""" recipients = model.VALID_RECIPIENTS model_name = model.__name__ import_data = [ ("object_type", model_name), ("Code", "{}-1".format(model_name)), ("Title", "{}-Title".format(model_name)), ("Admin", "*****@*****.**"), ("Recipients", ','.join(recipients)), ("Send by default", True), ("Assignee", "*****@*****.**"), ("Verifier", "*****@*****.**"), ] response = self.import_data(OrderedDict(import_data)) self._check_csv_response(response, {}) obj = model.query.first() self.assertEqual(sorted(obj.recipients.split(",")), sorted(recipients)) self.assertEqual(obj.send_by_default, True)
from ggrc.converters.import_helper import read_csv_file from ggrc.views.converters import check_import_file from ggrc.models import Revision, all_models from integration.ggrc import api_helper from integration.ggrc.api_helper import Api from integration.ggrc.models import factories # Hide errors during testing. Errors are still displayed after all tests are # done. This is for the bad request error messages while testing the api calls. logging.disable(logging.CRITICAL) THIS_ABS_PATH = os.path.abspath(os.path.dirname(__file__)) # Test relationship between Standard/Regulation and scope objects _SCOPING_MODELS = all_models.get_scope_models() READONLY_MAPPING_PAIRS = list( # make a list of object combinations from 1st and 2nd iterables # itertools.product("AB", "CD") is ["AC", "AD", "BC", "BD"] itertools.product(_SCOPING_MODELS, (all_models.Standard, all_models.Regulation)) ) # Test relationship for Control and Scope objects, Standard and Regulation READONLY_MAPPING_PAIRS.extend( (all_models.Control, m) for m in itertools.chain(_SCOPING_MODELS, (all_models.Standard, all_models.Regulation)) ) def read_imported_file(file_data): # pylint: disable=unused-argument
from ggrc.models import Revision, all_models from ggrc.utils import user_generator from integration.ggrc import api_helper from integration.ggrc.api_helper import Api from integration.ggrc.models import factories from integration.ggrc_basic_permissions.models \ import factories as rbac_factories # Hide errors during testing. Errors are still displayed after all tests are # done. This is for the bad request error messages while testing the api calls. logging.disable(logging.CRITICAL) THIS_ABS_PATH = os.path.abspath(os.path.dirname(__file__)) # Test relationship between Standard/Regulation and scope objects _SCOPING_MODELS = all_models.get_scope_models() READONLY_MAPPING_PAIRS = list( # make a list of object combinations from 1st and 2nd iterables # itertools.product("AB", "CD") is ["AC", "AD", "BC", "BD"] itertools.product(_SCOPING_MODELS, (all_models.Standard, all_models.Regulation))) # Test relationship for Control and Scope objects, Standard and Regulation READONLY_MAPPING_PAIRS.extend((all_models.Control, m) for m in itertools.chain(_SCOPING_MODELS, ( all_models.Standard, all_models.Regulation, ))) READONLY_MAPPING_PAIRS.extend((all_models.Risk, m) for m in itertools.chain(_SCOPING_MODELS, ( all_models.Standard,
class TestImportMappings(BaseTestImportMapUnmap): """Test import mappings via import""" def setUp(self): super(TestImportMappings, self).setUp() self.client.get("/login") @ddt.data(*ggrc.MAPPING_SCOPE_PAIRS) @ddt.unpack def test_map_scope_objects(self, model1, model2): """Test deprecated mapping between {0.__name__} and {1.__name__} """ response = self._get_import_csv_response(model1, model2, unmap=False) obj1 = model1.__name__ obj2 = utils.title_from_camelcase(model2.__name__).title() self._check_csv_response( [response[1]], { obj2: { "row_warnings": { errors.MAP_UNMAP_SCOPE_ERROR.format( line=7, object_type=obj1, ), }, }, }) @ddt.data(*ggrc.MAPPING_REGULATION_PAIRS) @ddt.unpack def test_map_regulation_objects(self, model1, model2): """Test deprecated mapping between {0.__name__} and {1.__name__} """ response = self._get_import_csv_response(model1, model2, unmap=False) obj1 = model1.__name__ obj2 = utils.title_from_camelcase(model2.__name__).title() self._check_csv_response( [response[1]], { obj2: { "row_warnings": { errors.MAP_UNMAP_REGULATION_ERROR.format( line=7, object_type=obj1, ), }, }, }) @ddt.data(*ggrc.MAPPING_STANDARD_PAIRS) @ddt.unpack def test_map_standard_objects(self, model1, model2): """Test deprecated mapping between {0.__name__} and {1.__name__} """ response = self._get_import_csv_response(model1, model2, unmap=False) obj1 = model1.__name__ obj2 = utils.title_from_camelcase(model2.__name__).title() self._check_csv_response( [response[1]], { obj2: { "row_warnings": { errors.MAP_UNMAP_STANDARD_ERROR.format( line=7, object_type=obj1, ), }, }, }) @ddt.data(*all_models.get_scope_models()) def test_map_policy_objects(self, scoping_model): """Test mapping between {0.__name__} and Policy.""" response = self._get_import_csv_response(scoping_model, all_models.Policy, unmap=False) self._check_csv_response(response, {})