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

chore: pytest path cleanup and port management #2734

Merged
merged 4 commits into from
Mar 5, 2024
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
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ venv:
pytest *args:
{{VENV_BIN}}/poetry -C tests install --no-root
{{VENV_BIN}}/poetry -C tests lock --no-update
{{VENV_BIN}}/poetry -C tests run pytest --rootdir={{invocation_directory()}}/tests {{ if args == "" {'tests'} else {args} }}
{{VENV_BIN}}/poetry -C tests run pytest -n auto -v --rootdir={{invocation_directory()}}/tests {{ if args == "" {'tests'} else {args} }}

# private helpers below
# ---------------------
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/dbt_project/profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ glaredb_dbt_test:
dbname: default
host: 127.0.0.1
pass: ""
port: 5432
port: "{{ env_var('GLAREDB_PORT') | as_number }}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a welcome improvement.

schema: public
threads: 1
type: postgres
user: "{{ env_var('DBT_USER') }}"
target: test_target
target: test_target
27 changes: 20 additions & 7 deletions tests/fixtures/glaredb.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
import socket
import contextlib
import pathlib
import time
import subprocess
import sys
import logging

import pytest
import psycopg2

import tests

logger = logging.getLogger("fixtures")


@pytest.fixture
def release_path() -> pathlib.Path:
return tests.PKG_DIRECTORY.joinpath("target", "release", "glaredb")
def glaredb_path() -> list[pathlib.Path]:
return [
tests.PKG_DIRECTORY.joinpath("target", "release", "glaredb"),
tests.PKG_DIRECTORY.joinpath("target", "debug", "glaredb"),
]


@pytest.fixture
def debug_path() -> pathlib.Path:
return tests.PKG_DIRECTORY.joinpath("target", "debug", "glaredb")
def binary_path(glaredb_path: list[pathlib.Path]) -> pathlib.Path:
return glaredb_path[0] if glaredb_path[0].exists() else glaredb_path[1]


@pytest.fixture
def glaredb_connection(
debug_path: pathlib.Path,
binary_path: list[pathlib.Path],
tmp_path_factory: pytest.TempPathFactory,
) -> psycopg2.extensions.connection:
addr = ("127.0.0.1", "5432")
with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(("", 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
addr = ("127.0.0.1", str(s.getsockname()[1]))

logger.info(f"starting glaredb on port {addr[0]}")

with subprocess.Popen(
[
debug_path.absolute(),
binary_path.absolute(),
"--verbose",
"server",
"--data-dir",
Expand Down
36 changes: 35 additions & 1 deletion tests/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pylance = "^0.9.6"
ruff = "0.1.14"
dbt-core = "^1.7.7"
dbt-postgres = "^1.7.7"
pytest-xdist = "^3.5.0"

[build-system]
requires = ["poetry-core"]
Expand Down
6 changes: 3 additions & 3 deletions tests/test_bson.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import psycopg2.extras
import pytest

from tests.fixtures.glaredb import glaredb_connection, debug_path
from tests.fixtures.glaredb import glaredb_connection, glaredb_path, binary_path
import tests.tools


def test_bson_copy_to(
glaredb_connection: psycopg2.extensions.connection,
debug_path: pathlib.Path,
binary_path: pathlib.Path,
tmp_path_factory: pytest.TempPathFactory,
):
curr = glaredb_connection.cursor()
Expand Down Expand Up @@ -48,7 +48,7 @@ def test_bson_copy_to(
with tests.tools.cd(output_dir):
out = subprocess.run(
[
f"{debug_path}",
f"{binary_path}",
"--query",
f"select count(*) as count from '{output_fn}'",
"--mode",
Expand Down
12 changes: 9 additions & 3 deletions tests/test_dbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest

import tests.tools
from tests.fixtures.glaredb import glaredb_connection, debug_path
from tests.fixtures.glaredb import glaredb_connection, glaredb_path, binary_path


@pytest.fixture
Expand Down Expand Up @@ -37,7 +37,10 @@ def test_dbt_glaredb(
curr.execute("SELECT * FROM public.dbt_test")
a = curr.fetchall()

with tests.tools.env("DBT_USER", glaredb_connection.info.user):
with (
tests.tools.env("GLAREDB_PORT", str(glaredb_connection.info.port)),
tests.tools.env("DBT_USER", glaredb_connection.info.user),
):
res: dbtRunnerResult = dbtRunner().invoke(
[
"run",
Expand Down Expand Up @@ -80,7 +83,10 @@ def test_dbt_glaredb_external_postgres(
"""
)

with tests.tools.env("DBT_USER", glaredb_connection.info.user):
with (
tests.tools.env("GLAREDB_PORT", str(glaredb_connection.info.port)),
tests.tools.env("DBT_USER", glaredb_connection.info.user),
):
res: dbtRunnerResult = dbtRunner().invoke(
[
"run",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import psycopg2

from tests.fixtures.glaredb import glaredb_connection, debug_path
from tests.fixtures.glaredb import glaredb_connection, glaredb_path, binary_path


def test_scalar_parsing(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import psycopg2.extras
import pytest

from tests.fixtures.glaredb import glaredb_connection, debug_path
from tests.fixtures.glaredb import glaredb_connection, glaredb_path, binary_path


@pytest.fixture
Expand Down
3 changes: 1 addition & 2 deletions tests/test_lance.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import psycopg2.extras
import pytest


import tests.tools
from tests.fixtures.glaredb import glaredb_connection, debug_path
from tests.fixtures.glaredb import glaredb_connection, glaredb_path, binary_path


def test_sanity_check(
Expand Down
19 changes: 8 additions & 11 deletions tests/test_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@
import pytest
import psycopg2

from tests.fixtures.glaredb import glaredb_connection, release_path, debug_path
from tests.fixtures.glaredb import glaredb_connection, glaredb_path, binary_path


def test_release_exists(release_path: pathlib.Path):
assert not release_path.exists() # the release binary does not exist
def test_binary_exists(glaredb_path: list[pathlib.Path]):
assert len(glaredb_path) == 2
assert glaredb_path[0].exists() or glaredb_path[1].exists()


def test_debug_exists(debug_path: pathlib.Path):
assert debug_path.exists() # the debug binary exists


def test_debug_executes(debug_path: pathlib.Path):
def test_binary_executes(binary_path: list[pathlib.Path]):
# run the binary and see if it returns:
assert subprocess.check_call([debug_path.absolute(), "-q", "SELECT 1;"]) == 0
assert subprocess.check_call([binary_path.absolute(), "-q", "SELECT 1;"]) == 0


def test_start(
Expand All @@ -29,14 +26,14 @@ def test_start(


@pytest.mark.skipif(not sys.platform.startswith("linux"), reason="linux version of the test")
def test_expected_linking_linux(debug_path: pathlib.Path):
def test_expected_linking_linux(binary_path: pathlib.Path):
out = [
ll
for cell in [
item
for item in [
line.split(" ")
for line in str(subprocess.check_output(["ldd", debug_path.absolute()], text=True))
for line in str(subprocess.check_output(["ldd", binary_path.absolute()], text=True))
.replace("\t", "")
.split("\n")
]
Expand Down