|
| 1 | +""" |
| 2 | +Tests for hypernets_processor_main module |
| 3 | +""" |
| 4 | + |
| 5 | +import unittest |
| 6 | +from unittest.mock import patch |
| 7 | +from hypernets_processor.version import __version__ |
| 8 | +from hypernets_processor.main.sequence_processor_main import main, get_target_sequences |
| 9 | +from hypernets_processor.test.test_functions import setup_test_context |
| 10 | +import string |
| 11 | +import random |
| 12 | +import os |
| 13 | +import shutil |
| 14 | + |
| 15 | + |
| 16 | +'''___Authorship___''' |
| 17 | +__author__ = "Sam Hunt" |
| 18 | +__created__ = "21/10/2020" |
| 19 | +__version__ = __version__ |
| 20 | +__maintainer__ = "Sam Hunt" |
| 21 | +__email__ = "sam.hunt@npl.co.uk" |
| 22 | +__status__ = "Development" |
| 23 | + |
| 24 | + |
| 25 | +class TestSequenceProcessorMain(unittest.TestCase): |
| 26 | + |
| 27 | + @patch('hypernets_processor.main.sequence_processor_main.configure_logging') |
| 28 | + @patch('hypernets_processor.main.sequence_processor_main.SequenceProcessor') |
| 29 | + @patch('hypernets_processor.main.sequence_processor_main.get_target_sequences') |
| 30 | + @patch('hypernets_processor.main.sequence_processor_main.Context') |
| 31 | + def test_main(self, mock_con, mock_gts, mock_sp, mock_cf): |
| 32 | + job_config_path = "jpath" |
| 33 | + processor_config_path = "ppath" |
| 34 | + main(job_config_path=job_config_path, processor_config_path=processor_config_path, to_archive=True) |
| 35 | + |
| 36 | + self.assertTrue(mock_sp.called) |
| 37 | + mock_sp.assert_called_once_with(context=mock_con.return_value) |
| 38 | + |
| 39 | + def test_get_target_sequences_toarchive(self): |
| 40 | + tmpdir = "tmp_" + "".join(random.choices(string.ascii_lowercase, k=6)) |
| 41 | + context = setup_test_context( |
| 42 | + raw_data_directory=os.path.join(tmpdir, "data"), |
| 43 | + archive_directory=os.path.join(tmpdir, "out"), |
| 44 | + metadata_db_url="sqlite:///"+tmpdir+"/metadata.db", |
| 45 | + anomoly_db_url="sqlite:///"+tmpdir+"/anomoly.db", |
| 46 | + archive_db_url="sqlite:///"+tmpdir+"/archive.db", |
| 47 | + create_directories=True, |
| 48 | + create_dbs=True |
| 49 | + ) |
| 50 | + |
| 51 | + expected_sequences = [os.path.join(tmpdir, "data", "SEQ20200311T112530")] |
| 52 | + sequences = get_target_sequences(context, True) |
| 53 | + |
| 54 | + self.assertCountEqual(expected_sequences, sequences) |
| 55 | + |
| 56 | + shutil.rmtree(tmpdir) |
| 57 | + |
| 58 | + def test_get_target_sequences_nottoarchive(self): |
| 59 | + tmpdir = "tmp_" + "".join(random.choices(string.ascii_lowercase, k=6)) |
| 60 | + context = setup_test_context( |
| 61 | + raw_data_directory=os.path.join(tmpdir, "data"), |
| 62 | + archive_directory=os.path.join(tmpdir, "out"), |
| 63 | + metadata_db_url="sqlite:///" + tmpdir + "/metadata.db", |
| 64 | + anomoly_db_url="sqlite:///" + tmpdir + "/anomoly.db", |
| 65 | + archive_db_url="sqlite:///" + tmpdir + "/archive.db", |
| 66 | + create_directories=True, |
| 67 | + create_dbs=True |
| 68 | + ) |
| 69 | + |
| 70 | + expected_sequences = [ |
| 71 | + os.path.join(tmpdir, "data", "SEQ20200311T112330"), |
| 72 | + os.path.join(tmpdir, "data", "SEQ20200311T112430"), |
| 73 | + os.path.join(tmpdir, "data", "SEQ20200311T112530") |
| 74 | + ] |
| 75 | + sequences = get_target_sequences(context, False) |
| 76 | + |
| 77 | + self.assertCountEqual(expected_sequences, sequences) |
| 78 | + |
| 79 | + shutil.rmtree(tmpdir) |
| 80 | + |
| 81 | + def test_get_target_sequences_1sequence(self): |
| 82 | + tmpdir = "tmp_" + "".join(random.choices(string.ascii_lowercase, k=6)) |
| 83 | + context = setup_test_context( |
| 84 | + raw_data_directory=os.path.join(tmpdir, "data"), |
| 85 | + archive_directory=os.path.join(tmpdir, "out"), |
| 86 | + metadata_db_url="sqlite:///" + tmpdir + "/metadata.db", |
| 87 | + anomoly_db_url="sqlite:///" + tmpdir + "/anomoly.db", |
| 88 | + archive_db_url="sqlite:///" + tmpdir + "/archive.db", |
| 89 | + create_directories=True, |
| 90 | + create_dbs=True |
| 91 | + ) |
| 92 | + |
| 93 | + context.set_config_value( |
| 94 | + "raw_data_directory", |
| 95 | + os.path.join(context.get_config_value("raw_data_directory"), "SEQ20200311T112230"), |
| 96 | + ) |
| 97 | + |
| 98 | + expected_sequences = [os.path.join(tmpdir, "data", "SEQ20200311T112230")] |
| 99 | + sequences = get_target_sequences(context, False) |
| 100 | + |
| 101 | + self.assertCountEqual(expected_sequences, sequences) |
| 102 | + |
| 103 | + shutil.rmtree(tmpdir) |
| 104 | + |
| 105 | + def test_get_target_sequences_0sequence(self): |
| 106 | + tmpdir = "tmp_" + "".join(random.choices(string.ascii_lowercase, k=6)) |
| 107 | + context = setup_test_context( |
| 108 | + raw_data_directory=os.path.join(tmpdir, "data"), |
| 109 | + archive_directory=os.path.join(tmpdir, "out"), |
| 110 | + metadata_db_url="sqlite:///" + tmpdir + "/metadata.db", |
| 111 | + anomoly_db_url="sqlite:///" + tmpdir + "/anomoly.db", |
| 112 | + archive_db_url="sqlite:///" + tmpdir + "/archive.db", |
| 113 | + create_directories=True, |
| 114 | + create_dbs=True |
| 115 | + ) |
| 116 | + |
| 117 | + context.set_config_value( |
| 118 | + "raw_data_directory", |
| 119 | + context.get_config_value("archive_directory"), |
| 120 | + ) |
| 121 | + |
| 122 | + expected_sequences = [] |
| 123 | + sequences = get_target_sequences(context, False) |
| 124 | + |
| 125 | + self.assertCountEqual(expected_sequences, sequences) |
| 126 | + |
| 127 | + shutil.rmtree(tmpdir) |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + unittest.main() |
0 commit comments