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.

test_create_project.py 2.9 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. """Test CreateProject class."""
  16. import os
  17. import shutil
  18. import tempfile
  19. from unittest.mock import patch
  20. from mindinsight.wizard.base.source_file import SourceFile
  21. from mindinsight.wizard.create_project import CreateProject
  22. from mindinsight.wizard.network.generic_network import GenericNetwork
  23. from tests.ut.wizard.utils import generate_file
  24. class TestCreateProject:
  25. """Test SourceFile"""
  26. workspace_dir = None
  27. def setup_method(self):
  28. """Setup before call test method."""
  29. self.workspace_dir = tempfile.mkdtemp()
  30. def teardown_method(self):
  31. """Tear down after call test method."""
  32. self._remove_dirs()
  33. self.workspace_dir = None
  34. def _remove_dirs(self):
  35. """Recursively delete a directory tree."""
  36. if self.workspace_dir and os.path.exists(self.workspace_dir):
  37. shutil.rmtree(self.workspace_dir)
  38. @staticmethod
  39. def _generate_file(file):
  40. """Create a file and write content."""
  41. generate_file(file, "template file.")
  42. @patch.object(GenericNetwork, 'generate')
  43. @patch.object(GenericNetwork, 'configure')
  44. @patch.object(CreateProject, 'ask_network')
  45. @patch.object(CreateProject, 'echo_notice')
  46. @patch('os.getcwd')
  47. def test_run(self, mock_getcwd, mock_echo_notice, mock_ask_network, mock_config, mock_generate):
  48. """Test run method of CreateProject."""
  49. source_file = SourceFile()
  50. source_file.template_file_path = os.path.join(self.workspace_dir, 'templates', 'train.py-tpl')
  51. source_file.file_relative_path = 'train.py'
  52. self._generate_file(source_file.template_file_path)
  53. # mock os.getcwd method
  54. mock_getcwd.return_value = self.workspace_dir
  55. mock_echo_notice.return_value = None
  56. mock_ask_network.return_value = 'lenet'
  57. mock_config.return_value = None
  58. mock_generate.return_value = [source_file]
  59. project_name = 'test'
  60. new_project = CreateProject()
  61. new_project.run({'name': project_name})
  62. assert os.path.exists(os.path.join(self.workspace_dir, project_name))
  63. assert os.access(os.path.join(self.workspace_dir, project_name, 'train.py'), mode=os.F_OK | os.R_OK | os.W_OK)