Move fixtures part 1 (#58902)

This commit is contained in:
Paulus Schoutsen 2021-11-01 20:47:05 -07:00 committed by GitHub
parent 0a94badb72
commit 31153ac155
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
561 changed files with 188 additions and 991 deletions

View File

@ -397,11 +397,22 @@ def async_fire_time_changed(
fire_time_changed = threadsafe_callback_factory(async_fire_time_changed) fire_time_changed = threadsafe_callback_factory(async_fire_time_changed)
def load_fixture(filename): def get_fixture_path(filename: str, integration: str | None = None) -> pathlib.Path:
"""Get path of fixture."""
if integration is None and "/" in filename and not filename.startswith("helpers/"):
integration, filename = filename.split("/", 1)
if integration is None:
return pathlib.Path(__file__).parent.joinpath("fixtures", filename)
else:
return pathlib.Path(__file__).parent.joinpath(
"components", integration, "fixtures", filename
)
def load_fixture(filename, integration=None):
"""Load a fixture.""" """Load a fixture."""
path = os.path.join(os.path.dirname(__file__), "fixtures", filename) return get_fixture_path(filename, integration).read_text()
with open(path, encoding="utf-8") as fptr:
return fptr.read()
def mock_state_change_event(hass, new_state, old_state=None): def mock_state_change_event(hass, new_state, old_state=None):

View File

@ -10,16 +10,18 @@ from tests.components.light.conftest import mock_light_profiles # noqa: F401
def requests_mock_fixture(requests_mock): def requests_mock_fixture(requests_mock):
"""Fixture to provide a requests mocker.""" """Fixture to provide a requests mocker."""
# Mocks the login response for abodepy. # Mocks the login response for abodepy.
requests_mock.post(CONST.LOGIN_URL, text=load_fixture("abode_login.json")) requests_mock.post(CONST.LOGIN_URL, text=load_fixture("login.json", "abode"))
# Mocks the logout response for abodepy. # Mocks the logout response for abodepy.
requests_mock.post(CONST.LOGOUT_URL, text=load_fixture("abode_logout.json")) requests_mock.post(CONST.LOGOUT_URL, text=load_fixture("logout.json", "abode"))
# Mocks the oauth claims response for abodepy. # Mocks the oauth claims response for abodepy.
requests_mock.get( requests_mock.get(
CONST.OAUTH_TOKEN_URL, text=load_fixture("abode_oauth_claims.json") CONST.OAUTH_TOKEN_URL, text=load_fixture("oauth_claims.json", "abode")
) )
# Mocks the panel response for abodepy. # Mocks the panel response for abodepy.
requests_mock.get(CONST.PANEL_URL, text=load_fixture("abode_panel.json")) requests_mock.get(CONST.PANEL_URL, text=load_fixture("panel.json", "abode"))
# Mocks the automations response for abodepy. # Mocks the automations response for abodepy.
requests_mock.get(CONST.AUTOMATION_URL, text=load_fixture("abode_automation.json")) requests_mock.get(
CONST.AUTOMATION_URL, text=load_fixture("automation.json", "abode")
)
# Mocks the devices response for abodepy. # Mocks the devices response for abodepy.
requests_mock.get(CONST.DEVICES_URL, text=load_fixture("abode_devices.json")) requests_mock.get(CONST.DEVICES_URL, text=load_fixture("devices.json", "abode"))

View File

@ -23,7 +23,7 @@ async def init_integration(hass, aioclient_mock) -> MockConfigEntry:
}, },
) )
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_valid_station.json")) aioclient_mock.get(API_POINT_URL, text=load_fixture("valid_station.json", "airly"))
entry.add_to_hass(hass) entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()

View File

@ -48,7 +48,7 @@ async def test_invalid_api_key(hass, aioclient_mock):
async def test_invalid_location(hass, aioclient_mock): async def test_invalid_location(hass, aioclient_mock):
"""Test that errors are shown when location is invalid.""" """Test that errors are shown when location is invalid."""
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_no_station.json")) aioclient_mock.get(API_POINT_URL, text=load_fixture("no_station.json", "airly"))
aioclient_mock.get( aioclient_mock.get(
API_NEAREST_URL, API_NEAREST_URL,
@ -64,7 +64,7 @@ async def test_invalid_location(hass, aioclient_mock):
async def test_duplicate_error(hass, aioclient_mock): async def test_duplicate_error(hass, aioclient_mock):
"""Test that errors are shown when duplicates are added.""" """Test that errors are shown when duplicates are added."""
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_valid_station.json")) aioclient_mock.get(API_POINT_URL, text=load_fixture("valid_station.json", "airly"))
MockConfigEntry(domain=DOMAIN, unique_id="123-456", data=CONFIG).add_to_hass(hass) MockConfigEntry(domain=DOMAIN, unique_id="123-456", data=CONFIG).add_to_hass(hass)
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
@ -77,7 +77,7 @@ async def test_duplicate_error(hass, aioclient_mock):
async def test_create_entry(hass, aioclient_mock): async def test_create_entry(hass, aioclient_mock):
"""Test that the user step works.""" """Test that the user step works."""
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_valid_station.json")) aioclient_mock.get(API_POINT_URL, text=load_fixture("valid_station.json", "airly"))
with patch("homeassistant.components.airly.async_setup_entry", return_value=True): with patch("homeassistant.components.airly.async_setup_entry", return_value=True):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
@ -95,9 +95,11 @@ async def test_create_entry(hass, aioclient_mock):
async def test_create_entry_with_nearest_method(hass, aioclient_mock): async def test_create_entry_with_nearest_method(hass, aioclient_mock):
"""Test that the user step works with nearest method.""" """Test that the user step works with nearest method."""
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_no_station.json")) aioclient_mock.get(API_POINT_URL, text=load_fixture("no_station.json", "airly"))
aioclient_mock.get(API_NEAREST_URL, text=load_fixture("airly_valid_station.json")) aioclient_mock.get(
API_NEAREST_URL, text=load_fixture("valid_station.json", "airly")
)
with patch("homeassistant.components.airly.async_setup_entry", return_value=True): with patch("homeassistant.components.airly.async_setup_entry", return_value=True):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(

View File

@ -66,7 +66,7 @@ async def test_config_without_unique_id(hass, aioclient_mock):
}, },
) )
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_valid_station.json")) aioclient_mock.get(API_POINT_URL, text=load_fixture("valid_station.json", "airly"))
entry.add_to_hass(hass) entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
assert entry.state is ConfigEntryState.LOADED assert entry.state is ConfigEntryState.LOADED
@ -87,7 +87,7 @@ async def test_config_with_turned_off_station(hass, aioclient_mock):
}, },
) )
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_no_station.json")) aioclient_mock.get(API_POINT_URL, text=load_fixture("no_station.json", "airly"))
entry.add_to_hass(hass) entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
assert entry.state is ConfigEntryState.SETUP_RETRY assert entry.state is ConfigEntryState.SETUP_RETRY
@ -115,7 +115,7 @@ async def test_update_interval(hass, aioclient_mock):
aioclient_mock.get( aioclient_mock.get(
API_POINT_URL, API_POINT_URL,
text=load_fixture("airly_valid_station.json"), text=load_fixture("valid_station.json", "airly"),
headers=HEADERS, headers=HEADERS,
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
@ -152,7 +152,7 @@ async def test_update_interval(hass, aioclient_mock):
aioclient_mock.get( aioclient_mock.get(
"https://airapi.airly.eu/v2/measurements/point?lat=66.660000&lng=111.110000", "https://airapi.airly.eu/v2/measurements/point?lat=66.660000&lng=111.110000",
text=load_fixture("airly_valid_station.json"), text=load_fixture("valid_station.json", "airly"),
headers=HEADERS, headers=HEADERS,
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
@ -203,7 +203,7 @@ async def test_migrate_device_entry(hass, aioclient_mock, old_identifier):
}, },
) )
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_valid_station.json")) aioclient_mock.get(API_POINT_URL, text=load_fixture("valid_station.json", "airly"))
config_entry.add_to_hass(hass) config_entry.add_to_hass(hass)
device_reg = mock_device_registry(hass) device_reg = mock_device_registry(hass)

View File

@ -149,7 +149,7 @@ async def test_availability(hass, aioclient_mock):
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE
aioclient_mock.clear_requests() aioclient_mock.clear_requests()
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_valid_station.json")) aioclient_mock.get(API_POINT_URL, text=load_fixture("valid_station.json", "airly"))
future = utcnow() + timedelta(minutes=120) future = utcnow() + timedelta(minutes=120)
async_fire_time_changed(hass, future) async_fire_time_changed(hass, future)
await hass.async_block_till_done() await hass.async_block_till_done()

View File

@ -1,6 +1,5 @@
"""The test for the bayesian sensor platform.""" """The test for the bayesian sensor platform."""
import json import json
from os import path
from unittest.mock import patch from unittest.mock import patch
from homeassistant import config as hass_config from homeassistant import config as hass_config
@ -19,6 +18,8 @@ from homeassistant.const import (
from homeassistant.core import Context, callback from homeassistant.core import Context, callback
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import get_fixture_path
async def test_load_values_when_added_to_hass(hass): async def test_load_values_when_added_to_hass(hass):
"""Test that sensor initializes with observations of relevant entities.""" """Test that sensor initializes with observations of relevant entities."""
@ -666,11 +667,8 @@ async def test_reload(hass):
assert hass.states.get("binary_sensor.test") assert hass.states.get("binary_sensor.test")
yaml_path = path.join( yaml_path = get_fixture_path("configuration.yaml", "bayesian")
_get_fixtures_base_path(),
"fixtures",
"bayesian/configuration.yaml",
)
with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path): with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -686,10 +684,6 @@ async def test_reload(hass):
assert hass.states.get("binary_sensor.test2") assert hass.states.get("binary_sensor.test2")
def _get_fixtures_base_path():
return path.dirname(path.dirname(path.dirname(__file__)))
async def test_template_triggers(hass): async def test_template_triggers(hass):
"""Test sensor with template triggers.""" """Test sensor with template triggers."""
hass.states.async_set("input_boolean.test", STATE_OFF) hass.states.async_set("input_boolean.test", STATE_OFF)

View File

@ -22,7 +22,7 @@ async def init_integration(hass, skip_setup=False) -> MockConfigEntry:
if not skip_setup: if not skip_setup:
with patch( with patch(
"brother.Brother._get_data", "brother.Brother._get_data",
return_value=json.loads(load_fixture("brother_printer_data.json")), return_value=json.loads(load_fixture("printer_data.json", "brother")),
): ):
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()

View File

@ -28,7 +28,7 @@ async def test_create_entry_with_hostname(hass):
"""Test that the user step works with printer hostname.""" """Test that the user step works with printer hostname."""
with patch( with patch(
"brother.Brother._get_data", "brother.Brother._get_data",
return_value=json.loads(load_fixture("brother_printer_data.json")), return_value=json.loads(load_fixture("printer_data.json", "brother")),
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=CONFIG DOMAIN, context={"source": SOURCE_USER}, data=CONFIG
@ -44,7 +44,7 @@ async def test_create_entry_with_ipv4_address(hass):
"""Test that the user step works with printer IPv4 address.""" """Test that the user step works with printer IPv4 address."""
with patch( with patch(
"brother.Brother._get_data", "brother.Brother._get_data",
return_value=json.loads(load_fixture("brother_printer_data.json")), return_value=json.loads(load_fixture("printer_data.json", "brother")),
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@ -62,7 +62,7 @@ async def test_create_entry_with_ipv6_address(hass):
"""Test that the user step works with printer IPv6 address.""" """Test that the user step works with printer IPv6 address."""
with patch( with patch(
"brother.Brother._get_data", "brother.Brother._get_data",
return_value=json.loads(load_fixture("brother_printer_data.json")), return_value=json.loads(load_fixture("printer_data.json", "brother")),
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@ -123,7 +123,7 @@ async def test_device_exists_abort(hass):
"""Test we abort config flow if Brother printer already configured.""" """Test we abort config flow if Brother printer already configured."""
with patch( with patch(
"brother.Brother._get_data", "brother.Brother._get_data",
return_value=json.loads(load_fixture("brother_printer_data.json")), return_value=json.loads(load_fixture("printer_data.json", "brother")),
): ):
MockConfigEntry(domain=DOMAIN, unique_id="0123456789", data=CONFIG).add_to_hass( MockConfigEntry(domain=DOMAIN, unique_id="0123456789", data=CONFIG).add_to_hass(
hass hass
@ -172,7 +172,7 @@ async def test_zeroconf_device_exists_abort(hass):
"""Test we abort zeroconf flow if Brother printer already configured.""" """Test we abort zeroconf flow if Brother printer already configured."""
with patch( with patch(
"brother.Brother._get_data", "brother.Brother._get_data",
return_value=json.loads(load_fixture("brother_printer_data.json")), return_value=json.loads(load_fixture("printer_data.json", "brother")),
): ):
MockConfigEntry(domain=DOMAIN, unique_id="0123456789", data=CONFIG).add_to_hass( MockConfigEntry(domain=DOMAIN, unique_id="0123456789", data=CONFIG).add_to_hass(
hass hass
@ -209,7 +209,7 @@ async def test_zeroconf_confirm_create_entry(hass):
"""Test zeroconf confirmation and create config entry.""" """Test zeroconf confirmation and create config entry."""
with patch( with patch(
"brother.Brother._get_data", "brother.Brother._get_data",
return_value=json.loads(load_fixture("brother_printer_data.json")), return_value=json.loads(load_fixture("printer_data.json", "brother")),
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(

View File

@ -46,7 +46,7 @@ async def test_sensors(hass):
test_time = datetime(2019, 11, 11, 9, 10, 32, tzinfo=UTC) test_time = datetime(2019, 11, 11, 9, 10, 32, tzinfo=UTC)
with patch("brother.datetime", utcnow=Mock(return_value=test_time)), patch( with patch("brother.datetime", utcnow=Mock(return_value=test_time)), patch(
"brother.Brother._get_data", "brother.Brother._get_data",
return_value=json.loads(load_fixture("brother_printer_data.json")), return_value=json.loads(load_fixture("printer_data.json", "brother")),
): ):
await hass.config_entries.async_setup(entry.entry_id) await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -296,7 +296,7 @@ async def test_availability(hass):
future = utcnow() + timedelta(minutes=10) future = utcnow() + timedelta(minutes=10)
with patch( with patch(
"brother.Brother._get_data", "brother.Brother._get_data",
return_value=json.loads(load_fixture("brother_printer_data.json")), return_value=json.loads(load_fixture("printer_data.json", "brother")),
): ):
async_fire_time_changed(hass, future) async_fire_time_changed(hass, future)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -311,7 +311,7 @@ async def test_manual_update_entity(hass):
"""Test manual update entity via service homeasasistant/update_entity.""" """Test manual update entity via service homeasasistant/update_entity."""
await init_integration(hass) await init_integration(hass)
data = json.loads(load_fixture("brother_printer_data.json")) data = json.loads(load_fixture("printer_data.json", "brother"))
await async_setup_component(hass, "homeassistant", {}) await async_setup_component(hass, "homeassistant", {})
with patch( with patch(

View File

@ -18,7 +18,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from tests.common import async_fire_time_changed from tests.common import async_fire_time_changed, get_fixture_path
async def setup_test_entity(hass: HomeAssistant, config_dict: dict[str, Any]) -> None: async def setup_test_entity(hass: HomeAssistant, config_dict: dict[str, Any]) -> None:
@ -133,11 +133,7 @@ async def test_reload(hass: HomeAssistant) -> None:
assert entity_state assert entity_state
assert entity_state.state == "unknown" assert entity_state.state == "unknown"
yaml_path = os.path.join( yaml_path = get_fixture_path("configuration.yaml", "command_line")
os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
"fixtures",
"command_line/configuration.yaml",
)
with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path): with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
await hass.services.async_call( await hass.services.async_call(
"command_line", "command_line",

View File

@ -16,7 +16,7 @@ CONFIG = {
CONF_SERVER: SERVER_US, CONF_SERVER: SERVER_US,
} }
GLUCOSE_READING = GlucoseReading(json.loads(load_fixture("dexcom_data.json"))) GLUCOSE_READING = GlucoseReading(json.loads(load_fixture("data.json", "dexcom")))
async def init_integration(hass) -> MockConfigEntry: async def init_integration(hass) -> MockConfigEntry:

Some files were not shown because too many files have changed in this diff Show More