Exemple #1
0
 def _verify_canload(self, state):
     if state is not None and \
         not self.mapper._canload(state,
                         allow_subtypes=not self.enable_typechecks):
         if self.mapper._canload(state, allow_subtypes=True):
             raise exc.FlushError('Attempting to flush an item of type '
                             '%(x)s as a member of collection '
                             '"%(y)s". Expected an object of type '
                             '%(z)s or a polymorphic subclass of '
                             'this type. If %(x)s is a subclass of '
                             '%(z)s, configure mapper "%(zm)s" to '
                             'load this subtype polymorphically, or '
                             'set enable_typechecks=False to allow '
                             'any subtype to be accepted for flush. '
                              % {
                 'x': state.class_,
                 'y': self.prop,
                 'z': self.mapper.class_,
                 'zm': self.mapper,
                 })
         else:
             raise exc.FlushError(
                 'Attempting to flush an item of type '
                 '%(x)s as a member of collection '
                 '"%(y)s". Expected an object of type '
                 '%(z)s or a polymorphic subclass of '
                 'this type.' % {
                     'x': state.class_,
                     'y': self.prop,
                     'z': self.mapper.class_,
                 })
Exemple #2
0
    def test_add_tenants_errors(self, mock_gfbu, mock_strin):
        ret_flavor = MagicMock()
        tenants = ['test_tenant']
        ret_flavor.flavor.tenants = tenants
        mock_gfbu.return_value = ret_flavor
        global error

        error = 1
        injector.override_injected_dependency(
            ('data_manager', get_datamanager_mock))
        self.assertRaises(flavor_logic.ErrorStatus, flavor_logic.add_tenants,
                          'uuid', TenantWrapper(tenants), 'transaction')

        # Flavor is public
        error = 5
        self.assertRaises(flavor_logic.ErrorStatus, flavor_logic.add_tenants,
                          'uuid', TenantWrapper(tenants), 'transaction')

        error = 31
        moq = MagicMock()
        moq.tenants = [1337]
        self.assertRaises(ValueError, flavor_logic.add_tenants, 'uuid', moq,
                          'transaction')

        mock_strin.side_effect = exc.FlushError(
            'conflicts with persistent instance')
        self.assertRaises(flavor_logic.ConflictError, flavor_logic.add_tenants,
                          'uuid', TenantWrapper(tenants), 'transaction')

        mock_strin.side_effect = exc.FlushError('')
        self.assertRaises(exc.FlushError, flavor_logic.add_tenants, 'uuid',
                          TenantWrapper(tenants), 'transaction')
Exemple #3
0
 def _verify_canload(self, state):
     if state is not None and not self.mapper._canload(state, allow_subtypes=not self.enable_typechecks):
         if self.mapper._canload(state, allow_subtypes=True):
             raise exc.FlushError("Attempting to flush an item of type %s on collection '%s', "
                             "which is not the expected type %s.  Configure mapper '%s' to load this "
                             "subtype polymorphically, or set enable_typechecks=False to allow subtypes.  "
                             "Mismatched typeloading may cause bi-directional relationships (backrefs) "
                             "to not function properly." % (state.class_, self.prop, self.mapper.class_, self.mapper))
         else:
             raise exc.FlushError("Attempting to flush an item of type %s on collection '%s', "
                             "whose mapper does not inherit from that of %s." % (state.class_, self.prop, self.mapper.class_))
Exemple #4
0
    def test_add_regions_errors(self, mock_gfbu, mock_strin):
        ret_flavor = MagicMock()
        ret_flavor.flavor.regions = [Region(name='test_region')]
        mock_gfbu.return_value = ret_flavor
        global error

        error = 1
        injector.override_injected_dependency(
            ('data_manager', get_datamanager_mock))
        self.assertRaises(flavor_logic.ErrorStatus, flavor_logic.add_regions,
                          'uuid', RegionWrapper([Region(name='test_region')]),
                          'transaction')

        error = 4
        injector.override_injected_dependency(
            ('data_manager', get_datamanager_mock))

        mock_strin.side_effect = exc.FlushError()
        self.assertRaises(exc.FlushError, flavor_logic.add_regions, 'uuid',
                          RegionWrapper([Region(name='test_region')]),
                          'transaction')
        mock_strin.side_effect = exc.FlushError(
            'conflicts with persistent instance')
        self.assertRaises(flavor_logic.ErrorStatus, flavor_logic.add_regions,
                          'uuid', RegionWrapper([Region(name='test_region')]),
                          'transaction')
        mock_strin.side_effect = ValueError()
        self.assertRaises(ValueError, flavor_logic.add_regions, 'uuid',
                          RegionWrapper([Region(name='test_region')]),
                          'transaction')
        mock_strin.side_effect = ValueError(
            'conflicts with persistent instance')
        self.assertRaises(flavor_logic.ConflictError, flavor_logic.add_regions,
                          'uuid', RegionWrapper([Region(name='test_region')]),
                          'transaction')

        self.assertRaises(flavor_logic.ErrorStatus,
                          flavor_logic.add_regions, 'uuid',
                          RegionWrapper([Region(name='')]), 'transaction')
        self.assertRaises(
            flavor_logic.ErrorStatus, flavor_logic.add_regions, 'uuid',
            RegionWrapper([Region(name='test_region', type='group')]),
            'transaction')
Exemple #5
0
def _organize_states_for_save(base_mapper, states, uowtransaction):
    """Make an initial pass across a set of states for INSERT or
    UPDATE.
    
    This includes splitting out into distinct lists for
    each, calling before_insert/before_update, obtaining
    key information for each state including its dictionary,
    mapper, the connection to use for the execution per state,
    and the identity flag.
    
    """

    states_to_insert = []
    states_to_update = []

    for state, dict_, mapper, connection in _connections_for_states(
                                            base_mapper, uowtransaction, 
                                            states):

        has_identity = bool(state.key)
        instance_key = state.key or mapper._identity_key_from_state(state)

        row_switch = None

        # call before_XXX extensions
        if not has_identity:
            mapper.dispatch.before_insert(mapper, connection, state)
        else:
            mapper.dispatch.before_update(mapper, connection, state)

        # detect if we have a "pending" instance (i.e. has 
        # no instance_key attached to it), and another instance 
        # with the same identity key already exists as persistent. 
        # convert to an UPDATE if so.
        if not has_identity and \
            instance_key in uowtransaction.session.identity_map:
            instance = \
                uowtransaction.session.identity_map[instance_key]
            existing = attributes.instance_state(instance)
            if not uowtransaction.is_deleted(existing):
                raise orm_exc.FlushError(
                    "New instance %s with identity key %s conflicts "
                    "with persistent instance %s" % 
                    (state_str(state), instance_key,
                     state_str(existing)))

            base_mapper._log_debug(
                "detected row switch for identity %s.  "
                "will update %s, remove %s from "
                "transaction", instance_key, 
                state_str(state), state_str(existing))

            # remove the "delete" flag from the existing element
            uowtransaction.remove_state_actions(existing)
            row_switch = existing

        if not has_identity and not row_switch:
            states_to_insert.append(
                (state, dict_, mapper, connection, 
                has_identity, instance_key, row_switch)
            )
        else:
            states_to_update.append(
                (state, dict_, mapper, connection, 
                has_identity, instance_key, row_switch)
            )

    return states_to_insert, states_to_update