Skip to content

Commit 2e76806

Browse files
committed
Added test actions
1 parent 1fcca9a commit 2e76806

File tree

6 files changed

+249
-7
lines changed

6 files changed

+249
-7
lines changed
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@ name: CI
22

33
on:
44
push:
5-
branches:
6-
- main
7-
- master
85
tags:
96
- '*'
10-
pull_request:
11-
workflow_dispatch:
127

138
permissions:
149
contents: read

.github/workflows/test.yaml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
name: 'Testing package'
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
py-lint:
8+
strategy:
9+
matrix:
10+
cmd:
11+
- black
12+
- isort
13+
- ruff
14+
- mypy
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: Set up Python
19+
uses: actions/setup-python@v4
20+
with:
21+
python-version: "3.12"
22+
- name: Run lint check
23+
uses: pre-commit/action@v3.0.0
24+
with:
25+
extra_args: -a ${{ matrix.cmd }}
26+
fmt:
27+
runs-on: ubuntu-latest
28+
steps:
29+
- uses: actions/checkout@v1
30+
- uses: actions-rs/toolchain@v1
31+
with:
32+
toolchain: stable
33+
components: rustfmt
34+
override: true
35+
- name: Check code format
36+
run: cargo fmt -- --check --config use_try_shorthand=true,imports_granularity=Crate
37+
38+
clippy:
39+
permissions:
40+
checks: write
41+
runs-on: ubuntu-latest
42+
steps:
43+
- uses: actions/checkout@v1
44+
- uses: actions-rs/toolchain@v1
45+
with:
46+
toolchain: stable
47+
components: clippy
48+
override: true
49+
- uses: actions-rs/clippy-check@v1
50+
with:
51+
token: ${{ secrets.GITHUB_TOKEN }}
52+
args: -p psqlpy --all-features -- -W clippy::all -W clippy::pedantic -D warnings
53+
pytest:
54+
name: ${{matrix.job.os}}-${{matrix.py_version}}
55+
services:
56+
scylla:
57+
image: scylladb/scylla:5.2
58+
options: >-
59+
--health-cmd="cqlsh -e 'select * from system.local' "
60+
--health-interval=5s
61+
--health-timeout=5s
62+
--health-retries=60
63+
ports:
64+
- 9042:9042
65+
strategy:
66+
matrix:
67+
py_version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
68+
job:
69+
- os: ubuntu-latest
70+
ssl_cmd: sudo apt-get update && sudo apt-get install libssl-dev openssl
71+
runs-on: ${{matrix.job.os}}
72+
steps:
73+
- uses: actions/checkout@v1
74+
- uses: ikalnytskyi/action-setup-postgres@v4
75+
with:
76+
username: postgres
77+
password: postgres
78+
database: psqlpy_test
79+
id: postgres
80+
- uses: actions-rs/toolchain@v1
81+
with:
82+
toolchain: stable
83+
components: clippy
84+
override: true
85+
- name: Setup OpenSSL
86+
run: ${{matrix.job.ssl_cmd}}
87+
- name: Setup python for test ${{ matrix.py_version }}
88+
uses: actions/setup-python@v4
89+
with:
90+
python-version: ${{ matrix.py_version }}
91+
- name: Install tox
92+
run: pip install "tox-gh>=1.2,<2"
93+
- name: Run pytest
94+
run: tox -v
95+

python/psqlpy/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
from ._internal import IsolationLevel, PSQLPool, QueryResult, ReadVariant, Transaction
1+
from ._internal import (
2+
Connection,
3+
IsolationLevel,
4+
PSQLPool,
5+
QueryResult,
6+
ReadVariant,
7+
Transaction,
8+
)
29

310
__all__ = [
411
"PSQLPool",
512
"QueryResult",
613
"Transaction",
714
"IsolationLevel",
815
"ReadVariant",
16+
"Connection",
917
]

python/tests/conftest.py

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import os
2+
import random
3+
from typing import AsyncGenerator
4+
15
import pytest
26

7+
from psqlpy import PSQLPool
8+
39

4-
@pytest.fixture(scope="session")
10+
@pytest.fixture()
511
def anyio_backend() -> str:
612
"""
713
Anyio backend.
@@ -10,3 +16,83 @@ def anyio_backend() -> str:
1016
:return: backend name.
1117
"""
1218
return "asyncio"
19+
20+
21+
def random_string(length: int = 10) -> str:
22+
return "".join(random.choice("AbCdEfG") for _ in range(length))
23+
24+
25+
@pytest.fixture()
26+
def postgres_host() -> str:
27+
return os.environ.get("POSTGRES_HOST", "localhost")
28+
29+
30+
@pytest.fixture()
31+
def postgres_user() -> str:
32+
return os.environ.get("POSTGRES_USER", "postgres")
33+
34+
35+
@pytest.fixture()
36+
def postgres_password() -> str:
37+
return os.environ.get("POSTGRES_PASSWORD", "postgres")
38+
39+
40+
@pytest.fixture()
41+
def postgres_port() -> int:
42+
return int(os.environ.get("POSTGRES_PORT", 5432))
43+
44+
45+
@pytest.fixture()
46+
def postgres_dbname() -> str:
47+
return os.environ.get("POSTGRES_DBNAME", "psqlpy_test")
48+
49+
50+
@pytest.fixture()
51+
def table_name() -> str:
52+
return random_string()
53+
54+
55+
@pytest.fixture()
56+
def number_database_records() -> int:
57+
return random.randint(10, 35)
58+
59+
60+
@pytest.fixture()
61+
async def psql_pool(
62+
postgres_host: str,
63+
postgres_user: str,
64+
postgres_password: str,
65+
postgres_port: int,
66+
postgres_dbname: str,
67+
) -> AsyncGenerator[PSQLPool, None]:
68+
pg_pool = PSQLPool(
69+
username=postgres_user,
70+
password=postgres_password,
71+
host=postgres_host,
72+
port=postgres_port,
73+
db_name=postgres_dbname,
74+
)
75+
await pg_pool.startup()
76+
yield pg_pool
77+
78+
79+
@pytest.fixture(autouse=True)
80+
async def create_deafult_data_for_tests(
81+
psql_pool: PSQLPool,
82+
table_name: str,
83+
number_database_records: int,
84+
) -> AsyncGenerator[None, None]:
85+
await psql_pool.execute(
86+
f"CREATE TABLE {table_name} (id SERIAL, name VARCHAR(255))",
87+
)
88+
89+
for table_id in range(1, number_database_records + 1):
90+
new_name = random_string()
91+
await psql_pool.execute(
92+
querystring=f"INSERT INTO {table_name} VALUES ($1, $2)",
93+
parameters=[table_id, new_name],
94+
)
95+
yield
96+
await psql_pool.execute(
97+
f"DROP TABLE {table_name}",
98+
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
3+
from psqlpy import Connection, PSQLPool, QueryResult
4+
5+
6+
@pytest.mark.anyio
7+
async def test_pool_execute(
8+
psql_pool: PSQLPool,
9+
table_name: str,
10+
number_database_records: int,
11+
) -> None:
12+
"""Test that PSQLPool can execute queries."""
13+
select_result = await psql_pool.execute(
14+
f"SELECT * FROM {table_name}",
15+
)
16+
17+
assert type(select_result) == QueryResult
18+
19+
inner_result = select_result.result()
20+
assert isinstance(inner_result, list)
21+
assert len(inner_result) == number_database_records
22+
23+
24+
@pytest.mark.anyio
25+
async def test_pool_connection(
26+
psql_pool: PSQLPool,
27+
) -> None:
28+
"""Test that PSQLPool can return single connection from the pool."""
29+
connection = await psql_pool.connection()
30+
assert isinstance(connection, Connection)

tox.ini

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[tox]
2+
isolated_build = true
3+
env_list =
4+
py312
5+
py311
6+
py310
7+
py39
8+
py38
9+
10+
[gh]
11+
python =
12+
3.12 = py312
13+
3.11 = py311
14+
3.10 = py310
15+
3.9 = py39
16+
3.8 = py38
17+
18+
[testenv]
19+
skip_install = true
20+
deps =
21+
pytest>=7,<8
22+
anyio>=3,<4
23+
maturin>=1,<2
24+
allowlist_externals = maturin
25+
commands_pre =
26+
maturin develop
27+
commands =
28+
pytest -vv

0 commit comments

Comments
 (0)