Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add txt_op unit test #177

Merged
merged 4 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added tests/op/extract/__init__.py
Empty file.
Empty file.
73 changes: 73 additions & 0 deletions tests/op/extract/load/test_txt_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import unittest
from unittest.mock import call, patch

from uniflow.node import Node
from uniflow.op.extract.load.txt_op import ExtractTxtOp


class TestExtractTxtOp(unittest.TestCase):
def setUp(self):
self.extract_txt_op = ExtractTxtOp(name="extract_txt_op")

@patch(
"uniflow.op.extract.load.txt_op.read_file", return_value="mocked file content"
)
def test_call_with_empty_node(self, mock_read_file):
# arrange
nodes = []

# act
output_nodes = self.extract_txt_op(nodes)

# assert
mock_read_file.assert_not_called()
self.assertEqual(len(output_nodes), 0)

@patch(
"uniflow.op.extract.load.txt_op.read_file", return_value="mocked file content"
)
def test_call_with_node_without_filename(self, mock_read_file):
# arrange
node = Node(name="node1", value_dict={})

# act
with self.assertRaises(KeyError):
self.extract_txt_op([node])

# assert
mock_read_file.assert_not_called()

@patch(
"uniflow.op.extract.load.txt_op.read_file", return_value="mocked file content"
)
def test_call_with_node(self, mock_read_file):
# arrange
node = Node(name="node1", value_dict={"filename": "mocked_file_path"})

# act
output_nodes = self.extract_txt_op([node])

# assert
mock_read_file.assert_called_once_with("mocked_file_path")
self.assertEqual(len(output_nodes), 1)
self.assertEqual(output_nodes[0].value_dict["text"], "mocked file content")

@patch(
"uniflow.op.extract.load.txt_op.read_file", return_value="mocked file content"
)
def test_call_with_multiple_nodes(self, mock_read_file):
# arrange
node1 = Node(name="node1", value_dict={"filename": "mocked_file_path1"})
node2 = Node(name="node2", value_dict={"filename": "mocked_file_path2"})
nodes = [node1, node2]

# act
output_nodes = self.extract_txt_op(nodes)

# assert
mock_read_file.assert_has_calls(
[call("mocked_file_path1"), call("mocked_file_path2")], any_order=True
)
self.assertEqual(len(output_nodes), 2)
self.assertEqual(output_nodes[0].value_dict["text"], "mocked file content")
self.assertEqual(output_nodes[1].value_dict["text"], "mocked file content")
Empty file.
28 changes: 0 additions & 28 deletions uniflow/op/extract/load/txt_op.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Extract txt op."""

import copy
import re
from typing import Sequence

from uniflow.node import Node
Expand Down Expand Up @@ -33,30 +32,3 @@ def __call__(self, nodes: Sequence[Node]) -> Sequence[Node]:
)
)
return output_nodes


class ProcessTxtOp(Op):
"""Process txt Op Class."""

def __call__(self, nodes: Sequence[Node]) -> Sequence[Node]:
"""Run Model Op.

Args:
nodes (Sequence[Node]): Nodes to run.

Returns:
Sequence[Node]: Nodes after running.
"""
output_nodes = []
for node in nodes:
value_dict = copy.deepcopy(node.value_dict)
text = value_dict["text"]
text = re.split(r"\s*\n\s*", text.strip())
output_nodes.append(
Node(
name=self.unique_name(),
value_dict={"text": text},
prev_nodes=[node],
)
)
return output_nodes
Loading