Skip to content

Commit

Permalink
moved tests folder and cleaned-up fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Apr 22, 2021
1 parent 89db460 commit 20731fa
Show file tree
Hide file tree
Showing 20 changed files with 61 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ def osparc_simcore_root_dir(request) -> Path:
return root_dir


@pytest.fixture(scope="session")
def osparc_simcore_api_specs_dir(osparc_simcore_root_dir) -> Path:
dirpath = osparc_simcore_root_dir / "api" / "specs"
assert dirpath.exists()
return dirpath


@pytest.fixture(scope="session")
def osparc_simcore_services_dir(osparc_simcore_root_dir) -> Path:
services_dir = osparc_simcore_root_dir / "services"
Expand Down
5 changes: 5 additions & 0 deletions scripts/common-service.Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export APP_VERSION
# COMMON TASKS
#

.env: .env-devel ## creates .env file from defaults in .env-devel
$(if $(wildcard $@), \
@echo "WARNING ##### $< is newer than $@ ####"; diff -uN $@ $<; false;,\
@echo "INFO $@ does not exist, cloning $< as $@"; cp $< $@)


.PHONY: install-dev install-prod install-ci

Expand Down
7 changes: 0 additions & 7 deletions services/storage/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,8 @@ openapi-specs: ## updates and validates openapi specifications
$(MAKE) -C $(CURDIR)/src/simcore_service_${APP_NAME}/api $@


.PHONY: tests
tests: ## runs unit tests
# running unit tests
@pytest -vv --exitfirst --failed-first --durations=10 --pdb $(CURDIR)/tests


# DEVELOPMENT ########
.env:
cp .env-devel $@

run-devel: .env
# start app (within $<) in prod mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
def create_storage_manager(
request: Request,
):

# TODO: minimum to request

DataStorageManager()


Expand Down
58 changes: 17 additions & 41 deletions services/storage/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

import datetime
import os
import sys
import uuid
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from random import randrange
Expand Down Expand Up @@ -39,41 +37,9 @@

pytest_plugins = [
"tests.fixtures.data_models",
"pytest_simcore.repository_paths",
]

CURRENT_DIR = Path(sys.argv[0] if __name__ == "__main__" else __file__).resolve().parent

# TODO: replace by pytest_simcore
sys.path.append(str(CURRENT_DIR / "helpers"))


@pytest.fixture(scope="session")
def here() -> Path:
return CURRENT_DIR


@pytest.fixture(scope="session")
def package_dir(here) -> Path:
dirpath = Path(simcore_service_storage.__file__).parent
assert dirpath.exists()
return dirpath


@pytest.fixture(scope="session")
def osparc_simcore_root_dir(here) -> Path:
root_dir = here.parent.parent.parent
assert root_dir.exists() and any(
root_dir.glob("services")
), "Is this service within osparc-simcore repo?"
return root_dir


@pytest.fixture(scope="session")
def osparc_api_specs_dir(osparc_simcore_root_dir) -> Path:
dirpath = osparc_simcore_root_dir / "api" / "specs"
assert dirpath.exists()
return dirpath


@pytest.fixture(scope="session")
def project_slug_dir(osparc_simcore_root_dir) -> Path:
Expand All @@ -84,6 +50,16 @@ def project_slug_dir(osparc_simcore_root_dir) -> Path:
return service_folder


@pytest.fixture(scope="session")
def package_dir() -> Path:
"""Notice that this might be under src (if installed as edit mode)
or in the installation folder
"""
dirpath = Path(simcore_service_storage.__file__).resolve().parent
assert dirpath.exists()
return dirpath


@pytest.fixture(scope="session")
def project_env_devel_dict(project_slug_dir: Path) -> Dict:
env_devel_file = project_slug_dir / ".env-devel"
Expand All @@ -93,13 +69,13 @@ def project_env_devel_dict(project_slug_dir: Path) -> Dict:


@pytest.fixture(scope="function")
def project_env_devel_environment(project_env_devel_dict, monkeypatch) -> None:
def patch_env_devel_environment(project_env_devel_dict, monkeypatch) -> None:
for key, value in project_env_devel_dict.items():
monkeypatch.setenv(key, value)


@pytest.fixture(scope="session")
def docker_compose_file(here) -> Iterator[str]:
def docker_compose_file(project_tests_dir: Path) -> Iterator[str]:
"""Overrides pytest-docker fixture"""
old = os.environ.copy()

Expand All @@ -111,7 +87,7 @@ def docker_compose_file(here) -> Iterator[str]:
os.environ["MINIO_ACCESS_KEY"] = ACCESS_KEY
os.environ["MINIO_SECRET_KEY"] = SECRET_KEY

dc_path = here / "docker-compose.yml"
dc_path = project_tests_dir / "docker-compose.yml"

assert dc_path.exists()
yield str(dc_path)
Expand Down Expand Up @@ -268,13 +244,13 @@ def dsm_mockup_complete_db(postgres_service_url, s3_client) -> Tuple[Dict, Dict]
f = DATA_DIR / "notebooks.zip"
object_name = "{project_id}/{node_id}/{filename}".format(**file_2)
s3_client.upload_file(bucket_name, object_name, f)
yield (file_1, file_2)
return (file_1, file_2)


@pytest.fixture(scope="function")
def dsm_mockup_db(
postgres_service_url, s3_client, mock_files_factory
) -> Dict[str, FileMetaData]:
) -> Iterator[Dict[str, FileMetaData]]:

# s3 client
bucket_name = BUCKET_NAME
Expand Down Expand Up @@ -409,7 +385,7 @@ def dsm_fixture(s3_client, postgres_engine, loop, moduleless_app):
api_secret = os.environ.get("BF_API_SECRET", "none")
dsm_fixture.datcore_tokens[USER_ID] = DatCoreApiToken(api_token, api_secret)

yield dsm_fixture
return dsm_fixture


@pytest.fixture(scope="function")
Expand Down
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_cli_help():
assert "Usage: simcore-service-storage [OPTIONS]" in completed_process.stdout


def test_cli_check_config_dumps_json(project_env_devel_environment):
def test_cli_check_config_dumps_json(patch_env_devel_environment):
completed_process = subprocess.run(
"simcore-service-storage --check-config".split(), **COMMON_KWARGS
)
Expand All @@ -41,7 +41,7 @@ def test_cli_check_config_dumps_json(project_env_devel_environment):


@pytest.mark.parametrize("config_name", resources.listdir("data"))
def test_cli_config_with_environs(config_name, project_env_devel_environment):
def test_cli_config_with_environs(config_name, patch_env_devel_environment):

config_path = Path(resources.get_path("data")) / config_name

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ def pylintrc(osparc_simcore_root_dir):
return pylintrc


@pytest.mark.skip(reason="TEMP out")
def test_run_pylint(pylintrc, package_dir):
assert_pylint_is_passing(pylintrc=pylintrc, package_dir=package_dir)
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ def assert_all_hits_found(filled_search_dict: Dict[str, int], file_path: str) ->
assert value > 0, message


def test_are_settings_present(search_dict, here):
def test_are_settings_present(search_dict, project_slug_dir):
# scanning all the files in the
client_sdk_dir = here / ".." / "client-sdk" / "python"
client_sdk_dir = project_slug_dir / "client-sdk" / "python"
all_python_files = client_sdk_dir.rglob("*.py")
for python_file in all_python_files:
print(f"seaching in file {python_file}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,29 @@
import tempfile
from pathlib import Path

import pytest
import tests.utils

# from blackfynn import Blackfynn
from blackfynn.models import Collection
from simcore_service_storage.datcore import DatcoreClient
from simcore_service_storage.models import FileMetaData

dir_path = os.path.dirname(os.path.realpath(__file__))
api_token = os.environ.get("BF_API_KEY")
api_secret = os.environ.get("BF_API_SECRET")
pytestmark = pytest.mark.skip(
reason=("FIXME: mguidon! Currently disabled"),
)


if tests.utils.has_datcore_tokens():
def test_all(patch_env_devel_environment, tmp_path: Path):
api_token = os.environ.get("BF_API_KEY")
api_secret = os.environ.get("BF_API_SECRET")

client = DatcoreClient(api_token=api_token, api_secret=api_secret)
api_secret = os.environ.get("BF_API_SECRET", "none")
destination = str(Path("MaG/level1/level2"))
fd, path = tempfile.mkstemp()

try:
with os.fdopen(fd, "w") as tmp:
# do stuff with temp file
tmp.write("stuff")

f = client.upload_file(destination, path)
f = client.delete_file(destination, Path(path).name)
finally:
os.remove(path)
path = tmp_path / "fd"
path.write_text("stuff")
assert client.upload_file(destination, str(path))
assert client.delete_file(destination, path.name)

files = []
if True:
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
from simcore_service_storage.settings import APP_CONFIG_KEY, SIMCORE_S3_ID
from tests.helpers.utils_assert import assert_status
from tests.helpers.utils_project import clone_project_data
from tests.utils import BUCKET_NAME, USER_ID, has_datcore_tokens

current_dir = Path(sys.argv[0] if __name__ == "__main__" else __file__).resolve().parent
from tests.utils import BUCKET_NAME, USER_ID, get_project_with_data, has_datcore_tokens


def parse_db(dsm_mockup_db):
Expand All @@ -50,7 +48,7 @@ def client(
aiohttp_client: TestClient,
postgres_service,
minio_service,
osparc_api_specs_dir,
osparc_simcore_api_specs_dir,
):
app = web.Application()

Expand All @@ -65,7 +63,7 @@ def client(
"test_datcore": {"api_token": api_token, "api_secret": api_secret},
}
rest_cfg = {
"oas_repo": str(osparc_api_specs_dir),
"oas_repo": str(osparc_simcore_api_specs_dir),
}
postgres_cfg = postgres_service
s3_cfg = minio_service
Expand Down Expand Up @@ -290,15 +288,6 @@ async def test_action_check(client):
assert data["query_value"] == QUERY


def get_project_with_data() -> Dict[str, Any]:
projects = []
with open(current_dir / "data/projects_with_data.json") as fp:
projects = json.load(fp)

# TODO: add schema validation
return projects


@pytest.fixture
def mock_datcore_download(mocker, client):
# Use to mock downloading from DATCore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from simcore_service_storage.settings import ApplicationSettings


def test_loading_env_devel_in_settings(project_env_devel_environment):
def test_loading_env_devel_in_settings(patch_env_devel_environment):
settings = ApplicationSettings.create_from_environ()
print("captured settings: \n", settings.json(indent=2))

Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions services/storage/tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import json
import logging
import os
import sys
from pathlib import Path
from typing import Any, Dict, List

import pandas as pd
import pytest
Expand Down Expand Up @@ -136,3 +138,12 @@ def fill_tables_from_csv_files(url):
)
finally:
engine.dispose()


def get_project_with_data() -> List[Dict[str, Any]]:
projects = []
with open(DATA_DIR / "data/projects_with_data.json") as fp:
projects = json.load(fp)

# TODO: add schema validation
return projects

0 comments on commit 20731fa

Please sign in to comment.