Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ The following APIs are available:
* `/v0/datasources`: `client.api.datasources`
* `/v0/events`: `client.api.events`
* `/v0/pipes`: `client.api.pipes`
* `/v0/sql`: `client.api.query`
* `/v0/tokens`: `client.api.tokens`
* `/v0/variables`: `client.api.variables`

Expand Down
120 changes: 120 additions & 0 deletions tests/integration/test_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import pytest


class TestQueryApi:
@pytest.fixture(autouse=True)
def _put_records(self, client):
client.api.events.send(
"simple",
wait=True,
records=[
{
"Id": "e7f2af3e-99d1-4d4f-8a8c-d6aee4ab89b0",
"Timestamp": "2024-01-23T10:30:00.123456",
"Key": "foo",
"Value": "bar",
},
{
"Id": "d7792957-21d8-46e6-a4e0-188eb36e2758",
"Timestamp": "2024-02-23T11:45:00.234567",
"Key": "baz",
"Value": "ed",
},
],
)

def test_query_datasource_json(self, client):
response = client.api.query.query("SELECT key, value FROM simple ORDER BY `key` ASC")

assert response.data == [{"key": "baz", "value": "ed"}, {"key": "foo", "value": "bar"}]
assert response.meta == [
{"name": "key", "type": "String"},
{"name": "value", "type": "String"},
]
assert response.rows == 2
assert response.statistics["rows_read"] == 2

def test_query_pipe(self, client):
response = client.api.query.query("SELECT * FROM simple_kv ORDER BY `key` ASC")

assert response.data == [{"key": "baz", "value": "ed"}, {"key": "foo", "value": "bar"}]
assert response.meta == [
{"name": "key", "type": "String"},
{"name": "value", "type": "String"},
]
assert response.rows == 2
assert response.statistics["rows_read"] == 2

def test_query_pipe_parameters(self, client):
response = client.api.query.query(
"SELECT key, value FROM simple_pipe", parameters={"key": "foo"}
)

assert response.data == [{"key": "foo", "value": "bar"}]
assert response.meta == [
{"name": "key", "type": "String"},
{"name": "value", "type": "String"},
]
assert response.rows == 1
assert response.statistics["rows_read"] == 2

def test_query_pipeline_json(self, client):
response = client.api.query.query(
"SELECT * FROM _ ORDER BY `key` ASC", pipeline="simple_kv"
)

assert response.data == [{"key": "baz", "value": "ed"}, {"key": "foo", "value": "bar"}]
assert response.meta == [
{"name": "key", "type": "String"},
{"name": "value", "type": "String"},
]
assert response.rows == 2
assert response.statistics["rows_read"] == 2

def test_query_csv(self, client):
response = client.api.query.query(
"SELECT key, value FROM simple ORDER BY `key` ASC", format="CSV"
)

assert response.text == '"baz","ed"\n"foo","bar"\n'

def test_query_csv_with_names(self, client):
response = client.api.query.query(
"SELECT key, value FROM simple ORDER BY `key` ASC", format="CSVWithNames"
)

assert (
response.text
== '"key","value"\n"baz","ed"\n"foo","bar"\n'
!= '"baz","ed"\n"foo","bar"\n'
)
# CSV with names can be parsed as data!
assert response.data == [{"key": "baz", "value": "ed"}, {"key": "foo", "value": "bar"}]

def test_query_tsv(self, client):
response = client.api.query.query(
"SELECT key, value FROM simple ORDER BY `key` ASC", format="TSV"
)

assert response.text == "baz\ted\nfoo\tbar\n"

def test_query_tsv_with_names(self, client):
response = client.api.query.query(
"SELECT key, value FROM simple ORDER BY `key` ASC", format="TSVWithNames"
)

assert response.text == "key\tvalue\nbaz\ted\nfoo\tbar\n"
assert response.data == [{"key": "baz", "value": "ed"}, {"key": "foo", "value": "bar"}]

def test_query_ndjson(self, client):
response = client.api.query.query(
"SELECT key, value FROM simple ORDER BY `key` ASC", format="JSONEachRow"
)

assert (
response.text
== '{"key":"baz","value":"ed"}\n{"key":"foo","value":"bar"}\n'
!= '"key","value"\n"baz","ed"\n"foo","bar"\n'
)
# CSV with names can be parsed as data!
assert response.data == [{"key": "baz", "value": "ed"}, {"key": "foo", "value": "bar"}]
2 changes: 1 addition & 1 deletion verdin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
name = "verdin"

__version__ = "0.5.0"
__version__ = "0.5.1"
5 changes: 5 additions & 0 deletions verdin/api/apis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .datasources import DataSourcesApi
from .events import EventsApi
from .pipes import PipesApi
from .query import QueryApi
from .tokens import TokensApi
from .variables import VariablesApi

Expand Down Expand Up @@ -29,6 +30,10 @@ def events(self) -> EventsApi:
def pipes(self) -> PipesApi:
return PipesApi(self._token, self._host)

@property
def query(self) -> QueryApi:
return QueryApi(self._token, self._host)

@property
def tokens(self) -> TokensApi:
return TokensApi(self._token, self._host)
Expand Down
Loading