-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
36 lines (26 loc) · 1.2 KB
/
tests.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
import unittest
from io import StringIO
from string import ascii_lowercase
from tempfile import TemporaryDirectory
from unittest.mock import patch, MagicMock
import ext_sort
class StringIOPipe(ext_sort.Pipe):
"""Use StringIO for testing."""
@staticmethod
def stream(string):
yield from (line.strip() for line in StringIO(string) if line)
class SimpleOrderCheckTestCase(unittest.TestCase):
@patch('ext_sort.Pipe', new=StringIOPipe)
@patch('ext_sort.TemporaryDirectory', new=MagicMock(spec=TemporaryDirectory))
@patch('ext_sort.dump_to_file', new=lambda it, *a, **k: '\n'.join(it), spec=ext_sort.dump_to_file)
@patch('ext_sort.input_stream', spec=ext_sort.input_stream)
def test_ext_sort(self, input_stream_mock):
result, elements = [], [x * 10 for x in ascii_lowercase]
input_stream_mock.return_value = StringIO('\n'.join(elements))
with patch('ext_sort.move', autospec=True) as move_mock:
move_mock.side_effect = lambda s, _: result.extend(line.strip() for line in StringIO(s))
ext_sort.ext_sort(mem_size=4)
# check order
self.assertEqual(result, list(sorted(elements)))
if __name__ == '__main__':
unittest.main()