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_lineage_api.py 4.9 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 module of lineage_api."""
  16. import json
  17. import os
  18. from unittest import TestCase, mock
  19. from flask import Response
  20. from mindinsight.backend.application import APP
  21. from mindinsight.lineagemgr.common.exceptions.exceptions import LineageQuerySummaryDataError
  22. LINEAGE_FILTRATION_BASE = {
  23. 'accuracy': None,
  24. 'mae': None,
  25. 'mse': None,
  26. 'loss_function': 'SoftmaxCrossEntropyWithLogits',
  27. 'train_dataset_path': None,
  28. 'train_dataset_count': 64,
  29. 'user_defined': {},
  30. 'test_dataset_path': None,
  31. 'test_dataset_count': None,
  32. 'network': 'str',
  33. 'optimizer': 'Momentum',
  34. 'learning_rate': 0.11999999731779099,
  35. 'epoch': 12,
  36. 'batch_size': 32,
  37. 'loss': 0.029999999329447746,
  38. 'model_size': 128
  39. }
  40. LINEAGE_FILTRATION_RUN1 = {
  41. 'accuracy': 0.78,
  42. 'mae': None,
  43. 'mse': None,
  44. 'loss_function': 'SoftmaxCrossEntropyWithLogits',
  45. 'train_dataset_path': None,
  46. 'train_dataset_count': 64,
  47. 'user_defined': {},
  48. 'test_dataset_path': None,
  49. 'test_dataset_count': 64,
  50. 'network': 'str',
  51. 'optimizer': 'Momentum',
  52. 'learning_rate': 0.11999999731779099,
  53. 'epoch': 14,
  54. 'batch_size': 32,
  55. 'loss': 0.029999999329447746,
  56. 'model_size': 128
  57. }
  58. class TestSearchModel(TestCase):
  59. """Test the restful api of search_model."""
  60. def setUp(self):
  61. """Test init."""
  62. APP.response_class = Response
  63. self.app_client = APP.test_client()
  64. self.url = '/v1/mindinsight/lineagemgr/lineages'
  65. @mock.patch('mindinsight.backend.lineagemgr.lineage_api.settings')
  66. @mock.patch('mindinsight.backend.lineagemgr.lineage_api.filter_summary_lineage')
  67. def test_search_model_success(self, *args):
  68. """Test the success of model_success."""
  69. base_dir = '/path/to/test_lineage_summary_dir_base'
  70. args[0].return_value = {
  71. 'object': [
  72. {
  73. 'summary_dir': base_dir,
  74. 'model_lineage': LINEAGE_FILTRATION_BASE
  75. },
  76. {
  77. 'summary_dir': os.path.join(base_dir, 'run1'),
  78. 'model_lineage': LINEAGE_FILTRATION_RUN1
  79. }
  80. ],
  81. 'count': 2
  82. }
  83. args[1].SUMMARY_BASE_DIR = base_dir
  84. body_data = {
  85. 'limit': 10,
  86. 'offset': 0,
  87. 'sorted_name': 'summary_dir',
  88. 'sorted_type': None
  89. }
  90. response = self.app_client.post(self.url, data=json.dumps(body_data))
  91. self.assertEqual(200, response.status_code)
  92. expect_result = {
  93. 'object': [
  94. {
  95. 'summary_dir': './',
  96. 'model_lineage': LINEAGE_FILTRATION_BASE
  97. },
  98. {
  99. 'summary_dir': './run1',
  100. 'model_lineage': LINEAGE_FILTRATION_RUN1
  101. }
  102. ],
  103. 'count': 2
  104. }
  105. self.assertDictEqual(expect_result, response.get_json())
  106. @mock.patch('mindinsight.backend.lineagemgr.lineage_api.settings')
  107. @mock.patch('mindinsight.backend.lineagemgr.lineage_api.filter_summary_lineage')
  108. def test_search_model_fail(self, *args):
  109. """Test the function of model_lineage with exception."""
  110. response = self.app_client.post(self.url, data='xxx')
  111. self.assertEqual(400, response.status_code)
  112. expect_result = {
  113. 'error_code': '50540002',
  114. 'error_msg': 'Invalid parameter value. Json data parse failed.'
  115. }
  116. self.assertDictEqual(expect_result, response.get_json())
  117. args[0].side_effect = LineageQuerySummaryDataError('xxx')
  118. args[1].SUMMARY_BASE_DIR = '/path/to/test_lineage_summary_dir_base'
  119. body_data = {
  120. 'limit': 10,
  121. 'offset': 0,
  122. 'sorted_name': 'summary_dir',
  123. 'sorted_type': None
  124. }
  125. response = self.app_client.post(self.url, data=json.dumps(body_data))
  126. self.assertEqual(400, response.status_code)
  127. expect_result = {
  128. 'error_code': '50542215',
  129. 'error_msg': 'Query summary data error: xxx'
  130. }
  131. self.assertDictEqual(expect_result, response.get_json())