def test__allocate_hosting_port_returns_none_if_binding_fails(self):
     with self.router() as r, self.port() as p1:
         r_id = r['router']['id']
         p1_id = p1['port']['id']
         self._router_interface_action('add', r_id, None, p1_id)
         plugging_drv_mock = mock.MagicMock()
         plugging_drv_mock.allocate_hosting_port.return_value = {
             'allocated_port_id': p1_id,
             'allocated_vlan': 10
         }
         adm_ctx = bc.context.get_admin_context()
         p1_db = self._core_plugin._get_port(adm_ctx, p1_id)
         ctx_mock = mock.MagicMock()
         ctx_mock.session = mock.MagicMock()
         ctx_mock.session.begin = adm_ctx.session.begin
         ctx_mock.session.add = mock.MagicMock()
         ctx_mock.session.add.side_effect = db_exc.DBReferenceError(
             'cisco_port_mappings',
             'foreign key constraint',
             p1_id,
             'ports',
             inner_exception=inner_db_exc.IntegrityError("Invalid insert",
                                                         params="",
                                                         orig=None))
         res = self.l3_plugin._allocate_hosting_port(
             ctx_mock, r_id, p1_db, 'fake_hd_id', plugging_drv_mock)
         self.assertIsNone(res)
示例#2
0
    def test_reprocess_in_create_raw_key(self):
        # This mock simulates the case where the create raw key function
        # is called and received a duplicate key. It raises an
        # IntegrityError which triggers our reprocessing code if the
        # reprocessing feature is enabled.
        mock_session = Mock()
        mock_session.flush = Mock(
            side_effect=core_exc.IntegrityError("test", "test", "test"))

        mock_session.query.filter = Mock()

        raw_id = Mock()
        raw_id.id = "123"

        filter_ret_mock = Mock()
        filter_ret_mock.one = Mock(return_value=raw_id)

        filter_mock = Mock()
        filter_mock.filter = Mock(return_value=filter_ret_mock)

        mock_session.query = Mock(return_value=filter_mock)

        test_etl = ETL(setup_path=example_setup_path)
        test_etl.settings = test_etl.settings._replace(
            should_reprocess=True)  # creating a new settings namedtuple.
        test_etl.RAW_KEY_MODEL = Mock()
        actual_id = test_etl._create_raw_key(mock_session, "123", "123")

        assert actual_id == '123'
示例#3
0
 def update_product(cls, product_id, name):
     try:
         row = cls.query.filter_by(ProductID=product_id).first()
         row.ProductName = name
         db.session.commit()
     except exc.IntegrityError:
         raise exc.IntegrityError("", "", 1)
示例#4
0
 def update_date_deleted(cls, product_id, date):
     try:
         row = cls.query.filter_by(ProductID=product_id).first()
         row.DateDeleted = date
         db.session.commit()
     except exc.IntegrityError:
         raise exc.IntegrityError("", "", 1)
示例#5
0
 def update_company(cls, companycode, name):
     try:
         row = cls.query.filter_by(CompanyCode=companycode).first()
         row.CompanyName = name
         db.session.commit()
     except exc.IntegrityError:
         raise exc.IntegrityError("", "", 1)
示例#6
0
 def update_date_deleted(cls, companycode, date):
     try:
         row = cls.query.filter_by(CompanyCode=companycode).first()
         row.DateDeleted = date
         db.session.commit()
     except exc.IntegrityError:
         raise exc.IntegrityError("", "", 1)
示例#7
0
 def update_product_sellers(cls, product_id, company_ID):
     try:
         row = cls.query.filter_by(ProductID=product_id).first()
         row.CompanyCode = company_ID
         db.session.commit()
     except exc.IntegrityError:
         raise exc.IntegrityError("", "", 1)
示例#8
0
 def update_product_valuations(cls, product_id, price, date_entered):
     try:
         row = cls.query.filter_by(ProductID=product_id).first()
         row.ProductPrice = price
         row.DateOfValuation = date_entered
         db.session.commit()
     except exc.IntegrityError:
         raise exc.IntegrityError("", "", 1)
示例#9
0
 def save_to_db(self):
     try:
         db.session.add(self)
         db.session.commit()
     except exc.IntegrityError:
         raise exc.IntegrityError("", "", 1)
     except exc.InterfaceError:
         raise exc.InterfaceError("", "", 1)
示例#10
0
def wrap_ibm_db_errors(statement, params):
    try:
        yield
    except Database.OperationalError as e:
        error = int(error_re.search(e._message).groups()[0])
        errors = [268, 691, 530, 391]
        if error in errors:
            raise exc.IntegrityError(statement, params, e) from e
        raise
 def test_main_success_no_dip_creation(self):
     """Test a success where one AIP was already processed."""
     effect = exc.IntegrityError({}, [], "")
     session_add_patch = mock.patch("sqlalchemy.orm.Session.add", side_effect=effect)
     with TmpDir(TMP_DIR), TmpDir(OUTPUT_DIR), session_add_patch:
         ret = create_dips_job.main(**self.args)
         assert ret is None
         dip_path = os.path.join(
             OUTPUT_DIR, "test_B-3ea465ac-ea0a-4a9c-a057-507e794de332"
         )
         assert not os.path.isdir(dip_path)
示例#12
0
 def save_to_db(self):
     try:
         db.session.add(self)
         db.session.commit()
     except exc.IntegrityError:
         raise exc.IntegrityError("", "", 1)
         traceback.print_exc(file=sys.stdout)
     except exc.InterfaceError:
         raise exc.InterfaceError("", "", 1)
     except:
         print("Unknown Error occurred")
示例#13
0
 def save_to_db(self):
     try:
         db.session.add(self)
         db.session.commit()
     except exc.IntegrityError:
         raise exc.IntegrityError("", "", 1)
         print("The currency couldn't be saved to the database")
         traceback.print_exc(file=sys.stdout)
     except exc.InterfaceError:
         raise exc.InterfaceError("", "", 1)
     except:
         print("Unknown Error occurred")
 def test_main_success_no_dip_creation(self):
     """Test a success where one AIP was already processed."""
     effect = exc.IntegrityError({}, [], '')
     session_add_patch = mock.patch('sqlalchemy.orm.Session.add',
                                    side_effect=effect)
     with TmpDir(TMP_DIR), TmpDir(OUTPUT_DIR), session_add_patch:
         ret = create_dips_job.main(SS_URL, SS_USER_NAME, SS_API_KEY,
                                    LOCATION_UUID, TMP_DIR, OUTPUT_DIR,
                                    DATABASE_FILE)
         assert ret is None
         dip_path = os.path.join(
             OUTPUT_DIR, 'test_B_3ea465ac-ea0a-4a9c-a057-507e794de332_DIP')
         assert not os.path.isdir(dip_path)
示例#15
0
 def test_ibm_db_sa_raise_if_duplicate_entry_error_no_match(self):
     # Tests that the session._raise_if_duplicate_entry_error method
     # does not raise a DBDuplicateEntry exception when it's not a matching
     # integrity error.
     statement = ('ALTER TABLE instance_types ADD CONSTRAINT '
                  'uniq_name_x_deleted UNIQUE (name, deleted)')
     params = None
     orig = sqla_exc.SQLAlchemyError(
         'SQL0542N  The column named "NAME" cannot be a column of a '
         'primary key or unique key constraint because it can contain null '
         'values.')
     integrity_error = sqla_exc.IntegrityError(statement, params, orig)
     session._raise_if_duplicate_entry_error(integrity_error, 'ibm_db_sa')
示例#16
0
    def setter(self, **kw):
        """ Inserts new entry or updates existing entry.
            Assumptions:
                - the table has a primary key named 'id'
                - the table has a single unique constraint on its natural key
            Insert details:
                - attempt insert
                - if it works, return new id otherwise continue onto update
            Update details
                - if id provided, update based on id
                - if id not provided, update based on unique constraint
                - if constraint violation occurs - manually generate exception!
            Returns number of rows inserted.
        """

        kw_insert = {}
        #broken code - not sure what it did exactly!
        for key in kw.keys():
            if kw[key] not in self.insert_defaulted:
                kw_insert[key] = kw[key]

        try:
            ins_sql = self._table.insert()
            result = ins_sql.execute(kw_insert)
            #print kw_insert
            if result.rowcount == 0:
                raise KeyError  # by missing column
            else:
                return result.lastrowid
        except exc.IntegrityError, except_detail:
            # possibly caused by violation of primary key constraint
            # possibly caused by violation of check or fk key constraint
            # print 'insert exception'
            # print e
            kw_update = {}
            for key in kw.keys():
                if kw[key] not in self.update_defaulted:
                    kw_update[key] = kw[key]
            upd_sql = self._table.update()
            upd_sql = self._create_where(upd_sql, kw_update)
            #print kw_update
            result = upd_sql.execute(kw_update)
            if result.rowcount == 0:  # this is the only way to catch
                print 'update exception'
                print except_detail  # might want to get rid of this
                print result
                # usually constraint violations
                raise exc.IntegrityError(upd_sql, kw_update, None)
            return 0
示例#17
0
 def test_ibm_db_sa_raise_if_duplicate_entry_error_duplicate(self):
     # Tests that the session._raise_if_duplicate_entry_error method
     # translates the duplicate entry integrity error for the DB2 engine.
     statement = ('INSERT INTO key_pairs (created_at, updated_at, '
                  'deleted_at, deleted, name, user_id, fingerprint) VALUES '
                  '(?, ?, ?, ?, ?, ?, ?)')
     params = ['20130918001123627099', None, None, 0, 'keypair-23474772',
               '974a7c9ffde6419f9811fcf94a917f47',
               '7d:2c:58:7f:97:66:14:3f:27:c7:09:3c:26:95:66:4d']
     orig = sqla_exc.SQLAlchemyError(
         'SQL0803N  One or more values in the INSERT statement, UPDATE '
         'statement, or foreign key update caused by a DELETE statement are'
         ' not valid because the primary key, unique constraint or unique '
         'index identified by "2" constrains table "NOVA.KEY_PAIRS" from '
         'having duplicate values for the index key.')
     integrity_error = sqla_exc.IntegrityError(statement, params, orig)
     self.assertRaises(db_exc.DBDuplicateEntry,
                       session._raise_if_duplicate_entry_error,
                       integrity_error, 'ibm_db_sa')
示例#18
0
 def update_trade(cls, tradeID, product, buyingParty, sellingParty,
                  notionalValue, notionalCurrency, quantity, maturityDate,
                  underlyingValue, underlyingCurrency, strikePrice):
     try:
         row = cls.query.filter_by(TradeID=tradeID).first()
         row.ProductID = product
         row.BuyingParty = buyingParty
         row.SellingParty = sellingParty
         row.NotionalValue = notionalValue
         row.NotionalCurrency = notionalCurrency
         row.Quantity = quantity
         row.MaturityDate = maturityDate
         row.UnderlyingValue = underlyingValue
         row.UnderlyingCurrency = underlyingCurrency
         row.StrikePrice = strikePrice
         row.LastModifiedDate = date_now = str(date_func.today())
         db.session.commit()
     except exc.IntegrityError:
         raise exc.IntegrityError("", "", 1)
def test_create_user_unsuccessful(config, runner_factory):
    runner = runner_factory(AccountsService)
    container = get_container(runner, AccountsService)
    storage = replace_dependencies(container, "storage")
    runner.start()

    storage.users.create.side_effect = exc.IntegrityError(statement="",
                                                          params="",
                                                          orig="")

    payload = {
        "email": "*****@*****.**",
        "password": "******",
        "display_name": "Test Account",
    }
    with entrypoint_hook(container, "create_user") as create_user:

        with pytest.raises(UserAlreadyExists):
            create_user(user_details=payload)

        assert storage.users.create.call_args == call(payload["email"],
                                                      payload["password"],
                                                      payload["display_name"])
示例#20
0
 def update(self):
     raise db_exception.DBError(
         inner_exception=exc.IntegrityError('a', 'a', 'a'))
示例#21
0
class TableTools(object):
    """ Provides generic and simple tools for managing data within a table.
    """
    def __init__(self, metadata, engine):
        self.metadata = metadata
        self.engine = engine
        self._table = None
        self._table_name = None
        self._unique_constraints = None
        self.insert_defaulted = []  # cols added here aren't inserted
        self.update_defaulted = []  # cols added here aren't updated

    def deleter(self, **kw):
        """ Requires the key of the row.
            Returns a count of rows deleted (will be either 0 or 1).
            Will only work on tables with primary keys.
            Does not confirm that it has the key for the row - so can be
            dangerous.
            Note - it may not delete anything if an insufficient unique
            key is provided.
            Uses 'generative sql' to build up query.
        """
        try:
            gener_sql = self._table.delete()
            gener_sql = self._create_where(gener_sql, kw)
            result = gener_sql.execute()
        except KeyError:
            # could mean row was not there
            # could mean that part of key was missing?
            return 0

        assert result.rowcount in [0, 1]
        return result.rowcount

    def getter(self, **kw):
        """ Requires the key of the row
            Returns a single row if found, otherwise returns None
            Will only work on tables with primary keys.
            Uses 'generative sql' to build up query.
        """
        gener_sql = self._table.select()
        gener_sql = self._create_where(gener_sql, kw)
        result = gener_sql.execute()
        rows = result.fetchall()
        assert len(rows) in [0, 1]
        try:
            return rows[0]
        except IndexError:  # no rows found
            return None

    def get_id(self, **kw):
        """ Requires the key of the row - for tables with a primary key
            that is a surrogate key and a natural key defined within a
            unique constraint.
            Returns a single row if found, otherwise returns None
            No longer being used - but kept around just in case
            Uses 'generative sql' to build up query.
        """
        gener_sql = self._table.select()
        #uk       = self._get_unique_constraints()
        gener_sql = self._create_where(gener_sql, kw)
        result = gener_sql.execute()
        rows = result.fetchall()
        try:
            return rows[0].id
        except AttributeError:  # no rows found
            return None

    def lister(self, **kw):
        """ Returns all rows for the table, not in any particular order.
            Inputs:
                - assumes some args, but ignores them
                - these are only included because of the way that these functions
                  are called.
            Outputs:
                - all rows & columns for table
        """
        sel_sql = self._table.select()
        result = sel_sql.execute()
        rows = result.fetchall()
        return rows

    def setter(self, **kw):
        """ Inserts new entry or updates existing entry.
            Assumptions:
                - the table has a primary key named 'id'
                - the table has a single unique constraint on its natural key
            Insert details:
                - attempt insert
                - if it works, return new id otherwise continue onto update
            Update details
                - if id provided, update based on id
                - if id not provided, update based on unique constraint
                - if constraint violation occurs - manually generate exception!
            Returns number of rows inserted.
        """
        kw_insert = {}
        #broken code - not sure what it did exactly!
        for key in kw.keys():
            if kw[key] not in self.insert_defaulted:
                kw_insert[key] = kw[key]

        try:
            ins_sql = self._table.insert()
            result = ins_sql.execute(kw_insert)
            #print kw_insert
            if result.rowcount == 0:
                raise KeyError  # by missing column
            else:
                return result.lastrowid
        except exc.IntegrityError, except_detail:
            # possibly caused by violation of primary key constraint
            # possibly caused by violation of check or fk key constraint
            # print 'insert exception'
            # print e
            #print except_detail
            kw_update = {}
            for key in kw.keys():
                if kw[key] not in self.update_defaulted:
                    kw_update[key] = kw[key]
            upd_sql = self._table.update()
            upd_sql = self._create_where(upd_sql, kw_update)
            #print kw_update
            try:
                result = upd_sql.execute(kw_update)
            except exc.IntegrityError, except_detail:
                print 'insert failed, update failed'
                print except_detail
                raise
            if result.rowcount == 0:  # this is the only way to catch
                print 'update exception'
                print except_detail  # might want to get rid of this
                print result
                # usually constraint violations
                raise exc.IntegrityError(upd_sql, kw_update, None)
            return 0
示例#22
0
 def delete_company(cls, companycode):
     try:
         cls.query.filter_by(CompanyCode=companycode).delete()
         db.session.commit()
     except exc.IntegrityError:
         raise exc.IntegrityError("", "", 1)
示例#23
0
 def delete_product(cls, product_id):
     try:
         cls.query.filter_by(ProductID=product_id).delete()
         db.session.commit()
     except exc.IntegrityError:
         raise exc.IntegrityError("", "", 1)
示例#24
0
 def delete_trade(cls, trade_id):
     try:
         cls.query.filter_by(TradeID=trade_id).delete()
         db.session.commit()
     except exc.IntegrityError:
         raise exc.IntegrityError("", "", 1)