From 17f9d59cc7b0a669ddbc2dc2184184fe8df02a5e Mon Sep 17 00:00:00 2001 From: Sophia Castellarin Date: Fri, 25 Oct 2024 16:10:14 -0700 Subject: [PATCH] Move seeding tests functionality into test module (#925) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .../conda_store_server/_internal/testing.py | 117 ------------------ conda-store-server/tests/conftest.py | 116 ++++++++++++++++- conda-store-server/tests/test_testing.py | 38 ------ 3 files changed, 113 insertions(+), 158 deletions(-) delete mode 100644 conda-store-server/conda_store_server/_internal/testing.py delete mode 100644 conda-store-server/tests/test_testing.py diff --git a/conda-store-server/conda_store_server/_internal/testing.py b/conda-store-server/conda_store_server/_internal/testing.py deleted file mode 100644 index 6bbce7c88..000000000 --- a/conda-store-server/conda_store_server/_internal/testing.py +++ /dev/null @@ -1,117 +0,0 @@ -# Copyright (c) conda-store development team. All rights reserved. -# Use of this source code is governed by a BSD-style -# license that can be found in the LICENSE file. - -import json -import typing -import uuid - -from sqlalchemy.orm import Session - -from conda_store_server import api -from conda_store_server._internal import conda_utils, orm, schema - - -def seed_conda_store( - db: Session, - conda_store, - config: typing.Dict[str, typing.Dict[str, schema.CondaSpecification]] = {}, -): - for namespace_name in config: - namespace = api.ensure_namespace(db, name=namespace_name) - for environment_name, specification in config[namespace_name].items(): - environment = api.ensure_environment( - db, - name=specification.name, - namespace_id=namespace.id, - ) - specification = api.ensure_specification(db, specification) - build = api.create_build(db, environment.id, specification.id) - db.commit() - - environment.current_build_id = build.id - db.commit() - - _create_build_artifacts(db, conda_store, build) - _create_build_packages(db, conda_store, build) - - api.create_solve(db, specification.id) - db.commit() - - -def _create_build_packages(db: Session, conda_store, build: orm.Build): - channel_name = conda_utils.normalize_channel_name( - conda_store.conda_channel_alias, "conda-forge" - ) - channel = api.ensure_conda_channel(db, channel_name) - - conda_package = orm.CondaPackage( - name=f"madeup-{uuid.uuid4()}", - version="1.2.3", - channel_id=channel.id, - ) - db.add(conda_package) - db.commit() - - conda_package_build = orm.CondaPackageBuild( - package_id=conda_package.id, - build="fakebuild", - build_number=1, - constrains=[], - depends=[], - md5=str(uuid.uuid4()), - sha256=str(uuid.uuid4()), - size=123456, - subdir="noarch", - timestamp=12345667, - ) - db.add(conda_package_build) - db.commit() - - build.package_builds.append(conda_package_build) - db.commit() - - -def _create_build_artifacts(db: Session, conda_store, build: orm.Build): - conda_store.storage.set( - db, - build.id, - build.log_key, - b"fake logs", - content_type="text/plain", - artifact_type=schema.BuildArtifactType.LOGS, - ) - - directory_build_artifact = orm.BuildArtifact( - build_id=build.id, - artifact_type=schema.BuildArtifactType.DIRECTORY, - key=str(build.build_path(conda_store)), - ) - db.add(directory_build_artifact) - - lockfile_build_artifact = orm.BuildArtifact( - build_id=build.id, artifact_type=schema.BuildArtifactType.LOCKFILE, key="" - ) - db.add(lockfile_build_artifact) - - conda_store.storage.set( - db, - build.id, - build.conda_env_export_key, - json.dumps( - dict(name="testing", channels=["conda-forge"], dependencies=["numpy"]) - ).encode("utf-8"), - content_type="text/yaml", - artifact_type=schema.BuildArtifactType.YAML, - ) - - conda_store.storage.set( - db, - build.id, - build.conda_pack_key, - b"testing-conda-package", - content_type="application/gzip", - artifact_type=schema.BuildArtifactType.CONDA_PACK, - ) - - # have not included docker at the moment diff --git a/conda-store-server/tests/conftest.py b/conda-store-server/tests/conftest.py index c51065e9e..a3aba05e9 100644 --- a/conda-store-server/tests/conftest.py +++ b/conda-store-server/tests/conftest.py @@ -3,13 +3,17 @@ # license that can be found in the LICENSE file. import datetime +import json import pathlib import sys +import typing +import uuid import pytest import yaml from fastapi.testclient import TestClient +from sqlalchemy.orm import Session from conda_store_server import api, app, storage @@ -17,9 +21,10 @@ from conda_store_server._internal import ( # isort:skip action, dbutil, - schema, - testing, utils, + conda_utils, + orm, + schema, ) from conda_store_server._internal.server import app as server_app # isort:skip @@ -118,7 +123,7 @@ def authenticate(testclient): @pytest.fixture def seed_conda_store(db, conda_store): - testing.seed_conda_store( + _seed_conda_store( db, conda_store, { @@ -275,3 +280,108 @@ def conda_prefix(conda_store, tmp_path, request): conda_prefix=conda_prefix, ) yield conda_prefix + + +def _seed_conda_store( + db: Session, + conda_store, + config: typing.Dict[str, typing.Dict[str, schema.CondaSpecification]] = {}, +): + for namespace_name in config: + namespace = api.ensure_namespace(db, name=namespace_name) + for environment_name, specification in config[namespace_name].items(): + environment = api.ensure_environment( + db, + name=specification.name, + namespace_id=namespace.id, + ) + specification = api.ensure_specification(db, specification) + build = api.create_build(db, environment.id, specification.id) + db.commit() + + environment.current_build_id = build.id + db.commit() + + _create_build_artifacts(db, conda_store, build) + _create_build_packages(db, conda_store, build) + + api.create_solve(db, specification.id) + db.commit() + + +def _create_build_packages(db: Session, conda_store, build: orm.Build): + channel_name = conda_utils.normalize_channel_name( + conda_store.conda_channel_alias, "conda-forge" + ) + channel = api.ensure_conda_channel(db, channel_name) + + conda_package = orm.CondaPackage( + name=f"madeup-{uuid.uuid4()}", + version="1.2.3", + channel_id=channel.id, + ) + db.add(conda_package) + db.commit() + + conda_package_build = orm.CondaPackageBuild( + package_id=conda_package.id, + build="fakebuild", + build_number=1, + constrains=[], + depends=[], + md5=str(uuid.uuid4()), + sha256=str(uuid.uuid4()), + size=123456, + subdir="noarch", + timestamp=12345667, + ) + db.add(conda_package_build) + db.commit() + + build.package_builds.append(conda_package_build) + db.commit() + + +def _create_build_artifacts(db: Session, conda_store, build: orm.Build): + conda_store.storage.set( + db, + build.id, + build.log_key, + b"fake logs", + content_type="text/plain", + artifact_type=schema.BuildArtifactType.LOGS, + ) + + directory_build_artifact = orm.BuildArtifact( + build_id=build.id, + artifact_type=schema.BuildArtifactType.DIRECTORY, + key=str(build.build_path(conda_store)), + ) + db.add(directory_build_artifact) + + lockfile_build_artifact = orm.BuildArtifact( + build_id=build.id, artifact_type=schema.BuildArtifactType.LOCKFILE, key="" + ) + db.add(lockfile_build_artifact) + + conda_store.storage.set( + db, + build.id, + build.conda_env_export_key, + json.dumps( + dict(name="testing", channels=["conda-forge"], dependencies=["numpy"]) + ).encode("utf-8"), + content_type="text/yaml", + artifact_type=schema.BuildArtifactType.YAML, + ) + + conda_store.storage.set( + db, + build.id, + build.conda_pack_key, + b"testing-conda-package", + content_type="application/gzip", + artifact_type=schema.BuildArtifactType.CONDA_PACK, + ) + + # have not included docker at the moment diff --git a/conda-store-server/tests/test_testing.py b/conda-store-server/tests/test_testing.py deleted file mode 100644 index c7e4d021d..000000000 --- a/conda-store-server/tests/test_testing.py +++ /dev/null @@ -1,38 +0,0 @@ -# Copyright (c) conda-store development team. All rights reserved. -# Use of this source code is governed by a BSD-style -# license that can be found in the LICENSE file. - -from conda_store_server import api -from conda_store_server._internal import schema, testing - - -def test_testing_initialize_database(db, conda_store): - config = { - "namespace1": { - "name1": schema.CondaSpecification( - name="name1", - channels=["conda-forge"], - dependencies=["numpy"], - ), - "name2": schema.CondaSpecification( - name="name2", - channels=["defaults"], - dependencies=["flask"], - ), - }, - "namespace2": { - "name3": schema.CondaSpecification( - name="name3", - channels=["bioconda"], - dependencies=["numba"], - ) - }, - } - - testing.seed_conda_store(db, conda_store, config) - - assert len(api.list_namespaces(db).all()) == 2 - assert len(api.list_environments(db).all()) == 3 - assert len(api.list_builds(db).all()) == 3 - assert len(api.list_solves(db).all()) == 3 - assert len(api.list_conda_packages(db).all()) == 3