-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SQL runner utility primitives to
io.sql
namespace
- Loading branch information
Showing
8 changed files
with
76 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
## Unreleased | ||
|
||
- Add SQL runner utility primitives to `io.sql` namespace | ||
|
||
|
||
## 2023/11/06 v0.0.2 | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from cratedb_toolkit.util.database import DatabaseAdapter, run_sql # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import io | ||
|
||
import pytest | ||
import sqlalchemy as sa | ||
|
||
from cratedb_toolkit.io.sql import run_sql | ||
|
||
|
||
def sqlcmd(sql): | ||
return run_sql(dburi="crate://crate@localhost:4200/", sql=sql, records=True) | ||
|
||
|
||
def test_run_sql_from_string(): | ||
sql_string = "SELECT 1;" | ||
outcome = sqlcmd(sql_string) | ||
assert outcome == [{"1": 1}] | ||
|
||
|
||
def test_run_sql_from_file(tmp_path): | ||
sql_file = tmp_path / "temp.sql" | ||
sql_file.write_text("SELECT 1;") | ||
outcome = sqlcmd(sql_file) | ||
assert outcome == [{"1": 1}] | ||
|
||
|
||
def test_run_sql_from_buffer(): | ||
sql_buffer = io.StringIO("SELECT 1;") | ||
outcome = sqlcmd(sql_buffer) | ||
assert outcome == [{"1": 1}] | ||
|
||
|
||
def test_run_sql_invalid_host(capsys): | ||
with pytest.raises(sa.exc.OperationalError) as ex: | ||
run_sql(dburi="crate://localhost:12345", sql="SELECT 1;") | ||
assert ex.match( | ||
".*ConnectionError.*No more Servers available.*HTTPConnectionPool.*" | ||
"Failed to establish a new connection.*Connection refused.*" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters