Skip to content

Commit 47f5e22

Browse files
authored
issue-58718: Adding Task SDK integration tests for Variable operations (#58357)
1 parent 25d36db commit 47f5e22

1 file changed

Lines changed: 52 additions & 10 deletions

File tree

task-sdk-integration-tests/tests/task_sdk_tests/test_variable_operations.py

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
Integration tests for Variable operations.
2020
2121
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
2325
"""
2426

2527
from __future__ import annotations
2628

27-
import pytest
28-
2929
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
3131
from task_sdk_tests import console
3232

3333

@@ -76,25 +76,67 @@ def test_variable_get_not_found(sdk_client):
7676
console.print("[green]✅ Variable get (not found) test passed!")
7777

7878

79-
@pytest.mark.skip(reason="TODO: Implement Variable set test")
8079
def test_variable_set(sdk_client):
8180
"""
8281
Test setting variable value.
8382
8483
Expected: OKResponse with ok=True
8584
Endpoint: PUT /execution/variables/{key}
8685
"""
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!")
8997

9098

91-
@pytest.mark.skip(reason="TODO: Implement Variable delete test")
9299
def test_variable_delete(sdk_client):
93100
"""
94101
Test deleting variable value.
95102
96103
Expected: OKResponse with ok=True
97104
Endpoint: DELETE /execution/variables/{key}
98105
"""
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

Comments
 (0)