Improve testing of option flow in Coinbase (#52870)

pull/52748/head
Tom Brien 2021-07-12 15:46:54 +01:00 committed by GitHub
parent 0f6a0f6bcd
commit 9308fa28e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 91 additions and 68 deletions

View File

@ -9,13 +9,6 @@ BAD_CURRENCY = "ETH"
BAD_EXCHANGE_RATE = "ETH"
MOCK_ACCOUNTS_RESPONSE = [
{
"balance": {"amount": "13.38", "currency": GOOD_CURRENCY_3},
"currency": GOOD_CURRENCY_3,
"id": "ABCDEF",
"name": "BTC Wallet",
"native_balance": {"amount": "15.02", "currency": GOOD_CURRENCY_2},
},
{
"balance": {"amount": "0.00001", "currency": GOOD_CURRENCY},
"currency": GOOD_CURRENCY,

View File

@ -19,14 +19,7 @@ from .common import (
mock_get_exchange_rates,
mocked_get_accounts,
)
from .const import (
BAD_CURRENCY,
BAD_EXCHANGE_RATE,
GOOD_CURRENCY,
GOOD_CURRENCY_2,
GOOD_EXCHNAGE_RATE,
GOOD_EXCHNAGE_RATE_2,
)
from .const import BAD_CURRENCY, BAD_EXCHANGE_RATE, GOOD_CURRENCY, GOOD_EXCHNAGE_RATE
from tests.common import MockConfigEntry
@ -144,19 +137,8 @@ async def test_form_catch_all_exception(hass):
assert result2["errors"] == {"base": "unknown"}
async def test_option_good_account_currency(hass):
async def test_option_form(hass):
"""Test we handle a good wallet currency option."""
config_entry = MockConfigEntry(
domain=DOMAIN,
entry_id="abcde12345",
title="Test User",
data={CONF_API_KEY: "123456", CONF_API_TOKEN: "AbCDeF"},
options={
CONF_CURRENCIES: [GOOD_CURRENCY_2],
CONF_EXCHANGE_RATES: [],
},
)
config_entry.add_to_hass(hass)
with patch(
"coinbase.wallet.client.Client.get_current_user",
@ -166,8 +148,11 @@ async def test_option_good_account_currency(hass):
), patch(
"coinbase.wallet.client.Client.get_exchange_rates",
return_value=mock_get_exchange_rates(),
):
assert await hass.config_entries.async_setup(config_entry.entry_id)
), patch(
"homeassistant.components.coinbase.update_listener"
) as mock_update_listener:
config_entry = await init_mock_coinbase(hass)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(config_entry.entry_id)
await hass.async_block_till_done()
@ -175,10 +160,12 @@ async def test_option_good_account_currency(hass):
result["flow_id"],
user_input={
CONF_CURRENCIES: [GOOD_CURRENCY],
CONF_EXCHANGE_RATES: [],
CONF_EXCHANGE_RATES: [GOOD_EXCHNAGE_RATE],
},
)
assert result2["type"] == "create_entry"
assert result2["type"] == "create_entry"
await hass.async_block_till_done()
assert len(mock_update_listener.mock_calls) == 1
async def test_form_bad_account_currency(hass):
@ -207,43 +194,6 @@ async def test_form_bad_account_currency(hass):
assert result2["errors"] == {"base": "currency_unavaliable"}
async def test_option_good_exchange_rate(hass):
"""Test we handle a good exchange rate option."""
config_entry = MockConfigEntry(
domain=DOMAIN,
entry_id="abcde12345",
title="Test User",
data={CONF_API_KEY: "123456", CONF_API_TOKEN: "AbCDeF"},
options={
CONF_CURRENCIES: [],
CONF_EXCHANGE_RATES: [GOOD_EXCHNAGE_RATE_2],
},
)
config_entry.add_to_hass(hass)
with patch(
"coinbase.wallet.client.Client.get_current_user",
return_value=mock_get_current_user(),
), patch(
"coinbase.wallet.client.Client.get_accounts", new=mocked_get_accounts
), patch(
"coinbase.wallet.client.Client.get_exchange_rates",
return_value=mock_get_exchange_rates(),
):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(config_entry.entry_id)
await hass.async_block_till_done()
result2 = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_CURRENCIES: [],
CONF_EXCHANGE_RATES: [GOOD_EXCHNAGE_RATE],
},
)
assert result2["type"] == "create_entry"
async def test_form_bad_exchange_rate(hass):
"""Test we handle a bad exchange rate."""
with patch(

View File

@ -9,6 +9,8 @@ from homeassistant.components.coinbase.const import (
DOMAIN,
)
from homeassistant.const import CONF_API_KEY
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry
from homeassistant.setup import async_setup_component
from .common import (
@ -78,3 +80,81 @@ async def test_unload_entry(hass):
assert entry.state == config_entries.ConfigEntryState.NOT_LOADED
assert not hass.data.get(DOMAIN)
async def test_option_updates(hass: HomeAssistant):
"""Test handling option updates."""
with patch(
"coinbase.wallet.client.Client.get_current_user",
return_value=mock_get_current_user(),
), patch(
"coinbase.wallet.client.Client.get_accounts", new=mocked_get_accounts
), patch(
"coinbase.wallet.client.Client.get_exchange_rates",
return_value=mock_get_exchange_rates(),
):
config_entry = await init_mock_coinbase(hass)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(config_entry.entry_id)
await hass.async_block_till_done()
await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_CURRENCIES: [GOOD_CURRENCY, GOOD_CURRENCY_2],
CONF_EXCHANGE_RATES: [GOOD_EXCHNAGE_RATE, GOOD_EXCHNAGE_RATE_2],
},
)
await hass.async_block_till_done()
registry = entity_registry.async_get(hass)
entities = entity_registry.async_entries_for_config_entry(
registry, config_entry.entry_id
)
assert len(entities) == 4
currencies = [
entity.unique_id.split("-")[-1]
for entity in entities
if "wallet" in entity.unique_id
]
rates = [
entity.unique_id.split("-")[-1]
for entity in entities
if "xe" in entity.unique_id
]
assert currencies == [GOOD_CURRENCY, GOOD_CURRENCY_2]
assert rates == [GOOD_EXCHNAGE_RATE, GOOD_EXCHNAGE_RATE_2]
result = await hass.config_entries.options.async_init(config_entry.entry_id)
await hass.async_block_till_done()
await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_CURRENCIES: [GOOD_CURRENCY],
CONF_EXCHANGE_RATES: [GOOD_EXCHNAGE_RATE],
},
)
await hass.async_block_till_done()
registry = entity_registry.async_get(hass)
entities = entity_registry.async_entries_for_config_entry(
registry, config_entry.entry_id
)
assert len(entities) == 2
currencies = [
entity.unique_id.split("-")[-1]
for entity in entities
if "wallet" in entity.unique_id
]
rates = [
entity.unique_id.split("-")[-1]
for entity in entities
if "xe" in entity.unique_id
]
assert currencies == [GOOD_CURRENCY]
assert rates == [GOOD_EXCHNAGE_RATE]