Skip to content

Commit 6fbcb2b

Browse files
committed
update test for bp
1 parent 1386b69 commit 6fbcb2b

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

src/python/tests/test_bp.py

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,55 @@
11
import pytest
2-
from unittest.mock import Mock
2+
from unittest.mock import Mock, MagicMock
33

44
from bp import FilterPostRoutingRule
55
from message import PostMessage
66
from obj import PostClass
77

8-
@pytest.fixture
9-
def mock_send_request_sync():
10-
return Mock()
8+
class TestFilterPostRoutingRule:
119

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
10+
def test_iris_to_python(self,mock_send_request_sync):
11+
# Create a mock iris.dc.Demo.PostMessage object
12+
iris_post = MagicMock()
13+
iris_post.Post.Title = "Test Title"
14+
iris_post.Post.Selftext = "Test Selftext"
15+
iris_post.Post.Author = "Test Author"
16+
iris_post.Post.Url = "https://www.example.com"
17+
iris_post.Post.CreatedUTC = 1234567890
18+
iris_post.Post.OriginalJSON = {"key": "value"}
1819

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)
20+
# Create a FilterPostRoutingRule object
21+
fpr = FilterPostRoutingRule()
22+
fpr.on_python_message = mock_send_request_sync
2123

22-
rule.on_python_message(message)
24+
# Call the iris_to_python method with the mock iris.dc.Demo.PostMessage object
25+
fpr.iris_to_python(iris_post)
2326

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'
27+
# Check that the result is a PostMessage object with the correct attributes
28+
assert mock_send_request_sync.called
29+
30+
# Check that the result is a PostMessage object with the correct attributes
31+
assert mock_send_request_sync.call_args[0][0] == PostMessage(post=PostClass(title='Test Title', selftext='Test Selftext', author='Test Author', url='https://www.example.com', created_utc=1234567890, original_json={'key': 'value'}))
2832

29-
mock_send_request_sync.assert_called_once_with(target, excepted)
33+
34+
@pytest.fixture
35+
def mock_send_request_sync(self):
36+
return Mock()
37+
38+
def test_on_python_message(self,mock_send_request_sync):
39+
rule = FilterPostRoutingRule()
40+
# fix the target to avoid the error
41+
target = 'Python.FileOperation'
42+
rule.target = target
43+
rule.send_request_sync = mock_send_request_sync
44+
45+
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='{}')
46+
message = PostMessage(post=post)
47+
48+
rule.on_python_message(message)
49+
50+
# assert that the send_request_sync has been called once
51+
excepted = message
52+
excepted.to_email_address = '[email protected]'
53+
excepted.found = 'Dog'
54+
55+
mock_send_request_sync.assert_called_once_with(target, excepted)

0 commit comments

Comments
 (0)