Skip to content
Snippets Groups Projects
Commit f1a3dff1 authored by Daniel Lyons's avatar Daniel Lyons
Browse files

Remove unittest from test_updater.py, rely on pytest instead

parent afe829b2
No related branches found
No related tags found
No related merge requests found
import logging
import os
import subprocess
import unittest
import warnings
from sqlalchemy import exc as sa_exc
......@@ -18,30 +17,21 @@ _LOG = get_console_logger("scode_project_updater_tests", logging.DEBUG)
_UPDATE_COMMAND = 'update_sproj'
PROFILE = 'local'
class UpdaterTestCase(unittest.TestCase):
class TestUpdater:
''' Exercises ScodeProjectUpdater '''
@classmethod
def setUpClass(cls) -> None:
@pytest.fixture(autouse=True, scope='function')
def install_test_data(self):
os.environ['CAPO_PROFILE'] = PROFILE
cls.return_values = build_updater_return_values()
@classmethod
def setUp(cls) -> None:
cls.initialize_test_data(cls)
@classmethod
def tearDownClass(cls) -> None:
cls.remove_test_data(cls)
self.initialize_test_data()
yield
self.remove_test_data()
def test_dry_run_does_not_update(self):
fake_project = ScodeTestProject().project
project_code = fake_project.project_code
try:
new_title = 'this is the new title'
self.assertNotEqual(fake_project.title, new_title,
f'new title should be {new_title}; got '
f'{fake_project.title}')
assert fake_project.title != new_title
args = [
'-C', project_code,
'-P', PROFILE,
......@@ -50,17 +40,9 @@ class UpdaterTestCase(unittest.TestCase):
]
updated = ScodeProjectUpdater(args=args).update_project()
# nothing should have been updated
self.assertEqual(fake_project.title, updated.title,
f'expecting same title, but before is '
f'{fake_project.title} and after is {updated.title}')
self.assertEqual(fake_project.abstract, updated.abstract,
f'expecting same abstract, but before is '
f'{fake_project.abstract} and updated is {updated.abstract}')
self.assertEqual(len(fake_project.authors),
len(updated.authors),
f'expecting same number of authors, '
f'but before has {len(fake_project.authors)} '
f'and after has {len(updated.authors)}')
assert fake_project.title == updated.title
assert fake_project.abstract == updated.abstract
assert len(fake_project.authors) == len(updated.authors)
except SystemExit as exc:
pytest.fail(f'unexpected failure with return code {exc.code}')
raise
......@@ -83,36 +65,24 @@ class UpdaterTestCase(unittest.TestCase):
pytest.fail(f'unexpected failure with return code {exc.code}')
raise
self.assertIsNotNone(updated, 'we should have gotten a project back')
self.assertEqual(fake_project.title, updated.title,
f'expecting same title, but before is '
f'{fake_project.title} and after is {updated.title}')
self.assertEqual(fake_project.abstract, updated.abstract,
f'expecting same abstract, but before is '
f'{fake_project.abstract} and updated is {updated.abstract}')
self.assertEqual(len(fake_project.authors),
len(updated.authors),
f'expecting same number of authors, '
f'but before has {len(fake_project.authors)} '
f'and after has {len(updated.authors)}')
assert updated != None
assert fake_project.title == updated.title
assert fake_project.abstract == updated.abstract
assert len(fake_project.authors) == len(updated.authors)
count = 0
for orig_author in fake_project.authors:
for author in updated.authors:
if author.username == orig_author.username:
count += 1
break
self.assertEqual(len(fake_project.authors), count,
'before and after projects should have '
'same authors')
assert len(fake_project.authors) == count
def test_updates_abstract_only(self):
fake_project = ScodeTestProject().project
project_code = fake_project.project_code
new_abstract = "Well, here's another nice mess you've gotten us into, Ollie"
self.assertNotEqual(fake_project.abstract, new_abstract,
f'expecting new abstract {new_abstract} '
f'but got {fake_project.abstract}')
assert fake_project.abstract != new_abstract
args = [
'-C', project_code,
'-P', PROFILE,
......@@ -122,14 +92,9 @@ class UpdaterTestCase(unittest.TestCase):
updated = ScodeProjectUpdater(args=args).update_project()
# only abstract should have been updated;
# all else should be same
self.assertEqual(fake_project.title, updated.title,
f'expecting same title, but before is '
f'{fake_project.title} and after is {updated.title}')
self.assertEqual(new_abstract, updated.abstract,
f'expecting same abstract, but before is '
f'{fake_project.abstract} and updated is {updated.abstract}')
self.assertEqual(len(fake_project.authors),
len(updated.authors))
assert fake_project.title == updated.title
assert new_abstract == updated.abstract
assert len(fake_project.authors) == len(updated.authors)
except SystemExit as exc:
pytest.fail(f'unexpected failure; return code = {exc.code}')
raise
......@@ -139,12 +104,8 @@ class UpdaterTestCase(unittest.TestCase):
project_code = fake_project.project_code
new_abstract = "I think you ought to know I'm feeling very depressed"
new_title = 'A Survey of the Mattresses of Sqornshellous Zeta'
self.assertNotEqual(fake_project.abstract, new_abstract,
f'expecting new abstract {new_abstract}, '
f'but abstract was not changed from {fake_project.abstract}')
self.assertNotEqual(fake_project.title, new_title,
f'expecting new title {new_title}, '
f'but abstract was not changed from {fake_project.title}')
assert fake_project.abstract != new_abstract
assert fake_project.title != new_title
args = [
'-C', project_code,
'-P', PROFILE,
......@@ -153,13 +114,9 @@ class UpdaterTestCase(unittest.TestCase):
]
try:
updated = ScodeProjectUpdater(args=args).update_project()
self.assertEqual(new_title, updated.title,
'title should not have changed')
self.assertEqual(new_abstract, updated.abstract,
'abstract should not have changed')
self.assertEqual(len(fake_project.authors),
len(updated.authors),
'authors should not have changed')
assert new_title == updated.title
assert new_abstract == updated.abstract
assert len(fake_project.authors) == len(updated.authors)
except SystemExit as exc:
pytest.fail(f'unexpected failure; exit code = {exc.code}')
raise
......@@ -172,16 +129,13 @@ class UpdaterTestCase(unittest.TestCase):
abstract=fake_project.abstract)
new_abstract = "First there is a mountain, then there is no " \
"mountain, then there is"
self.assertNotEqual(new_abstract, fake_project.abstract)
assert new_abstract != fake_project.abstract
new_project.abstract = new_abstract
original_authors = fake_project.authors.copy()
self.assertEqual(4, len(original_authors),
'expected 4 authors before update')
assert 4 == len(original_authors)
last_author = original_authors[3]
new_authors = original_authors[:3]
self.assertEqual(len(original_authors) - 1, len(new_authors),
f'expecting {len(original_authors) - 1} new authors, '
f'but there are {len(new_authors)}')
assert len(original_authors) - 1 == len(new_authors)
new_project.authors = new_authors
args = [
'-C', project_code,
......@@ -195,30 +149,23 @@ class UpdaterTestCase(unittest.TestCase):
updated = None
try:
updated = ScodeProjectUpdater(args=args).update_project()
self.assertIsNotNone(updated, 'project should have been returned')
assert updated is not None
except SystemExit as exc:
pytest.fail(f'unexpected failure; return code = {exc.code}')
raise
self.assertNotEqual(fake_project.abstract, updated.abstract,
'abstract should have changed')
self.assertEqual(fake_project.title, updated.title,
'title should not have changed')
expected = len(original_authors) - 1
actual = len(updated.authors)
self.assertEqual(expected, actual,
'one author should have been removed')
assert fake_project.abstract != updated.abstract
assert fake_project.title == updated.title
assert len(original_authors) - 1 == len(updated.authors)
authors_updated = last_author in updated.authors
self.assertFalse(authors_updated, 'THIS IS THE MESSAGE')
assert not authors_updated
count = 0
for orig_author in original_authors[:3]:
for new_author in updated.authors:
if new_author.username == orig_author.username:
count += 1
break
self.assertEqual(len(new_authors), count,
f'expected {len(new_authors)} authors in '
f'updated project; there were {count}')
assert len(new_authors) == count
def test_output_is_as_expected(self):
fake_project = ScodeTestProject().project
......@@ -230,22 +177,19 @@ class UpdaterTestCase(unittest.TestCase):
updater = ScodeProjectUpdater(args=args)
updater.update_project()
output = updater.get_project_info()
self.assertIsNotNone(output, 'program output is expected')
self.assertTrue('Title: ' + fake_project.title in output,
'title should be in output')
self.assertTrue('Abstract: ' + fake_project.abstract in output,
'abstract should be in output')
assert output is not None
assert ('Title: ' + fake_project.title) in output
assert ('Abstract: ' + fake_project.abstract) in output
pst_ids = [str(id) for id in get_author_pst_ids(fake_project)]
pst_id_str = ' '.join(pst_ids)
self.assertTrue('Authors: ' + pst_id_str in output,
f'output should have PST IDs {pst_ids}')
assert 'Authors: ' + pst_id_str in output
def test_copes_with_single_pi(self):
project = ScodeTestProject().project
args = ['-P', PROFILE, '-C', project.project_code, '-I', '4686']
try:
updated = ScodeProjectUpdater(args=args).update_project()
self.assertEqual(1, len(updated.authors))
assert 1 == len(updated.authors)
except SystemExit as ex:
pytest.fail(f'update failed with exit code {ex.code}')
raise
......@@ -258,13 +202,12 @@ class UpdaterTestCase(unittest.TestCase):
with pytest.raises(SystemExit) as exc:
ScodeProjectUpdater(args=args).update_project()
self.assertEqual(2, exc.code, 'ALMA project should be rejected')
assert 2 == exc.code
def test_update_failure_returns_expected_code(self):
result = FailingUpdater().update_project()
self.assertIsInstance(result, SystemExit)
self.assertEqual(5, result.code,
'expecting return code 5 for update failure')
assert isinstance(result, SystemExit)
assert 5 == result.code
""" The following test should be moved to another test case,
where we'll use a bash script, via subprocess.call(), to create an
......@@ -282,57 +225,47 @@ class UpdaterTestCase(unittest.TestCase):
# minimum required arguments -- profile & project -- omitted
return_code = CommandLineUpdaterLauncher([]).run()
self.assertEqual(return_code, 2,
'expected return code 2 for no args')
assert return_code == 2
project_code = ScodeTestProject().project.project_code
# profile not specified
args = ['-C', project_code,]
return_code = CommandLineUpdaterLauncher(args).run()
self.assertEqual(return_code, 2,
'expecting return code 2 when profile not specified')
assert return_code == 2
# project code not specified
args = ['-P', PROFILE]
self.assertEqual(CommandLineUpdaterLauncher(args).run(), 2,
'expecting return code 2 when project not specified')
assert CommandLineUpdaterLauncher(args).run() == 2
# profile value missing
args = ['-P', '-C', project_code]
return_code = CommandLineUpdaterLauncher(args).run()
self.assertEqual(return_code, 2,
'expecting return code 2 for missing profile')
assert return_code == 2
# project code missing
args = ['-P', PROFILE, '-C']
self.assertEqual(CommandLineUpdaterLauncher(args).run(), 2,
'expecting return code 2 for missing project code')
assert CommandLineUpdaterLauncher(args).run() == 2
# bad project code
args = ['-P', PROFILE, '-C', 'bogus']
self.assertEqual(CommandLineUpdaterLauncher(args).run(), 3,
'expecting return code 3 for invalid project code')
assert CommandLineUpdaterLauncher(args).run() == 3
# bad profile
args = ['-P', 'not_a_profile', '-C', project_code]
self.assertEqual(CommandLineUpdaterLauncher(args).run(), 1,
'expecting return code 1 for invalid Capo profile')
assert CommandLineUpdaterLauncher(args).run() == 1
# missing title as last argument
args = ['-P', PROFILE, '-C', project_code, '-T']
self.assertEqual(CommandLineUpdaterLauncher(args).run(), 2,
'expecting return code 2 for missing title')
assert CommandLineUpdaterLauncher(args).run() == 2
# missing title as first argument
args = ['-T', '-P', PROFILE, '-C', project_code,]
self.assertEqual(CommandLineUpdaterLauncher(args).run(), 2,
'expecting return code 2 for missing title')
assert CommandLineUpdaterLauncher(args).run() == 2
# nonexistent investigator
args = ['-P', PROFILE, '-C', project_code, '-I', '-22']
self.assertEqual(CommandLineUpdaterLauncher(args).run(), 4,
'expecting return code 4 for invalid investigator')
assert CommandLineUpdaterLauncher(args).run() == 4
### UTILITIES ###
......@@ -454,16 +387,3 @@ class CommandLineUpdaterLauncher:
except Exception as exc:
_LOG.error(f'{exc}')
return exc.returncode
def build_updater_return_values():
''' return codes and messages in the updater's "usage" string '''
return {
1: 'error with capo configuration',
2: 'error with input parameters',
3: 'project not found',
4: 'investigator not found',
5: 'update failed',
}
if __name__ == '__main__':
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment