-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add tests
- Loading branch information
Showing
13 changed files
with
248 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: Pytest | ||
|
||
on: | ||
push: | ||
pull_request: | ||
schedule: | ||
- cron: "0 7 1-28/7 * *" | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: [3.8, 3.9] | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v1 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements_test.txt | ||
- name: Generate coverage report | ||
run: | | ||
python -m pytest | ||
pip install pytest-cov | ||
pytest ./tests/ --cov=custom_components/nws_alerts/ --cov-report=xml | ||
- name: Upload coverage to Codecov | ||
uses: codecov/codecov-action@v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
*.pyc | ||
.coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"""For testing""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
black | ||
isort | ||
pytest | ||
pytest-cov | ||
pytest-homeassistant-custom-component |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""Fixtures for tests""" | ||
import pytest | ||
|
||
pytest_plugins = "pytest_homeassistant_custom_component" | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def auto_enable_custom_integrations(enable_custom_integrations): | ||
yield |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"""Constants for tests.""" | ||
|
||
CONFIG_DATA = {"name": "NWS Alerts", "zone_id": "AZZ540,AZC013"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
"""Test for config flow""" | ||
from tests.const import CONFIG_DATA | ||
from unittest.mock import patch | ||
import pytest | ||
from homeassistant import config_entries, data_entry_flow, setup | ||
from pytest_homeassistant_custom_component.common import MockConfigEntry | ||
|
||
from custom_components.nws_alerts.const import DOMAIN | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input,step_id,title,data", | ||
[ | ||
( | ||
{ | ||
"name": "Testing Alerts", | ||
"zone_id": "AZZ540,AZC013", | ||
"interval": 5, | ||
"timeout": 120, | ||
}, | ||
"user", | ||
"Testing Alerts", | ||
{ | ||
"name": "Testing Alerts", | ||
"zone_id": "AZZ540,AZC013", | ||
"interval": 5, | ||
"timeout": 120, | ||
}, | ||
), | ||
], | ||
) | ||
async def test_form( | ||
input, | ||
step_id, | ||
title, | ||
data, | ||
hass, | ||
): | ||
"""Test we get the form.""" | ||
await setup.async_setup_component(hass, "persistent_notification", {}) | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": config_entries.SOURCE_USER} | ||
) | ||
assert result["type"] == "form" | ||
assert result["errors"] == {} | ||
# assert result["title"] == title_1 | ||
|
||
with patch( | ||
"custom_components.nws_alerts.async_setup", return_value=True | ||
) as mock_setup, patch( | ||
"custom_components.nws_alerts.async_setup_entry", | ||
return_value=True, | ||
) as mock_setup_entry: | ||
|
||
result2 = await hass.config_entries.flow.async_configure( | ||
result["flow_id"], input | ||
) | ||
|
||
assert result2["type"] == "create_entry" | ||
assert result2["title"] == title | ||
assert result2["data"] == data | ||
|
||
await hass.async_block_till_done() | ||
assert len(mock_setup.mock_calls) == 1 | ||
assert len(mock_setup_entry.mock_calls) == 1 | ||
|
||
|
||
async def test_setup_user(hass): | ||
"""Test that the user setup works""" | ||
with patch("custom_components.nws_alerts.async_setup", return_value=True), patch( | ||
"custom_components.nws_alerts.async_setup_entry", | ||
return_value=True, | ||
): | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=CONFIG_DATA | ||
) | ||
await hass.async_block_till_done() | ||
|
||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY | ||
assert result["title"] == "NWS Alerts" | ||
assert result["data"] == CONFIG_DATA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Tests for init.""" | ||
|
||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN | ||
from pytest_homeassistant_custom_component.common import MockConfigEntry | ||
|
||
from custom_components.nws_alerts.const import DOMAIN | ||
from tests.const import CONFIG_DATA | ||
|
||
|
||
async def test_setup_entry( | ||
hass, | ||
): | ||
"""Test settting up entities.""" | ||
entry = MockConfigEntry( | ||
domain=DOMAIN, | ||
title="NWS Alerts", | ||
data=CONFIG_DATA, | ||
) | ||
|
||
entry.add_to_hass(hass) | ||
assert await hass.config_entries.async_setup(entry.entry_id) | ||
await hass.async_block_till_done() | ||
|
||
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 1 | ||
entries = hass.config_entries.async_entries(DOMAIN) | ||
assert len(entries) == 1 | ||
|
||
|
||
async def test_unload_entry(hass): | ||
"""Test unloading entities.""" | ||
entry = MockConfigEntry( | ||
domain=DOMAIN, | ||
title="NWS Alerts", | ||
data=CONFIG_DATA, | ||
) | ||
|
||
entry.add_to_hass(hass) | ||
assert await hass.config_entries.async_setup(entry.entry_id) | ||
await hass.async_block_till_done() | ||
|
||
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 1 | ||
entries = hass.config_entries.async_entries(DOMAIN) | ||
assert len(entries) == 1 | ||
|
||
assert await hass.config_entries.async_unload(entries[0].entry_id) | ||
await hass.async_block_till_done() | ||
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 1 | ||
assert len(hass.states.async_entity_ids(DOMAIN)) == 0 | ||
|
||
assert await hass.config_entries.async_remove(entries[0].entry_id) | ||
await hass.async_block_till_done() | ||
assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"""Test NWS Alerts Sensors""" | ||
from pytest_homeassistant_custom_component.common import MockConfigEntry | ||
|
||
from custom_components.nws_alerts.const import DOMAIN | ||
from tests.const import CONFIG_DATA | ||
|
||
|
||
async def test_sensor(hass): | ||
|
||
entry = MockConfigEntry( | ||
domain=DOMAIN, | ||
title="NWS Alerts", | ||
data=CONFIG_DATA, | ||
) | ||
|
||
entry.add_to_hass(hass) | ||
assert await hass.config_entries.async_setup(entry.entry_id) | ||
await hass.async_block_till_done() | ||
|
||
assert "nws_alerts" in hass.config.components |