scene_test_builder.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. import os
  7. import json
  8. import pytest
  9. from typing import List
  10. from dataclasses import dataclass
  11. from _pytest.mark import ParameterSet
  12. from automatedtesting_shared import asset_database_utils as asset_db_utils
  13. @dataclass
  14. class BlackboxAssetTest:
  15. test_name: str
  16. asset_folder: str
  17. override_asset_folder: str = ""
  18. scene_debug_file: str = ""
  19. override_scene_debug_file: str = ""
  20. assets: List[asset_db_utils.DBSourceAsset] = ()
  21. override_assets: List[asset_db_utils.DBSourceAsset] = ()
  22. def populate_product_list_from_json(data_products: {iter}) -> list[asset_db_utils.DBProduct]:
  23. """
  24. Helper function used by the test_builder.py module to populate an expected product assets list from a json file.
  25. :param data_products: The "product" key from the target json dict.
  26. :return: list_products: A list of products.
  27. """
  28. list_products = []
  29. for product in data_products:
  30. list_products.append(
  31. asset_db_utils.DBProduct(
  32. product_name=product["product_name"],
  33. sub_id=product["sub_id"],
  34. asset_type=bytes(product["asset_type"], 'utf-8')
  35. )
  36. )
  37. return list_products
  38. def build_test(scenetest_file: object) -> BlackboxAssetTest:
  39. """
  40. A function that reads the contents of a scenetest json file to create a test.
  41. Returns the test parameters and data as an instance of the BlackboxAssetTest dataclass.
  42. Scene Test json template example at '\assetpipeline\scene_tests\tests\template.json'
  43. :param scenetest_file: A json formatted file that provides the data required to drive a test.
  44. :return: Test data to be parametrized using pytest.param() from within the test runner module in
  45. '\assetpipeline\scene_tests\scene_tests.py'.
  46. """
  47. # Open the json file and load it into a variable.
  48. with open(scenetest_file) as file:
  49. data = json.load(file)
  50. # Create an instance of the BlackboxAssetTest dataclass and populate the parameters with data from the json file.
  51. TestParams = BlackboxAssetTest(
  52. test_name=data["test_name"],
  53. asset_folder=data["asset_folder"],
  54. override_asset_folder=data["override_asset_folder"],
  55. scene_debug_file=data["scene_debug_file"],
  56. override_scene_debug_file=data["override_scene_debug_file"],
  57. assets=[
  58. asset_db_utils.DBSourceAsset(
  59. source_file_name=data["assets"]["source_file_name"],
  60. uuid=bytes(data["assets"]["uuid"], 'utf-8'),
  61. jobs=[
  62. asset_db_utils.DBJob(
  63. job_key=data["assets"]["jobs"]["job_key"],
  64. builder_guid=bytes(data["assets"]["jobs"]["builder_guid"], 'utf-8'),
  65. status=data["assets"]["jobs"]["status"],
  66. error_count=data["assets"]["jobs"]["error_count"],
  67. products=populate_product_list_from_json(data["assets"]["jobs"]["products"])
  68. )
  69. ]
  70. )
  71. ],
  72. override_assets=[
  73. asset_db_utils.DBSourceAsset(
  74. source_file_name=data["override_assets"]["source_file_name"],
  75. uuid=bytes(data["override_assets"]["uuid"], 'utf-8'),
  76. jobs=[
  77. asset_db_utils.DBJob(
  78. job_key=data["override_assets"]["jobs"]["job_key"],
  79. builder_guid=bytes(data["override_assets"]["jobs"]["builder_guid"], 'utf-8'),
  80. status=data["override_assets"]["jobs"]["status"],
  81. error_count=data["override_assets"]["jobs"]["error_count"],
  82. products=populate_product_list_from_json(data["override_assets"]["jobs"]["products"])
  83. )
  84. ]
  85. )
  86. ] if data["override_assets"] is not None else None
  87. )
  88. return TestParams
  89. def parametrize_blackbox_scene_test(dir_test_path: str) -> tuple[list[ParameterSet], list]:
  90. """
  91. Summary:
  92. Helper function that parametrizes tests based on json files found within the test directory and all sub-folders.
  93. :returns:
  94. blackbox_scene_tests: List of parametrized tests
  95. blackbox_test_ids: Name of tests to be used as IDs during parametrization
  96. """
  97. blackbox_scene_tests = []
  98. blackbox_test_ids = []
  99. for root, dirs, files in os.walk(dir_test_path):
  100. file_list = [file for file in files if file.endswith(".scenetest")]
  101. for test in file_list:
  102. test_path = os.path.join(root, os.path.relpath(test))
  103. blackbox_scene_tests.append(
  104. pytest.param(
  105. build_test(test_path)
  106. )
  107. )
  108. blackbox_test_ids.append(test.split(".")[0])
  109. return blackbox_scene_tests, blackbox_test_ids