houqp commented on a change in pull request #9273:
URL: https://github.com/apache/airflow/pull/9273#discussion_r441892187
##########
File path: tests/api_connexion/endpoints/test_variable_endpoint.py
##########
@@ -29,38 +31,142 @@ def setUpClass(cls) -> None:
def setUp(self) -> None:
self.client = self.app.test_client() # type:ignore
+ clear_db_variables()
+
+ def tearDown(self) -> None:
+ clear_db_variables()
class TestDeleteVariable(TestVariableEndpoint):
- @pytest.mark.skip(reason="Not implemented yet")
- def test_should_response_200(self):
- response = self.client.delete("/api/v1/variables/TEST_VARIABLE_KEY")
+ def test_should_response_204(self):
+ Variable.set("delete_var1", 1)
+ # make sure variable is added
+ response = self.client.get("/api/v1/variables/delete_var1")
+ assert response.status_code == 200
+
+ response = self.client.delete("/api/v1/variables/delete_var1")
assert response.status_code == 204
+ # make sure variable is deleted
+ response = self.client.get("/api/v1/variables/delete_var1")
+ assert response.status_code == 404
+
class TestGetVariable(TestVariableEndpoint):
- @pytest.mark.skip(reason="Not implemented yet")
+
def test_should_response_200(self):
+ expected_value = '{"foo": 1}'
+ Variable.set("TEST_VARIABLE_KEY", expected_value)
response = self.client.get("/api/v1/variables/TEST_VARIABLE_KEY")
assert response.status_code == 200
+ assert response.json == {"key": "TEST_VARIABLE_KEY", "value":
expected_value}
+
+ def test_should_response_404_if_not_found(self):
+ response = self.client.get("/api/v1/variables/NONEXIST_VARIABLE_KEY")
+ assert response.status_code == 404
class TestGetVariables(TestVariableEndpoint):
- @pytest.mark.skip(reason="Not implemented yet")
- def test_should_response_200(self):
+ @parameterized.expand([
+ ("/api/v1/variables?limit=2&offset=0", {
+ "variables": [
+ {"key": "var1"},
+ {"key": "var2"},
+ ],
+ "total_entries": 2,
+ }),
+ ("/api/v1/variables?limit=2&offset=1", {
+ "variables": [
+ {"key": "var2"},
+ {"key": "var3"},
+ ],
+ "total_entries": 2,
+ }),
+ ("/api/v1/variables?limit=1&offset=2", {
+ "variables": [
+ {"key": "var3"},
+ ],
+ "total_entries": 1,
+ }),
+ ])
+ def test_should_get_list_variables(self, query, expected):
+ Variable.set("var1", 1)
+ Variable.set("var2", "foo")
+ Variable.set("var3", "[100, 101]")
+ response = self.client.get(query)
+ assert response.status_code == 200
+ assert response.json == expected
+
+ def test_should_honor_100_limit_default(self):
+ for i in range(101):
+ Variable.set(f"var{i}", i)
response = self.client.get("/api/v1/variables")
assert response.status_code == 200
+ assert response.json["total_entries"] == 100
+ assert len(response.json["variables"]) == 100
class TestPatchVariable(TestVariableEndpoint):
- @pytest.mark.skip(reason="Not implemented yet")
- def test_should_response_200(self):
- response = self.client.patch("/api/v1/variables/TEST_VARIABLE_KEY")
- assert response.status_code == 200
+ def test_should_update_variable(self):
+ Variable.set("var1", "foo")
+ response = self.client.patch("/api/v1/variables/var1", json={
+ "key": "var1",
+ "value": "updated",
+ })
+ assert response.status_code == 204
+ response = self.client.get("/api/v1/variables/var1")
+ assert response.json == {
Review comment:
That's strange, assert always give me nested diff on errors:
```
assert response.status_code == 400
> assert response.json == {
"title": "Invalid post body",
"status": 401,
"type": "about:blank",
"detail": "key from request body doesn't match uri parameter",
}
E AssertionError: assert {'detail': "key from request body doesn't
match uri parameter",\n 'status': 400,\n 'title': 'Invalid post body',\n
'type': 'about:blank'} == {'detail': "key from request body doesn't match uri
parameter",\n 'status': 401,\n 'title': 'Invalid post body',\n 'type':
'about:blank'}
E Common items:
E {'detail': "key from request body doesn't match uri parameter",
E 'title': 'Invalid post body',
E 'type': 'about:blank'}
E Differing items:
E {'status': 400} != {'status': 401}
E Full diff:
E {
E 'detail': "key from request body doesn't match uri parameter",
E - 'status': 401,
E ? ^
E + 'status': 400,
E ? ^
E 'title': 'Invalid post body',
E 'type': 'about:blank',
E }
tests/api_connexion/endpoints/test_variable_endpoint.py:133: AssertionError
```
Do you mind sharing how you ran tests? I usually just invoke `py.test
tests/api_connexion/endpoints/test_variable_endpoint.py` to run tests.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]