-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test library execution against Clickhouse engine
There has been a couple of recent issues in which test execution failed in certain scenarios (#48 and #53). These weren't caught by the libraries test as it would require executing actual tests against actual SQL queries. This commit will add a framework for running these tests against a running database engine (Clickhouse in the first instance) to better enable these kinds of bugs to be caught.
- Loading branch information
Showing
6 changed files
with
126 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.DEFAULT_GOAL := help | ||
SHELL := /bin/bash | ||
|
||
.PHONY: help | ||
help: ## Show all available commands | ||
@awk 'BEGIN {FS = ":.*##"; printf "Usage: make \033[36m<target>\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-13s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST); | ||
|
||
.PHONY: test | ||
test: ## Run test pipeline | ||
poetry run pytest tests/ | ||
|
||
.PHONY: test-integration | ||
test-integration: ## Run integration tests | ||
poetry run pytest -m "integration" tests/ | ||
|
||
.PHONY: test-unit | ||
test-unit: ## Run unit tests | ||
poetry run pytest -m "not integration" tests/ |
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,10 @@ | ||
services: | ||
clickhouse: | ||
image: clickhouse/clickhouse-server:24.1.5.6 | ||
ports: | ||
- 8123:8123 | ||
- 9000:9000 | ||
ulimits: | ||
nofile: | ||
soft: "262144" | ||
hard: "262144" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
from sql_mock.clickhouse import column_mocks as col | ||
from sql_mock.clickhouse.table_mocks import ClickHouseTableMock | ||
from sql_mock.table_mocks import table_meta | ||
|
||
pytestmark = pytest.mark.integration | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def set_env(): | ||
if not os.getenv("SQL_MOCK_CLICKHOUSE_HOST"): | ||
os.environ["SQL_MOCK_CLICKHOUSE_HOST"] = "localhost" | ||
if not os.getenv("SQL_MOCK_CLICKHOUSE_PORT"): | ||
os.environ["SQL_MOCK_CLICKHOUSE_PORT"] = "8123" | ||
if not os.getenv("SQL_MOCK_CLICKHOUSE_USER"): | ||
os.environ["SQL_MOCK_CLICKHOUSE_USER"] = "default" | ||
if not os.getenv("SQL_MOCK_CLICKHOUSE_PASSWORD"): | ||
os.environ["SQL_MOCK_CLICKHOUSE_PASSWORD"] = "" | ||
|
||
|
||
def test_simple_query(): | ||
query = """SELECT | ||
user_id, | ||
count() AS sessions | ||
FROM sessions | ||
GROUP BY user_id | ||
""" | ||
|
||
@table_meta(table_ref="sessions") | ||
class SessionsMock(ClickHouseTableMock): | ||
user_id = col.String(default="foo") | ||
|
||
@table_meta(query=query) | ||
class ResultMock(ClickHouseTableMock): | ||
user_id = col.String(default="foo") | ||
sessions = col.Int(default=0) | ||
|
||
sessions_mock = SessionsMock.from_dicts( | ||
[ | ||
{"user_id": "a"}, | ||
{"user_id": "a"}, | ||
{"user_id": "a"}, | ||
{"user_id": "b"}, | ||
], | ||
) | ||
|
||
result = ResultMock.from_mocks(input_data=[sessions_mock]) | ||
|
||
expected = [ | ||
{"user_id": "a", "sessions": 3}, | ||
{"user_id": "b", "sessions": 1}, | ||
] | ||
|
||
result.assert_equal(expected) |