|
9 | 9 | from gspread.exceptions import APIError |
10 | 10 | import pytest |
11 | 11 | from requests.models import Response |
| 12 | +from syrupy.assertion import SnapshotAssertion |
12 | 13 |
|
13 | 14 | from homeassistant.components.application_credentials import ( |
14 | 15 | ClientCredential, |
15 | 16 | async_import_client_credential, |
16 | 17 | ) |
17 | 18 | from homeassistant.components.google_sheets.const import DOMAIN |
| 19 | +from homeassistant.components.google_sheets.services import ( |
| 20 | + DATA_CONFIG_ENTRY, |
| 21 | + ROWS, |
| 22 | + SERVICE_GET_SHEET, |
| 23 | + WORKSHEET, |
| 24 | +) |
18 | 25 | from homeassistant.config_entries import ConfigEntryState |
19 | 26 | from homeassistant.core import HomeAssistant |
20 | | -from homeassistant.exceptions import HomeAssistantError |
| 27 | +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError |
21 | 28 | from homeassistant.setup import async_setup_component |
22 | 29 |
|
23 | 30 | from tests.common import MockConfigEntry |
@@ -213,6 +220,40 @@ async def test_append_sheet( |
213 | 220 | assert len(mock_client.mock_calls) == 8 |
214 | 221 |
|
215 | 222 |
|
| 223 | +async def test_get_sheet( |
| 224 | + hass: HomeAssistant, |
| 225 | + setup_integration: ComponentSetup, |
| 226 | + config_entry: MockConfigEntry, |
| 227 | + snapshot: SnapshotAssertion, |
| 228 | +) -> None: |
| 229 | + """Test service call getting data from a sheet.""" |
| 230 | + await setup_integration() |
| 231 | + |
| 232 | + entries = hass.config_entries.async_entries(DOMAIN) |
| 233 | + assert len(entries) == 1 |
| 234 | + assert entries[0].state is ConfigEntryState.LOADED |
| 235 | + |
| 236 | + with patch("homeassistant.components.google_sheets.services.Client") as mock_client: |
| 237 | + mock_client.return_value.open_by_key.return_value.worksheet.return_value.get_values.return_value = [ |
| 238 | + ["col1", "col2"], |
| 239 | + ["a", "b"], |
| 240 | + ["c", "d"], |
| 241 | + ] |
| 242 | + response = await hass.services.async_call( |
| 243 | + DOMAIN, |
| 244 | + SERVICE_GET_SHEET, |
| 245 | + { |
| 246 | + DATA_CONFIG_ENTRY: config_entry.entry_id, |
| 247 | + WORKSHEET: "Sheet1", |
| 248 | + ROWS: 2, |
| 249 | + }, |
| 250 | + blocking=True, |
| 251 | + return_response=True, |
| 252 | + ) |
| 253 | + assert len(mock_client.mock_calls) == 4 |
| 254 | + assert response == snapshot |
| 255 | + |
| 256 | + |
216 | 257 | async def test_append_sheet_multiple_rows( |
217 | 258 | hass: HomeAssistant, |
218 | 259 | setup_integration: ComponentSetup, |
@@ -330,3 +371,98 @@ async def test_append_sheet_invalid_config_entry( |
330 | 371 | }, |
331 | 372 | blocking=True, |
332 | 373 | ) |
| 374 | + |
| 375 | + |
| 376 | +async def test_get_sheet_invalid_config_entry( |
| 377 | + hass: HomeAssistant, |
| 378 | + setup_integration: ComponentSetup, |
| 379 | + config_entry: MockConfigEntry, |
| 380 | + expires_at: int, |
| 381 | + scopes: list[str], |
| 382 | +) -> None: |
| 383 | + """Test service call get sheet with invalid config entries.""" |
| 384 | + config_entry2 = MockConfigEntry( |
| 385 | + domain=DOMAIN, |
| 386 | + unique_id=TEST_SHEET_ID + "2", |
| 387 | + data={ |
| 388 | + "auth_implementation": DOMAIN, |
| 389 | + "token": { |
| 390 | + "access_token": "mock-access-token", |
| 391 | + "refresh_token": "mock-refresh-token", |
| 392 | + "expires_at": expires_at, |
| 393 | + "scope": " ".join(scopes), |
| 394 | + }, |
| 395 | + }, |
| 396 | + ) |
| 397 | + config_entry2.add_to_hass(hass) |
| 398 | + |
| 399 | + await setup_integration() |
| 400 | + |
| 401 | + assert config_entry.state is ConfigEntryState.LOADED |
| 402 | + assert config_entry2.state is ConfigEntryState.LOADED |
| 403 | + |
| 404 | + # Exercise service call on a config entry that does not exist |
| 405 | + with pytest.raises(ServiceValidationError, match="Invalid config entry"): |
| 406 | + await hass.services.async_call( |
| 407 | + DOMAIN, |
| 408 | + SERVICE_GET_SHEET, |
| 409 | + { |
| 410 | + DATA_CONFIG_ENTRY: config_entry.entry_id + "XXX", |
| 411 | + WORKSHEET: "Sheet1", |
| 412 | + ROWS: 2, |
| 413 | + }, |
| 414 | + blocking=True, |
| 415 | + return_response=True, |
| 416 | + ) |
| 417 | + |
| 418 | + # Unload the config entry invoke the service on the unloaded entry id |
| 419 | + await hass.config_entries.async_unload(config_entry2.entry_id) |
| 420 | + await hass.async_block_till_done() |
| 421 | + assert config_entry2.state is ConfigEntryState.NOT_LOADED |
| 422 | + |
| 423 | + |
| 424 | +async def test_get_sheet_invalid_worksheet( |
| 425 | + hass: HomeAssistant, |
| 426 | + setup_integration: ComponentSetup, |
| 427 | + config_entry: MockConfigEntry, |
| 428 | + expires_at: int, |
| 429 | + scopes: list[str], |
| 430 | +) -> None: |
| 431 | + """Test service call get sheet with invalid config entries.""" |
| 432 | + config_entry2 = MockConfigEntry( |
| 433 | + domain=DOMAIN, |
| 434 | + unique_id=TEST_SHEET_ID + "2", |
| 435 | + data={ |
| 436 | + "auth_implementation": DOMAIN, |
| 437 | + "token": { |
| 438 | + "access_token": "mock-access-token", |
| 439 | + "refresh_token": "mock-refresh-token", |
| 440 | + "expires_at": expires_at, |
| 441 | + "scope": " ".join(scopes), |
| 442 | + }, |
| 443 | + }, |
| 444 | + ) |
| 445 | + config_entry2.add_to_hass(hass) |
| 446 | + |
| 447 | + await setup_integration() |
| 448 | + |
| 449 | + assert config_entry.state is ConfigEntryState.LOADED |
| 450 | + assert config_entry2.state is ConfigEntryState.LOADED |
| 451 | + |
| 452 | + # Exercise service call on a worksheet that does not exist |
| 453 | + with patch("homeassistant.components.google_sheets.services.Client") as mock_client: |
| 454 | + mock_client.return_value.open_by_key.return_value.worksheet.side_effect = ( |
| 455 | + APIError(Response()) |
| 456 | + ) |
| 457 | + with pytest.raises(APIError): |
| 458 | + await hass.services.async_call( |
| 459 | + DOMAIN, |
| 460 | + SERVICE_GET_SHEET, |
| 461 | + { |
| 462 | + DATA_CONFIG_ENTRY: config_entry.entry_id, |
| 463 | + WORKSHEET: "DoesNotExist", |
| 464 | + ROWS: 2, |
| 465 | + }, |
| 466 | + blocking=True, |
| 467 | + return_response=True, |
| 468 | + ) |
0 commit comments