test_request_error.py 3.5 KB
Newer Older
G
gaocongli 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Copyright 2019 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""
Function:
    Test error handler.
Usage:
    pytest tests/ut/datavisual
"""
from unittest.mock import patch

李鸿章 已提交
23
from werkzeug.exceptions import MethodNotAllowed, NotFound
G
gaocongli 已提交
24 25 26

from mindinsight.datavisual.processors.scalars_processor import ScalarsProcessor

李鸿章 已提交
27 28 29
from ....utils.tools import get_url
from ...backend.datavisual.conftest import TRAIN_ROUTES

G
gaocongli 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

class TestErrorHandler:
    """Test train visual api."""

    @patch.object(ScalarsProcessor, 'get_metadata_list')
    def test_handle_http_exception_error_not_found(self, mock_scalar_processor, client):
        """Test handle http exception error not found."""
        text = 'Test Message'

        # NotFound
        def get_metadata_list(train_ids, tag):
            raise NotFound("%s" % text)

        mock_scalar_processor.side_effect = get_metadata_list

        test_train_ids = "aa"
        test_tag = "bb"
        params = dict(train_ids=test_train_ids, tag=test_tag)
        url = get_url(TRAIN_ROUTES['scalar_metadata'], params)
        response = client.get(url)

        assert response.status_code == 404
        response = response.get_json()
        assert response['error_code'] == '50545001'
        assert response['error_msg'] == '404 Not Found.'

    @patch.object(ScalarsProcessor, 'get_metadata_list')
    def test_handle_http_exception_error_method_not_allowed(self, mock_scalar_processor, client):
        """Test handling http exception error method not allowed."""
        text = 'Test Message'

        # MethodNotAllowed
        def get_metadata_list(train_ids, tag):
            raise MethodNotAllowed("%s" % text)

        mock_scalar_processor.side_effect = get_metadata_list

        test_train_ids = "aa"
        test_tag = "bb"
        params = dict(train_ids=test_train_ids, tag=test_tag)
        url = get_url(TRAIN_ROUTES['scalar_metadata'], params)
        response = client.get(url)

        assert response.status_code == 405
        response = response.get_json()
        assert response['error_code'] == '50545002'
        assert response['error_msg'] == '405 Method Not Allowed.'

    @patch.object(ScalarsProcessor, 'get_metadata_list')
    def test_handle_http_exception_error_method_other_errors(self, mock_scalar_processor, client):
        """Test handling http exception error method other errors."""
        text = 'Test Message'

        # Other errors
        def get_metadata_list(train_ids, tag):
            raise KeyError("%s" % text)

        mock_scalar_processor.side_effect = get_metadata_list

        test_train_ids = "aa"
        test_tag = "bb"
        params = dict(train_ids=test_train_ids, tag=test_tag)
        url = get_url(TRAIN_ROUTES['scalar_metadata'], params)
        response = client.get(url)

        assert response.status_code == 500
        response = response.get_json()
        assert response['error_code'] == '50540000'
        assert response['error_msg'] == 'System error.'