-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtest_validate.py
More file actions
executable file
·119 lines (95 loc) · 4.3 KB
/
Copy pathtest_validate.py
File metadata and controls
executable file
·119 lines (95 loc) · 4.3 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
from unittest import mock, TestCase
from unittest.mock import call
import kb_python.validate as validate
from tests.mixins import TestMixin
@validate.validate_files()
def dummy(*args, **kwargs):
return
@validate.validate_files()
def dummy_str(*args, **kwargs):
return 'test'
@validate.validate_files()
def dummy_dict(*args, **kwargs):
return {'test': 'testfile'}
@validate.validate_files()
def dummy_tuple(*args, **kwargs):
return 'test1', 'test2'
class TestValidate(TestMixin, TestCase):
def test_validate_bus(self):
validate.validate_bus(self.bus_path)
def test_validate_bus_failed_parse(self):
with mock.patch('kb_python.validate.run_executable') as run_executable:
run_executable.return_value = None, '', None
with self.assertRaises(validate.ValidateError):
validate.validate_bus('path')
def test_validate_bus_no_records(self):
with mock.patch('kb_python.validate.run_executable') as run_executable:
run_executable.return_value = None, 'Read in 0 BUS records', None
with self.assertRaises(validate.ValidateError):
validate.validate_bus('path')
def test_validate_mtx(self):
validate.validate_mtx(self.matrix_path)
def test_validate_mtx_raises_on_error(self):
with mock.patch('kb_python.validate.scipy.io.mminfo') as mminfo:
mminfo.side_effect = ValueError('test')
with self.assertRaises(validate.ValidateError):
validate.validate_mtx('path')
def test_validate(self):
mock_validators = {
'.bus': mock.MagicMock(),
'.mtx': mock.MagicMock(),
}
bus_path = os.path.join(self.temp_dir, 'bus.bus')
open(bus_path, 'w')
mtx_path = os.path.join(self.temp_dir, 'mtx.mtx')
open(mtx_path, 'w')
with mock.patch('kb_python.validate.VALIDATORS', mock_validators):
validate.validate(bus_path)
mock_validators['.bus'].assert_called_once_with(bus_path)
validate.validate(mtx_path)
mock_validators['.mtx'].assert_called_once_with(mtx_path)
def test_validate_doesnt_exist(self):
with self.assertRaises(validate.ValidateError):
validate.validate('nonexistent/path')
def validate_files(self):
with mock.patch('kb_python.validate.validate') as v,\
mock.patch('kb_python.validate.os.path.exists') as exists:
exists.return_value = True
self.assertIsNone(dummy('f1', 1, kwarg1='f2', kwarg2=2))
self.assertEqual(2, v.call_count)
v.assert_has_calls([call('f1'), call('f2')])
def validate_files_str(self):
with mock.patch('kb_python.validate.validate') as v,\
mock.patch('kb_python.validate.os.path.exists') as exists:
exists.return_value = True
self.assertEqual('test', dummy_str('f1', 1, kwarg1='f2', kwarg2=2))
self.assertEqual(3, v.call_count)
v.assert_has_calls([call('f1'), call('f2'), call('test')])
def validate_files_dict(self):
with mock.patch('kb_python.validate.validate') as v,\
mock.patch('kb_python.validate.os.path.exists') as exists:
exists.return_value = True
self.assertEqual({'test': 'testfile'},
dummy_str('f1', 1, kwarg1='f2', kwarg2=2))
self.assertEqual(3, v.call_count)
v.assert_has_calls([call('f1'), call('f2'), call('testfile')])
def validate_files_tuple(self):
with mock.patch('kb_python.validate.validate') as v,\
mock.patch('kb_python.validate.os.path.exists') as exists:
exists.return_value = True
self.assertEqual(('test1', 'test2'),
dummy_str('f1', 1, kwarg1='f2', kwarg2=2))
self.assertEqual(4, v.call_count)
v.assert_has_calls([
call('f1'),
call('f2'),
call('test1'),
call('test2')
])
def test_validate_off(self):
with mock.patch('kb_python.validate.is_validate') as is_validate,\
mock.patch('kb_python.validate.validate_bus') as v:
is_validate.return_value = False
validate.validate('path/to/bus.bus')
v.assert_not_called()