Rewrite pushbullet unittest tests to pytest style test function (#41266)
This commit is contained in:
parent
06306f5dfe
commit
8adc5ee452
1 changed files with 246 additions and 274 deletions
|
@ -1,35 +1,28 @@
|
||||||
"""The tests for the pushbullet notification platform."""
|
"""The tests for the pushbullet notification platform."""
|
||||||
import json
|
import json
|
||||||
import unittest
|
|
||||||
|
|
||||||
from pushbullet import PushBullet
|
from pushbullet import PushBullet
|
||||||
import requests_mock
|
import pytest
|
||||||
|
|
||||||
import homeassistant.components.notify as notify
|
import homeassistant.components.notify as notify
|
||||||
from homeassistant.setup import setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.async_mock import patch
|
from tests.async_mock import patch
|
||||||
from tests.common import assert_setup_component, get_test_home_assistant, load_fixture
|
from tests.common import assert_setup_component, load_fixture
|
||||||
|
|
||||||
|
|
||||||
class TestPushBullet(unittest.TestCase):
|
@pytest.fixture
|
||||||
"""Tests the Pushbullet Component."""
|
def mock_pushbullet():
|
||||||
|
"""Mock pushbullet."""
|
||||||
def setUp(self):
|
with patch.object(
|
||||||
"""Initialize values for this test case class."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
self.addCleanup(self.tear_down_cleanup)
|
|
||||||
|
|
||||||
def tear_down_cleanup(self):
|
|
||||||
"""Stop everything that we started."""
|
|
||||||
self.hass.stop()
|
|
||||||
|
|
||||||
@patch.object(
|
|
||||||
PushBullet,
|
PushBullet,
|
||||||
"_get_data",
|
"_get_data",
|
||||||
return_value=json.loads(load_fixture("pushbullet_devices.json")),
|
return_value=json.loads(load_fixture("pushbullet_devices.json")),
|
||||||
)
|
):
|
||||||
def test_pushbullet_config(self, mock__get_data):
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
async def test_pushbullet_config(hass, mock_pushbullet):
|
||||||
"""Test setup."""
|
"""Test setup."""
|
||||||
config = {
|
config = {
|
||||||
notify.DOMAIN: {
|
notify.DOMAIN: {
|
||||||
|
@ -39,23 +32,21 @@ class TestPushBullet(unittest.TestCase):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
with assert_setup_component(1) as handle_config:
|
with assert_setup_component(1) as handle_config:
|
||||||
assert setup_component(self.hass, notify.DOMAIN, config)
|
assert await async_setup_component(hass, notify.DOMAIN, config)
|
||||||
|
await hass.async_block_till_done()
|
||||||
assert handle_config[notify.DOMAIN]
|
assert handle_config[notify.DOMAIN]
|
||||||
|
|
||||||
def test_pushbullet_config_bad(self):
|
|
||||||
|
async def test_pushbullet_config_bad(hass):
|
||||||
"""Test set up the platform with bad/missing configuration."""
|
"""Test set up the platform with bad/missing configuration."""
|
||||||
config = {notify.DOMAIN: {"platform": "pushbullet"}}
|
config = {notify.DOMAIN: {"platform": "pushbullet"}}
|
||||||
with assert_setup_component(0) as handle_config:
|
with assert_setup_component(0) as handle_config:
|
||||||
assert setup_component(self.hass, notify.DOMAIN, config)
|
assert await async_setup_component(hass, notify.DOMAIN, config)
|
||||||
|
await hass.async_block_till_done()
|
||||||
assert not handle_config[notify.DOMAIN]
|
assert not handle_config[notify.DOMAIN]
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
|
||||||
@patch.object(
|
async def test_pushbullet_push_default(hass, requests_mock, mock_pushbullet):
|
||||||
PushBullet,
|
|
||||||
"_get_data",
|
|
||||||
return_value=json.loads(load_fixture("pushbullet_devices.json")),
|
|
||||||
)
|
|
||||||
def test_pushbullet_push_default(self, mock, mock__get_data):
|
|
||||||
"""Test pushbullet push to default target."""
|
"""Test pushbullet push to default target."""
|
||||||
config = {
|
config = {
|
||||||
notify.DOMAIN: {
|
notify.DOMAIN: {
|
||||||
|
@ -65,30 +56,26 @@ class TestPushBullet(unittest.TestCase):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
with assert_setup_component(1) as handle_config:
|
with assert_setup_component(1) as handle_config:
|
||||||
assert setup_component(self.hass, notify.DOMAIN, config)
|
assert await async_setup_component(hass, notify.DOMAIN, config)
|
||||||
|
await hass.async_block_till_done()
|
||||||
assert handle_config[notify.DOMAIN]
|
assert handle_config[notify.DOMAIN]
|
||||||
mock.register_uri(
|
requests_mock.register_uri(
|
||||||
requests_mock.POST,
|
"POST",
|
||||||
"https://api.pushbullet.com/v2/pushes",
|
"https://api.pushbullet.com/v2/pushes",
|
||||||
status_code=200,
|
status_code=200,
|
||||||
json={"mock_response": "Ok"},
|
json={"mock_response": "Ok"},
|
||||||
)
|
)
|
||||||
data = {"title": "Test Title", "message": "Test Message"}
|
data = {"title": "Test Title", "message": "Test Message"}
|
||||||
self.hass.services.call(notify.DOMAIN, "test", data)
|
await hass.services.async_call(notify.DOMAIN, "test", data)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert mock.called
|
assert requests_mock.called
|
||||||
assert mock.call_count == 1
|
assert requests_mock.call_count == 1
|
||||||
|
|
||||||
expected_body = {"body": "Test Message", "title": "Test Title", "type": "note"}
|
expected_body = {"body": "Test Message", "title": "Test Title", "type": "note"}
|
||||||
assert mock.last_request.json() == expected_body
|
assert requests_mock.last_request.json() == expected_body
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
|
||||||
@patch.object(
|
async def test_pushbullet_push_device(hass, requests_mock, mock_pushbullet):
|
||||||
PushBullet,
|
|
||||||
"_get_data",
|
|
||||||
return_value=json.loads(load_fixture("pushbullet_devices.json")),
|
|
||||||
)
|
|
||||||
def test_pushbullet_push_device(self, mock, mock__get_data):
|
|
||||||
"""Test pushbullet push to default target."""
|
"""Test pushbullet push to default target."""
|
||||||
config = {
|
config = {
|
||||||
notify.DOMAIN: {
|
notify.DOMAIN: {
|
||||||
|
@ -98,10 +85,11 @@ class TestPushBullet(unittest.TestCase):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
with assert_setup_component(1) as handle_config:
|
with assert_setup_component(1) as handle_config:
|
||||||
assert setup_component(self.hass, notify.DOMAIN, config)
|
assert await async_setup_component(hass, notify.DOMAIN, config)
|
||||||
|
await hass.async_block_till_done()
|
||||||
assert handle_config[notify.DOMAIN]
|
assert handle_config[notify.DOMAIN]
|
||||||
mock.register_uri(
|
requests_mock.register_uri(
|
||||||
requests_mock.POST,
|
"POST",
|
||||||
"https://api.pushbullet.com/v2/pushes",
|
"https://api.pushbullet.com/v2/pushes",
|
||||||
status_code=200,
|
status_code=200,
|
||||||
json={"mock_response": "Ok"},
|
json={"mock_response": "Ok"},
|
||||||
|
@ -111,10 +99,10 @@ class TestPushBullet(unittest.TestCase):
|
||||||
"message": "Test Message",
|
"message": "Test Message",
|
||||||
"target": ["device/DESKTOP"],
|
"target": ["device/DESKTOP"],
|
||||||
}
|
}
|
||||||
self.hass.services.call(notify.DOMAIN, "test", data)
|
await hass.services.async_call(notify.DOMAIN, "test", data)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert mock.called
|
assert requests_mock.called
|
||||||
assert mock.call_count == 1
|
assert requests_mock.call_count == 1
|
||||||
|
|
||||||
expected_body = {
|
expected_body = {
|
||||||
"body": "Test Message",
|
"body": "Test Message",
|
||||||
|
@ -122,15 +110,10 @@ class TestPushBullet(unittest.TestCase):
|
||||||
"title": "Test Title",
|
"title": "Test Title",
|
||||||
"type": "note",
|
"type": "note",
|
||||||
}
|
}
|
||||||
assert mock.last_request.json() == expected_body
|
assert requests_mock.last_request.json() == expected_body
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
|
||||||
@patch.object(
|
async def test_pushbullet_push_devices(hass, requests_mock, mock_pushbullet):
|
||||||
PushBullet,
|
|
||||||
"_get_data",
|
|
||||||
return_value=json.loads(load_fixture("pushbullet_devices.json")),
|
|
||||||
)
|
|
||||||
def test_pushbullet_push_devices(self, mock, mock__get_data):
|
|
||||||
"""Test pushbullet push to default target."""
|
"""Test pushbullet push to default target."""
|
||||||
config = {
|
config = {
|
||||||
notify.DOMAIN: {
|
notify.DOMAIN: {
|
||||||
|
@ -140,10 +123,11 @@ class TestPushBullet(unittest.TestCase):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
with assert_setup_component(1) as handle_config:
|
with assert_setup_component(1) as handle_config:
|
||||||
assert setup_component(self.hass, notify.DOMAIN, config)
|
assert await async_setup_component(hass, notify.DOMAIN, config)
|
||||||
|
await hass.async_block_till_done()
|
||||||
assert handle_config[notify.DOMAIN]
|
assert handle_config[notify.DOMAIN]
|
||||||
mock.register_uri(
|
requests_mock.register_uri(
|
||||||
requests_mock.POST,
|
"POST",
|
||||||
"https://api.pushbullet.com/v2/pushes",
|
"https://api.pushbullet.com/v2/pushes",
|
||||||
status_code=200,
|
status_code=200,
|
||||||
json={"mock_response": "Ok"},
|
json={"mock_response": "Ok"},
|
||||||
|
@ -153,11 +137,11 @@ class TestPushBullet(unittest.TestCase):
|
||||||
"message": "Test Message",
|
"message": "Test Message",
|
||||||
"target": ["device/DESKTOP", "device/My iPhone"],
|
"target": ["device/DESKTOP", "device/My iPhone"],
|
||||||
}
|
}
|
||||||
self.hass.services.call(notify.DOMAIN, "test", data)
|
await hass.services.async_call(notify.DOMAIN, "test", data)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert mock.called
|
assert requests_mock.called
|
||||||
assert mock.call_count == 2
|
assert requests_mock.call_count == 2
|
||||||
assert len(mock.request_history) == 2
|
assert len(requests_mock.request_history) == 2
|
||||||
|
|
||||||
expected_body = {
|
expected_body = {
|
||||||
"body": "Test Message",
|
"body": "Test Message",
|
||||||
|
@ -165,22 +149,17 @@ class TestPushBullet(unittest.TestCase):
|
||||||
"title": "Test Title",
|
"title": "Test Title",
|
||||||
"type": "note",
|
"type": "note",
|
||||||
}
|
}
|
||||||
assert mock.request_history[0].json() == expected_body
|
assert requests_mock.request_history[0].json() == expected_body
|
||||||
expected_body = {
|
expected_body = {
|
||||||
"body": "Test Message",
|
"body": "Test Message",
|
||||||
"device_iden": "identity2",
|
"device_iden": "identity2",
|
||||||
"title": "Test Title",
|
"title": "Test Title",
|
||||||
"type": "note",
|
"type": "note",
|
||||||
}
|
}
|
||||||
assert mock.request_history[1].json() == expected_body
|
assert requests_mock.request_history[1].json() == expected_body
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
|
||||||
@patch.object(
|
async def test_pushbullet_push_email(hass, requests_mock, mock_pushbullet):
|
||||||
PushBullet,
|
|
||||||
"_get_data",
|
|
||||||
return_value=json.loads(load_fixture("pushbullet_devices.json")),
|
|
||||||
)
|
|
||||||
def test_pushbullet_push_email(self, mock, mock__get_data):
|
|
||||||
"""Test pushbullet push to default target."""
|
"""Test pushbullet push to default target."""
|
||||||
config = {
|
config = {
|
||||||
notify.DOMAIN: {
|
notify.DOMAIN: {
|
||||||
|
@ -190,10 +169,11 @@ class TestPushBullet(unittest.TestCase):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
with assert_setup_component(1) as handle_config:
|
with assert_setup_component(1) as handle_config:
|
||||||
assert setup_component(self.hass, notify.DOMAIN, config)
|
assert await async_setup_component(hass, notify.DOMAIN, config)
|
||||||
|
await hass.async_block_till_done()
|
||||||
assert handle_config[notify.DOMAIN]
|
assert handle_config[notify.DOMAIN]
|
||||||
mock.register_uri(
|
requests_mock.register_uri(
|
||||||
requests_mock.POST,
|
"POST",
|
||||||
"https://api.pushbullet.com/v2/pushes",
|
"https://api.pushbullet.com/v2/pushes",
|
||||||
status_code=200,
|
status_code=200,
|
||||||
json={"mock_response": "Ok"},
|
json={"mock_response": "Ok"},
|
||||||
|
@ -203,11 +183,11 @@ class TestPushBullet(unittest.TestCase):
|
||||||
"message": "Test Message",
|
"message": "Test Message",
|
||||||
"target": ["email/user@host.net"],
|
"target": ["email/user@host.net"],
|
||||||
}
|
}
|
||||||
self.hass.services.call(notify.DOMAIN, "test", data)
|
await hass.services.async_call(notify.DOMAIN, "test", data)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert mock.called
|
assert requests_mock.called
|
||||||
assert mock.call_count == 1
|
assert requests_mock.call_count == 1
|
||||||
assert len(mock.request_history) == 1
|
assert len(requests_mock.request_history) == 1
|
||||||
|
|
||||||
expected_body = {
|
expected_body = {
|
||||||
"body": "Test Message",
|
"body": "Test Message",
|
||||||
|
@ -215,15 +195,10 @@ class TestPushBullet(unittest.TestCase):
|
||||||
"title": "Test Title",
|
"title": "Test Title",
|
||||||
"type": "note",
|
"type": "note",
|
||||||
}
|
}
|
||||||
assert mock.request_history[0].json() == expected_body
|
assert requests_mock.request_history[0].json() == expected_body
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
|
||||||
@patch.object(
|
async def test_pushbullet_push_mixed(hass, requests_mock, mock_pushbullet):
|
||||||
PushBullet,
|
|
||||||
"_get_data",
|
|
||||||
return_value=json.loads(load_fixture("pushbullet_devices.json")),
|
|
||||||
)
|
|
||||||
def test_pushbullet_push_mixed(self, mock, mock__get_data):
|
|
||||||
"""Test pushbullet push to default target."""
|
"""Test pushbullet push to default target."""
|
||||||
config = {
|
config = {
|
||||||
notify.DOMAIN: {
|
notify.DOMAIN: {
|
||||||
|
@ -233,10 +208,11 @@ class TestPushBullet(unittest.TestCase):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
with assert_setup_component(1) as handle_config:
|
with assert_setup_component(1) as handle_config:
|
||||||
assert setup_component(self.hass, notify.DOMAIN, config)
|
assert await async_setup_component(hass, notify.DOMAIN, config)
|
||||||
|
await hass.async_block_till_done()
|
||||||
assert handle_config[notify.DOMAIN]
|
assert handle_config[notify.DOMAIN]
|
||||||
mock.register_uri(
|
requests_mock.register_uri(
|
||||||
requests_mock.POST,
|
"POST",
|
||||||
"https://api.pushbullet.com/v2/pushes",
|
"https://api.pushbullet.com/v2/pushes",
|
||||||
status_code=200,
|
status_code=200,
|
||||||
json={"mock_response": "Ok"},
|
json={"mock_response": "Ok"},
|
||||||
|
@ -246,11 +222,11 @@ class TestPushBullet(unittest.TestCase):
|
||||||
"message": "Test Message",
|
"message": "Test Message",
|
||||||
"target": ["device/DESKTOP", "email/user@host.net"],
|
"target": ["device/DESKTOP", "email/user@host.net"],
|
||||||
}
|
}
|
||||||
self.hass.services.call(notify.DOMAIN, "test", data)
|
await hass.services.async_call(notify.DOMAIN, "test", data)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert mock.called
|
assert requests_mock.called
|
||||||
assert mock.call_count == 2
|
assert requests_mock.call_count == 2
|
||||||
assert len(mock.request_history) == 2
|
assert len(requests_mock.request_history) == 2
|
||||||
|
|
||||||
expected_body = {
|
expected_body = {
|
||||||
"body": "Test Message",
|
"body": "Test Message",
|
||||||
|
@ -258,22 +234,17 @@ class TestPushBullet(unittest.TestCase):
|
||||||
"title": "Test Title",
|
"title": "Test Title",
|
||||||
"type": "note",
|
"type": "note",
|
||||||
}
|
}
|
||||||
assert mock.request_history[0].json() == expected_body
|
assert requests_mock.request_history[0].json() == expected_body
|
||||||
expected_body = {
|
expected_body = {
|
||||||
"body": "Test Message",
|
"body": "Test Message",
|
||||||
"email": "user@host.net",
|
"email": "user@host.net",
|
||||||
"title": "Test Title",
|
"title": "Test Title",
|
||||||
"type": "note",
|
"type": "note",
|
||||||
}
|
}
|
||||||
assert mock.request_history[1].json() == expected_body
|
assert requests_mock.request_history[1].json() == expected_body
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
|
||||||
@patch.object(
|
async def test_pushbullet_push_no_file(hass, requests_mock, mock_pushbullet):
|
||||||
PushBullet,
|
|
||||||
"_get_data",
|
|
||||||
return_value=json.loads(load_fixture("pushbullet_devices.json")),
|
|
||||||
)
|
|
||||||
def test_pushbullet_push_no_file(self, mock, mock__get_data):
|
|
||||||
"""Test pushbullet push to default target."""
|
"""Test pushbullet push to default target."""
|
||||||
config = {
|
config = {
|
||||||
notify.DOMAIN: {
|
notify.DOMAIN: {
|
||||||
|
@ -283,10 +254,11 @@ class TestPushBullet(unittest.TestCase):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
with assert_setup_component(1) as handle_config:
|
with assert_setup_component(1) as handle_config:
|
||||||
assert setup_component(self.hass, notify.DOMAIN, config)
|
assert await async_setup_component(hass, notify.DOMAIN, config)
|
||||||
|
await hass.async_block_till_done()
|
||||||
assert handle_config[notify.DOMAIN]
|
assert handle_config[notify.DOMAIN]
|
||||||
mock.register_uri(
|
requests_mock.register_uri(
|
||||||
requests_mock.POST,
|
"POST",
|
||||||
"https://api.pushbullet.com/v2/pushes",
|
"https://api.pushbullet.com/v2/pushes",
|
||||||
status_code=200,
|
status_code=200,
|
||||||
json={"mock_response": "Ok"},
|
json={"mock_response": "Ok"},
|
||||||
|
@ -297,5 +269,5 @@ class TestPushBullet(unittest.TestCase):
|
||||||
"target": ["device/DESKTOP", "device/My iPhone"],
|
"target": ["device/DESKTOP", "device/My iPhone"],
|
||||||
"data": {"file": "not_a_file"},
|
"data": {"file": "not_a_file"},
|
||||||
}
|
}
|
||||||
assert not self.hass.services.call(notify.DOMAIN, "test", data)
|
assert not await hass.services.async_call(notify.DOMAIN, "test", data)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue