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 typing import Union | from typing import Union | ||||
from unittest.mock import patch | |||||
from pytest_postgresql import factories | from pytest_postgresql import factories | ||||
from pytest_postgresql.janitor import DatabaseJanitor, psycopg2, Version | from pytest_postgresql.janitor import DatabaseJanitor, psycopg2, Version | ||||
from os import path, environ | from os import path, environ | ||||
from hypothesis import settings | from hypothesis import settings | ||||
from typing import Dict | from typing import Dict | ||||
import swh.storage | import swh.storage | ||||
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 | from swh.model.tests.generate_testdata import gen_contents, gen_origins | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
Content, | Content, | ||||
Directory, | Directory, | ||||
Origin, | Origin, | ||||
OriginVisit, | OriginVisit, | ||||
Release, | Release, | ||||
Revision, | Revision, | ||||
SkippedContent, | SkippedContent, | ||||
Snapshot, | Snapshot, | ||||
) | ) | ||||
from swh.journal.writer.inmemory import InMemoryJournalWriter | |||||
OBJECT_FACTORY = { | OBJECT_FACTORY = { | ||||
"content": Content.from_dict, | "content": Content.from_dict, | ||||
"directory": Directory.from_dict, | "directory": Directory.from_dict, | ||||
"origin": Origin.from_dict, | "origin": Origin.from_dict, | ||||
"origin_visit": OriginVisit.from_dict, | "origin_visit": OriginVisit.from_dict, | ||||
"release": Release.from_dict, | "release": Release.from_dict, | ||||
Show All 25 Lines | yield { | ||||
user="postgres", | user="postgres", | ||||
dbname="tests", | dbname="tests", | ||||
), | ), | ||||
"objstorage": {"cls": "memory", "args": {}}, | "objstorage": {"cls": "memory", "args": {}}, | ||||
"journal_writer": {"cls": "memory",}, | "journal_writer": {"cls": "memory",}, | ||||
} | } | ||||
class BWCompatInMemoryJournalWriter(InMemoryJournalWriter): | |||||
"""InMemoryJournalWriter that enforces conversion of objects to model entities | |||||
This is required until swh.journal 0.0.30 is available | |||||
""" | |||||
def write_addition(self, object_type, object_): | |||||
if isinstance(object_, dict): | |||||
object_ = OBJECT_FACTORY[object_type](object_) | |||||
self.objects.append((object_type, object_)) | |||||
write_update = write_addition | |||||
@pytest.fixture | @pytest.fixture | ||||
def swh_storage(swh_storage_backend_config): | def swh_storage(swh_storage_backend_config): | ||||
storage_config = {"cls": "validate", "storage": swh_storage_backend_config} | return swh.storage.get_storage(cls="validate", storage=swh_storage_backend_config) | ||||
with patch( | |||||
"swh.journal.writer.inmemory.InMemoryJournalWriter", | |||||
return_value=BWCompatInMemoryJournalWriter(), | |||||
): | |||||
storage = swh.storage.get_storage(**storage_config) | |||||
return storage | |||||
@pytest.fixture | @pytest.fixture | ||||
def swh_contents(swh_storage): | def swh_contents(swh_storage): | ||||
contents = gen_contents(n=20) | contents = gen_contents(n=20) | ||||
swh_storage.content_add([c for c in contents if c["status"] != "absent"]) | swh_storage.content_add([c for c in contents if c["status"] != "absent"]) | ||||
swh_storage.skipped_content_add([c for c in contents if c["status"] == "absent"]) | swh_storage.skipped_content_add([c for c in contents if c["status"] == "absent"]) | ||||
return contents | return contents | ||||
▲ Show 20 Lines • Show All 170 Lines • Show Last 20 Lines |