|
1 | 1 | import unittest
|
2 |
| -from unittest.mock import call, patch |
| 2 | +from unittest.mock import MagicMock, patch |
3 | 3 |
|
4 | 4 | from uniflow.flow.extract.extract_txt_flow import ExtractTxtFlow
|
5 | 5 | from uniflow.node import Node
|
6 |
| -from uniflow.op.extract.load.txt_op import ExtractTxtOp |
7 | 6 | from uniflow.op.extract.split.constants import PARAGRAPH_SPLITTER
|
8 |
| -from uniflow.op.extract.split.splitter_factory import SplitterOpsFactory |
9 | 7 |
|
10 | 8 |
|
11 | 9 | class TestExtractTxtFlow(unittest.TestCase):
|
12 |
| - |
13 |
| - def setUp(self): |
| 10 | + @patch("uniflow.flow.extract.extract_txt_flow.ExtractTxtOp") |
| 11 | + @patch("uniflow.flow.extract.extract_txt_flow.SplitterOpsFactory") |
| 12 | + def setUp(self, mock_splitter_ops_factory, mock_extract_txt_op): |
| 13 | + self.mock_splitter_ops_factory = mock_splitter_ops_factory |
| 14 | + self.mock_extract_txt_op = mock_extract_txt_op |
14 | 15 | self.extract_txt_flow = ExtractTxtFlow()
|
15 |
| - self.extract_txt_flow._extract_txt_op = ExtractTxtOp(name="extract_txt_op") |
16 |
| - self.extract_txt_flow._split_op = SplitterOpsFactory.get(PARAGRAPH_SPLITTER) |
17 |
| - |
18 |
| - @patch( |
19 |
| - "uniflow.op.extract.load.txt_op.read_file", |
20 |
| - return_value="mocked\n\nfile\n\ncontent", |
21 |
| - ) |
22 |
| - def test_call_with_empty_node(self, mock_read_file): |
23 |
| - # arrange |
24 |
| - nodes = [] |
25 | 16 |
|
26 |
| - # act |
27 |
| - output_nodes = self.extract_txt_flow.run(nodes) |
| 17 | + def test_init(self): |
| 18 | + self.mock_extract_txt_op.assert_called_once_with(name="extract_txt_op") |
| 19 | + self.mock_splitter_ops_factory.get.assert_called_once_with(PARAGRAPH_SPLITTER) |
28 | 20 |
|
29 |
| - # assert |
30 |
| - mock_read_file.assert_not_called() |
31 |
| - self.assertEqual(len(output_nodes), 0) |
32 |
| - |
33 |
| - @patch( |
34 |
| - "uniflow.op.extract.load.txt_op.read_file", |
35 |
| - return_value="mocked\n\nfile\n\ncontent", |
36 |
| - ) |
37 |
| - def test_call_with_node_without_filename(self, mock_read_file): |
| 21 | + def test_run(self): |
38 | 22 | # arrange
|
39 |
| - node = Node(name="node1", value_dict={}) |
40 |
| - |
41 |
| - # act |
42 |
| - with self.assertRaises(KeyError): |
43 |
| - self.extract_txt_flow.run([node]) |
44 |
| - |
45 |
| - # assert |
46 |
| - mock_read_file.assert_not_called() |
47 |
| - |
48 |
| - @patch( |
49 |
| - "uniflow.op.extract.load.txt_op.read_file", |
50 |
| - return_value="mocked\n\nfile\n\ncontent", |
51 |
| - ) |
52 |
| - def test_call_with_node(self, mock_read_file): |
53 |
| - # arrange |
54 |
| - node = Node(name="node1", value_dict={"filename": "mocked_file_path"}) |
55 |
| - |
56 |
| - # act |
57 |
| - output_nodes = self.extract_txt_flow.run([node]) |
58 |
| - |
59 |
| - # assert |
60 |
| - mock_read_file.assert_called_once_with("mocked_file_path") |
61 |
| - self.assertEqual(len(output_nodes), 1) |
| 23 | + nodes = [ |
| 24 | + Node(name="node1", value_dict={"filename": "filepath"}), |
| 25 | + Node(name="node2", value_dict={"filename": "filepath"}), |
| 26 | + ] |
| 27 | + |
| 28 | + self.mock_splitter_ops_factory.get.return_value.return_value = MagicMock() |
| 29 | + self.mock_extract_txt_op.return_value.return_value = MagicMock() |
| 30 | + result = self.extract_txt_flow.run(nodes) |
| 31 | + |
| 32 | + self.mock_extract_txt_op.return_value.assert_called_once_with(nodes) |
| 33 | + self.mock_splitter_ops_factory.get.return_value.assert_called_once_with( |
| 34 | + self.mock_extract_txt_op.return_value.return_value |
| 35 | + ) |
62 | 36 | self.assertEqual(
|
63 |
| - output_nodes[0].value_dict["text"], ["mocked", "file", "content"] |
| 37 | + result, self.mock_splitter_ops_factory.get.return_value.return_value |
64 | 38 | )
|
65 | 39 |
|
66 |
| - @patch( |
67 |
| - "uniflow.op.extract.load.txt_op.read_file", |
68 |
| - return_value="mocked\n\nfile\n\ncontent", |
69 |
| - ) |
70 |
| - def test_call_with_multiple_nodes(self, mock_read_file): |
71 |
| - # arrange |
72 |
| - node1 = Node(name="node1", value_dict={"filename": "mocked_file_path1"}) |
73 |
| - node2 = Node(name="node2", value_dict={"filename": "mocked_file_path2"}) |
74 |
| - nodes = [node1, node2] |
75 | 40 |
|
76 |
| - # act |
77 |
| - output_nodes = self.extract_txt_flow.run(nodes) |
78 |
| - |
79 |
| - # assert |
80 |
| - mock_read_file.assert_has_calls( |
81 |
| - [call("mocked_file_path1"), call("mocked_file_path2")], any_order=True |
82 |
| - ) |
83 |
| - self.assertEqual(len(output_nodes), 2) |
84 |
| - self.assertEqual( |
85 |
| - output_nodes[0].value_dict["text"], ["mocked", "file", "content"] |
86 |
| - ) |
87 |
| - self.assertEqual( |
88 |
| - output_nodes[1].value_dict["text"], ["mocked", "file", "content"] |
89 |
| - ) |
| 41 | +if __name__ == "__main__": |
| 42 | + unittest.main() |
0 commit comments