Skip to content

Commit 1386b69

Browse files
committed
add unittest
1 parent 3dd4136 commit 1386b69

File tree

5 files changed

+124
-8
lines changed

5 files changed

+124
-8
lines changed

.vscode/settings.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"service": "iris",
1313
"internalPort": 52773
1414
},
15-
"active": true,
15+
"active": false,
1616
"links": {
1717
"Production": "http://localhost:${port}/csp/irisapp/EnsPortal.ProductionConfig.zen?PRODUCTION=dc.Python.Production"
1818
}
@@ -46,6 +46,14 @@
4646
"[python]": {
4747
"editor.defaultFormatter": "ms-python.autopep8"
4848
},
49-
"python.formatting.provider": "none"
49+
"python.formatting.provider": "none",
50+
"python.testing.pytestArgs": [
51+
"src"
52+
],
53+
"python.testing.unittestEnabled": false,
54+
"python.testing.pytestEnabled": true,
55+
"python.analysis.extraPaths": [
56+
"./src/python/reddit"
57+
],
5058

5159
}

src/python/reddit/bp.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from message import PostMessage
44
from obj import PostClass
55

6-
6+
import iris
77

88
class FilterPostRoutingRule(BusinessProcess):
99
"""
@@ -19,18 +19,24 @@ def on_init(self):
1919

2020
return
2121

22+
def iris_to_python(self, request:'iris.dc.Demo.PostMessage'):
23+
24+
request = PostMessage(post=PostClass(title=request.Post.Title,
25+
selftext=request.Post.Selftext,
26+
author=request.Post.Author,
27+
url=request.Post.Url,
28+
created_utc=request.Post.CreatedUTC,
29+
original_json=request.Post.OriginalJSON))
30+
return self.on_python_message(request)
31+
2232
def on_python_message(self, request: PostMessage):
23-
"""
24-
This method is called when a PostMessage is received.
25-
"""
2633
if 'dog'.upper() in request.post.selftext.upper():
2734
request.to_email_address = '[email protected]'
2835
request.found = 'Dog'
2936
if 'cat'.upper() in request.post.selftext.upper():
3037
request.to_email_address = '[email protected]'
3138
request.found = 'Cat'
32-
self.log_info("Found a cat")
39+
3340
if request.found is not None:
3441
self.send_request_sync(self.target,request)
35-
3642
return

src/python/tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import sys
2+
from os.path import dirname as d
3+
from os.path import abspath,join
4+
root_dir = d(d(abspath(__file__)))
5+
# add reddit to path
6+
sys.path.append(join(root_dir,"reddit"))
7+

src/python/tests/test_bo.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from bo import FileOperation
2+
import os
3+
from unittest.mock import patch, mock_open, MagicMock
4+
5+
class TestFileOperation():
6+
7+
def test_on_init(self):
8+
"""
9+
This method is called when the operation is initialized.
10+
"""
11+
# Create a FileOperation instance
12+
operation = FileOperation()
13+
14+
# Set the path attribute
15+
operation.path = "/path/to/directory"
16+
17+
# Use a context manager to temporarily change the current working directory
18+
with patch.object(os, "chdir") as mock_chdir:
19+
# Call the on_init method
20+
operation.on_init()
21+
22+
# Assert that the chdir function was called with the correct argument
23+
mock_chdir.assert_called_once_with("/path/to/directory")
24+
25+
@patch('bo.FileOperation.put_line')
26+
def test_on_post_message(self, mock_put_line):
27+
# Create a mock PostMessage object
28+
post_message = MagicMock()
29+
post_message.post.title = "Test Title"
30+
post_message.post.author = "Test Author"
31+
post_message.post.url = "http://test.com"
32+
post_message.post.selftext = "Test Text"
33+
post_message.found = "Test Company"
34+
35+
# Create a FileOperation object and call the on_post_message method with the mock PostMessage object
36+
file_operation = FileOperation()
37+
file_operation.on_post_message(post_message)
38+
39+
# assert that the put_line has been called 4 times
40+
assert mock_put_line.call_count == 4
41+
42+
# assert that the put_line has been called with the correct arguments
43+
mock_put_line.call_args_list[0][0][0] == "Test Company.txt"
44+
mock_put_line.call_args_list[0][0][1] == '1970-01-01 01:00:01 : Test Title : Test Author : http://test.com'
45+
mock_put_line.call_args_list[1][0][0] == "Test Company.txt"
46+
mock_put_line.call_args_list[1][0][1] == ""
47+
mock_put_line.call_args_list[2][0][0] == "Test Company.txt"
48+
mock_put_line.call_args_list[2][0][1] == "Test Text"
49+
mock_put_line.call_args_list[3][0][0] == "Test Company.txt"
50+
mock_put_line.call_args_list[3][0][1] == ' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *'
51+
52+
def test_put_line(self):
53+
# create a mock file object
54+
m = mock_open()
55+
56+
# patch the built-in open function with the mock object
57+
with patch('builtins.open', m):
58+
# call the function with some test data
59+
filename = 'test.txt'
60+
string = 'hello world'
61+
FileOperation().put_line(filename, string)
62+
63+
# check if the mock file object was called with the expected arguments
64+
m.assert_called_once_with(filename, 'a', encoding='utf-8')
65+
handle = m()
66+
handle.write.assert_called_once_with(string)

src/python/tests/test_bp.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
from unittest.mock import Mock
3+
4+
from bp import FilterPostRoutingRule
5+
from message import PostMessage
6+
from obj import PostClass
7+
8+
@pytest.fixture
9+
def mock_send_request_sync():
10+
return Mock()
11+
12+
def test_on_python_message(mock_send_request_sync):
13+
rule = FilterPostRoutingRule()
14+
# fix the target to avoid the error
15+
target = 'Python.FileOperation'
16+
rule.target = target
17+
rule.send_request_sync = mock_send_request_sync
18+
19+
post = PostClass(title='Test Post', selftext='This is a test post about dogs.', author='Test Author', url='http://test.com', created_utc=1234567890, original_json='{}')
20+
message = PostMessage(post=post)
21+
22+
rule.on_python_message(message)
23+
24+
# assert that the send_request_sync has been called once
25+
excepted = message
26+
excepted.to_email_address = '[email protected]'
27+
excepted.found = 'Dog'
28+
29+
mock_send_request_sync.assert_called_once_with(target, excepted)

0 commit comments

Comments
 (0)