Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/tests/test_storage.py
# Copyright (C) 2015-2020 The Software Heritage developers | # Copyright (C) 2015-2020 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 General Public License version 3, or any later version | # License: GNU 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 copy | import copy | ||||
from contextlib import contextmanager | from contextlib import contextmanager | ||||
import datetime | import datetime | ||||
import inspect | |||||
import itertools | import itertools | ||||
import math | import math | ||||
import queue | import queue | ||||
import random | import random | ||||
import threading | import threading | ||||
from collections import defaultdict | from collections import defaultdict | ||||
from datetime import timedelta | from datetime import timedelta | ||||
from unittest.mock import Mock | from unittest.mock import Mock | ||||
import psycopg2 | import psycopg2 | ||||
import pytest | import pytest | ||||
from hypothesis import given, strategies, settings, HealthCheck | from hypothesis import given, strategies, settings, HealthCheck | ||||
from typing import ClassVar, Optional | from typing import ClassVar, Optional | ||||
from swh.model import from_disk, identifiers | from swh.model import from_disk, identifiers | ||||
from swh.model.hashutil import hash_to_bytes | from swh.model.hashutil import hash_to_bytes | ||||
from swh.model.hypothesis_strategies import objects | from swh.model.hypothesis_strategies import objects | ||||
from swh.storage import HashCollision | from swh.storage import HashCollision | ||||
from swh.storage.converters import origin_url_to_sha1 as sha1 | from swh.storage.converters import origin_url_to_sha1 as sha1 | ||||
from swh.storage.interface import StorageInterface | |||||
from .storage_data import data | from .storage_data import data | ||||
@contextmanager | @contextmanager | ||||
def db_transaction(storage): | def db_transaction(storage): | ||||
with storage.db() as db: | with storage.db() as db: | ||||
with db.transaction() as cur: | with db.transaction() as cur: | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | class TestStorage: | ||||
test_remote_storage.py. | test_remote_storage.py. | ||||
We need to have the two classes inherit from this base class | We need to have the two classes inherit from this base class | ||||
separately to avoid nosetests running the tests from the base | separately to avoid nosetests running the tests from the base | ||||
class twice. | class twice. | ||||
""" | """ | ||||
maxDiff = None # type: ClassVar[Optional[int]] | maxDiff = None # type: ClassVar[Optional[int]] | ||||
def test_types(self, swh_storage): | |||||
ardumont: here. | |||||
"""Checks all methods of StorageInterface are implemented by this | |||||
backend, and that they have the same signature.""" | |||||
# Create an instance of the protocol (which cannot be instantiated | |||||
# directly, so this creates a subclass, then instantiates it) | |||||
interface = type('_', (StorageInterface,), {})() | |||||
ardumontUnsubmitted Not Done Inline Actionsthx for the comment! ardumont: thx for the comment! | |||||
assert 'content_add' in dir(interface) | |||||
ardumontUnsubmitted Not Done Inline Actionswhy is this one aside? ardumont: why is this one aside?
isn't it some left-overs from a tryout? | |||||
vlorentzAuthorUnsubmitted Done Inline ActionsI'm using a bit of magic, so I added an assertion to check the magic doesn't remove all the declared methods from dir(). vlorentz: I'm using a bit of magic, so I added an assertion to check the magic doesn't remove all the… | |||||
missing_methods = [] | |||||
for meth_name in dir(interface): | |||||
if meth_name.startswith('_'): | |||||
continue | |||||
interface_meth = getattr(interface, meth_name) | |||||
try: | |||||
concrete_meth = getattr(swh_storage, meth_name) | |||||
except AttributeError: | |||||
missing_methods.append(meth_name) | |||||
continue | |||||
expected_signature = inspect.signature(interface_meth) | |||||
actual_signature = inspect.signature(concrete_meth) | |||||
assert expected_signature == actual_signature, meth_name | |||||
assert missing_methods == [] | |||||
def test_check_config(self, swh_storage): | def test_check_config(self, swh_storage): | ||||
assert swh_storage.check_config(check_write=True) | assert swh_storage.check_config(check_write=True) | ||||
assert swh_storage.check_config(check_write=False) | assert swh_storage.check_config(check_write=False) | ||||
def test_content_add(self, swh_storage): | def test_content_add(self, swh_storage): | ||||
cont = data.cont | cont = data.cont | ||||
insertion_start_time = datetime.datetime.now(tz=datetime.timezone.utc) | insertion_start_time = datetime.datetime.now(tz=datetime.timezone.utc) | ||||
▲ Show 20 Lines • Show All 3,645 Lines • Show Last 20 Lines |
here.