test_end_to_end.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright 2023 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. assert (set(scenario) - set(json.load(open("tests/rest/json/expected/scenario.json")))) == set()
  19. response = client.post(url_for("api.scenario_submit", scenario_id=scenario.get("id")))
  20. assert response.status_code == 200
  21. return scenario
  22. def get(url, name, client) -> Dict:
  23. response = client.get(url)
  24. returned_data = response.json.get(name)
  25. assert (set(returned_data) - set(json.load(open(f"tests/rest/json/expected/{name}.json")))) == set()
  26. return returned_data
  27. def get_assert_status(url, client, status_code) -> None:
  28. response = client.get(url)
  29. assert response.status_code == status_code
  30. def get_all(url, expected_quantity, client):
  31. response = client.get(url)
  32. assert len(response.json) == expected_quantity
  33. def delete(url, client):
  34. response = client.delete(url)
  35. assert response.status_code == 200
  36. def test_end_to_end(client, setup_end_to_end):
  37. # Create Scenario: Should also create all of its dependencies(sequences, tasks, datanodes, etc)
  38. scenario = create_and_submit_scenario("scenario", client)
  39. # Get other models and verify if they return the necessary fields
  40. cycle = get(url_for("api.cycle_by_id", cycle_id=scenario.get("cycle")), "cycle", client)
  41. sequence = get(
  42. url_for("api.sequence_by_id", sequence_id=f"SEQUENCE_sequence_{scenario['id']}"),
  43. "sequence",
  44. client,
  45. )
  46. task = get(url_for("api.task_by_id", task_id=sequence.get("tasks")[0]), "task", client)
  47. datanode = get(
  48. url_for("api.datanode_by_id", datanode_id=task.get("input_ids")[0]),
  49. "datanode",
  50. client,
  51. )
  52. # Get All
  53. get_all(url_for("api.scenarios"), 1, client)
  54. get_all(url_for("api.cycles"), 1, client)
  55. get_all(url_for("api.sequences"), 1, client)
  56. get_all(url_for("api.tasks"), 2, client)
  57. get_all(url_for("api.datanodes"), 5, client)
  58. get_all(url_for("api.jobs"), 2, client)
  59. # Delete entities
  60. delete(url_for("api.cycle_by_id", cycle_id=cycle.get("id")), client)
  61. delete(url_for("api.sequence_by_id", sequence_id=sequence.get("id")), client)
  62. delete(url_for("api.task_by_id", task_id=task.get("id")), client)
  63. delete(url_for("api.datanode_by_id", datanode_id=datanode.get("id")), client)
  64. # Check status code
  65. # Non-existing entities should return 404
  66. get_assert_status(url_for("api.cycle_by_id", cycle_id=9999999), client, 404)
  67. get_assert_status(url_for("api.scenario_by_id", scenario_id=9999999), client, 404)
  68. get_assert_status(url_for("api.sequence_by_id", sequence_id=9999999), client, 404)
  69. get_assert_status(url_for("api.task_by_id", task_id=9999999), client, 404)
  70. get_assert_status(url_for("api.datanode_by_id", datanode_id=9999999), client, 404)
  71. # Check URL with and without trailing slashes
  72. url_with_slash = url_for("api.scenarios")
  73. url_without_slash = url_for("api.scenarios")[:-1]
  74. get_all(url_with_slash, 1, client)
  75. get_all(url_without_slash, 1, client)