action_test.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import json
  2. import requests
  3. import pytest
  4. @pytest.fixture
  5. def api_url():
  6. return "http://127.0.0.1:8086/api/v1/actions"
  7. @pytest.fixture
  8. def valid_payload():
  9. return {
  10. "openapi_schema": {
  11. "openapi": "3.0.0",
  12. "info": {
  13. "title": "OpenWeatherMap One Call API",
  14. "description": "API for accessing comprehensive weather data from OpenWeatherMap.",
  15. "version": "1.0.0",
  16. },
  17. "servers": [
  18. {
  19. "url": "https://api.openweathermap.org/data/3.0",
  20. "description": "OpenWeatherMap One Call API server",
  21. }
  22. ],
  23. "paths": {
  24. "/onecall": {
  25. "get": {
  26. "summary": "Get Comprehensive Weather Data",
  27. "description": "Retrieves weather data for a specific latitude and longitude.",
  28. "operationId": "get_weather_data",
  29. "parameters": [
  30. {
  31. "in": "query",
  32. "name": "lat",
  33. "schema": {"type": "number", "format": "float", "minimum": -90, "maximum": 90},
  34. "required": True,
  35. "description": "Latitude, decimal (-90 to 90).",
  36. },
  37. {
  38. "in": "query",
  39. "name": "lon",
  40. "schema": {"type": "number", "format": "float", "minimum": -180, "maximum": 180},
  41. "required": True,
  42. "description": "Longitude, decimal (-180 to 180).",
  43. },
  44. {
  45. "in": "query",
  46. "name": "exclude",
  47. "schema": {"type": "string"},
  48. "required": False,
  49. "description": "Exclude some parts of the weather data(current, minutely, hourly, daily, alerts).",
  50. },
  51. {
  52. "in": "query",
  53. "name": "appid",
  54. "schema": {"type": "string", "enum": ["101f41d3ff4095824722d57a513cb80a"]},
  55. "required": True,
  56. "description": "Your unique API key.",
  57. },
  58. ],
  59. "responses": {
  60. "200": {
  61. "description": "Successful response with comprehensive weather data.",
  62. "content": {"application/json": {"schema": {"type": "object", "properties": {}}}},
  63. }
  64. },
  65. }
  66. }
  67. },
  68. }
  69. }
  70. @pytest.fixture
  71. def created_action_id(api_url, valid_payload):
  72. # 在创建动作的 fixture 中执行创建动作并返回动作 ID
  73. headers = {"Content-Type": "application/json"}
  74. response = requests.post(api_url, headers=headers, json=valid_payload)
  75. assert response.status_code == 200
  76. return response.json()[0]["id"]
  77. # 测试创建动作
  78. def test_create_action(api_url, valid_payload):
  79. headers = {"Content-Type": "application/json"}
  80. response = requests.post(api_url, headers=headers, json=valid_payload)
  81. assert response.status_code == 200
  82. # 测试获取动作
  83. def test_get_action(api_url, created_action_id):
  84. get_url = f"{api_url}/{created_action_id}"
  85. response = requests.get(get_url)
  86. assert response.status_code == 200
  87. # 测试更新动作
  88. def test_update_action(api_url, valid_payload, created_action_id):
  89. update_url = f"{api_url}/{created_action_id}"
  90. valid_payload["openapi_schema"]["paths"]["/onecall"]["get"]["operationId"] = "update_test"
  91. headers = {"Content-Type": "application/json"}
  92. response = requests.post(update_url, headers=headers, json=valid_payload)
  93. assert response.status_code == 200
  94. assert response.json()["operation_id"] == "update_test"
  95. # 测试删除动作
  96. def test_delete_action(api_url, created_action_id):
  97. delete_url = f"{api_url}/{created_action_id}"
  98. response = requests.delete(delete_url)
  99. assert response.status_code == 200
  100. # 测试删除动作
  101. def test_run_action(api_url, created_action_id):
  102. run_action_url = f"{api_url}/{created_action_id}/run"
  103. payload = json.dumps({"parameters": {"lon": 120.1552, "lat": 30.2741}})
  104. headers = {"Content-Type": "application/json"}
  105. response = requests.post(run_action_url, headers=headers, data=payload)
  106. assert response.status_code == 200