Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/tests/conftest.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 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 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 glob | import glob | ||||
import pytest | import pytest | ||||
from pytest_postgresql import factories | from pytest_postgresql import factories | ||||
from pytest_postgresql.janitor import DatabaseJanitor, psycopg2 | from pytest_postgresql.janitor import DatabaseJanitor, psycopg2 | ||||
from os import path, environ | from os import path, environ | ||||
from hypothesis import settings, strategies | from hypothesis import settings | ||||
from typing import Dict | from typing import Dict | ||||
import swh.storage | import swh.storage | ||||
from swh.model.hypothesis_strategies import origins, contents | |||||
from swh.core.utils import numfile_sortkey as sortkey | from swh.core.utils import numfile_sortkey as sortkey | ||||
from swh.model.tests.generate_testdata import gen_contents, gen_origins | |||||
SQL_DIR = path.join(path.dirname(swh.storage.__file__), 'sql') | SQL_DIR = path.join(path.dirname(swh.storage.__file__), 'sql') | ||||
environ['LC_ALL'] = 'C.UTF-8' | environ['LC_ALL'] = 'C.UTF-8' | ||||
DUMP_FILES = path.join(SQL_DIR, '*.sql') | DUMP_FILES = path.join(SQL_DIR, '*.sql') | ||||
# define tests profile. Full documentation is at: | # define tests profile. Full documentation is at: | ||||
Show All 20 Lines | storage_config = { | ||||
'cls': 'memory', | 'cls': 'memory', | ||||
}, | }, | ||||
}, | }, | ||||
} | } | ||||
storage = swh.storage.get_storage(**storage_config) | storage = swh.storage.get_storage(**storage_config) | ||||
return storage | return storage | ||||
def gen_origins(n=20): | |||||
return strategies.lists( | |||||
origins().map(lambda x: x.to_dict()), | |||||
unique_by=lambda x: x['url'], | |||||
min_size=n, max_size=n).example() | |||||
def gen_contents(n=20): | |||||
return strategies.lists( | |||||
contents().map(lambda x: x.to_dict()), | |||||
unique_by=lambda x: (x['sha1'], x['sha1_git']), | |||||
min_size=n, max_size=n).example() | |||||
@pytest.fixture | @pytest.fixture | ||||
def swh_contents(swh_storage): | def swh_contents(swh_storage): | ||||
contents = gen_contents() | contents = gen_contents(n=20) | ||||
swh_storage.content_add(contents) | swh_storage.content_add(contents) | ||||
return contents | return contents | ||||
@pytest.fixture | @pytest.fixture | ||||
def swh_origins(swh_storage): | def swh_origins(swh_storage): | ||||
origins = gen_origins() | origins = gen_origins(n=100) | ||||
swh_storage.origin_add(origins) | swh_storage.origin_add(origins) | ||||
return origins | return origins | ||||
# the postgres_fact factory fixture below is mostly a copy of the code | # the postgres_fact factory fixture below is mostly a copy of the code | ||||
# from pytest-postgresql. We need a custom version here to be able to | # from pytest-postgresql. We need a custom version here to be able to | ||||
# specify our version of the DBJanitor we use. | # specify our version of the DBJanitor we use. | ||||
def postgresql_fact(process_fixture_name, db_name=None): | def postgresql_fact(process_fixture_name, db_name=None): | ||||
▲ Show 20 Lines • Show All 178 Lines • Show Last 20 Lines |