Skip to content

Commit

Permalink
Unittesting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bruxy70 committed May 8, 2022
1 parent bbef7eb commit 5dfbe67
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 3 deletions.
4 changes: 3 additions & 1 deletion custom_components/garbage_collection/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ async def async_setup_entry(
elif frequency == "every-n-days":
async_add_devices([DailyCollection(config_entry)], True)
elif frequency == "monthly":
async_add_devices([DailyCollection(config_entry)], True)
async_add_devices([MonthlyCollection(config_entry)], True)
elif frequency == "annual":
async_add_devices([AnnualCollection(config_entry)], True)
elif frequency == "group":
async_add_devices([GroupCollection(config_entry)], True)
elif frequency == "blank":
async_add_devices([BlankCollection(config_entry)], True)
else:
_LOGGER.error("(%s) Unknown frequency %s", name, frequency)
raise ValueError
Expand Down
2 changes: 1 addition & 1 deletion requirements_tests.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
python-dateutil>=2.8.2
homeassistant>=2022.5.0
pytest-homeassistant-custom-component>=0.8.5
pytest-homeassistant-custom-component>=0.9.0
aiohttp_cors>=0.7.0
49 changes: 49 additions & 0 deletions test/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ async def test_annual_config_flow(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert "type" in result and "step_id" in result and "flow_id" in result

# Check that the config flow shows the user form as the first step
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
Expand All @@ -25,6 +27,12 @@ async def test_annual_config_flow(hass: HomeAssistant) -> None:
result["flow_id"],
user_input={"name": "test", "frequency": "annual"},
)
assert (
"type" in result
and "step_id" in result
and "flow_id" in result
and "errors" in result
)

# Check that the config flow is complete and a new entry is created with
# the input data
Expand All @@ -45,6 +53,7 @@ async def test_annual_config_flow(hass: HomeAssistant) -> None:
"date": "04/01",
},
)
assert "type" in result and "data" in result
# Should create entry
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
del result["data"]["unique_id"]
Expand All @@ -61,6 +70,7 @@ async def test_blank_config_flow(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert "type" in result and "step_id" in result and "flow_id" in result
# Check that the config flow shows the user form as the first step
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
Expand All @@ -71,6 +81,12 @@ async def test_blank_config_flow(hass: HomeAssistant) -> None:
result["flow_id"],
user_input={"name": "test", "frequency": "blank", "verbose_state": True},
)
assert (
"type" in result
and "step_id" in result
and "flow_id" in result
and "errors" in result
)

# Check that the config flow is complete and a new entry is created with
# the input data
Expand All @@ -92,6 +108,7 @@ async def test_blank_config_flow(hass: HomeAssistant) -> None:
"verbose_format": "on {date}, in {days} days",
},
)
assert "type" in result and "data" in result
# Should create entry
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
del result["data"]["unique_id"]
Expand All @@ -113,6 +130,7 @@ async def test_every_n_days_config_flow(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert "type" in result and "step_id" in result and "flow_id" in result
# Check that the config flow shows the user form as the first step
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
Expand All @@ -123,6 +141,12 @@ async def test_every_n_days_config_flow(hass: HomeAssistant) -> None:
result["flow_id"],
user_input={"name": "test", "frequency": "every-n-days"},
)
assert (
"type" in result
and "step_id" in result
and "flow_id" in result
and "errors" in result
)

# Check that the config flow is complete and a new entry is created with
# the input data
Expand All @@ -144,6 +168,7 @@ async def test_every_n_days_config_flow(hass: HomeAssistant) -> None:
"first_date": "2020-01-01",
},
)
assert "type" in result and "data" in result
# Should create entry
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
del result["data"]["unique_id"]
Expand All @@ -164,6 +189,7 @@ async def test_every_n_weeks_config_flow(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert "type" in result and "step_id" in result and "flow_id" in result
# Check that the config flow shows the user form as the first step
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
Expand All @@ -174,6 +200,12 @@ async def test_every_n_weeks_config_flow(hass: HomeAssistant) -> None:
result["flow_id"],
user_input={"name": "test", "frequency": "every-n-weeks"},
)
assert (
"type" in result
and "step_id" in result
and "flow_id" in result
and "errors" in result
)

# Check that the config flow is complete and a new entry is created with
# the input data
Expand All @@ -196,6 +228,7 @@ async def test_every_n_weeks_config_flow(hass: HomeAssistant) -> None:
"collection_days": ["wed"],
},
)
assert "type" in result and "data" in result
# Should create entry
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
del result["data"]["unique_id"]
Expand All @@ -217,6 +250,7 @@ async def test_monthly_config_flow(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert "type" in result and "step_id" in result and "flow_id" in result
# Check that the config flow shows the user form as the first step
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
Expand All @@ -227,6 +261,12 @@ async def test_monthly_config_flow(hass: HomeAssistant) -> None:
result["flow_id"],
user_input={"name": "test", "frequency": "monthly"},
)
assert (
"type" in result
and "step_id" in result
and "flow_id" in result
and "errors" in result
)

# Check that the config flow is complete and a new entry is created with
# the input data
Expand All @@ -249,6 +289,7 @@ async def test_monthly_config_flow(hass: HomeAssistant) -> None:
"period": 1,
},
)
assert "type" in result and "data" in result
# Should create entry
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
del result["data"]["unique_id"]
Expand All @@ -270,6 +311,7 @@ async def test_weekly_config_flow(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert "type" in result and "step_id" in result and "flow_id" in result
# Check that the config flow shows the user form as the first step
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
Expand All @@ -280,6 +322,12 @@ async def test_weekly_config_flow(hass: HomeAssistant) -> None:
result["flow_id"],
user_input={"name": "test", "frequency": "weekly"},
)
assert (
"type" in result
and "step_id" in result
and "flow_id" in result
and "errors" in result
)

# Check that the config flow is complete and a new entry is created with
# the input data
Expand All @@ -298,6 +346,7 @@ async def test_weekly_config_flow(hass: HomeAssistant) -> None:
result["flow_id"],
user_input={"collection_days": ["wed"]},
)
assert "type" in result and "data" in result
# Should create entry
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
del result["data"]["unique_id"]
Expand Down
8 changes: 8 additions & 0 deletions test/test_frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ async def test_annual(hass: HomeAssistant) -> None:
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
sensor = hass.states.get("sensor.annual")
assert sensor is not None
state = sensor.state
days = sensor.attributes["days"]
next_date = sensor.attributes["next_date"]
Expand All @@ -52,6 +53,7 @@ async def test_even_weeks(hass: HomeAssistant) -> None:
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
sensor = hass.states.get("sensor.even_weeks")
assert sensor is not None
state = sensor.state
days = sensor.attributes["days"]
next_date = sensor.attributes["next_date"]
Expand Down Expand Up @@ -80,6 +82,7 @@ async def test_every_n_days(hass: HomeAssistant) -> None:
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
sensor = hass.states.get("sensor.every_n_days")
assert sensor is not None
state = sensor.state
days = sensor.attributes["days"]
next_date = sensor.attributes["next_date"]
Expand Down Expand Up @@ -109,6 +112,7 @@ async def test_every_n_weeks(hass: HomeAssistant) -> None:
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
sensor = hass.states.get("sensor.every_n_weeks")
assert sensor is not None
state = sensor.state
days = sensor.attributes["days"]
next_date = sensor.attributes["next_date"]
Expand Down Expand Up @@ -137,6 +141,7 @@ async def test_monthly(hass: HomeAssistant) -> None:
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
sensor = hass.states.get("sensor.monthly")
assert sensor is not None
state = sensor.state
days = sensor.attributes["days"]
next_date = sensor.attributes["next_date"]
Expand Down Expand Up @@ -166,6 +171,7 @@ async def test_monthly2(hass: HomeAssistant) -> None:
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
sensor = hass.states.get("sensor.monthly_2")
assert sensor is not None
state = sensor.state
days = sensor.attributes["days"]
next_date = sensor.attributes["next_date"]
Expand All @@ -190,6 +196,7 @@ async def test_odd_weeks(hass: HomeAssistant) -> None:
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
sensor = hass.states.get("sensor.odd_weeks")
assert sensor is not None
state = sensor.state
days = sensor.attributes["days"]
next_date = sensor.attributes["next_date"]
Expand All @@ -214,6 +221,7 @@ async def test_weekly(hass: HomeAssistant) -> None:
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
sensor = hass.states.get("sensor.weekly")
assert sensor is not None
state = sensor.state
days = sensor.attributes["days"]
next_date = sensor.attributes["next_date"]
Expand Down
3 changes: 2 additions & 1 deletion test/test_manual_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


async def test_manual_update(hass: HomeAssistant) -> None:
"""Annual collection."""
"""Blank collection."""

config_entry: MockConfigEntry = MockConfigEntry(
domain=const.DOMAIN,
Expand All @@ -30,6 +30,7 @@ async def test_manual_update(hass: HomeAssistant) -> None:
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
sensor = hass.states.get("sensor.blank")
assert sensor is not None
assert sensor.state == "2"
assert sensor.attributes["days"] is None
assert sensor.attributes["next_date"] is None
Expand Down
Loading

0 comments on commit 5dfbe67

Please sign in to comment.