Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_stat.py
# Copyright (C) 2015-2018 The Software Heritage developers | # Copyright (C) 2015-2018 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 nose.tools import istest | |||||
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.tests.testcase import SWHWebTestCase | from swh.web.tests.testcase import SWHWebTestCase | ||||
class StatApiTestCase(SWHWebTestCase, APITestCase): | class StatApiTestCase(SWHWebTestCase, APITestCase): | ||||
@patch('swh.web.api.views.stat.service') | @patch('swh.web.api.views.stat.service') | ||||
@istest | def test_api_1_stat_counters_raise_error(self, mock_service): | ||||
def api_1_stat_counters_raise_error(self, mock_service): | |||||
# given | # given | ||||
mock_service.stat_counters.side_effect = ValueError( | mock_service.stat_counters.side_effect = ValueError( | ||||
'voluntary error to check the bad request middleware.') | 'voluntary error to check the bad request middleware.') | ||||
# when | # when | ||||
rv = self.client.get('/api/1/stat/counters/') | rv = self.client.get('/api/1/stat/counters/') | ||||
# then | # then | ||||
self.assertEquals(rv.status_code, 400) | self.assertEquals(rv.status_code, 400) | ||||
self.assertEquals(rv['Content-Type'], 'application/json') | self.assertEquals(rv['Content-Type'], 'application/json') | ||||
self.assertEquals(rv.data, { | self.assertEquals(rv.data, { | ||||
'exception': 'ValueError', | 'exception': 'ValueError', | ||||
'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') | ||||
@istest | def test_api_1_stat_counters_raise_from_db(self, mock_service): | ||||
def api_1_stat_counters_raise_swh_storage_error_db(self, mock_service): | |||||
# given | # given | ||||
mock_service.stat_counters.side_effect = StorageDBError( | mock_service.stat_counters.side_effect = StorageDBError( | ||||
'SWH Storage exploded! Will be back online shortly!') | 'SWH Storage exploded! Will be back online shortly!') | ||||
# when | # when | ||||
rv = self.client.get('/api/1/stat/counters/') | rv = self.client.get('/api/1/stat/counters/') | ||||
# then | # then | ||||
self.assertEquals(rv.status_code, 503) | self.assertEquals(rv.status_code, 503) | ||||
self.assertEquals(rv['Content-Type'], 'application/json') | self.assertEquals(rv['Content-Type'], 'application/json') | ||||
self.assertEquals(rv.data, { | self.assertEquals(rv.data, { | ||||
'exception': 'StorageDBError', | 'exception': 'StorageDBError', | ||||
'reason': | 'reason': | ||||
'An unexpected error occurred in the backend: ' | 'An unexpected error occurred in the backend: ' | ||||
'SWH Storage exploded! Will be back online shortly!'}) | 'SWH Storage exploded! Will be back online shortly!'}) | ||||
@patch('swh.web.api.views.stat.service') | @patch('swh.web.api.views.stat.service') | ||||
@istest | def test_api_1_stat_counters_raise_from_api(self, mock_service): | ||||
def api_1_stat_counters_raise_swh_storage_error_api(self, mock_service): | |||||
# given | # given | ||||
mock_service.stat_counters.side_effect = StorageAPIError( | mock_service.stat_counters.side_effect = StorageAPIError( | ||||
'SWH Storage API dropped dead! Will resurrect from its ashes asap!' | 'SWH Storage API dropped dead! Will resurrect from its ashes asap!' | ||||
) | ) | ||||
# when | # when | ||||
rv = self.client.get('/api/1/stat/counters/') | rv = self.client.get('/api/1/stat/counters/') | ||||
# then | # then | ||||
self.assertEquals(rv.status_code, 503) | self.assertEquals(rv.status_code, 503) | ||||
self.assertEquals(rv['Content-Type'], 'application/json') | self.assertEquals(rv['Content-Type'], 'application/json') | ||||
self.assertEquals(rv.data, { | self.assertEquals(rv.data, { | ||||
'exception': 'StorageAPIError', | 'exception': 'StorageAPIError', | ||||
'reason': | 'reason': | ||||
'An unexpected error occurred in the api backend: ' | 'An unexpected error occurred in the api backend: ' | ||||
'SWH Storage API dropped dead! Will resurrect from its ashes asap!' | 'SWH Storage API dropped dead! Will resurrect from its ashes asap!' | ||||
}) | }) | ||||
@patch('swh.web.api.views.stat.service') | @patch('swh.web.api.views.stat.service') | ||||
@istest | def test_api_1_stat_counters(self, mock_service): | ||||
def api_1_stat_counters(self, mock_service): | |||||
# given | # given | ||||
stub_stats = { | stub_stats = { | ||||
"content": 1770830, | "content": 1770830, | ||||
"directory": 211683, | "directory": 211683, | ||||
"directory_entry_dir": 209167, | "directory_entry_dir": 209167, | ||||
"directory_entry_file": 1807094, | "directory_entry_file": 1807094, | ||||
"directory_entry_rev": 0, | "directory_entry_rev": 0, | ||||
"entity": 0, | "entity": 0, | ||||
Show All 18 Lines |