mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Fix vultr tests (#11477)
This commit is contained in:
parent
c714658384
commit
ff9688bb7a
@ -1,5 +1,8 @@
|
|||||||
"""Test the Vultr binary sensor platform."""
|
"""Test the Vultr binary sensor platform."""
|
||||||
|
import json
|
||||||
import unittest
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
import requests_mock
|
import requests_mock
|
||||||
import pytest
|
import pytest
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -50,10 +53,6 @@ class TestVultrBinarySensorSetup(unittest.TestCase):
|
|||||||
"""Stop our started services."""
|
"""Stop our started services."""
|
||||||
self.hass.stop()
|
self.hass.stop()
|
||||||
|
|
||||||
def test_failed_hub(self):
|
|
||||||
"""Test a hub setup failure."""
|
|
||||||
base_vultr.setup(self.hass, VALID_CONFIG)
|
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
@requests_mock.Mocker()
|
||||||
def test_binary_sensor(self, mock):
|
def test_binary_sensor(self, mock):
|
||||||
"""Test successful instance."""
|
"""Test successful instance."""
|
||||||
@ -61,12 +60,12 @@ class TestVultrBinarySensorSetup(unittest.TestCase):
|
|||||||
'https://api.vultr.com/v1/account/info?api_key=ABCDEFG1234567',
|
'https://api.vultr.com/v1/account/info?api_key=ABCDEFG1234567',
|
||||||
text=load_fixture('vultr_account_info.json'))
|
text=load_fixture('vultr_account_info.json'))
|
||||||
|
|
||||||
mock.get(
|
with patch(
|
||||||
'https://api.vultr.com/v1/server/list?api_key=ABCDEFG1234567',
|
'vultr.Vultr.server_list',
|
||||||
text=load_fixture('vultr_server_list.json'))
|
return_value=json.loads(
|
||||||
|
load_fixture('vultr_server_list.json'))):
|
||||||
# Setup hub
|
# Setup hub
|
||||||
base_vultr.setup(self.hass, VALID_CONFIG)
|
base_vultr.setup(self.hass, VALID_CONFIG)
|
||||||
|
|
||||||
# Setup each of our test configs
|
# Setup each of our test configs
|
||||||
for config in self.configs:
|
for config in self.configs:
|
||||||
@ -137,11 +136,12 @@ class TestVultrBinarySensorSetup(unittest.TestCase):
|
|||||||
'https://api.vultr.com/v1/account/info?api_key=ABCDEFG1234567',
|
'https://api.vultr.com/v1/account/info?api_key=ABCDEFG1234567',
|
||||||
text=load_fixture('vultr_account_info.json'))
|
text=load_fixture('vultr_account_info.json'))
|
||||||
|
|
||||||
mock.get(
|
with patch(
|
||||||
'https://api.vultr.com/v1/server/list?api_key=ABCDEFG1234567',
|
'vultr.Vultr.server_list',
|
||||||
text=load_fixture('vultr_server_list.json'))
|
return_value=json.loads(
|
||||||
|
load_fixture('vultr_server_list.json'))):
|
||||||
base_vultr.setup(self.hass, VALID_CONFIG)
|
# Setup hub
|
||||||
|
base_vultr.setup(self.hass, VALID_CONFIG)
|
||||||
|
|
||||||
bad_conf = {} # No subscription
|
bad_conf = {} # No subscription
|
||||||
|
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
"""The tests for the Vultr sensor platform."""
|
"""The tests for the Vultr sensor platform."""
|
||||||
import pytest
|
import json
|
||||||
import unittest
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
import requests_mock
|
import requests_mock
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -59,11 +62,12 @@ class TestVultrSensorSetup(unittest.TestCase):
|
|||||||
'https://api.vultr.com/v1/account/info?api_key=ABCDEFG1234567',
|
'https://api.vultr.com/v1/account/info?api_key=ABCDEFG1234567',
|
||||||
text=load_fixture('vultr_account_info.json'))
|
text=load_fixture('vultr_account_info.json'))
|
||||||
|
|
||||||
mock.get(
|
with patch(
|
||||||
'https://api.vultr.com/v1/server/list?api_key=ABCDEFG1234567',
|
'vultr.Vultr.server_list',
|
||||||
text=load_fixture('vultr_server_list.json'))
|
return_value=json.loads(
|
||||||
|
load_fixture('vultr_server_list.json'))):
|
||||||
base_vultr.setup(self.hass, VALID_CONFIG)
|
# Setup hub
|
||||||
|
base_vultr.setup(self.hass, VALID_CONFIG)
|
||||||
|
|
||||||
for config in self.configs:
|
for config in self.configs:
|
||||||
setup = vultr.setup_platform(self.hass,
|
setup = vultr.setup_platform(self.hass,
|
||||||
@ -146,11 +150,12 @@ class TestVultrSensorSetup(unittest.TestCase):
|
|||||||
'https://api.vultr.com/v1/account/info?api_key=ABCDEFG1234567',
|
'https://api.vultr.com/v1/account/info?api_key=ABCDEFG1234567',
|
||||||
text=load_fixture('vultr_account_info.json'))
|
text=load_fixture('vultr_account_info.json'))
|
||||||
|
|
||||||
mock.get(
|
with patch(
|
||||||
'https://api.vultr.com/v1/server/list?api_key=ABCDEFG1234567',
|
'vultr.Vultr.server_list',
|
||||||
text=load_fixture('vultr_server_list.json'))
|
return_value=json.loads(
|
||||||
|
load_fixture('vultr_server_list.json'))):
|
||||||
base_vultr.setup(self.hass, VALID_CONFIG)
|
# Setup hub
|
||||||
|
base_vultr.setup(self.hass, VALID_CONFIG)
|
||||||
|
|
||||||
bad_conf = {
|
bad_conf = {
|
||||||
CONF_MONITORED_CONDITIONS: vultr.MONITORED_CONDITIONS,
|
CONF_MONITORED_CONDITIONS: vultr.MONITORED_CONDITIONS,
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
"""Test the Vultr switch platform."""
|
"""Test the Vultr switch platform."""
|
||||||
|
import json
|
||||||
import unittest
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
import requests_mock
|
import requests_mock
|
||||||
import pytest
|
import pytest
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -57,12 +60,12 @@ class TestVultrSwitchSetup(unittest.TestCase):
|
|||||||
'https://api.vultr.com/v1/account/info?api_key=ABCDEFG1234567',
|
'https://api.vultr.com/v1/account/info?api_key=ABCDEFG1234567',
|
||||||
text=load_fixture('vultr_account_info.json'))
|
text=load_fixture('vultr_account_info.json'))
|
||||||
|
|
||||||
mock.get(
|
with patch(
|
||||||
'https://api.vultr.com/v1/server/list?api_key=ABCDEFG1234567',
|
'vultr.Vultr.server_list',
|
||||||
text=load_fixture('vultr_server_list.json'))
|
return_value=json.loads(
|
||||||
|
load_fixture('vultr_server_list.json'))):
|
||||||
# Setup hub
|
# Setup hub
|
||||||
base_vultr.setup(self.hass, VALID_CONFIG)
|
base_vultr.setup(self.hass, VALID_CONFIG)
|
||||||
|
|
||||||
# Setup each of our test configs
|
# Setup each of our test configs
|
||||||
for config in self.configs:
|
for config in self.configs:
|
||||||
@ -128,36 +131,30 @@ class TestVultrSwitchSetup(unittest.TestCase):
|
|||||||
@requests_mock.Mocker()
|
@requests_mock.Mocker()
|
||||||
def test_turn_on(self, mock):
|
def test_turn_on(self, mock):
|
||||||
"""Test turning a subscription on."""
|
"""Test turning a subscription on."""
|
||||||
mock.get(
|
with patch(
|
||||||
'https://api.vultr.com/v1/server/list?api_key=ABCDEFG1234567',
|
'vultr.Vultr.server_list',
|
||||||
text=load_fixture('vultr_server_list.json'))
|
return_value=json.loads(load_fixture('vultr_server_list.json'))), \
|
||||||
|
patch('vultr.Vultr.server_start') as mock_start:
|
||||||
|
for device in self.DEVICES:
|
||||||
|
if device.name == 'Failed Server':
|
||||||
|
device.turn_on()
|
||||||
|
|
||||||
mock.post(
|
# Turn on
|
||||||
'https://api.vultr.com/v1/server/start?api_key=ABCDEFG1234567')
|
self.assertEqual(1, mock_start.call_count)
|
||||||
|
|
||||||
for device in self.DEVICES:
|
|
||||||
if device.name == 'Failed Server':
|
|
||||||
device.turn_on()
|
|
||||||
|
|
||||||
# Turn on, force date update
|
|
||||||
self.assertEqual(2, mock.call_count)
|
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
@requests_mock.Mocker()
|
||||||
def test_turn_off(self, mock):
|
def test_turn_off(self, mock):
|
||||||
"""Test turning a subscription off."""
|
"""Test turning a subscription off."""
|
||||||
mock.get(
|
with patch(
|
||||||
'https://api.vultr.com/v1/server/list?api_key=ABCDEFG1234567',
|
'vultr.Vultr.server_list',
|
||||||
text=load_fixture('vultr_server_list.json'))
|
return_value=json.loads(load_fixture('vultr_server_list.json'))), \
|
||||||
|
patch('vultr.Vultr.server_halt') as mock_halt:
|
||||||
|
for device in self.DEVICES:
|
||||||
|
if device.name == 'A Server':
|
||||||
|
device.turn_off()
|
||||||
|
|
||||||
mock.post(
|
# Turn off
|
||||||
'https://api.vultr.com/v1/server/halt?api_key=ABCDEFG1234567')
|
self.assertEqual(1, mock_halt.call_count)
|
||||||
|
|
||||||
for device in self.DEVICES:
|
|
||||||
if device.name == 'A Server':
|
|
||||||
device.turn_off()
|
|
||||||
|
|
||||||
# Turn off, force update
|
|
||||||
self.assertEqual(2, mock.call_count)
|
|
||||||
|
|
||||||
def test_invalid_switch_config(self):
|
def test_invalid_switch_config(self):
|
||||||
"""Test config type failures."""
|
"""Test config type failures."""
|
||||||
@ -173,11 +170,12 @@ class TestVultrSwitchSetup(unittest.TestCase):
|
|||||||
'https://api.vultr.com/v1/account/info?api_key=ABCDEFG1234567',
|
'https://api.vultr.com/v1/account/info?api_key=ABCDEFG1234567',
|
||||||
text=load_fixture('vultr_account_info.json'))
|
text=load_fixture('vultr_account_info.json'))
|
||||||
|
|
||||||
mock.get(
|
with patch(
|
||||||
'https://api.vultr.com/v1/server/list?api_key=ABCDEFG1234567',
|
'vultr.Vultr.server_list',
|
||||||
text=load_fixture('vultr_server_list.json'))
|
return_value=json.loads(
|
||||||
|
load_fixture('vultr_server_list.json'))):
|
||||||
base_vultr.setup(self.hass, VALID_CONFIG)
|
# Setup hub
|
||||||
|
base_vultr.setup(self.hass, VALID_CONFIG)
|
||||||
|
|
||||||
bad_conf = {} # No subscription
|
bad_conf = {} # No subscription
|
||||||
|
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
"""The tests for the Vultr component."""
|
"""The tests for the Vultr component."""
|
||||||
|
from copy import deepcopy
|
||||||
|
import json
|
||||||
import unittest
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
import requests_mock
|
import requests_mock
|
||||||
|
|
||||||
from copy import deepcopy
|
|
||||||
from homeassistant import setup
|
from homeassistant import setup
|
||||||
import homeassistant.components.vultr as vultr
|
import homeassistant.components.vultr as vultr
|
||||||
|
|
||||||
@ -31,14 +34,11 @@ class TestVultr(unittest.TestCase):
|
|||||||
@requests_mock.Mocker()
|
@requests_mock.Mocker()
|
||||||
def test_setup(self, mock):
|
def test_setup(self, mock):
|
||||||
"""Test successful setup."""
|
"""Test successful setup."""
|
||||||
mock.get(
|
with patch(
|
||||||
'https://api.vultr.com/v1/account/info?api_key=ABCDEFG1234567',
|
'vultr.Vultr.server_list',
|
||||||
text=load_fixture('vultr_account_info.json'))
|
return_value=json.loads(
|
||||||
mock.get(
|
load_fixture('vultr_server_list.json'))):
|
||||||
'https://api.vultr.com/v1/server/list?api_key=ABCDEFG1234567',
|
response = vultr.setup(self.hass, self.config)
|
||||||
text=load_fixture('vultr_server_list.json'))
|
|
||||||
|
|
||||||
response = vultr.setup(self.hass, self.config)
|
|
||||||
self.assertTrue(response)
|
self.assertTrue(response)
|
||||||
|
|
||||||
def test_setup_no_api_key(self):
|
def test_setup_no_api_key(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user