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)
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."""
path = os.path.join(os.path.dirname(__file__), "fixtures", filename)
with open(path, encoding="utf-8") as fptr:
return fptr.read()
return get_fixture_path(filename, integration).read_text()
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):
"""Fixture to provide a requests mocker."""
# 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.
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.
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.
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.
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.
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)
await hass.config_entries.async_setup(entry.entry_id)
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):
"""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(
API_NEAREST_URL,
@ -64,7 +64,7 @@ async def test_invalid_location(hass, aioclient_mock):
async def test_duplicate_error(hass, aioclient_mock):
"""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)
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):
"""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):
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):
"""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):
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)
await hass.config_entries.async_setup(entry.entry_id)
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)
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state is ConfigEntryState.SETUP_RETRY
@ -115,7 +115,7 @@ async def test_update_interval(hass, aioclient_mock):
aioclient_mock.get(
API_POINT_URL,
text=load_fixture("airly_valid_station.json"),
text=load_fixture("valid_station.json", "airly"),
headers=HEADERS,
)
entry.add_to_hass(hass)
@ -152,7 +152,7 @@ async def test_update_interval(hass, aioclient_mock):
aioclient_mock.get(
"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,
)
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)
device_reg = mock_device_registry(hass)

View file

@ -149,7 +149,7 @@ async def test_availability(hass, aioclient_mock):
assert state.state == STATE_UNAVAILABLE
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)
async_fire_time_changed(hass, future)
await hass.async_block_till_done()

View file

@ -1,6 +1,5 @@
"""The test for the bayesian sensor platform."""
import json
from os import path
from unittest.mock import patch
from homeassistant import config as hass_config
@ -19,6 +18,8 @@ from homeassistant.const import (
from homeassistant.core import Context, callback
from homeassistant.setup import async_setup_component
from tests.common import get_fixture_path
async def test_load_values_when_added_to_hass(hass):
"""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")
yaml_path = path.join(
_get_fixtures_base_path(),
"fixtures",
"bayesian/configuration.yaml",
)
yaml_path = get_fixture_path("configuration.yaml", "bayesian")
with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
await hass.services.async_call(
DOMAIN,
@ -686,10 +684,6 @@ async def test_reload(hass):
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):
"""Test sensor with template triggers."""
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:
with patch(
"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.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."""
with patch(
"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(
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."""
with patch(
"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(
DOMAIN,
@ -62,7 +62,7 @@ async def test_create_entry_with_ipv6_address(hass):
"""Test that the user step works with printer IPv6 address."""
with patch(
"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(
DOMAIN,
@ -123,7 +123,7 @@ async def test_device_exists_abort(hass):
"""Test we abort config flow if Brother printer already configured."""
with patch(
"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(
hass
@ -172,7 +172,7 @@ async def test_zeroconf_device_exists_abort(hass):
"""Test we abort zeroconf flow if Brother printer already configured."""
with patch(
"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(
hass
@ -209,7 +209,7 @@ async def test_zeroconf_confirm_create_entry(hass):
"""Test zeroconf confirmation and create config entry."""
with patch(
"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(

View file

@ -46,7 +46,7 @@ async def test_sensors(hass):
test_time = datetime(2019, 11, 11, 9, 10, 32, tzinfo=UTC)
with patch("brother.datetime", utcnow=Mock(return_value=test_time)), patch(
"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.async_block_till_done()
@ -296,7 +296,7 @@ async def test_availability(hass):
future = utcnow() + timedelta(minutes=10)
with patch(
"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)
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."""
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", {})
with patch(

View file

@ -18,7 +18,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant
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:
@ -133,11 +133,7 @@ async def test_reload(hass: HomeAssistant) -> None:
assert entity_state
assert entity_state.state == "unknown"
yaml_path = os.path.join(
os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
"fixtures",
"command_line/configuration.yaml",
)
yaml_path = get_fixture_path("configuration.yaml", "command_line")
with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
await hass.services.async_call(
"command_line",

View file

@ -16,7 +16,7 @@ CONFIG = {
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:

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