Skip to content

Commit

Permalink
tests: addition of external_pids related tests
Browse files Browse the repository at this point in the history
Signed-off-by: Dinos Kousidis <[email protected]>
  • Loading branch information
Dinos Kousidis committed Jan 16, 2018
1 parent 8413e12 commit 7101d58
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 18 deletions.
24 changes: 7 additions & 17 deletions tests/b2share_unit_tests/deposit/test_deposit_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
from jsonschema.exceptions import ValidationError
from invenio_pidstore.models import PersistentIdentifier, PIDStatus
from b2share.modules.records.errors import AlteredRecordError
from b2share_unit_tests.helpers import create_deposit, pid_of
from b2share_unit_tests.helpers import assert_external_files, \
create_deposit


def test_deposit_create(app, draft_deposits):
"""Test deposit creation."""
Expand Down Expand Up @@ -221,23 +223,11 @@ def test_change_deposit_schema_fails(app, draft_deposits):


def test_create_deposit_with_external_pids(app, deposit_with_external_pids):
expected_files = deposit_with_external_pids.data['_deposit']['external_pids']
expected_uris = { exp['ePIC_PID'] for exp in expected_files }
expected_keys = { exp['key'] for exp in expected_files }
expected_files = \
deposit_with_external_pids.data['_deposit']['external_pids']
with app.app_context():
files = [f for f in deposit_with_external_pids.get_deposit().files]
assert len(files) == len(expected_files)
# Check that the URIs match
uris = {file.obj.file.uri for file in files}
assert uris == expected_uris
# Check that the keys match
keys = {file.obj.key for file in files}
assert keys == expected_keys
# Check that all files are redirect files
assert all([file.obj.file.storage_class == 'B' for file in files])
# Check that the files are deduplicated in the DB
file_ids = {file.obj.file_id for file in files}
assert len(file_ids) == len(uris)
assert_external_files(deposit_with_external_pids.get_deposit(),
expected_files)


def test_modify_record_adding_b2safe_files(app):
Expand Down
59 changes: 58 additions & 1 deletion tests/b2share_unit_tests/files/test_b2share_storage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# create new storage class for invenio-files-rest
# which disables almost everything but redirects when getting a file (send_file)
# which disables almost everything but redirects when getting a file
# (send_file)

# create the FileInstance with the planted PID
# - and the required ObjectVersion
Expand Down Expand Up @@ -32,13 +33,16 @@

"""Test B2Share Storage Class."""

import pytest
from flask import url_for
from invenio_files_rest.models import Bucket, FileInstance, \
ObjectVersion, Location
from b2share.modules.deposit.api import Deposit
from b2share.modules.files.storage import B2ShareFileStorage
from invenio_db import db
from invenio_records_rest.utils import allow_all
from invenio_files_rest.proxies import current_files_rest
from b2share_unit_tests.helpers import assert_external_files


def test_b2share_storage_with_pid(base_app, app, tmp_location, login_user, test_users):
Expand Down Expand Up @@ -68,3 +72,56 @@ def test_b2share_storage_with_pid(base_app, app, tmp_location, login_user, test_
finally:
with app.app_context():
current_files_rest.permission_factory = permission


@pytest.mark.parametrize('new_external_pids',
[[{
"key": "file1.txt",
"ePIC_PID":
"http://hdl.handle.net/11304/0d8dbdec-74e4-4774-954e-1a98e5c0cfa2"
}], [{
"key": "renamed_file.txt",
"ePIC_PID":
"http://hdl.handle.net/11304/0d8dbdec-74e4-4774-954e-1a98e5c0cfa4"
}]])
def test_modify_external_files(app, deposit_with_external_pids,
new_external_pids):
"""Test changing PID, renaming and deletion of external files."""
with app.app_context():
deposit = Deposit.get_record(deposit_with_external_pids.deposit_id)
deposit = deposit.patch([
{'op': 'replace', 'path': '/external_pids',
'value': new_external_pids}
])
deposit.commit()
assert_external_files(deposit, new_external_pids)


def test_adding_external_files(app, deposit_with_external_pids):
"""Test the addition of external files."""
with app.app_context():
new_external_pids = [
{
"key": "file1.txt",
"ePIC_PID": "http://hdl.handle.net/11304/0d8dbdec-74e4-4774-954e-1a98e5c0cfa3"
}, {
"key": "file1_copy.txt",
"ePIC_PID": "http://hdl.handle.net/11304/0d8dbdec-74e4-4774-954e-1a98e5c0cfa3"
}, {
"key": "file2.txt",
"ePIC_PID": "http://hdl.handle.net/11304/50fafc50-4227-4464-bacc-2d85295c18a7"
}, {
"key": "file3.txt",
"ePIC_PID": "http://hdl.handle.net/11304/50fafc50-4227-4464-bacc-2d85295c18a6"
}, {
"key": "file4.txt",
"ePIC_PID": "http://hdl.handle.net/11304/50fafc50-4227-4464-bacc-2d85295c18a8"
}
]
deposit = Deposit.get_record(deposit_with_external_pids.deposit_id)
deposit = deposit.patch([
{'op': 'replace', 'path': '/external_pids',
'value': new_external_pids}
])
deposit.commit()
assert_external_files(deposit, new_external_pids)
19 changes: 19 additions & 0 deletions tests/b2share_unit_tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,22 @@ def pid_of(record):
pid = PersistentIdentifier.get(pid_type=pids[0]['type'],
pid_value=pids[0]['value'])
return pid, pid.pid_value


def assert_external_files(record, expected_files):
"""Assert that a record or deposit has the expected external_files."""
expected_uris = {exp['ePIC_PID'] for exp in expected_files}
expected_keys = {exp['key'] for exp in expected_files}
files = [f for f in record.files]
assert len(files) == len(expected_files)
# Check that the URIs match
uris = {file.obj.file.uri for file in files}
assert uris == expected_uris
# Check that the keys match
keys = {file.obj.key for file in files}
assert keys == expected_keys
# Check that all files are redirect files
assert all([file.obj.file.storage_class == 'B' for file in files])
# Check that the files are deduplicated in the DB
file_ids = {file.obj.file_id for file in files}
assert len(file_ids) == len(uris)

0 comments on commit 7101d58

Please sign in to comment.