Skip to content

Commit cafbf96

Browse files
authored
♻️ Cleanup of setup and tests tooling (#5446)
1 parent 1c8189c commit cafbf96

File tree

20 files changed

+483
-228
lines changed

20 files changed

+483
-228
lines changed

api/tests/test_individual_openapi_schemas.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
from pathlib import Path
88

99
import pytest
10-
from openapi_spec_validator import validate_spec
11-
from openapi_spec_validator.exceptions import OpenAPISpecValidatorError
1210
from utils import dump_specs, is_json_schema, is_openapi_schema, load_specs
1311

1412
# Conventions
@@ -132,15 +130,3 @@ def converted_specs_testdir(api_specs_dir, all_api_specs_tails, tmpdir_factory):
132130
shutil.copy2(basedir / tail, testdir / tail)
133131

134132
return testdir
135-
136-
137-
@pytest.mark.skip(reason="Implementing in PR 324")
138-
def test_valid_individual_openapi_specs(api_specs_tail, converted_specs_testdir):
139-
# NOTE: api_specs_tail is a parametrized **fixture**
140-
#
141-
api_specs_path = converted_specs_testdir / api_specs_tail
142-
try:
143-
specs = load_specs(api_specs_path)
144-
validate_spec(specs, spec_url=api_specs_path.as_uri())
145-
except OpenAPISpecValidatorError as err:
146-
pytest.fail(f"Failed validating {api_specs_path}:\n{err.message}")

packages/pytest-simcore/src/pytest_simcore/helpers/utils_docker.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import docker
1111
import yaml
12-
from pytest_simcore.helpers.typing_env import EnvVarsDict
1312
from tenacity import retry
1413
from tenacity.after import after_log
1514
from tenacity.stop import stop_after_attempt
@@ -61,17 +60,19 @@ def get_service_published_port(
6160

6261
services = [s for s in client.services.list() if str(s.name).endswith(service_name)]
6362
if not services:
64-
raise RuntimeError(
63+
msg = (
6564
f"Cannot find published port for service '{service_name}'."
6665
"Probably services still not started."
6766
)
67+
raise RuntimeError(msg)
6868

6969
service_ports = services[0].attrs["Endpoint"].get("Ports")
7070
if not service_ports:
71-
raise RuntimeError(
71+
msg = (
7272
f"Cannot find published port for service '{service_name}' in endpoint."
7373
"Probably services still not started."
7474
)
75+
raise RuntimeError(msg)
7576

7677
published_port = None
7778
msg = ", ".join(
@@ -89,7 +90,7 @@ def get_service_published_port(
8990

9091
else:
9192
ports_to_look_for: list = (
92-
[target_ports] if isinstance(target_ports, (int, str)) else target_ports
93+
[target_ports] if isinstance(target_ports, int | str) else target_ports
9394
)
9495

9596
for target_port in ports_to_look_for:
@@ -100,7 +101,8 @@ def get_service_published_port(
100101
break
101102

102103
if published_port is None:
103-
raise RuntimeError(f"Cannot find published port for {target_ports}. Got {msg}")
104+
msg = f"Cannot find published port for {target_ports}. Got {msg}"
105+
raise RuntimeError(msg)
104106

105107
return str(published_port)
106108

@@ -111,7 +113,6 @@ def run_docker_compose_config(
111113
project_dir: Path,
112114
env_file_path: Path,
113115
destination_path: Path | None = None,
114-
additional_envs: EnvVarsDict | None = None,
115116
) -> dict:
116117
"""Runs docker compose config to validate and resolve a compose file configuration
117118
@@ -140,13 +141,12 @@ def run_docker_compose_config(
140141
], "Expected yaml/yml file as destination path"
141142

142143
# SEE https://docs.docker.com/compose/reference/
143-
144-
global_options = [
144+
bash_options = [
145145
"-p",
146146
str(project_dir), # Specify an alternate working directory
147147
]
148148
# https://docs.docker.com/compose/environment-variables/#using-the---env-file--option
149-
global_options += [
149+
bash_options += [
150150
"-e",
151151
str(env_file_path), # Custom environment variables
152152
]
@@ -155,26 +155,22 @@ def run_docker_compose_config(
155155
# - When you use multiple Compose files, all paths in the files are relative to the first configuration file specified with -f.
156156
# You can use the --project-directory option to override this base path.
157157
for docker_compose_path in docker_compose_paths:
158-
global_options += [os.path.relpath(docker_compose_path, project_dir)]
158+
bash_options += [os.path.relpath(docker_compose_path, project_dir)]
159159

160160
# SEE https://docs.docker.com/compose/reference/config/
161161
docker_compose_path = scripts_dir / "docker" / "docker-compose-config.bash"
162162
assert docker_compose_path.exists()
163163

164-
cmd = [f"{docker_compose_path}"] + global_options
165-
print(" ".join(cmd))
166-
167-
process_environment_variables = dict(os.environ)
168-
if additional_envs:
169-
process_environment_variables |= additional_envs
164+
args = [f"{docker_compose_path}", *bash_options]
165+
print(" ".join(args))
170166

171167
process = subprocess.run(
172-
cmd,
168+
args,
173169
shell=False,
174-
check=True,
175170
cwd=project_dir,
176171
capture_output=True,
177-
env=process_environment_variables,
172+
check=True,
173+
env=None, # NOTE: Do not use since since we pass all necessary env vars via --env-file option of docker compose
178174
)
179175

180176
compose_file_str = process.stdout.decode("utf-8")

packages/pytest-simcore/src/pytest_simcore/helpers/utils_postgres.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
2+
from collections.abc import Iterator
23
from contextlib import contextmanager
3-
from typing import Iterator, TypedDict
4+
from typing import TypedDict
45

56
import simcore_postgres_database.cli
67
import sqlalchemy as sa

packages/service-library/setup.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ def read_reqs(reqs_path: Path) -> set[str]:
3030

3131

3232
SETUP = {
33+
"name": "simcore-service-library",
34+
"version": Path(CURRENT_DIR / "VERSION").read_text().strip(),
3335
"author": "Pedro Crespo-Valero (pcrespov)",
3436
"description": "Core service library for simcore (or servicelib)",
37+
"license": "MIT license",
38+
"python_requires": "~=3.10",
39+
"install_requires": tuple(PROD_REQUIREMENTS),
40+
"packages": find_packages(where="src"),
41+
"package_dir": {"": "src"},
42+
"test_suite": "tests",
43+
"tests_require": tuple(TEST_REQUIREMENTS),
3544
"extras_require": {
3645
"aiohttp": tuple(AIOHTTP_REQUIREMENTS),
37-
"all": tuple(AIOHTTP_REQUIREMENTS | FASTAPI_REQUIREMENTS),
3846
"fastapi": tuple(FASTAPI_REQUIREMENTS),
47+
"all": tuple(AIOHTTP_REQUIREMENTS | FASTAPI_REQUIREMENTS),
3948
"test": tuple(TEST_REQUIREMENTS),
4049
},
41-
"install_requires": tuple(PROD_REQUIREMENTS),
42-
"license": "MIT license",
43-
"name": "simcore-service-library",
44-
"package_dir": {"": "src"},
45-
"packages": find_packages(where="src"),
46-
"python_requires": "~=3.10",
47-
"test_suite": "tests",
48-
"tests_require": tuple(TEST_REQUIREMENTS),
49-
"version": Path(CURRENT_DIR / "VERSION").read_text().strip(),
5050
}
5151

5252

packages/service-library/tests/fastapi/test_httpx_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ async def test_to_curl_command(client: AsyncClient):
6262

6363
assert (
6464
cmd_short
65-
== f'curl -X POST -H "host: test_base_http_api" -H "accept: */*" -H "accept-encoding: gzip, deflate" -H "connection: keep-alive" -H "user-agent: python-httpx/0.25.0" -H "x-secret: {_PLACEHOLDER}" -H "content-length: 9" -H "content-type: application/json" -d \'{{"y": 12}}\' https://test_base_http_api/foo?x=3'
65+
== f'curl -X POST -H "host: test_base_http_api" -H "accept: */*" -H "accept-encoding: gzip, deflate" -H "connection: keep-alive" -H "user-agent: python-httpx/{httpx.__version__}" -H "x-secret: {_PLACEHOLDER}" -H "content-length: 9" -H "content-type: application/json" -d \'{{"y": 12}}\' https://test_base_http_api/foo?x=3'
6666
)
6767

6868
cmd_long = to_curl_command(response.request, use_short_options=False)
@@ -81,14 +81,14 @@ async def test_to_curl_command(client: AsyncClient):
8181
assert (
8282
cmd_multiline
8383
== textwrap.dedent(
84-
"""\
84+
f"""\
8585
curl \\
8686
-X GET \\
8787
-H "host: test_base_http_api" \\
8888
-H "accept: */*" \\
8989
-H "accept-encoding: gzip, deflate" \\
9090
-H "connection: keep-alive" \\
91-
-H "user-agent: python-httpx/0.25.0" \\
91+
-H "user-agent: python-httpx/{httpx.__version__}" \\
9292
https://test_base_http_api/foo?x=3
9393
"""
9494
).strip()
@@ -115,5 +115,5 @@ async def test_to_httpx_command(client: AsyncClient):
115115
print(cmd_short)
116116
assert (
117117
cmd_short
118-
== f'httpx -m POST -c \'{{"y": 12}}\' -h "host" "test_base_http_api" -h "accept" "*/*" -h "accept-encoding" "gzip, deflate" -h "connection" "keep-alive" -h "user-agent" "python-httpx/0.25.0" -h "x-secret" "{_PLACEHOLDER}" -h "content-length" "9" -h "content-type" "application/json" https://test_base_http_api/foo?x=3'
118+
== f'httpx -m POST -c \'{{"y": 12}}\' -h "host" "test_base_http_api" -h "accept" "*/*" -h "accept-encoding" "gzip, deflate" -h "connection" "keep-alive" -h "user-agent" "python-httpx/{httpx.__version__}" -h "x-secret" "{_PLACEHOLDER}" -h "content-length" "9" -h "content-type" "application/json" https://test_base_http_api/foo?x=3'
119119
)

packages/simcore-sdk/setup.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import re
22
import sys
33
from pathlib import Path
4-
from typing import Set
54

65
from setuptools import find_packages, setup
76

87

9-
def read_reqs(reqs_path: Path) -> Set[str]:
8+
def read_reqs(reqs_path: Path) -> set[str]:
109
return {
1110
r
1211
for r in re.findall(
@@ -31,17 +30,17 @@ def read_reqs(reqs_path: Path) -> Set[str]:
3130
}
3231
)
3332

34-
SETUP = dict(
35-
name="simcore-sdk",
36-
version=Path(CURRENT_DIR / "VERSION").read_text().strip(),
37-
packages=find_packages(where="src"),
38-
package_dir={"": "src"},
39-
python_requires=">=3.6",
40-
install_requires=INSTALL_REQUIREMENTS,
41-
tests_require=TEST_REQUIREMENTS,
42-
extras_require={"test": TEST_REQUIREMENTS},
43-
test_suite="tests",
44-
)
33+
SETUP = {
34+
"name": "simcore-sdk",
35+
"version": Path(CURRENT_DIR / "VERSION").read_text().strip(),
36+
"packages": find_packages(where="src"),
37+
"package_dir": {"": "src"},
38+
"python_requires": ">=3.6",
39+
"install_requires": INSTALL_REQUIREMENTS,
40+
"tests_require": TEST_REQUIREMENTS,
41+
"extras_require": {"test": TEST_REQUIREMENTS},
42+
"test_suite": "tests",
43+
}
4544

4645

4746
if __name__ == "__main__":

packages/simcore-sdk/tests/conftest.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020

2121
pytest_plugins = [
22-
"pytest_simcore.aws_server",
2322
"pytest_simcore.aws_s3_service",
23+
"pytest_simcore.aws_server",
2424
"pytest_simcore.docker_compose",
2525
"pytest_simcore.docker_swarm",
2626
"pytest_simcore.file_extra",
@@ -42,30 +42,6 @@ def package_dir():
4242
return pdir
4343

4444

45-
@pytest.fixture(scope="session")
46-
def osparc_simcore_root_dir() -> Path:
47-
"""osparc-simcore repo root dir"""
48-
WILDCARD = "packages/simcore-sdk"
49-
50-
root_dir = Path(current_dir)
51-
while not any(root_dir.glob(WILDCARD)) and root_dir != Path("/"):
52-
root_dir = root_dir.parent
53-
54-
msg = f"'{root_dir}' does not look like the git root directory of osparc-simcore"
55-
assert root_dir.exists(), msg
56-
assert any(root_dir.glob(WILDCARD)), msg
57-
assert any(root_dir.glob(".git")), msg
58-
59-
return root_dir
60-
61-
62-
@pytest.fixture(scope="session")
63-
def env_devel_file(osparc_simcore_root_dir) -> Path:
64-
env_devel_fpath = osparc_simcore_root_dir / ".env-devel"
65-
assert env_devel_fpath.exists()
66-
return env_devel_fpath
67-
68-
6945
@pytest.fixture(scope="session")
7046
def default_configuration_file() -> Path:
7147
path = current_dir / "mock" / "default_config.json"
@@ -75,8 +51,7 @@ def default_configuration_file() -> Path:
7551

7652
@pytest.fixture(scope="session")
7753
def default_configuration(default_configuration_file: Path) -> dict[str, Any]:
78-
config = json.loads(default_configuration_file.read_text())
79-
return config
54+
return json.loads(default_configuration_file.read_text())
8055

8156

8257
@pytest.fixture(scope="session")

packages/simcore-sdk/tests/integration/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ def user_id(postgres_db: sa.engine.Engine) -> Iterable[UserID]:
3939
# which would turn this test too complex.
4040

4141
# pylint: disable=no-value-for-parameter
42-
stmt = users.insert().values(**random_user(name="test")).returning(users.c.id)
43-
print(f"{stmt}")
4442
with postgres_db.connect() as conn:
45-
result = conn.execute(stmt)
43+
result = conn.execute(
44+
users.insert().values(**random_user(name="test")).returning(users.c.id)
45+
)
4646
row = result.first()
4747
assert row
4848
usr_id = row[users.c.id]

0 commit comments

Comments
 (0)