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_model.py 11 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 the model module."""
  16. from unittest import TestCase, mock
  17. from unittest.mock import MagicMock
  18. from mindinsight.lineagemgr.model import get_summary_lineage, filter_summary_lineage, _convert_relative_path_to_abspath
  19. from mindinsight.lineagemgr.common.exceptions.exceptions import LineageParamSummaryPathError, \
  20. LineageFileNotFoundError, LineageSummaryParseException, LineageQuerierParamException, \
  21. LineageQuerySummaryDataError, LineageSearchConditionParamError, LineageParamTypeError, \
  22. LineageParamValueError
  23. from mindinsight.lineagemgr.common.path_parser import SummaryPathParser
  24. class TestModel(TestCase):
  25. """Test the function of get_summary_lineage and filter_summary_lineage."""
  26. @mock.patch('mindinsight.lineagemgr.model.Querier')
  27. @mock.patch('mindinsight.lineagemgr.model.LineageParser')
  28. @mock.patch('os.path.isdir')
  29. def test_get_summary_lineage_success(self, isdir_mock, parser_mock, qurier_mock):
  30. """Test the function of get_summary_lineage."""
  31. isdir_mock.return_value = True
  32. parser_mock.return_value = MagicMock()
  33. mock_querier = MagicMock()
  34. qurier_mock.return_value = mock_querier
  35. mock_querier.get_summary_lineage.return_value = [{'algorithm': {'network': 'ResNet'}}]
  36. summary_dir = '/path/to/summary_dir'
  37. result = get_summary_lineage(None, summary_dir, keys=['algorithm'])
  38. self.assertEqual(result, {'algorithm': {'network': 'ResNet'}})
  39. def test_get_summary_lineage_failed(self):
  40. """Test get_summary_lineage failed."""
  41. invalid_path = '../fake_dir'
  42. self.assertRaisesRegex(
  43. LineageParamSummaryPathError,
  44. 'The summary path is invalid.',
  45. get_summary_lineage,
  46. None,
  47. invalid_path
  48. )
  49. @mock.patch('mindinsight.lineagemgr.common.utils.validate_path')
  50. @mock.patch.object(SummaryPathParser, 'get_lineage_summaries')
  51. def test_get_summary_lineage_failed2(self, mock_summary, mock_valid):
  52. """Test get_summary_lineage failed."""
  53. mock_summary.return_value = []
  54. mock_valid.return_value = '/path/to/summary/dir'
  55. self.assertRaisesRegex(
  56. LineageFileNotFoundError,
  57. 'no summary log file under summary_dir',
  58. get_summary_lineage,
  59. None,
  60. '/path/to/summary_dir'
  61. )
  62. @mock.patch('mindinsight.lineagemgr.lineage_parser.FileHandler')
  63. @mock.patch('mindinsight.lineagemgr.lineage_parser.LineageParser._parse_summary_log')
  64. @mock.patch('mindinsight.lineagemgr.common.utils.validate_path')
  65. @mock.patch.object(SummaryPathParser, 'get_lineage_summaries')
  66. def test_get_summary_lineage_failed3(self,
  67. mock_summary,
  68. mock_valid,
  69. mock_parser,
  70. mock_file_handler):
  71. """Test get_summary_lineage failed."""
  72. mock_summary.return_value = ['/path/to/summary/file']
  73. mock_valid.return_value = '/path/to/summary_dir'
  74. mock_parser.return_value = None
  75. mock_file_handler = MagicMock()
  76. mock_file_handler.size = 1
  77. result = get_summary_lineage(None, '/path/to/summary_dir')
  78. assert {} == result
  79. @mock.patch('mindinsight.lineagemgr.model.validate_path')
  80. def test_convert_relative_path_to_abspath(self, validate_path_mock):
  81. """Test the function of converting realtive path to abspath."""
  82. validate_path_mock.return_value = '/path/to/summary_base_dir/summary_dir'
  83. summary_base_dir = '/path/to/summary_base_dir'
  84. search_condition = {
  85. 'summary_dir': {
  86. 'in': ['/path/to/summary_base_dir']
  87. }
  88. }
  89. result = _convert_relative_path_to_abspath(summary_base_dir,
  90. search_condition)
  91. self.assertDictEqual(
  92. result, {'summary_dir': {'in': ['/path/to/summary_base_dir/summary_dir']}})
  93. search_condition = {
  94. 'summary_dir': {
  95. 'in': ['./summary_dir']
  96. }
  97. }
  98. result = _convert_relative_path_to_abspath(summary_base_dir, search_condition)
  99. self.assertDictEqual(
  100. result, {'summary_dir': {'in': ['/path/to/summary_base_dir/summary_dir']}}
  101. )
  102. search_condition = {
  103. 'summary_dir': {
  104. 'eq': '/summary_dir'
  105. }
  106. }
  107. result = _convert_relative_path_to_abspath(summary_base_dir, search_condition)
  108. self.assertDictEqual(
  109. result, {'summary_dir': {'eq': '/path/to/summary_base_dir/summary_dir'}})
  110. search_condition = {
  111. 'summary_dir': None
  112. }
  113. result = _convert_relative_path_to_abspath(summary_base_dir, search_condition)
  114. self.assertDictEqual(
  115. result, search_condition
  116. )
  117. class TestFilterAPI(TestCase):
  118. """Test the function of filter_summary_lineage."""
  119. @mock.patch('mindinsight.lineagemgr.model.LineageOrganizer')
  120. @mock.patch('mindinsight.lineagemgr.model.Querier')
  121. @mock.patch('mindinsight.lineagemgr.lineage_parser.SummaryPathParser.get_lineage_summaries')
  122. @mock.patch('mindinsight.lineagemgr.model._convert_relative_path_to_abspath')
  123. @mock.patch('mindinsight.lineagemgr.model.normalize_summary_dir')
  124. def test_filter_summary_lineage(self, validate_path_mock, convert_path_mock,
  125. latest_summary_mock, qurier_mock, organizer_mock):
  126. """Test the function of filter_summary_lineage."""
  127. convert_path_mock.return_value = {
  128. 'summary_dir': {
  129. 'in': ['/path/to/summary_base_dir']
  130. },
  131. 'loss': {
  132. 'gt': 2.0
  133. }
  134. }
  135. organizer_mock = MagicMock()
  136. organizer_mock.super_lineage_objs = None
  137. validate_path_mock.return_value = True
  138. latest_summary_mock.return_value = ['/path/to/summary_base_dir/summary_dir']
  139. mock_querier = MagicMock()
  140. qurier_mock.return_value = mock_querier
  141. mock_querier.filter_summary_lineage.return_value = [{'loss': 3.0}]
  142. summary_base_dir = '/path/to/summary_base_dir'
  143. result = filter_summary_lineage(None, summary_base_dir)
  144. self.assertEqual(result, [{'loss': 3.0}])
  145. def test_invalid_path(self):
  146. """Test filter_summary_lineage with invalid path."""
  147. invalid_path = '../fake_dir'
  148. self.assertRaisesRegex(
  149. LineageParamSummaryPathError,
  150. 'The summary path is invalid.',
  151. filter_summary_lineage,
  152. None,
  153. invalid_path
  154. )
  155. @mock.patch('mindinsight.lineagemgr.model.validate_condition')
  156. @mock.patch('mindinsight.lineagemgr.model.normalize_summary_dir')
  157. def test_invalid_search_condition(self, mock_path, mock_valid):
  158. """Test filter_summary_lineage with invalid invalid param."""
  159. mock_path.return_value = None
  160. mock_valid.side_effect = LineageParamTypeError(
  161. 'Invalid search_condition type.')
  162. self.assertRaisesRegex(
  163. LineageSearchConditionParamError,
  164. 'Invalid search_condition type.',
  165. filter_summary_lineage,
  166. None,
  167. '/path/to/summary/dir',
  168. 'invalid_condition'
  169. )
  170. @mock.patch('mindinsight.lineagemgr.model.validate_search_model_condition')
  171. @mock.patch('mindinsight.lineagemgr.model.validate_condition')
  172. @mock.patch('mindinsight.lineagemgr.common.utils.validate_path')
  173. @mock.patch('mindinsight.lineagemgr.model._convert_relative_path_to_abspath')
  174. def test_failed_to_convert_path(self, mock_convert, *args):
  175. """Test filter_summary_lineage with invalid invalid param."""
  176. mock_convert.side_effect = LineageParamValueError('invalid path')
  177. args[0].return_value = None
  178. self.assertRaisesRegex(
  179. LineageParamSummaryPathError,
  180. 'invalid path',
  181. filter_summary_lineage,
  182. None,
  183. '/path/to/summary/dir',
  184. {}
  185. )
  186. @mock.patch('mindinsight.lineagemgr.model._convert_relative_path_to_abspath')
  187. @mock.patch('mindinsight.lineagemgr.model.validate_search_model_condition')
  188. @mock.patch('mindinsight.lineagemgr.model.validate_condition')
  189. @mock.patch('mindinsight.lineagemgr.model.normalize_summary_dir')
  190. @mock.patch.object(SummaryPathParser, 'get_lineage_summaries')
  191. def test_failed_to_get_summary_filesh(self, mock_parse, *args):
  192. """Test filter_summary_lineage with invalid invalid param."""
  193. path = '/path/to/summary/dir'
  194. mock_parse.return_value = []
  195. args[0].return_value = path
  196. self.assertRaisesRegex(
  197. LineageFileNotFoundError,
  198. 'There is no summary log file under summary_base_dir.',
  199. filter_summary_lineage,
  200. None,
  201. path
  202. )
  203. @mock.patch('mindinsight.lineagemgr.model._convert_relative_path_to_abspath')
  204. @mock.patch('mindinsight.lineagemgr.model.validate_search_model_condition')
  205. @mock.patch('mindinsight.lineagemgr.model.validate_condition')
  206. @mock.patch('mindinsight.lineagemgr.model.normalize_summary_dir')
  207. @mock.patch.object(SummaryPathParser, 'get_lineage_summaries')
  208. @mock.patch('mindinsight.lineagemgr.model.Querier')
  209. def test_failed_to_querier(self, mock_query, mock_parse, *args):
  210. """Test filter_summary_lineage with invalid invalid param."""
  211. mock_query.side_effect = LineageSummaryParseException()
  212. mock_parse.return_value = ['/path/to/summary/file']
  213. args[0].return_value = None
  214. res = filter_summary_lineage(None, '/path/to/summary')
  215. assert res == {'object': [], 'count': 0}
  216. mock_query.side_effect = LineageQuerierParamException(['keys'], 'key')
  217. self.assertRaisesRegex(
  218. LineageQuerySummaryDataError,
  219. 'Filter summary lineage failed.',
  220. filter_summary_lineage,
  221. None,
  222. '/path/to/summary/dir'
  223. )