-
Notifications
You must be signed in to change notification settings - Fork 13
/
unit_test_tsm.py
56 lines (46 loc) · 2.09 KB
/
unit_test_tsm.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
import tsm
import unittest
import unittest.mock as mock
import io
class TestLoadData(unittest.TestCase):
"""
Test the tsm.load_data function, which has two behaviors:
* given anything but a string, return a deep copy of it
* given a string, return a list of lists from CV
"""
def test_deep_copy_happens(self):
original = [1, 2, [3, 4]]
loaded = tsm.load_data(original)
# Test copy is effective:
self.assertEqual(original, loaded)
# Test copy is not by reference:
self.assertIsNot(original, loaded)
# Test copy is deep:
self.assertIsNot(original[2], loaded[2])
@mock.patch('builtins.open')
def test_calls_open_with_default_args(self, mock_open):
result = tsm.load_data('path')
mock_open.assert_called_once_with('path', 'r',
encoding='utf-8', errors='replace')
@mock.patch('builtins.open')
def test_calls_open_with_encoding(self, mock_open):
result = tsm.load_data('path', enc='other')
mock_open.assert_called_once_with('path', 'r', encoding='other',
errors='replace')
def test_returns_list_of_lists_from_csv(self):
fake_file = io.StringIO('one,two,three\n1,2,3\n')
with mock.patch('builtins.open', return_value=fake_file):
result = tsm.load_data('path')
self.assertEqual(result, [['one', 'two', 'three'], ['1', '2', '3']])
def test_strips_null_bytes_from_csv(self):
fake_file = io.StringIO('\0one,t\0wo,three\n1\0,2,3\0\n')
with mock.patch('builtins.open', return_value=fake_file):
result = tsm.load_data('path')
self.assertEqual(result, [['one', 'two', 'three'], ['1', '2', '3']])
def test_strips_empty_lines_from_csv(self):
fake_file = io.StringIO('\0one,t\0wo,three\n\n1\0,2,3\n\0')
with mock.patch('builtins.open', return_value=fake_file):
result = tsm.load_data('path')
self.assertEqual(result, [['one', 'two', 'three'], ['1', '2', '3']])
if __name__ == '__main__':
unittest.main()