Skip to content

Commit

Permalink
test: object deletion; use real local backend for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlougheed committed Apr 16, 2024
1 parent bc06ea5 commit 18ec9e9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 20 deletions.
14 changes: 1 addition & 13 deletions chord_drs/backends/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC, abstractmethod


__all__ = ["Backend", "FakeBackend"]
__all__ = ["Backend"]


class Backend(ABC):
Expand All @@ -12,15 +12,3 @@ def save(self, current_location: str, filename: str) -> str: # pragma: no cover
@abstractmethod
def delete(self, location: str) -> None: # pragma: no cover
pass


class FakeBackend(Backend):
"""
For the tests
"""

def save(self, current_location: str, filename: str) -> str:
return current_location

def delete(self, location: str):
return None
19 changes: 12 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
import os
import pathlib
import pytest
import shutil

from flask import g
from flask.testing import FlaskClient
from moto import mock_s3
from pytest_lazyfixture import lazy_fixture

# Must only be imports that don't import authz/app/config/db
from chord_drs.backends.base import FakeBackend
from chord_drs.backends.minio import MinioBackend
from chord_drs.data_sources import DATA_SOURCE_LOCAL, DATA_SOURCE_MINIO

Expand Down Expand Up @@ -47,7 +48,7 @@ def empty_file_path(): # Function rather than constant so we can set environ fi


@pytest.fixture
def client_minio():
def client_minio() -> FlaskClient:
os.environ["BENTO_AUTHZ_SERVICE_URL"] = AUTHZ_URL

from chord_drs.app import application, db
Expand All @@ -72,28 +73,32 @@ def client_minio():


@pytest.fixture
def client_local():
def client_local() -> FlaskClient:
local_test_volume = (pathlib.Path(__file__).parent / "data").absolute()
local_test_volume.mkdir(parents=True, exist_ok=True)

os.environ["BENTO_AUTHZ_SERVICE_URL"] = AUTHZ_URL
os.environ["DATA"] = str((pathlib.Path(__file__).parent / "data").absolute())
os.environ["DATA"] = str(local_test_volume)

from chord_drs.app import application, db

application.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
application.config["SERVICE_DATA_SOURCE"] = DATA_SOURCE_LOCAL

with application.app_context():
g.backend = FakeBackend()

db.create_all()

yield application.test_client()

db.session.remove()
db.drop_all()

# clear test volume
shutil.rmtree(local_test_volume)


@pytest.fixture(params=[lazy_fixture("client_minio"), lazy_fixture("client_local")])
def client(request):
def client(request) -> FlaskClient:
return request.param


Expand Down
52 changes: 52 additions & 0 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,58 @@ def test_object_delete(client):
assert res.status_code == 404


@responses.activate
def test_object_multi_delete(client):
from chord_drs.models import DrsBlob

authz_everything_true()

contents = str(uuid.uuid4())

# first, ingest two new objects with the same contents
with tempfile.NamedTemporaryFile(mode="w") as tf:
tf.write(contents) # random content, so checksum is unique
tf.flush()

# two different projects to ensure we have two objects pointing to the same resource:
res1 = client.post("/ingest", data={"path": tf.name, "project_id": "project1"})
assert res1.status_code == 201
res2 = client.post("/ingest", data={"path": tf.name, "project_id": "project2"})
assert res2.status_code == 201

i1 = res1.get_json()
i2 = res2.get_json()

assert i1["id"] != i2["id"]

b1 = DrsBlob.query.filter_by(id=i1["id"]).first()
b2 = DrsBlob.query.filter_by(id=i2["id"]).first()

assert b1.location == b2.location

# make sure we can get the bytes of i2
assert client.get(f"/objects/{i2['id']}/download").status_code == 200

# delete i2
rd2 = client.delete(f"/objects/{i2['id']}")
assert rd2.status_code == 204

# make sure we can still get the bytes of i1
assert client.get(f"/objects/{i1['id']}/download").status_code == 200

# check file exists if local
if b1.location.startswith("/"):
assert os.path.exists(b1.location)

# delete i1
rd1 = client.delete(f"/objects/{i1['id']}")
assert rd1.status_code == 204

# check file doesn't exist if local
if b1.location.startswith("/"):
assert not os.path.exists(b1.location)


@responses.activate
def test_bundle_and_download(client, drs_bundle):
authz_everything_true()
Expand Down

0 comments on commit 18ec9e9

Please sign in to comment.