Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Habitica integration #131780

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add tests for Habitica integration
  • Loading branch information
tr4nt0r committed Dec 16, 2024
commit 5606ede7e26afcd1f84439bc44b5766c77769eec
50 changes: 50 additions & 0 deletions tests/components/habitica/fixtures/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,56 @@
"updatedAt": "2024-07-07T17:51:53.266Z",
"userId": "5f359083-ef78-4af0-985a-0b2c6d05797c",
"id": "5e2ea1df-f6e6-4ba3-bccb-97c5ec63e99b"
},
{
"repeat": {
"m": false,
"t": false,
"w": false,
"th": false,
"f": false,
"s": false,
"su": true
},
"challenge": {},
"group": {
"completedBy": {},
"assignedUsers": []
},
"_id": "6e53f1f5-a315-4edd-984d-8d762e4a08ef",
"frequency": "monthly",
"everyX": 1,
"streak": 1,
"nextDue": [
"2024-12-14T23:00:00.000Z",
"2025-01-18T23:00:00.000Z",
"2025-02-15T23:00:00.000Z",
"2025-03-15T23:00:00.000Z",
"2025-04-19T23:00:00.000Z",
"2025-05-17T23:00:00.000Z"
],
"yesterDaily": true,
"history": [],
"completed": false,
"collapseChecklist": false,
"type": "daily",
"text": "Arbeite an einem kreativen Projekt",
"notes": "Klicke um den Namen Deines aktuellen Projekts anzugeben & setze einen Terminplan!",
"tags": [],
"value": -0.9215181434950852,
"priority": 1,
"attribute": "str",
"byHabitica": false,
"startDate": "2024-09-20T23:00:00.000Z",
"daysOfMonth": [],
"weeksOfMonth": [3],
"checklist": [],
"reminders": [],
"createdAt": "2024-10-10T15:57:14.304Z",
"updatedAt": "2024-11-27T23:47:29.986Z",
"userId": "5f359083-ef78-4af0-985a-0b2c6d05797c",
"isDue": false,
"id": "6e53f1f5-a315-4edd-984d-8d762e4a08ef"
}
],
"notifications": [
Expand Down
3 changes: 2 additions & 1 deletion tests/components/habitica/fixtures/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"e97659e0-2c42-4599-a7bb-00282adc410d",
"564b9ac9-c53d-4638-9e7f-1cd96fe19baa",
"f2c85972-1a19-4426-bc6d-ce3337b9d99f",
"2c6d136c-a1c3-4bef-b7c4-fa980784b1e1"
"2c6d136c-a1c3-4bef-b7c4-fa980784b1e1",
"6e53f1f5-a315-4edd-984d-8d762e4a08ef"
],
"habits": ["1d147de6-5c02-4740-8e2f-71d3015a37f4"]
},
Expand Down
24 changes: 20 additions & 4 deletions tests/components/habitica/snapshots/test_calendar.ambr
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# serializer version: 1
# name: test_api_events[calendar.test_user_dailies]
# name: test_api_events[date range in the past-calendar.test_user_dailies]
list([
])
# ---
# name: test_api_events[date range in the past-calendar.test_user_daily_reminders]
list([
])
# ---
# name: test_api_events[date range in the past-calendar.test_user_to_do_reminders]
list([
])
# ---
# name: test_api_events[date range in the past-calendar.test_user_to_do_s]
list([
])
# ---
# name: test_api_events[default date range-calendar.test_user_dailies]
list([
dict({
'description': 'Klicke um Deinen Terminplan festzulegen!',
Expand Down Expand Up @@ -577,7 +593,7 @@
}),
])
# ---
# name: test_api_events[calendar.test_user_daily_reminders]
# name: test_api_events[default date range-calendar.test_user_daily_reminders]
list([
dict({
'description': 'Klicke um Deinen Terminplan festzulegen!',
Expand Down Expand Up @@ -819,7 +835,7 @@
}),
])
# ---
# name: test_api_events[calendar.test_user_to_do_reminders]
# name: test_api_events[default date range-calendar.test_user_to_do_reminders]
list([
dict({
'description': 'Strom- und Internetrechnungen rechtzeitig überweisen.',
Expand All @@ -837,7 +853,7 @@
}),
])
# ---
# name: test_api_events[calendar.test_user_to_do_s]
# name: test_api_events[default date range-calendar.test_user_to_do_s]
list([
dict({
'description': 'Strom- und Internetrechnungen rechtzeitig überweisen.',
Expand Down
61 changes: 61 additions & 0 deletions tests/components/habitica/snapshots/test_diagnostics.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,66 @@
'userId': '5f359083-ef78-4af0-985a-0b2c6d05797c',
'value': 10,
}),
dict({
'_id': '6e53f1f5-a315-4edd-984d-8d762e4a08ef',
'attribute': 'str',
'byHabitica': False,
'challenge': dict({
}),
'checklist': list([
]),
'collapseChecklist': False,
'completed': False,
'createdAt': '2024-10-10T15:57:14.304Z',
'daysOfMonth': list([
]),
'everyX': 1,
'frequency': 'monthly',
'group': dict({
'assignedUsers': list([
]),
'completedBy': dict({
}),
}),
'history': list([
]),
'id': '6e53f1f5-a315-4edd-984d-8d762e4a08ef',
'isDue': False,
'nextDue': list([
'2024-12-14T23:00:00.000Z',
'2025-01-18T23:00:00.000Z',
'2025-02-15T23:00:00.000Z',
'2025-03-15T23:00:00.000Z',
'2025-04-19T23:00:00.000Z',
'2025-05-17T23:00:00.000Z',
]),
'notes': 'Klicke um den Namen Deines aktuellen Projekts anzugeben & setze einen Terminplan!',
'priority': 1,
'reminders': list([
]),
'repeat': dict({
'f': False,
'm': False,
's': False,
'su': True,
't': False,
'th': False,
'w': False,
}),
'startDate': '2024-09-20T23:00:00.000Z',
'streak': 1,
'tags': list([
]),
'text': 'Arbeite an einem kreativen Projekt',
'type': 'daily',
'updatedAt': '2024-11-27T23:47:29.986Z',
'userId': '5f359083-ef78-4af0-985a-0b2c6d05797c',
'value': -0.9215181434950852,
'weeksOfMonth': list([
3,
]),
'yesterDaily': True,
}),
]),
'user': dict({
'api_user': 'test-api-user',
Expand Down Expand Up @@ -695,6 +755,7 @@
'564b9ac9-c53d-4638-9e7f-1cd96fe19baa',
'f2c85972-1a19-4426-bc6d-ce3337b9d99f',
'2c6d136c-a1c3-4bef-b7c4-fa980784b1e1',
'6e53f1f5-a315-4edd-984d-8d762e4a08ef',
]),
'habits': list([
'1d147de6-5c02-4740-8e2f-71d3015a37f4',
Expand Down
41 changes: 40 additions & 1 deletion tests/components/habitica/snapshots/test_sensor.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,45 @@
'value': -2.9663035443712333,
'yester_daily': True,
}),
'6e53f1f5-a315-4edd-984d-8d762e4a08ef': dict({
'created_at': '2024-10-10T15:57:14.304Z',
'every_x': 1,
'frequency': 'monthly',
'group': dict({
'assignedUsers': list([
]),
'completedBy': dict({
}),
}),
'next_due': list([
'2024-12-14T23:00:00.000Z',
'2025-01-18T23:00:00.000Z',
'2025-02-15T23:00:00.000Z',
'2025-03-15T23:00:00.000Z',
'2025-04-19T23:00:00.000Z',
'2025-05-17T23:00:00.000Z',
]),
'notes': 'Klicke um den Namen Deines aktuellen Projekts anzugeben & setze einen Terminplan!',
'priority': 1,
'repeat': dict({
'f': False,
'm': False,
's': False,
'su': True,
't': False,
'th': False,
'w': False,
}),
'start_date': '2024-09-20T23:00:00.000Z',
'streak': 1,
'text': 'Arbeite an einem kreativen Projekt',
'type': 'daily',
'value': -0.9215181434950852,
'weeks_of_month': list([
3,
]),
'yester_daily': True,
}),
'f2c85972-1a19-4426-bc6d-ce3337b9d99f': dict({
'created_at': '2024-07-07T17:51:53.266Z',
'every_x': 1,
Expand Down Expand Up @@ -270,7 +309,7 @@
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '3',
'state': '4',
})
# ---
# name: test_sensors[sensor.test_user_display_name-entry]
Expand Down
9 changes: 8 additions & 1 deletion tests/components/habitica/snapshots/test_todo.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
'summary': 'Fitnessstudio besuchen',
'uid': '2c6d136c-a1c3-4bef-b7c4-fa980784b1e1',
}),
dict({
'description': 'Klicke um den Namen Deines aktuellen Projekts anzugeben & setze einen Terminplan!',
'due': '2024-12-14',
'status': 'needs_action',
'summary': 'Arbeite an einem kreativen Projekt',
'uid': '6e53f1f5-a315-4edd-984d-8d762e4a08ef',
}),
]),
}),
})
Expand Down Expand Up @@ -137,7 +144,7 @@
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '2',
'state': '3',
})
# ---
# name: test_todos[todo.test_user_to_do_s-entry]
Expand Down
71 changes: 70 additions & 1 deletion tests/components/habitica/test_button.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tests for Habitica button platform."""

from collections.abc import Generator
from datetime import timedelta
from http import HTTPStatus
import re
from unittest.mock import patch
Expand All @@ -15,10 +16,16 @@
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from homeassistant.helpers import entity_registry as er
import homeassistant.util.dt as dt_util

from .conftest import mock_called_with

from tests.common import MockConfigEntry, load_json_object_fixture, snapshot_platform
from tests.common import (
MockConfigEntry,
async_fire_time_changed,
load_json_object_fixture,
snapshot_platform,
)
from tests.test_util.aiohttp import AiohttpClientMocker


Expand Down Expand Up @@ -340,3 +347,65 @@ async def test_button_unavailable(
for entity_id in entity_ids:
assert (state := hass.states.get(entity_id))
assert state.state == STATE_UNAVAILABLE


async def test_class_change(
hass: HomeAssistant,
config_entry: MockConfigEntry,
aioclient_mock: AiohttpClientMocker,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
) -> None:
"""Test removing and adding skills after class change."""
mage_skills = [
"button.test_user_chilling_frost",
"button.test_user_earthquake",
"button.test_user_ethereal_surge",
]
healer_skills = [
"button.test_user_healing_light",
"button.test_user_protective_aura",
"button.test_user_searing_brightness",
"button.test_user_blessing",
]
aioclient_mock.get(
f"{DEFAULT_URL}/api/v3/user",
json=load_json_object_fixture("wizard_fixture.json", DOMAIN),
)
aioclient_mock.get(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in general, I don't like direct aioclient mocks in HA. Instead I'd recommend mocking the library's client. Since the other tests are already using this pattern, I'll allow it. Maybe you think about changing this from the entire tests in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i know i know, but that is not possible here. There's a PR that introduces a new library and all tests have been rewritten accordingly

f"{DEFAULT_URL}/api/v3/tasks/user",
params={"type": "completedTodos"},
json=load_json_object_fixture("completed_todos.json", DOMAIN),
)
aioclient_mock.get(
f"{DEFAULT_URL}/api/v3/tasks/user",
json=load_json_object_fixture("tasks.json", DOMAIN),
)
aioclient_mock.get(
f"{DEFAULT_URL}/api/v3/content",
params={"language": "en"},
json=load_json_object_fixture("content.json", DOMAIN),
)
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

assert config_entry.state is ConfigEntryState.LOADED

for skill in mage_skills:
assert hass.states.get(skill)

aioclient_mock._mocks.pop(0)
aioclient_mock.get(
f"{DEFAULT_URL}/api/v3/user",
json=load_json_object_fixture("healer_fixture.json", DOMAIN),
)

async_fire_time_changed(hass, dt_util.now() + timedelta(seconds=60))
await hass.async_block_till_done()

for skill in mage_skills:
assert not hass.states.get(skill)

for skill in healer_skills:
assert hass.states.get(skill)
15 changes: 14 additions & 1 deletion tests/components/habitica/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ async def test_calendar_platform(
"calendar.test_user_to_do_reminders",
],
)
@pytest.mark.parametrize(
("start_date", "end_date"),
[
("2024-08-29", "2024-10-08"),
("2023-08-01", "2023-08-02"),
],
ids=[
"default date range",
"date range in the past",
],
)
@pytest.mark.freeze_time("2024-09-20T22:00:00.000Z")
@pytest.mark.usefixtures("mock_habitica")
async def test_api_events(
Expand All @@ -67,6 +78,8 @@ async def test_api_events(
config_entry: MockConfigEntry,
hass_client: ClientSessionGenerator,
entity: str,
start_date: str,
end_date: str,
) -> None:
"""Test calendar event."""

Expand All @@ -76,7 +89,7 @@ async def test_api_events(

client = await hass_client()
response = await client.get(
f"/api/calendars/{entity}?start=2024-08-29&end=2024-10-08"
f"/api/calendars/{entity}?start={start_date}&end={end_date}"
)

assert await response.json() == snapshot