|
19 | 19 | Integration tests for Variable operations. |
20 | 20 |
|
21 | 21 | These tests validate the Execution API endpoints for Variable operations: |
22 | | -- get(): Get variable value |
| 22 | +- get(): Get Variable value (positive, negative) |
| 23 | +- set(): Set Variable value |
| 24 | +- delete(): Delete Variable value |
23 | 25 | """ |
24 | 26 |
|
25 | 27 | from __future__ import annotations |
26 | 28 |
|
27 | | -import pytest |
28 | | - |
29 | 29 | from airflow.sdk.api.datamodels._generated import VariableResponse |
30 | | -from airflow.sdk.execution_time.comms import ErrorResponse |
| 30 | +from airflow.sdk.execution_time.comms import ErrorResponse, OKResponse |
31 | 31 | from task_sdk_tests import console |
32 | 32 |
|
33 | 33 |
|
@@ -76,25 +76,67 @@ def test_variable_get_not_found(sdk_client): |
76 | 76 | console.print("[green]✅ Variable get (not found) test passed!") |
77 | 77 |
|
78 | 78 |
|
79 | | -@pytest.mark.skip(reason="TODO: Implement Variable set test") |
80 | 79 | def test_variable_set(sdk_client): |
81 | 80 | """ |
82 | 81 | Test setting variable value. |
83 | 82 |
|
84 | 83 | Expected: OKResponse with ok=True |
85 | 84 | Endpoint: PUT /execution/variables/{key} |
86 | 85 | """ |
87 | | - console.print("[yellow]TODO: Implement test_variable_set") |
88 | | - raise NotImplementedError("test_variable_set not implemented") |
| 86 | + response = sdk_client.variables.set("test_variable_key", "test_variable_value") |
| 87 | + |
| 88 | + console.print(" Variable Set Response ".center(72, "=")) |
| 89 | + console.print(f"[bright_blue]Response Type:[/] {type(response).__name__}") |
| 90 | + console.print(f"[bright_blue]Status:[/] {response.ok}") |
| 91 | + console.print("=" * 72) |
| 92 | + |
| 93 | + assert isinstance(response, OKResponse) |
| 94 | + assert response.ok is True |
| 95 | + |
| 96 | + console.print("[green]✅ Variable set test passed!") |
89 | 97 |
|
90 | 98 |
|
91 | | -@pytest.mark.skip(reason="TODO: Implement Variable delete test") |
92 | 99 | def test_variable_delete(sdk_client): |
93 | 100 | """ |
94 | 101 | Test deleting variable value. |
95 | 102 |
|
96 | 103 | Expected: OKResponse with ok=True |
97 | 104 | Endpoint: DELETE /execution/variables/{key} |
98 | 105 | """ |
99 | | - console.print("[yellow]TODO: Implement test_variable_delete") |
100 | | - raise NotImplementedError("test_variable_delete not implemented") |
| 106 | + console.print("[yellow]Deleting variable...") |
| 107 | + |
| 108 | + # When using the test_variable_key, we noticed an issue where it appears that the variable that was |
| 109 | + # being set/deleted was still available when retrieved later. Per @amoghrajesh, in Docker Compose [we've] |
| 110 | + # defined an environment variable for test_variable_key and secrets backends are read-only, so when [our] |
| 111 | + # test calls Variable.set(), the value is written to the database — and when you delete it, Airflow |
| 112 | + # correctly removes it from the DB... However, a calling Variable.get() once the Variable had been |
| 113 | + # deleted pointed to the Secrets Backend, which still had that key available. |
| 114 | + key: str = "test_variable_delete_key" |
| 115 | + |
| 116 | + # First, set the variable |
| 117 | + sdk_client.variables.set(key, "test_variable_value") |
| 118 | + |
| 119 | + # Now, delete the variable |
| 120 | + response = sdk_client.variables.delete(key) |
| 121 | + |
| 122 | + console.print(" Variable Delete Response ".center(72, "=")) |
| 123 | + console.print(f"[bright_blue]Response Type:[/] {type(response).__name__}") |
| 124 | + console.print(f"[bright_blue]Status:[/] {response.ok}") |
| 125 | + console.print("=" * 72) |
| 126 | + |
| 127 | + assert isinstance(response, OKResponse) |
| 128 | + assert response.ok is True |
| 129 | + |
| 130 | + # Validate that the Variable has in fact been deleted |
| 131 | + get_response = sdk_client.variables.get(key) |
| 132 | + |
| 133 | + console.print(" Variable Get After Delete ".center(72, "=")) |
| 134 | + console.print(f"[bright_blue]Response Type:[/] {type(get_response).__name__}") |
| 135 | + console.print(f"[bright_blue]Error Type:[/] {get_response.error}") |
| 136 | + console.print(f"[bright_blue]Detail:[/] {get_response.detail}") |
| 137 | + console.print("=" * 72) |
| 138 | + |
| 139 | + assert isinstance(get_response, ErrorResponse) |
| 140 | + assert str(get_response.error).find("VARIABLE_NOT_FOUND") != -1 |
| 141 | + |
| 142 | + console.print("[green]✅ Variable delete test passed!") |
0 commit comments