Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_stat.py
# Copyright (C) 2015-2019 The Software Heritage developers | # Copyright (C) 2015-2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from rest_framework.test import APITestCase | from rest_framework.test import APITestCase | ||||
from unittest.mock import patch | from unittest.mock import patch | ||||
from swh.storage.exc import StorageDBError, StorageAPIError | from swh.storage.exc import StorageDBError, StorageAPIError | ||||
from swh.web.common.exc import BadInputExc | |||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
from swh.web.tests.testcase import WebTestCase | from swh.web.tests.testcase import WebTestCase | ||||
class StatApiTestCase(WebTestCase, APITestCase): | class StatApiTestCase(WebTestCase, APITestCase): | ||||
@patch('swh.web.api.views.stat.service') | @patch('swh.web.api.views.stat.service') | ||||
def test_api_1_stat_counters_raise_error(self, mock_service): | def test_api_1_stat_counters_raise_error(self, mock_service): | ||||
mock_service.stat_counters.side_effect = ValueError( | mock_service.stat_counters.side_effect = BadInputExc( | ||||
'voluntary error to check the bad request middleware.') | 'voluntary error to check the bad request middleware.') | ||||
url = reverse('api-1-stat-counters') | url = reverse('api-1-stat-counters') | ||||
rv = self.client.get(url) | rv = self.client.get(url) | ||||
self.assertEqual(rv.status_code, 400, rv.data) | self.assertEqual(rv.status_code, 400, rv.data) | ||||
self.assertEqual(rv['Content-Type'], 'application/json') | self.assertEqual(rv['Content-Type'], 'application/json') | ||||
self.assertEqual(rv.data, { | self.assertEqual(rv.data, { | ||||
'exception': 'ValueError', | 'exception': 'BadInputExc', | ||||
'reason': 'voluntary error to check the bad request middleware.'}) | 'reason': 'voluntary error to check the bad request middleware.'}) | ||||
@patch('swh.web.api.views.stat.service') | @patch('swh.web.api.views.stat.service') | ||||
def test_api_1_stat_counters_raise_from_db(self, mock_service): | def test_api_1_stat_counters_raise_from_db(self, mock_service): | ||||
mock_service.stat_counters.side_effect = StorageDBError( | mock_service.stat_counters.side_effect = StorageDBError( | ||||
'Storage exploded! Will be back online shortly!') | 'Storage exploded! Will be back online shortly!') | ||||
Show All 39 Lines |