Migrated coinmarketcap tests to utilise pytests instead of unittests (#42013)

This commit is contained in:
Adam Cooper
2020-10-19 11:08:55 +01:00
committed by GitHub
parent 97b1fffad4
commit fe7c8dd80a

View File

@@ -1,43 +1,42 @@
"""Tests for the CoinMarketCap sensor platform.""" """Tests for the CoinMarketCap sensor platform."""
import json import json
import unittest
import homeassistant.components.sensor as sensor import pytest
from homeassistant.setup import setup_component
from homeassistant.components.sensor import DOMAIN
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
VALID_CONFIG = { VALID_CONFIG = {
"platform": "coinmarketcap", DOMAIN: {
"currency_id": 1027, "platform": "coinmarketcap",
"display_currency": "EUR", "currency_id": 1027,
"display_currency_decimals": 3, "display_currency": "EUR",
"display_currency_decimals": 3,
}
} }
class TestCoinMarketCapSensor(unittest.TestCase): @pytest.fixture
"""Test the CoinMarketCap sensor.""" async def setup_sensor(hass):
"""Set up demo sensor component."""
with assert_setup_component(1, DOMAIN):
with patch(
"coinmarketcap.Market.ticker",
return_value=json.loads(load_fixture("coinmarketcap.json")),
):
await async_setup_component(hass, DOMAIN, VALID_CONFIG)
await hass.async_block_till_done()
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.config = VALID_CONFIG
self.addCleanup(self.hass.stop)
@patch( async def test_setup(hass, setup_sensor):
"coinmarketcap.Market.ticker", """Test the setup with custom settings."""
return_value=json.loads(load_fixture("coinmarketcap.json")), state = hass.states.get("sensor.ethereum")
) assert state is not None
def test_setup(self, mock_request):
"""Test the setup with custom settings."""
with assert_setup_component(1, sensor.DOMAIN):
assert setup_component(self.hass, sensor.DOMAIN, {"sensor": VALID_CONFIG})
self.hass.block_till_done()
state = self.hass.states.get("sensor.ethereum") assert state.name == "Ethereum"
assert state is not None assert state.state == "493.455"
assert state.attributes.get("symbol") == "ETH"
assert state.state == "493.455" assert state.attributes.get("unit_of_measurement") == "EUR"
assert state.attributes.get("symbol") == "ETH"
assert state.attributes.get("unit_of_measurement") == "EUR"