mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Rewrite Facebook unit test to pytest style test function (#41671)
* refactoring - Rewrite Facebook unit test to pytest style test function * Modifying email author * modified code formating * Run CI
This commit is contained in:
parent
d1041efedf
commit
b6855816ed
@ -1,29 +1,26 @@
|
|||||||
"""The test for the Facebook notify module."""
|
"""The test for the Facebook notify module."""
|
||||||
import unittest
|
import pytest
|
||||||
|
|
||||||
import requests_mock
|
import requests_mock
|
||||||
|
|
||||||
# import homeassistant.components.facebook as facebook
|
import homeassistant.components.facebook.notify as fb
|
||||||
import homeassistant.components.facebook.notify as facebook
|
|
||||||
|
|
||||||
|
|
||||||
class TestFacebook(unittest.TestCase):
|
@pytest.fixture
|
||||||
"""Tests for Facebook notification service."""
|
def facebook():
|
||||||
|
"""Fixture for facebook."""
|
||||||
def setUp(self):
|
|
||||||
"""Set up test variables."""
|
|
||||||
access_token = "page-access-token"
|
access_token = "page-access-token"
|
||||||
self.facebook = facebook.FacebookNotificationService(access_token)
|
return fb.FacebookNotificationService(access_token)
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
|
||||||
def test_send_simple_message(self, mock):
|
async def test_send_simple_message(hass, facebook):
|
||||||
"""Test sending a simple message with success."""
|
"""Test sending a simple message with success."""
|
||||||
mock.register_uri(requests_mock.POST, facebook.BASE_URL, status_code=200)
|
with requests_mock.Mocker() as mock:
|
||||||
|
mock.register_uri(requests_mock.POST, fb.BASE_URL, status_code=200)
|
||||||
|
|
||||||
message = "This is just a test"
|
message = "This is just a test"
|
||||||
target = ["+15555551234"]
|
target = ["+15555551234"]
|
||||||
|
|
||||||
self.facebook.send_message(message=message, target=target)
|
facebook.send_message(message=message, target=target)
|
||||||
assert mock.called
|
assert mock.called
|
||||||
assert mock.call_count == 1
|
assert mock.call_count == 1
|
||||||
|
|
||||||
@ -38,15 +35,16 @@ class TestFacebook(unittest.TestCase):
|
|||||||
expected_params = {"access_token": ["page-access-token"]}
|
expected_params = {"access_token": ["page-access-token"]}
|
||||||
assert mock.last_request.qs == expected_params
|
assert mock.last_request.qs == expected_params
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
|
||||||
def test_sending_multiple_messages(self, mock):
|
async def test_send_multiple_message(hass, facebook):
|
||||||
"""Test sending a message to multiple targets."""
|
"""Test sending a message to multiple targets."""
|
||||||
mock.register_uri(requests_mock.POST, facebook.BASE_URL, status_code=200)
|
with requests_mock.Mocker() as mock:
|
||||||
|
mock.register_uri(requests_mock.POST, fb.BASE_URL, status_code=200)
|
||||||
|
|
||||||
message = "This is just a test"
|
message = "This is just a test"
|
||||||
targets = ["+15555551234", "+15555551235"]
|
targets = ["+15555551234", "+15555551235"]
|
||||||
|
|
||||||
self.facebook.send_message(message=message, target=targets)
|
facebook.send_message(message=message, target=targets)
|
||||||
assert mock.called
|
assert mock.called
|
||||||
assert mock.call_count == 2
|
assert mock.call_count == 2
|
||||||
|
|
||||||
@ -63,10 +61,11 @@ class TestFacebook(unittest.TestCase):
|
|||||||
expected_params = {"access_token": ["page-access-token"]}
|
expected_params = {"access_token": ["page-access-token"]}
|
||||||
assert request.qs == expected_params
|
assert request.qs == expected_params
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
|
||||||
def test_send_message_attachment(self, mock):
|
async def test_send_message_attachment(hass, facebook):
|
||||||
"""Test sending a message with a remote attachment."""
|
"""Test sending a message with a remote attachment."""
|
||||||
mock.register_uri(requests_mock.POST, facebook.BASE_URL, status_code=200)
|
with requests_mock.Mocker() as mock:
|
||||||
|
mock.register_uri(requests_mock.POST, fb.BASE_URL, status_code=200)
|
||||||
|
|
||||||
message = "This will be thrown away."
|
message = "This will be thrown away."
|
||||||
data = {
|
data = {
|
||||||
@ -77,7 +76,7 @@ class TestFacebook(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
target = ["+15555551234"]
|
target = ["+15555551234"]
|
||||||
|
|
||||||
self.facebook.send_message(message=message, data=data, target=target)
|
facebook.send_message(message=message, data=data, target=target)
|
||||||
assert mock.called
|
assert mock.called
|
||||||
assert mock.call_count == 1
|
assert mock.call_count == 1
|
||||||
|
|
||||||
@ -92,20 +91,20 @@ class TestFacebook(unittest.TestCase):
|
|||||||
expected_params = {"access_token": ["page-access-token"]}
|
expected_params = {"access_token": ["page-access-token"]}
|
||||||
assert mock.last_request.qs == expected_params
|
assert mock.last_request.qs == expected_params
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
async def test_send_targetless_message(hass, facebook):
|
||||||
def test_send_targetless_message(self, mock):
|
|
||||||
"""Test sending a message without a target."""
|
"""Test sending a message without a target."""
|
||||||
mock.register_uri(requests_mock.POST, facebook.BASE_URL, status_code=200)
|
with requests_mock.Mocker() as mock:
|
||||||
|
mock.register_uri(requests_mock.POST, fb.BASE_URL, status_code=200)
|
||||||
|
|
||||||
self.facebook.send_message(message="going nowhere")
|
facebook.send_message(message="going nowhere")
|
||||||
assert not mock.called
|
assert not mock.called
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
async def test_send_message_with_400(hass, facebook):
|
||||||
def test_send_message_with_400(self, mock):
|
|
||||||
"""Test sending a message with a 400 from Facebook."""
|
"""Test sending a message with a 400 from Facebook."""
|
||||||
|
with requests_mock.Mocker() as mock:
|
||||||
mock.register_uri(
|
mock.register_uri(
|
||||||
requests_mock.POST,
|
requests_mock.POST,
|
||||||
facebook.BASE_URL,
|
fb.BASE_URL,
|
||||||
status_code=400,
|
status_code=400,
|
||||||
json={
|
json={
|
||||||
"error": {
|
"error": {
|
||||||
@ -116,6 +115,6 @@ class TestFacebook(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
self.facebook.send_message(message="nope!", target=["+15555551234"])
|
facebook.send_message(message="nope!", target=["+15555551234"])
|
||||||
assert mock.called
|
assert mock.called
|
||||||
assert mock.call_count == 1
|
assert mock.call_count == 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user