You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

conftest.py 4.1 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """
  16. Description: This file is used for some common util.
  17. """
  18. import os
  19. import shutil
  20. from unittest.mock import Mock
  21. import pytest
  22. from flask import Response
  23. from mindinsight.conf import settings
  24. from mindinsight.datavisual.data_transform import data_manager
  25. from mindinsight.datavisual.data_transform.data_manager import DataManager
  26. from mindinsight.datavisual.data_transform.loader_generators.loader_generator import MAX_DATA_LOADER_SIZE
  27. from mindinsight.datavisual.utils import tools
  28. from ....utils.log_operations import LogOperations
  29. from . import constants
  30. from . import globals as gbl
  31. summaries_metadata = None
  32. mock_data_manager = None
  33. summary_base_dir = constants.SUMMARY_BASE_DIR
  34. @pytest.fixture(autouse=True)
  35. def set_summary_base_dir(monkeypatch):
  36. """Mock settings.SUMMARY_BASE_DIR."""
  37. monkeypatch.setattr(settings, 'SUMMARY_BASE_DIR', summary_base_dir)
  38. @pytest.fixture(scope="session")
  39. def init_summary_logs():
  40. """Init summary logs."""
  41. try:
  42. if os.path.exists(summary_base_dir):
  43. shutil.rmtree(summary_base_dir)
  44. permissions = os.R_OK | os.W_OK | os.X_OK
  45. mode = permissions << 6
  46. if not os.path.exists(summary_base_dir):
  47. os.mkdir(summary_base_dir, mode=mode)
  48. global summaries_metadata, mock_data_manager
  49. log_operations = LogOperations()
  50. summaries_metadata = log_operations.create_summary_logs(summary_base_dir, constants.SUMMARY_DIR_NUM_FIRST,
  51. constants.SUMMARY_DIR_PREFIX)
  52. mock_data_manager = DataManager(summary_base_dir)
  53. mock_data_manager.start_load_data().join()
  54. summaries_metadata.update(
  55. log_operations.create_summary_logs(summary_base_dir, constants.SUMMARY_DIR_NUM_SECOND,
  56. constants.SUMMARY_DIR_PREFIX,
  57. constants.SUMMARY_DIR_NUM_FIRST))
  58. summaries_metadata.update(
  59. log_operations.create_multiple_logs(summary_base_dir, constants.MULTIPLE_DIR_NAME,
  60. constants.MULTIPLE_LOG_NUM))
  61. summaries_metadata.update(
  62. log_operations.create_reservoir_log(summary_base_dir, constants.RESERVOIR_DIR_NAME,
  63. constants.RESERVOIR_STEP_NUM))
  64. mock_data_manager.start_load_data().join()
  65. # Maximum number of loads is `MAX_DATA_LOADER_SIZE`.
  66. for i in range(len(summaries_metadata) - MAX_DATA_LOADER_SIZE):
  67. summaries_metadata.pop("./%s%d" % (constants.SUMMARY_DIR_PREFIX, i))
  68. yield
  69. finally:
  70. if os.path.exists(summary_base_dir):
  71. shutil.rmtree(summary_base_dir)
  72. @pytest.fixture(autouse=True)
  73. def populate_globals():
  74. """Populate globals."""
  75. gbl.summaries_metadata = summaries_metadata
  76. gbl.mock_data_manager = mock_data_manager
  77. @pytest.fixture
  78. def client():
  79. """This fixture is flask client."""
  80. gbl.mock_data_manager.start_load_data().join()
  81. data_manager.DATA_MANAGER = gbl.mock_data_manager
  82. packages = ["mindinsight.backend.datavisual"]
  83. mock_obj = Mock(return_value=packages)
  84. tools.find_app_package = mock_obj
  85. from mindinsight.backend.application import APP
  86. APP.response_class = Response
  87. app_client = APP.test_client()
  88. yield app_client