ashb commented on code in PR #44449:
URL: https://github.com/apache/airflow/pull/44449#discussion_r1862269813


##########
tests/api_fastapi/execution_api/routes/test_variables.py:
##########
@@ -75,3 +84,81 @@ def test_variable_get_access_denied(self, client):
                 "message": "Task does not have access to variable key1",
             }
         }
+
+
+class TestPostVariable:
+    @pytest.mark.parametrize(
+        "payload",
+        [
+            pytest.param({"value": "{}", "description": "description"}, 
id="valid-payload"),
+            pytest.param({"value": "{}"}, id="missing-description"),
+        ],
+    )
+    def test_should_create_variable(self, client, payload, session):
+        key = "var_create"
+        response = client.put(
+            f"/execution/variables/{key}",
+            json=payload,
+        )
+        assert response.status_code == 201
+
+        var_from_db = session.query(Variable).where(Variable.key == 
"var_create").first()
+        assert var_from_db is not None
+        assert var_from_db.key == key
+        assert var_from_db.val == payload["value"]
+        if "description" in payload:
+            assert var_from_db.description == payload["description"]
+
+    @pytest.mark.parametrize(
+        "key, status_code, payload",
+        [
+            pytest.param("", 404, {"value": "{}", "description": 
"description"}, id="missing-key"),
+            pytest.param("var_create", 422, {"description": "description"}, 
id="missing-value"),
+        ],
+    )
+    def test_variable_missing_fields(self, client, key, status_code, payload, 
session):
+        response = client.put(
+            f"/execution/variables/{key}",
+            json=payload,
+        )
+        assert response.status_code == status_code
+        if response.status_code == 422:
+            assert response.json()["detail"][0]["type"] == "missing"
+            assert response.json()["detail"][0]["msg"] == "Field required"
+
+    def test_overwriting_existing_variable(self, client, session):
+        Variable.set(key="var_create", value="value", session=session)
+        session.commit()
+
+        key = "var_create"

Review Comment:
   Nit:
   ```suggestion
           key = "var_create"
           Variable.set(key=key, value="value", session=session)
           session.commit()
   
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to