test_end_to_end.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import json
  12. from typing import Dict
  13. from flask import url_for
  14. def create_and_submit_scenario(config_id: str, client) -> Dict:
  15. response = client.post(url_for("api.scenarios", config_id=config_id))
  16. assert response.status_code == 201
  17. scenario = response.json.get("scenario")
  18. path = "tests/rest/json/expected/scenario.json"
  19. with open(path) as file:
  20. expected_scenario = json.load(file)
  21. assert (set(scenario) - set(expected_scenario)) == set()
  22. response = client.post(url_for("api.scenario_submit", scenario_id=scenario.get("id")))
  23. assert response.status_code == 200
  24. return scenario
  25. def get(url, name, client) -> Dict:
  26. response = client.get(url)
  27. returned_data = response.json.get(name)
  28. path = f"tests/rest/json/expected/{name}.json"
  29. with open(path) as file:
  30. expected_data = json.load(file)
  31. assert (set(returned_data) - set(expected_data)) == set()
  32. return returned_data
  33. def get_assert_status(url, client, status_code) -> None:
  34. response = client.get(url)
  35. assert response.status_code == status_code
  36. def get_all(url, expected_quantity, client):
  37. response = client.get(url)
  38. assert len(response.json) == expected_quantity
  39. def delete(url, client):
  40. response = client.delete(url)
  41. assert response.status_code == 200
  42. def test_end_to_end(client, setup_end_to_end):
  43. # Create Scenario: Should also create all of its dependencies(sequences, tasks, datanodes, etc)
  44. scenario = create_and_submit_scenario("scenario", client)
  45. # Get other models and verify if they return the necessary fields
  46. cycle = get(url_for("api.cycle_by_id", cycle_id=scenario.get("cycle")), "cycle", client)
  47. sequence = get(
  48. url_for("api.sequence_by_id", sequence_id=f"SEQUENCE_sequence_{scenario['id']}"),
  49. "sequence",
  50. client,
  51. )
  52. task = get(url_for("api.task_by_id", task_id=sequence.get("tasks")[0]), "task", client)
  53. datanode = get(
  54. url_for("api.datanode_by_id", datanode_id=task.get("input_ids")[0]),
  55. "datanode",
  56. client,
  57. )
  58. # Get All
  59. get_all(url_for("api.scenarios"), 1, client)
  60. get_all(url_for("api.cycles"), 1, client)
  61. get_all(url_for("api.sequences"), 1, client)
  62. get_all(url_for("api.tasks"), 2, client)
  63. get_all(url_for("api.datanodes"), 5, client)
  64. get_all(url_for("api.jobs"), 2, client)
  65. # Delete entities
  66. delete(url_for("api.cycle_by_id", cycle_id=cycle.get("id")), client)
  67. delete(url_for("api.sequence_by_id", sequence_id=sequence.get("id")), client)
  68. delete(url_for("api.task_by_id", task_id=task.get("id")), client)
  69. delete(url_for("api.datanode_by_id", datanode_id=datanode.get("id")), client)
  70. # Check status code
  71. # Non-existing entities should return 404
  72. get_assert_status(url_for("api.cycle_by_id", cycle_id=9999999), client, 404)
  73. get_assert_status(url_for("api.scenario_by_id", scenario_id=9999999), client, 404)
  74. get_assert_status(url_for("api.sequence_by_id", sequence_id=9999999), client, 404)
  75. get_assert_status(url_for("api.task_by_id", task_id=9999999), client, 404)
  76. get_assert_status(url_for("api.datanode_by_id", datanode_id=9999999), client, 404)
  77. # Check URL with and without trailing slashes
  78. url_with_slash = url_for("api.scenarios")
  79. url_without_slash = url_for("api.scenarios")[:-1]
  80. get_all(url_with_slash, 1, client)
  81. get_all(url_without_slash, 1, client)