Beispiel #1
0
def test_drop_table_raises_error():

    etcd_client = mock.Mock()

    # Database is not selected
    with pytest.raises(OperationalError):
        drop_table(etcd_client, SQLTree())
Beispiel #2
0
def test_drop_table_calls_rmdir(db_sql_tree, db_arg):
    tree = SQLTree()
    tree.db = db_sql_tree
    tree.table = 'bar'

    etcd_client = mock.Mock()

    drop_table(etcd_client, tree, db=db_arg)

    etcd_client.rmdir.assert_called_once_with('/foo/bar', recursive=True)
Beispiel #3
0
def test_drop_table_raises_on_unknown_db():
    tree = SQLTree()
    tree.db = 'foo'
    tree.table = 'bar'

    etcd_client = mock.Mock()
    etcd_client.read.side_effect = EtcdKeyNotFound

    # unknown database
    with pytest.raises(OperationalError):
        drop_table(etcd_client, tree)
Beispiel #4
0
def test_drop_table_if_exists():
    tree = SQLTree()
    tree.db = 'foo'
    tree.table = 'bar'
    tree.options['if_exists'] = True

    etcd_client = mock.Mock()
    etcd_client.rmdir.side_effect = EtcdKeyNotFound

    drop_table(etcd_client, tree, db='foo')

    etcd_client.rmdir.assert_called_once_with('/foo/bar', recursive=True)
Beispiel #5
0
    def execute(self, query, args=None):
        """Prepare and execute a database operation (query or command).

        :param query: Query text.
        :type query: str
        :param args: Optional query arguments.
        :type args: tuple
        :raise ProgrammingError: if query can't be parsed."""

        query = self.morgify(query, args)

        LOG.debug('Executing: %s', query)

        try:
            tree = self._sql_parser.parse(query)
        except SQLParserError as err:
            raise ProgrammingError(err)

        if not self._db:
            self._db = tree.db

        self._result_set = None
        self._rowcount = 0

        if tree.query_type == "SHOW_DATABASES":
            self._result_set = show_databases(self.connection.client)

        elif tree.query_type == "CREATE_DATABASE":
            create_database(self.connection.client, tree)

        elif tree.query_type == "DROP_DATABASE":
            drop_database(self.connection.client, tree)

        elif tree.query_type == "USE_DATABASE":
            self._db = use_database(self.connection.client, tree)

        elif tree.query_type == "CREATE_TABLE":
            create_table(self.connection.client, tree, db=self._db)

        elif tree.query_type == "DROP_TABLE":
            drop_table(self.connection.client, tree, db=self._db)

        elif tree.query_type == "SHOW_TABLES":
            self._result_set = show_tables(self.connection.client,
                                           tree,
                                           db=self._db)
        elif tree.query_type == "DESC_TABLE":
            self._result_set = desc_table(self.connection.client,
                                          tree,
                                          db=self._db)
        elif tree.query_type == "INSERT":
            self._rowcount = insert(self.connection.client, tree, db=self._db)
        elif tree.query_type == 'SELECT':
            self._result_set = execute_select(self.connection.client,
                                              tree,
                                              db=self._db)
        elif tree.query_type == 'WAIT':
            self._result_set = execute_wait(self.connection.client,
                                            tree,
                                            db=self._db)
        elif tree.query_type == "UPDATE":
            self._rowcount = execute_update(self.connection.client,
                                            tree,
                                            db=self._db)
        elif tree.query_type == "DELETE":
            self._rowcount = execute_delete(self.connection.client,
                                            tree,
                                            db=self._db)
        if self._result_set is not None:
            self._rowcount = self._result_set.n_rows