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