Skip to content

Commit

Permalink
tests: increase covergage
Browse files Browse the repository at this point in the history
  • Loading branch information
remileduc committed Aug 25, 2017
1 parent fbcbbe9 commit 0952a35
Show file tree
Hide file tree
Showing 10 changed files with 353 additions and 82 deletions.
2 changes: 1 addition & 1 deletion invenio_archivematica/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def transfer_rsync(uuid, config):
# then we rsync to the final dest
src_path = archiver.get_fullpath('')
dest_path = config['destination']
if 'server' in config and 'user' in config:
if config.get('server', None) and config.get('user', None):
dest_path = '{user}@{server}:{dest}'.format(user=config['user'],
server=config['server'],
dest=dest_path)
Expand Down
13 changes: 10 additions & 3 deletions invenio_archivematica/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,19 @@ def status_converter(status, aip_processing=False):
transfer
"""
statuses = {
'AIP_PROCESSING': ArchiveStatus.PROCESSING_AIP,
'COMPLETE': ArchiveStatus.REGISTERED,
'DELETED': ArchiveStatus.DELETED,
'FAILED': ArchiveStatus.FAILED,
'IGNORED': ArchiveStatus.IGNORED,
'NEW': ArchiveStatus.NEW,
'PROCESSING_AIP': ArchiveStatus.PROCESSING_AIP,
'PROCESSING_TRANSFER': ArchiveStatus.PROCESSING_TRANSFER,
'REGISTERED': ArchiveStatus.REGISTERED,
'REJECTED': ArchiveStatus.FAILED,
'USER_INPUT': ArchiveStatus.FAILED,
'COMPLETE': ArchiveStatus.REGISTERED,
'SIP_PROCESSING': ArchiveStatus.PROCESSING_TRANSFER,
'AIP_PROCESSING': ArchiveStatus.PROCESSING_AIP
'USER_INPUT': ArchiveStatus.FAILED,
'WAITING': ArchiveStatus.WAITING
}
if status == 'PROCESSING' and aip_processing:
status = 'AIP_PROCESSING'
Expand Down
3 changes: 2 additions & 1 deletion invenio_archivematica/views/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def __init__(self, **kwargs):
kwargs['default_media_type'] = 'application/json'
super(Archive, self).__init__(**kwargs)

def _to_json(self, ark):
@staticmethod
def _to_json(ark):
"""Return the archive as a JSON object.
Used to return JSON as an answer.
Expand Down
8 changes: 5 additions & 3 deletions invenio_archivematica/views/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from flask_babelex import gettext as _

from invenio_archivematica.factories import create_accession_id
from invenio_archivematica.models import Archive

blueprint = Blueprint(
'invenio_archivematica',
Expand All @@ -49,9 +50,10 @@ def index():
module_name=_('Invenio-Archivematica'))


@blueprint.route("/test/<string:pid>/")
def test(pid):
@blueprint.route("/test/<string:accession_id>/")
def test(accession_id):
"""Show a test page."""
ark = Archive.get_from_accession_id(accession_id)
return """<DOCTYPE html><html><head></head><body>
<h1>{}</h1>
</body></html>""".format(create_accession_id(pid, 'recid'))
</body></html>""".format(create_accession_id(ark))
19 changes: 19 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
from invenio_accounts import InvenioAccounts
from invenio_db import db as db_
from invenio_db import InvenioDB
from invenio_files_rest import InvenioFilesREST
from invenio_files_rest.models import Location
from invenio_oauth2server import InvenioOAuth2Server, InvenioOAuth2ServerREST
from invenio_oauth2server.views import server_blueprint
from invenio_rest import InvenioREST
Expand Down Expand Up @@ -83,6 +85,7 @@ def app(base_app):
InvenioDB(base_app)
InvenioAccess(base_app)
InvenioAccounts(base_app)
InvenioFilesREST(base_app)
InvenioOAuth2Server(base_app)
InvenioOAuth2ServerREST(base_app)
InvenioREST(base_app)
Expand All @@ -105,6 +108,22 @@ def db(app):
drop_database(str(db_.engine.url))


@pytest.yield_fixture()
def location(app, db):
"""Define a location to write SIPs with factories."""
path = os.path.abspath('./tmp/')
try:
os.mkdir(path)
except:
pass
loc = Location(name='archive', uri=path, default=True)
db.session.add(loc)
db.session.commit()
app.config['SIPSTORE_ARCHIVER_LOCATION_NAME'] = 'archive'
yield loc
shutil.rmtree(path)


@pytest.yield_fixture()
def client(app):
"""Flask client fixture."""
Expand Down
36 changes: 36 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2017 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Test helpers."""


def archive_directory_builder(sip):
"""Build a directory for the archived SIP."""
return ['test']


def transfer_fail(*args, **kwargs):
"""Return 1, as if a transfer had failed."""
return 1
90 changes: 85 additions & 5 deletions tests/test_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@

"""Test the factories."""

from invenio_sipstore.models import SIP
import json
from os import path

from invenio_files_rest.models import FileInstance
from invenio_sipstore.models import SIP, SIPFile, SIPMetadata, SIPMetadataType
from six import BytesIO

from invenio_archivematica import factories
from invenio_archivematica.models import Archive
Expand Down Expand Up @@ -56,11 +61,86 @@ def test_is_archivable_none(db):
assert not factories.is_archivable_none(sip2)


def test_transfer_cp(db):
def test_transfer_cp(app, db, location):
"""Test factories.transfer_cp function."""
# TODO
# config
app.config['SIPSTORE_ARCHIVER_DIRECTORY_BUILDER'] = \
'helpers:archive_directory_builder'
app.config['SIPSTORE_ARCHIVER_METADATA_TYPES'] = ['test']
# SIP
sip = SIP.create()
# SIPMetadataType
mtype = SIPMetadataType(title='Test', name='test', format='json')
db.session.add(mtype)
# SIPMetadata
mcontent = {'title': 'title', 'author': 'me'}
meth = SIPMetadata(sip=sip, type=mtype, content=json.dumps(mcontent))
db.session.add(meth)
# SIPFile
f = FileInstance.create()
fcontent = b'weighted companion cube\n'
f.set_contents(BytesIO(fcontent), default_location=location.name)
sfile = SIPFile(sip=sip, file=f, filepath='portal.txt')
db.session.add(sfile)
db.session.commit()

# EXPORT
factories.transfer_cp(sip.id, None)

# TEST
folder = path.join(location.uri, 'test')
assert path.isdir(folder)
assert path.isdir(path.join(folder, 'files'))
assert path.isfile(path.join(folder, 'files', 'portal.txt'))
assert path.isdir(path.join(folder, 'metadata'))
assert path.isfile(path.join(folder, 'metadata', 'test.json'))
with open(path.join(folder, 'files', 'portal.txt'), 'rb') as fp:
assert fp.read() == fcontent
with open(path.join(folder, 'metadata', 'test.json'), 'r') as fp:
assert json.loads(fp.read()) == mcontent


def test_transfer_rsync(db):
def test_transfer_rsync(app, db, location):
"""Test factories.transfer_rsync function."""
# TODO
# config
app.config['SIPSTORE_ARCHIVER_DIRECTORY_BUILDER'] = \
'helpers:archive_directory_builder'
app.config['SIPSTORE_ARCHIVER_METADATA_TYPES'] = ['test']
# SIP
sip = SIP.create()
# SIPMetadataType
mtype = SIPMetadataType(title='Test', name='test', format='json')
db.session.add(mtype)
# SIPMetadata
mcontent = {'title': 'title', 'author': 'me'}
meth = SIPMetadata(sip=sip, type=mtype, content=json.dumps(mcontent))
db.session.add(meth)
# SIPFile
f = FileInstance.create()
fcontent = b'weighted companion cube\n'
f.set_contents(BytesIO(fcontent), default_location=location.name)
sfile = SIPFile(sip=sip, file=f, filepath='portal.txt')
db.session.add(sfile)
db.session.commit()

# EXPORT
folder = path.join(location.uri, 'lulz')
params = {
'server': '',
'user': '',
'destination': folder,
'args': '-az'
}
factories.transfer_rsync(sip.id, params)

# TEST
assert not path.exists(path.join(location.uri, 'test'))
assert path.isdir(folder)
assert path.isdir(path.join(folder, 'files'))
assert path.isfile(path.join(folder, 'files', 'portal.txt'))
assert path.isdir(path.join(folder, 'metadata'))
assert path.isfile(path.join(folder, 'metadata', 'test.json'))
with open(path.join(folder, 'files', 'portal.txt'), 'rb') as fp:
assert fp.read() == fcontent
with open(path.join(folder, 'metadata', 'test.json'), 'r') as fp:
assert json.loads(fp.read()) == mcontent
12 changes: 8 additions & 4 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
oais_start_transfer


def test_oais_start_transfer(db):
def test_oais_start_transfer(app, db, location):
"""Test the oais_start_transfer function."""
assert Archive.query.count() == 0
# let's create a SIP
Expand All @@ -48,14 +48,17 @@ def test_oais_start_transfer(db):
ark = Archive.get_from_sip(sip.id)
assert ark.status == ArchiveStatus.WAITING
assert ark.accession_id == '1991'
# we try the case where no archive exist
# we try the case where no archive exist and transfer fails
db.session.delete(ark)
db.session.commit()
app.config['ARCHIVEMATICA_TRANSFER_FACTORY'] = 'helpers:transfer_fail'
assert Archive.query.count() == 0
oais_start_transfer(sip.id, '1991')
ark = Archive.get_from_sip(sip.id)
assert Archive.query.count() == 1
assert ark.status == ArchiveStatus.WAITING
assert ark.status == ArchiveStatus.FAILED
assert ark.accession_id == '1991'
assert ark.sip.archived is False


def test_oais_process_transfer(db):
Expand Down Expand Up @@ -100,6 +103,7 @@ def test_oais_finish_transfer(db):
ark = Archive.get_from_sip(sip.id)
assert ark.status == ArchiveStatus.REGISTERED
assert ark.archivematica_id == aipid
assert ark.sip.archived is True


def test_oais_fail_transfer(db):
Expand All @@ -115,7 +119,7 @@ def test_oais_fail_transfer(db):
assert ark.status == ArchiveStatus.FAILED


def test_archive_new_sips(db):
def test_archive_new_sips(db, location):
"""Test the archive_new_sips function."""
# we create 2 SIP
sip1 = SIP.create()
Expand Down
65 changes: 0 additions & 65 deletions tests/test_views.py

This file was deleted.

0 comments on commit 0952a35

Please sign in to comment.