From a4f441d3c40170f29afdbb6114d248821bf6a541 Mon Sep 17 00:00:00 2001 From: Johan Viklund Date: Thu, 13 Sep 2018 16:29:08 +0200 Subject: [PATCH 1/2] Increased test-coverage of db module. This is achieved by running through more of the failure paths of the ingest worker --- tests/test_ingest.py | 86 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/tests/test_ingest.py b/tests/test_ingest.py index ff964666..f7675045 100644 --- a/tests/test_ingest.py +++ b/tests/test_ingest.py @@ -4,7 +4,11 @@ from testfixtures import tempdir from pathlib import PosixPath from . import pgp_data +from lega.utils.exceptions import FromUser +def stupid_shit(*args, **kwargs): + for i in range(10): + print("HELLO IN THE GAME WHAT IS GOING ON!!") class testIngest(unittest.TestCase): """Ingest @@ -25,7 +29,7 @@ def test_main(self, mock_consume, mock_connection): @mock.patch('lega.ingest.db') def test_work(self, mock_db, mock_path, mock_header, filedir): """Test ingest worker, should send a messge.""" - # Mocking a lot of stuff, ast it is previously tested + # Mocking a lot of stuff, as it is previously tested mock_path = mock.Mock(spec=PosixPath) mock_path.return_value = '' mock_header.return_value = b'beginning', b'header' @@ -45,3 +49,83 @@ def test_work(self, mock_db, mock_path, mock_header, filedir): 'vault_path': 'smth'} self.assertEqual(mocked, result) filedir.cleanup() + + @tempdir() + @mock.patch('lega.ingest.get_header') + @mock.patch('lega.ingest.Path') + @mock.patch('lega.ingest.db') + def test_db_fail(self, mock_db, mock_path, mock_header, filedir): + """Test ingest worker, insert_file fails.""" + # Mocking a lot of stuff, as it is previously tested + mock_path = mock.Mock(spec=PosixPath) + mock_path.return_value = '' + mock_header.return_value = b'beginning', b'header' + mock_db.insert_file.side_effect = Exception("Some strange exception") + + store = mock.MagicMock() + store.location.return_value = 'smth' + store.open.return_value = mock.MagicMock() + mock_broker = mock.MagicMock(name='channel') + mock_broker.channel.return_value = mock.Mock() + infile = filedir.write('infile.in', bytearray.fromhex(pgp_data.ENC_FILE)) + + data = {'filepath': infile, 'user': 'user_id@elixir-europe.org'} + result = work(store, mock_broker, data) + self.assertEqual(None, result) + filedir.cleanup() + + @tempdir() + @mock.patch('lega.ingest.get_header') + @mock.patch('lega.ingest.Path') + @mock.patch('lega.ingest.db') + @mock.patch('lega.utils.db.set_error') + def test_mark_in_progress_fail(self, mock_set_error, mock_db, mock_path, mock_header, filedir): + """Test ingest worker, mark_in_progress fails.""" + # Mocking a lot of stuff, as it is previously tested + mock_path = mock.Mock(spec=PosixPath) + mock_path.return_value = '' + mock_header.return_value = b'beginning', b'header' + mock_db.mark_in_progress.side_effect = Exception("Some strange exception") + + store = mock.MagicMock() + store.location.return_value = 'smth' + store.open.return_value = mock.MagicMock() + mock_broker = mock.MagicMock(name='channel') + mock_broker.channel.return_value = mock.Mock() + infile = filedir.write('infile.in', bytearray.fromhex(pgp_data.ENC_FILE)) + + data = {'filepath': infile, 'user': 'user_id@elixir-europe.org'} + result = work(store, mock_broker, data) + self.assertEqual(None, result) + mock_set_error.assert_called() + filedir.cleanup() + + @tempdir() + @mock.patch('lega.ingest.get_header') + @mock.patch('lega.ingest.Path') + @mock.patch('lega.ingest.db') + @mock.patch('lega.utils.db.set_error') + @mock.patch('lega.utils.db.get_connection') + @mock.patch('lega.utils.db.publish') + def test_mark_in_progress_fail_with_from_user_error(self, mock_publish, mock_get_connection, mock_set_error, mock_db, mock_path, mock_header, filedir): + """Test ingest worker, mark_in_progress fails.""" + # Mocking a lot of stuff, as it is previously tested + mock_path = mock.Mock(spec=PosixPath) + mock_path.return_value = '' + mock_header.return_value = b'beginning', b'header' + mock_db.mark_in_progress.side_effect = FromUser() + + store = mock.MagicMock() + store.location.return_value = 'smth' + store.open.return_value = mock.MagicMock() + mock_broker = mock.MagicMock(name='channel') + mock_broker.channel.return_value = mock.Mock() + infile = filedir.write('infile.in', bytearray.fromhex(pgp_data.ENC_FILE)) + + data = {'filepath': infile, 'user': 'user_id@elixir-europe.org'} + result = work(store, mock_broker, data) + self.assertEqual(None, result) + mock_set_error.assert_called() + mock_publish.assert_called() + mock_get_connection.assert_called() + filedir.cleanup() From edf6f4271592f86b98cebf1e997d7ec9d104bea0 Mon Sep 17 00:00:00 2001 From: Johan Viklund Date: Fri, 14 Sep 2018 09:07:56 +0200 Subject: [PATCH 2/2] Removed private debug function --- tests/test_ingest.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/test_ingest.py b/tests/test_ingest.py index f7675045..ae316931 100644 --- a/tests/test_ingest.py +++ b/tests/test_ingest.py @@ -6,9 +6,6 @@ from . import pgp_data from lega.utils.exceptions import FromUser -def stupid_shit(*args, **kwargs): - for i in range(10): - print("HELLO IN THE GAME WHAT IS GOING ON!!") class testIngest(unittest.TestCase): """Ingest