Skip to content

Commit

Permalink
Fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
costrouc committed Aug 4, 2023
1 parent da7acc0 commit 38b5ceb
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 132 deletions.
7 changes: 0 additions & 7 deletions conda-store-server/conda_store_server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,6 @@ def session_factory(self):
)
return self._session_factory

# @property
# def db(self):
# # we are using a scoped_session which always returns the same
# # session if within the same thread
# # https://docs.sqlalchemy.org/en/14/orm/contextual.html
# return self.session_factory()

@property
def redis(self):
import redis
Expand Down
1 change: 0 additions & 1 deletion conda-store-server/conda_store_server/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,5 +691,4 @@ def new_session_factory(url="sqlite:///:memory:", reset=False, **kwargs):
engine = create_engine(url, **kwargs)

session_factory = sessionmaker(bind=engine)
# session_factory = scoped_session(sessionmaker(bind=engine))
return session_factory
49 changes: 26 additions & 23 deletions conda-store-server/conda_store_server/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,51 @@
import json
import uuid

from sqlalchemy.orm import Session

from conda_store_server import schema, api, orm, conda_utils


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(conda_store.db, name=namespace_name)
namespace = api.ensure_namespace(db, name=namespace_name)
for environment_name, specification in config[namespace_name].items():
environment = api.ensure_environment(
conda_store.db,
db,
name=specification.name,
namespace_id=namespace.id,
)
specification = api.ensure_specification(conda_store.db, specification)
build = api.create_build(conda_store.db, environment.id, specification.id)
conda_store.db.commit()
specification = api.ensure_specification(db, specification)
build = api.create_build(db, environment.id, specification.id)
db.commit()

environment.current_build_id = build.id
conda_store.db.commit()
db.commit()

_create_build_artifacts(conda_store, build)
_create_build_packages(conda_store, build)
_create_build_artifacts(db, conda_store, build)
_create_build_packages(db, conda_store, build)

api.create_solve(conda_store.db, specification.id)
conda_store.db.commit()
api.create_solve(db, specification.id)
db.commit()


def _create_build_packages(conda_store, build: orm.Build):
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(conda_store.db, channel_name)
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,
)
conda_store.db.add(conda_package)
conda_store.db.commit()
db.add(conda_package)
db.commit()

conda_package_build = orm.CondaPackageBuild(
package_id=conda_package.id,
Expand All @@ -57,16 +60,16 @@ def _create_build_packages(conda_store, build: orm.Build):
subdir="noarch",
timestamp=12345667,
)
conda_store.db.add(conda_package_build)
conda_store.db.commit()
db.add(conda_package_build)
db.commit()

build.package_builds.append(conda_package_build)
conda_store.db.commit()
db.commit()


def _create_build_artifacts(conda_store, build: orm.Build):
def _create_build_artifacts(db: Session, conda_store, build: orm.Build):
conda_store.storage.set(
conda_store.db,
db,
build.id,
build.log_key,
b"fake logs",
Expand All @@ -79,15 +82,15 @@ def _create_build_artifacts(conda_store, build: orm.Build):
artifact_type=schema.BuildArtifactType.DIRECTORY,
key=str(build.build_path(conda_store)),
)
conda_store.db.add(directory_build_artifact)
db.add(directory_build_artifact)

lockfile_build_artifact = orm.BuildArtifact(
build_id=build.id, artifact_type=schema.BuildArtifactType.LOCKFILE, key=""
)
conda_store.db.add(lockfile_build_artifact)
db.add(lockfile_build_artifact)

conda_store.storage.set(
conda_store.db,
db,
build.id,
build.conda_env_export_key,
json.dumps(
Expand All @@ -98,7 +101,7 @@ def _create_build_artifacts(conda_store, build: orm.Build):
)

conda_store.storage.set(
conda_store.db,
db,
build.id,
build.conda_pack_key,
b"testing-conda-package",
Expand Down
56 changes: 33 additions & 23 deletions conda-store-server/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,28 @@ def conda_store_config(tmp_path):
def conda_store_server(conda_store_config):
_conda_store_server = server_app.CondaStoreServer(config=conda_store_config)
_conda_store_server.initialize()

_conda_store = _conda_store_server.conda_store

pathlib.Path(_conda_store.store_directory).mkdir(exist_ok=True)

dbutil.upgrade(_conda_store.database_url)

_conda_store.ensure_settings()
_conda_store.celery_app
with _conda_store.session_factory() as db:
_conda_store.ensure_settings(db)
_conda_store.configuration(db).update_storage_metrics(
db, _conda_store.store_directory
)

# must import tasks after a celery app has been initialized
import conda_store_server.worker.tasks # noqa
# _conda_store.celery_app

# ensure that models are created
from celery.backends.database.session import ResultModelBase
# # must import tasks after a celery app has been initialized
# import conda_store_server.worker.tasks # noqa

ResultModelBase.metadata.create_all(_conda_store.db.get_bind())
# # ensure that models are created
# from celery.backends.database.session import ResultModelBase

_conda_store.configuration.update_storage_metrics(
_conda_store.db, _conda_store.store_directory
)
# ResultModelBase.metadata.create_all(_conda_store.db.get_bind())

yield _conda_store_server

Expand All @@ -69,8 +71,9 @@ def authenticate(testclient):


@pytest.fixture
def seed_conda_store(conda_store):
def seed_conda_store(db, conda_store):
testing.seed_conda_store(
db,
conda_store,
{
"default": {
Expand Down Expand Up @@ -103,11 +106,11 @@ def seed_conda_store(conda_store):
)

# for testing purposes make build 4 complete
build = api.get_build(conda_store.db, build_id=4)
build = api.get_build(db, build_id=4)
build.started_on = datetime.datetime.utcnow()
build.ended_on = datetime.datetime.utcnow()
build.status = schema.BuildStatus.COMPLETED
conda_store.db.commit()
db.commit()


@pytest.fixture
Expand All @@ -118,24 +121,31 @@ def conda_store(conda_store_config):

dbutil.upgrade(_conda_store.database_url)

_conda_store.ensure_settings()
_conda_store.celery_app
with _conda_store.session_factory() as db:
_conda_store.ensure_settings(db)
_conda_store.configuration(db).update_storage_metrics(
db, _conda_store.store_directory
)

# must import tasks after a celery app has been initialized
import conda_store_server.worker.tasks # noqa
# _conda_store.celery_app

# ensure that models are created
from celery.backends.database.session import ResultModelBase
# # must import tasks after a celery app has been initialized
# import conda_store_server.worker.tasks # noqa

ResultModelBase.metadata.create_all(_conda_store.db.get_bind())
# # ensure that models are created
# from celery.backends.database.session import ResultModelBase

_conda_store.configuration.update_storage_metrics(
_conda_store.db, _conda_store.store_directory
)
# ResultModelBase.metadata.create_all(_conda_store.db.get_bind())

yield _conda_store


@pytest.fixture
def db(conda_store):
with conda_store.session_factory() as _db:
yield _db


@pytest.fixture
def simple_specification():
yield schema.CondaSpecification(
Expand Down
18 changes: 10 additions & 8 deletions conda-store-server/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,31 +175,33 @@ def test_get_conda_prefix_stats(tmp_path, conda_store, simple_conda_lock):
assert context.result["disk_usage"] > 0


def test_add_conda_prefix_packages(conda_store, simple_specification, current_prefix):
def test_add_conda_prefix_packages(
db, conda_store, simple_specification, current_prefix
):
build_id = conda_store.register_environment(
specification=simple_specification, namespace="pytest"
db, specification=simple_specification, namespace="pytest"
)

action.action_add_conda_prefix_packages(
db=conda_store.db,
db=db,
conda_prefix=current_prefix,
build_id=build_id,
)

build = api.get_build(conda_store.db, build_id=build_id)
build = api.get_build(db, build_id=build_id)
assert len(build.package_builds) > 0


def test_add_lockfile_packages(
conda_store, simple_specification, simple_conda_lock, current_prefix
db, conda_store, simple_specification, simple_conda_lock, current_prefix
):
task, solve_id = conda_store.register_solve(specification=simple_specification)
task, solve_id = conda_store.register_solve(db, specification=simple_specification)

action.action_add_lockfile_packages(
db=conda_store.db,
db=db,
conda_lock_spec=simple_conda_lock,
solve_id=solve_id,
)

solve = api.get_solve(conda_store.db, solve_id=solve_id)
solve = api.get_solve(db, solve_id=solve_id)
assert len(solve.package_builds) > 0
Loading

0 comments on commit 38b5ceb

Please sign in to comment.