55 lines
2.2 KiB
Python
55 lines
2.2 KiB
Python
import unittest
|
|
import unittest.mock
|
|
import io
|
|
from search_file import print_num_list, search_all_match, select_path
|
|
import search_file
|
|
|
|
|
|
class TestSearchFile(unittest.TestCase):
|
|
|
|
data_dir = "test/data/search_test"
|
|
mock_list = [data_dir+"/match1.txt",\
|
|
data_dir+"/subdir21/match1.txt",\
|
|
data_dir+"/subdir1/match1.txt",\
|
|
data_dir+"/subdir2/match1.txt"]
|
|
|
|
print_mock = "1. test/data/search_test/match1.txt\n\
|
|
2. test/data/search_test/subdir21/match1.txt\n\
|
|
3. test/data/search_test/subdir1/match1.txt\n\
|
|
4. test/data/search_test/subdir2/match1.txt\n"
|
|
|
|
target = "match1.txt"
|
|
|
|
@unittest.mock.patch('sys.stdout', new_callable=io.StringIO)
|
|
def assert_stdout(self, lst, expected_output, mock_stdout):
|
|
print_num_list(lst)
|
|
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
|
|
|
def test_print_num_list(self):
|
|
self.assert_stdout(self.mock_list, self.print_mock)
|
|
|
|
def test_search_all_match(self):
|
|
search_result = search_all_match(self.target, self.data_dir)
|
|
self.assertEqual(search_result, self.mock_list)
|
|
search_result = search_all_match("inexistent.txt", self.data_dir)
|
|
self.assertEqual(search_result, [])
|
|
|
|
@unittest.mock.patch("search_file.input", create=True)
|
|
def test_select_path(self, mocked_input):
|
|
print("\n")
|
|
search_file.input.side_effect=["1", "2", "3", "4"]
|
|
search_result = search_all_match(self.target, self.data_dir)
|
|
path1 = self.data_dir+"/match1.txt"
|
|
self.assertEqual(select_path(search_result), path1)
|
|
path2 = self.data_dir+"/subdir21/match1.txt"
|
|
self.assertEqual(select_path(search_result), path2)
|
|
|
|
@unittest.mock.patch("search_file.input", create=True)
|
|
def test_search_file(self, mock_input):
|
|
search_file.input.side_effect=["1"]
|
|
with self.assertRaises(Exception):
|
|
search_file.search_file("inexistent.txt", self.data_dir)
|
|
search_result = search_all_match(self.target, self.data_dir)
|
|
self.assertTrue(search_result, self.data_dir+"/match1.txt")
|
|
self.assertEqual(search_file.search_file("with space.txt", self.data_dir), "test/data/search_test/subdir2/with space.txt")
|