-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_seq_file_sorter.py
82 lines (65 loc) · 3.24 KB
/
test_seq_file_sorter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import unittest
import os
import tempfile
import seq_file_sorter as fs
from unittest.mock import patch, MagicMock
from test import support
class TestSeqFileSorter(unittest.TestCase):
def setUp(self):
self.file_path = os.path.dirname(os.path.abspath(__file__))
def tearDown(self):
pass
def test_seq_files_present(self):
#finds a temp ab1 file
with tempfile.NamedTemporaryFile(suffix='.ab1',dir=self.file_path) as tf:
self.assertTrue(fs.seq_files_present(),"Should return true if .ab1 file in directory")
# finds temp .seq
with tempfile.NamedTemporaryFile(suffix='.seq',dir=self.file_path) as tf:
self.assertTrue(fs.seq_files_present(),"Should return true if .seq file in directory")
# should fail if
with tempfile.NamedTemporaryFile(suffix='.anythingelse',dir=self.file_path) as tf:
self.assertFalse(fs.seq_files_present(),"Should return false if not .seq or .ab1 file in directory")
def test_file_name_pattern(self):
# returns correct regex for a given file name
self.assertEqual(fs.choose_regex('JL_470815-501_US_NDT80_pUC-Seq-F_A12.ab1')[0],
'([A-Z]+_[0-9]+-[0-9]+_)(.*)([-|_][A-Z]+[0-9]+)(.(ab1|seq))','The correct regular expression for ELIM')
self.assertEqual(fs.choose_regex('JL_470815-501_US_NDT80_pUC-Seq-F_A12.ab1')[1],
(2,4,5),'need to return correct important groups')
self.assertEqual(fs.choose_regex('H1_JL62_2017-01-20_E06.ab1')[0],
'(.*)(_\d{4}-\d{2}-\d{2}_[a-hA-H]\d*)(.(ab1|seq))','The correct regular expression needs to be returned')
self.assertEqual(fs.choose_regex('H1_JL62_2017-01-20_E06.ab1')[1],
(1,3,4),'need to return correct important groups')
self.assertEqual(fs.choose_regex('JL_535622-623_7-1_oJL330C04.seq')[0],
'([A-Z]+_[0-9]+-[0-9]+_)(.*[_|-][a-zA-Z]+[0-9]+)([a-hA-H]\d*)(.(ab1|seq))','The correct regular expression needs to be returned ELIM2')
self.assertEqual(fs.choose_regex('JL_535622-623_7-1_oJL330C04.seq')[1],
(2,4,5),'need to return correct important groups')
with self.assertRaises(ValueError) as cm:
fs.choose_regex('anyfilename.ab1')
self.assertEqual('The file anyfilename.ab1 did not match existing file patterns.', str(cm.exception)) # checks correct error
@patch('os.makedirs')
@patch('os.path.exists')
def test_folders_not_created_if_present(self, mock_exists, mock_make_dirs):
mock_exists.return_value = True
fs.create_seq_folders()
self.assertEqual(len(mock_make_dirs.mock_calls),0)
@patch('os.makedirs')
@patch('os.path.exists')
def test_folders_created_if_not_present(self, mock_exists, mock_make_dirs):
mock_exists.return_value = False
fs.create_seq_folders()
self.assertEqual(len(mock_make_dirs.mock_calls),2)
@patch('builtins.input', return_value ='yes')
def test_yes_or_no_reply_y(self, reply):
self.assertTrue(fs.yes_or_no('Any question?'))
@patch('builtins.input', return_value ='no')
def test_yes_or_no_reply_n(self, reply):
self.assertFalse(fs.yes_or_no('Any question?'))
@patch('builtins.input', return_value ='no')
def test_yes_or_no_reply_n(self, reply):
self.assertFalse(fs.yes_or_no('Any question?'))
@patch('builtins.input', side_effect=['1','y'])
def test_yes_or_no_bad_reply(self, mock):
self.assertTrue(fs.yes_or_no('here is a silly question'))
# self to desktop is not tested
if __name__ == '__main__':
unittest.main()