Skip to content
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.

Increased test-coverage of db module. #353

Merged
merged 2 commits into from
Sep 14, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 82 additions & 1 deletion tests/test_ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from testfixtures import tempdir
from pathlib import PosixPath
from . import pgp_data
from lega.utils.exceptions import FromUser


class testIngest(unittest.TestCase):
Expand All @@ -25,7 +26,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'
Expand All @@ -45,3 +46,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()