Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/views/test_origin.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 | ||||
import random | |||||
from hypothesis import given | from hypothesis import given | ||||
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.utils import reverse | from swh.web.common.utils import reverse | ||||
from swh.web.common.origin_visits import get_origin_visits | from swh.web.common.origin_visits import get_origin_visits | ||||
from swh.web.tests.strategies import ( | from swh.web.tests.strategies import ( | ||||
origin, new_origin, visit_dates, new_snapshots | origin, new_origin, new_origins, visit_dates, new_snapshots | ||||
) | ) | ||||
from swh.web.tests.testcase import WebTestCase | from swh.web.tests.testcase import WebTestCase | ||||
class OriginApiTestCase(WebTestCase, APITestCase): | class OriginApiTestCase(WebTestCase, APITestCase): | ||||
@patch('swh.web.api.views.origin.get_origin_visits') | @patch('swh.web.api.views.origin.get_origin_visits') | ||||
def test_api_lookup_origin_visits_raise_error( | def test_api_lookup_origin_visits_raise_error( | ||||
▲ Show 20 Lines • Show All 301 Lines • ▼ Show 20 Lines | class OriginApiTestCase(WebTestCase, APITestCase): | ||||
@patch('swh.web.common.service.idx_storage') | @patch('swh.web.common.service.idx_storage') | ||||
def test_api_origin_metadata_search_invalid(self, mock_idx_storage): | def test_api_origin_metadata_search_invalid(self, mock_idx_storage): | ||||
url = reverse('api-origin-metadata-search') | url = reverse('api-origin-metadata-search') | ||||
rv = self.client.get(url) | rv = self.client.get(url) | ||||
self.assertEqual(rv.status_code, 400, rv.content) | self.assertEqual(rv.status_code, 400, rv.content) | ||||
mock_idx_storage.assert_not_called() | mock_idx_storage.assert_not_called() | ||||
@given(new_origins(20)) | |||||
def test_api_lookup_origins(self, new_origins): | |||||
nb_origins = len(new_origins) | |||||
expected_origins = self.storage.origin_add(new_origins) | |||||
vlorentz: Is this how `example()` is supposed to be used with hypothesis? It looks like it exepects that… | |||||
Done Inline ActionsThe issue here is that I restrain the maximum number of examples to 1 to speedup overall tests execution. However, I agree that this code could be put in a dedicated strategy function as anlambert: The issue here is that I restrain the maximum number of examples to 1 to speedup overall tests… | |||||
origin_from_idx = random.randint(1, nb_origins-1) - 1 | |||||
origin_from = expected_origins[origin_from_idx]['id'] | |||||
max_origin_id = expected_origins[-1]['id'] | |||||
origin_count = random.randint(1, max_origin_id - origin_from) | |||||
url = reverse('api-origins', | |||||
query_params={'origin_from': origin_from, | |||||
'origin_count': origin_count}) | |||||
rv = self.client.get(url) | |||||
self.assertEqual(rv.status_code, 200) | |||||
start = origin_from_idx | |||||
end = origin_from_idx + origin_count | |||||
expected_origins = expected_origins[start:end] | |||||
for expected_origin in expected_origins: | |||||
expected_origin['origin_visits_url'] = reverse( | |||||
'api-origin-visits', | |||||
url_args={'origin_id': expected_origin['id']}) | |||||
self.assertEqual(rv.data, expected_origins) | |||||
next_origin_id = expected_origins[-1]['id']+1 | |||||
if self.storage.origin_get({'id': next_origin_id}): | |||||
self.assertIn('Link', rv) | |||||
next_url = reverse('api-origins', | |||||
query_params={'origin_from': next_origin_id, | |||||
'origin_count': origin_count}) | |||||
self.assertIn(next_url, rv['Link']) |
Is this how example() is supposed to be used with hypothesis? It looks like it exepects that "one call of the test function = one example".
Shouldn't we ask hypothesis to build a list of origins instead?