Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pytest-clickhouse): add pytest-clickhouse plugin #844

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions pytest-clickhouse/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2024-present DecFox <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 changes: 21 additions & 0 deletions pytest-clickhouse/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# pytest-clickhouse

[![PyPI - Version](https://img.shields.io/pypi/v/pytest-clickhouse.svg)](https://pypi.org/project/pytest-clickhouse)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pytest-clickhouse.svg)](https://pypi.org/project/pytest-clickhouse)

-----

**Table of Contents**

- [Installation](#installation)
- [License](#license)

## Installation

```console
pip install pytest-clickhouse
```

## License

`pytest-clickhouse` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
Binary file not shown.
Binary file not shown.
87 changes: 87 additions & 0 deletions pytest-clickhouse/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "pytest-clickhouse"
dynamic = ["version"]
description = 'pytest plugin for clickhouse'

dependencies = [
"clickhouse-driver ~= 0.2.6",
"docker ~= 6.1.3",
]

readme = "README.md"
requires-python = ">=3.8"
license = "BSD-3-Clause"
keywords = []
authors = [
{ name = "OONI", email = "[email protected]" },
]
classifiers = [
"Development Status :: 4 - Beta",
"Programming Language :: Python",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]

[project.urls]
Documentation = "https://docs.ooni.org"
Issues = "https://github.com/ooni/backend/issues"
Source = "https://github.com/ooni/backend"

[project.entry-points.pytest11]
pytest_clickhouse = "pytest_clickhouse.plugin"

[tool.hatch.version]
path = "pytest_clickhouse/__about__.py"

[tool.hatch.envs.default]
dependencies = [
"pytest",
"pytest-cov",
"black",
"click",
]
path=".venv/"

[tool.hatch.envs.default.scripts]
test = "pytesli-lt {args:tests}"
test-cov = "pytest -s --full-trace --log-level=INFO --log-cevel=INFO -v --setup-show --cov=./ --cov-report=xml --cov-report=html --cov-report=term {args:tests}"
cov-report = ["coverage report"]
cov = ["test-cov", "cov-report"]

[[tool.hatch.envs.all.matrix]]
python = ["3.8", "3.9", "3.10", "3.11", "3.12"]

[tool.hatch.envs.types]
dependencies = [
"mypy>=1.0.0",
]
[tool.hatch.envs.types.scripts]
check = "mypy --install-types --non-interactive {args:pytest_clickhouse tests}"

[tool.coverage.run]
source_pkgs = ["pytest_clickhouse", "tests"]
branch = true
parallel = true
omit = [
"pytest_clickhouse/__about__.py",
]

[tool.coverage.paths]
pytest_clickhouse = ["pytest_clickhouse", "*/pytest-clickhouse/pytest_clickhouse"]
tests = ["tests", "*/pytest-clickhouse/tests"]

[tool.coverage.report]
exclude_lines = [
"no cov",
"if __name__ == .__main__.:",
"if TYPE_CHECKING:",
]
1 change: 1 addition & 0 deletions pytest-clickhouse/pytest_clickhouse/__about__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.0.1"
Empty file.
32 changes: 32 additions & 0 deletions pytest-clickhouse/pytest_clickhouse/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from pathlib import Path
from typing import TypedDict, Optional, List, Union

from pytest import FixtureRequest


class ClickhouseConfigDict(TypedDict):
"""
Typed Config Dictionary
"""

image_name: str
host: str
port: Optional[str]
dbname: str


def get_config(request: FixtureRequest) -> ClickhouseConfigDict:
"""
Return a dictionary with config options
"""

def get_clickhouse_option(option: str) -> any:
name = f"clickhouse_{option}"
return request.config.getoption(name) or request.config.getini(name)

return ClickhouseConfigDict(
image_name=get_clickhouse_option("image"),
host=get_clickhouse_option("host"),
port=get_clickhouse_option("port"),
dbname=get_clickhouse_option("dbname"),
)
70 changes: 70 additions & 0 deletions pytest-clickhouse/pytest_clickhouse/executor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from typing import Optional
import time

import docker
from docker.models.containers import Container as DockerContainer

class ClickhouseExecutor:
"""
Clickhouse executor running on docker
"""


def __init__(self, image_name: str, dbname: str):
self.image_name = image_name
self.container = None
self.clickhouse_url = ""
self.dbname = dbname


def start(self):
client = docker.from_env()
image_name = self.image_name
client.images.pull(image_name)

# run a container with a random port mapping for ClickHouse default port 9000
container = client.containers.run(
image_name,
ports={
"9000/tcp": None
},
detach=True,
)
assert isinstance(container, DockerContainer)
self.container = container

# obtain the port mapping
container.reload()
assert isinstance(container.attrs, dict)
host_port = container.attrs["NetworkSettings"]["Ports"]["9000/tcp"][0]["HostPort"]

# construct the connection string
clickhouse_url = f"clickhouse://localhost:{host_port}"
self.clickhouse_url = clickhouse_url


def wait_for_clickhouse(self):
while 1:
if self.running():
break
time.sleep(1)


def running(self) -> bool:
if self.container and self.container.status == 'running':
return True
return False


def terminate(self):
if self.container:
self.container.stop()
self.container.remove()


def __enter__(self):
self.start()


def __exit__(self):
self.terminate()
20 changes: 20 additions & 0 deletions pytest-clickhouse/pytest_clickhouse/executor_noop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import Optional, Union

class ClickhouseNoopExecutor:
"""
Clickhouse nooperator executor
"""


def __init__(
self,
host: str,
port: Union[str, int],
dbname: str
):
self.host = host
self.port = int(port)
self.clickhouse_url = f"clickhouse://{self.host}:{self.port}"
self.dbname = dbname


5 changes: 5 additions & 0 deletions pytest-clickhouse/pytest_clickhouse/factories/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pytest_clickhouse.factories.client import clickhouse
from pytest_clickhouse.factories.process import clickhouse_proc
from pytest_clickhouse.factories.noprocess import clickhouse_noproc

__all__ = {"clickhouse_proc", "clickhouse_noproc", "clickhouse"}
28 changes: 28 additions & 0 deletions pytest-clickhouse/pytest_clickhouse/factories/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import Callable, Union, Optional, List

import pytest
from pytest import FixtureRequest
from clickhouse_driver import Client as Clickhouse

from pytest_clickhouse.executor import ClickhouseExecutor
from pytest_clickhouse.executor_noop import ClickhouseNoopExecutor
from pytest_clickhouse.janitor import ClickhouseJanitor

def clickhouse(
process_fixture_name: str,
dbname: Optional[str] = None,
url: Optional[str] = None,
) -> Callable[[FixtureRequest], Clickhouse]:

@pytest.fixture
def clickhouse_factory(request: FixtureRequest) -> Clickhouse:
proc_fixture: Union[ClickhouseExecutor, ClickhouseNoopExecutor] = request.getfixturevalue(
process_fixture_name
)

clickhouse_url = url or proc_fixture.clickhouse_url

client = Clickhouse.from_url(clickhouse_url)
yield client

return clickhouse_factory
34 changes: 34 additions & 0 deletions pytest-clickhouse/pytest_clickhouse/factories/noprocess.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import Callable, Optional

import pytest
from pytest import FixtureRequest

from pytest_clickhouse.config import get_config
from pytest_clickhouse.executor_noop import ClickhouseNoopExecutor
from pytest_clickhouse.janitor import ClickhouseJanitor

def clickhouse_noproc(
host: Optional[str] = None,
port: Optional[str] = None,
dbname: Optional[str] = None,
) -> Callable[[FixtureRequest], ClickhouseNoopExecutor]:

@pytest.fixture(scope="session")
def clickhouse_noproc_fixture(
request: FixtureRequest
) -> ClickhouseNoopExecutor:
config = get_config(request)
clickhouse_noop_exec = ClickhouseNoopExecutor(
host=host or config["host"],
port=port or config["port"],
dbname=dbname or config["dbname"],
)

with clickhouse_noop_exec:
with ClickhouseJanitor(
dbname=clickhouse_noop_exec.dbname,
clickhouse_url=clickhouse_noop_exec.clickhouse_url
):
yield clickhouse_noop_exec

return clickhouse_noproc_fixture
33 changes: 33 additions & 0 deletions pytest-clickhouse/pytest_clickhouse/factories/process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from typing import Callable, Iterator, Optional

import pytest
from pytest import FixtureRequest

from pytest_clickhouse.executor import ClickhouseExecutor
from pytest_clickhouse.config import get_config
from pytest_clickhouse.janitor import ClickhouseJanitor

def clickhouse_proc(
image_name: Optional[str] = None,
dbname: Optional[str] = None,
) -> Callable[[FixtureRequest], ClickhouseExecutor]:

@pytest.fixture(scope="session")
def clickhouse_proc_fixture(
request: FixtureRequest
) -> ClickhouseExecutor:
config = get_config(request)
clickhouse_executor = ClickhouseExecutor(
image_name=image_name or config["image_name"],
dbname=dbname or config["dbname"]
)

with clickhouse_executor:
clickhouse_executor.wait_for_clickhouse()
with ClickhouseJanitor(
dbname=clickhouse_executor.dbname,
clickhouse_url=clickhouse_executor.clickhouse_url
):
yield clickhouse_executor

return clickhouse_proc_fixture
44 changes: 44 additions & 0 deletions pytest-clickhouse/pytest_clickhouse/janitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from contextlib import contextmanager

from clickhouse_driver import Client as Clickhouse

class ClickhouseJanitor:
"""
Manage clickhouse database state
"""


def __init__(self, dbname: str, clickhouse_url: str):
self.dbname = dbname
self.clickhouse_url = clickhouse_url
self.click = None


def init(self):
"""
Initialize client and test database in clickhouse
"""
self.click: Clickhouse = Clickhouse.from_url(self.clickhouse_url)
query = f"""
CREATE DATABASE IF NOT EXISTS {self.dbname}
COMMENT 'test database'
"""
self.click.execute(query, {})


def drop(self):
"""
Drop test database
"""
query = f"""
DROP DATABASE IF EXISTS {self.dbname}
"""
self.click.execute(query, {})


def __enter__(self):
self.init()


def __exit__(self):
self.drop()
Loading
Loading