Exemplo n.º 1
0
    async def close_async(self):
        """Close the current connection."""
        if self.in_transaction():
            raise pw.OperationalError(
                'Attempting to close database while transaction is open.')

        self.close()
Exemplo n.º 2
0
 def execute_sql(self, sql, *args, **kwargs):
     self.fail ^= True
     if self.fail:
         self.fail_count += 1
         raise pw.OperationalError("Fail every other time")
     else:
         return super(FailingSqliteDatabase,
                      self).execute_sql(sql, *args, **kwargs)
Exemplo n.º 3
0
def test_bad_db_connection(mock_is_table_exists):
    """Test a failed db connection."""
    mock_is_table_exists.side_effect = peewee.OperationalError(mock.Mock(), 'Error')
    hit_exception = False
    try:
        create_tables(18)
    except peewee.OperationalError:
        hit_exception = True
    assert hit_exception
Exemplo n.º 4
0
def test_bad_db_connection(mock_is_table_exists):
    """Test a failed db connection."""
    mock_is_table_exists.side_effect = peewee.OperationalError(
        mock.Mock(), 'Error')
    hit_exception = False
    os.environ['DATABASE_CONNECT_ATTEMPTS'] = '1'
    os.environ['DATABASE_CONNECT_WAIT'] = '1'
    try:
        OrmSync.dbconn_blocking()
    except peewee.OperationalError:
        hit_exception = True
    assert hit_exception
Exemplo n.º 5
0
def test_bad_db_connection(mock_db_connect):
    """Test a failed db connection."""
    mock_db_connect.side_effect = peewee.OperationalError(mock.Mock(), 'Error')
    environ['DATABASE_CONNECT_ATTEMPTS'] = '2'
    environ['DATABASE_CONNECT_WAIT'] = '2'
    hit_exception = False
    try:
        OrmSync.dbconn_blocking()
    except peewee.OperationalError:
        hit_exception = True
    del environ['DATABASE_CONNECT_ATTEMPTS']
    del environ['DATABASE_CONNECT_WAIT']
    assert hit_exception
Exemplo n.º 6
0
    def tables(self):
        """Return a list of table objects (peewee.Model) introspected from the database on every get."""
        try:
            self._tables = Introspector.from_database(
                self.database).generate_models(literal_column_names=True)
        except peewee.OperationalError as e:
            raise peewee.OperationalError(
                f'{e}\nCheck your keyword arguments connect(database=..., user=..., password=..., ...) or if none provided check cnf file specified by keyword `read_default_file` [default: "~/.my.cnf"]. '
            ) from e

        if len(self._tables) == 0:
            warnings.warn("You're working with an empty database.",
                          FutureWarning)

        return self._tables
Exemplo n.º 7
0
    def test_update_customer_credit_errors(self):
        """
        Validates value error thrown when updating a customer's credit limit
        if an exception (IntegrityError or OperationalError) is thrown.
        """
        mock_module = "basic_operations.Customers.get_or_none"
        with patch(mock_module) as get_none:
            get_none.side_effect = [
                peewee.IntegrityError("integrity"),
                peewee.OperationalError("operational")
            ]

            with self.assertRaises(ValueError):
                BaseOps.update_customer_credit("[]", 2.71)

            with self.assertRaises(ValueError):
                BaseOps.update_customer_credit("[]", 2.71)
Exemplo n.º 8
0
    def test_delete_customer_error_handle(self):
        """
        Validates OperationalErrors are handled and elegantly logged
        during customer delete
        """
        mock_module = "basic_operations.Customers.delete_by_id"
        with patch(mock_module) as delete_handle:
            delete_handle.side_effect = peewee.OperationalError("operational")

            with self.assertLogs() as mock_logger:
                BaseOps.delete_customer("[]")

                msg = "ERROR:basic_operations:operational"
                self.assertIn(msg, mock_logger.output)

                log_msg = "Failed to delete customer with customer_id: []"
                msg = f"INFO:basic_operations:{log_msg}"
                self.assertIn(msg, mock_logger.output)
Exemplo n.º 9
0
    def test_search_customer_error_handle(self):
        """
        Validates OperationalErrors are handled and elegantly logged
        during customer search
        """
        mock_module = "basic_operations.Customers.get_or_none"
        with patch(mock_module) as get_none:
            get_none.side_effect = peewee.OperationalError("operational")

            with self.assertLogs() as mock_logger:
                cust_dict = BaseOps.search_customer("[]")
                self.assertEqual(cust_dict, {})

                msg = "ERROR:basic_operations:operational"
                self.assertIn(msg, mock_logger.output)

                log_msg = "Failed look up of customer with customer_id: []"
                msg = f"INFO:basic_operations:{log_msg}"
                self.assertIn(msg, mock_logger.output)
Exemplo n.º 10
0
    def test_write_customer_errors(self):
        """ Validates errors on writing customers to the DB """
        db_module = "create_customers_db.Customers.get_or_create"
        with patch(db_module) as db_helper:
            db_helper.side_effect = [
                peewee.IntegrityError("integrity"),
                peewee.OperationalError("operational")
            ]

            customer_list = [
                "1,2,3,4,5,6,active,2.01", "2,3,4,5,6,7,active,2.01"
            ]

            with self.assertLogs() as mock_logger:
                CreateDb.write_customers(customer_list)
                msg = "ERROR:root:integrity"
                self.assertIn(msg, mock_logger.output)

                msg = "ERROR:root:operational"
                self.assertIn(msg, mock_logger.output)
Exemplo n.º 11
0
    def test_add_customer_exceptions(self):
        """
        Validates add customer exceptions (IntegrityError and OperationalError)
        are handled and elegantly logged.
        """
        with patch("basic_operations.Customers.get_or_create") as handle_get:
            handle_get.side_effect = [
                peewee.IntegrityError("integrity"),
                peewee.OperationalError("operational")
            ]

            with self.assertLogs() as mock_logger:
                BaseOps.add_customer("123", "Amelia", "Bedelia",
                                     "123 Starshine Ln.", "Pennsylvania 65000",
                                     "*****@*****.**", "active", 3.14)
                msg = "ERROR:basic_operations:integrity"
                self.assertIn(msg, mock_logger.output)

                BaseOps.add_customer("123", "Amelia", "Bedelia",
                                     "123 Starshine Ln.", "Pennsylvania 65000",
                                     "*****@*****.**", "active", 3.14)
                msg = "ERROR:basic_operations:operational"
                self.assertIn(msg, mock_logger.output)
Exemplo n.º 12
0
 async def close_async(self):
     if self.in_transaction():
         raise pw.OperationalError(
             'Attempting to close database while transaction is open.')
     super(PooledDatabaseAsync, self).close()
     await self._release()