diff --git a/swh/storage/pytest_plugin.py b/swh/storage/pytest_plugin.py --- a/swh/storage/pytest_plugin.py +++ b/swh/storage/pytest_plugin.py @@ -6,7 +6,7 @@ import glob from os import path, environ -from typing import Dict, Union +from typing import Dict, Iterable, Union import pytest @@ -16,6 +16,17 @@ from pytest_postgresql.janitor import DatabaseJanitor, psycopg2, Version from swh.core.utils import numfile_sortkey as sortkey +from swh.model.model import ( + BaseModel, + Content, + Directory, + Origin, + Person, + Release, + Revision, + SkippedContent, + Snapshot, +) from swh.storage import get_storage from swh.storage.tests.storage_data import data @@ -206,3 +217,33 @@ "authority": [data.metadata_authority], "origin_metadata": [data.origin_metadata, data.origin_metadata2], } + + +# FIXME: Add the metadata keys when we can (right now, we cannot as the data model +# changed but not the endpoints yet) +OBJECT_FACTORY = { + "content": Content.from_dict, + "content_metadata": Content.from_dict, + "skipped_content": SkippedContent.from_dict, + "person": Person.from_dict, + "directory": Directory.from_dict, + "revision": Revision.from_dict, + "release": Release.from_dict, + "snapshot": Snapshot.from_dict, + "origin": Origin.from_dict, +} + + +@pytest.fixture +def sample_data_model(sample_data) -> Dict[str, Iterable[BaseModel]]: + """Pre-defined sample storage object model to manipulate + + Returns: + Dict of data (keys: content, directory, revision, release, person, origin, ...) + values list of object data model with the corresponding types + + """ + return { + object_type: [convert_fn(obj) for obj in sample_data[object_type]] + for object_type, convert_fn in OBJECT_FACTORY.items() + } diff --git a/swh/storage/tests/conftest.py b/swh/storage/tests/conftest.py --- a/swh/storage/tests/conftest.py +++ b/swh/storage/tests/conftest.py @@ -14,29 +14,6 @@ pytest_cov = None from swh.model.tests.generate_testdata import gen_contents, gen_origins -from swh.model.model import ( - Content, - Directory, - Origin, - OriginVisit, - Release, - Revision, - SkippedContent, - Snapshot, -) - - -OBJECT_FACTORY = { - "content": Content.from_dict, - "directory": Directory.from_dict, - "origin": Origin.from_dict, - "origin_visit": OriginVisit.from_dict, - "release": Release.from_dict, - "revision": Revision.from_dict, - "skipped_content": SkippedContent.from_dict, - "snapshot": Snapshot.from_dict, -} - # define tests profile. Full documentation is at: # https://hypothesis.readthedocs.io/en/latest/settings.html#settings-profiles diff --git a/swh/storage/tests/test_pytest_plugin.py b/swh/storage/tests/test_pytest_plugin.py new file mode 100644 --- /dev/null +++ b/swh/storage/tests/test_pytest_plugin.py @@ -0,0 +1,60 @@ +# Copyright (C) 2020 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + + +from swh.storage.pytest_plugin import OBJECT_FACTORY + + +from swh.model.model import BaseModel + + +def test_sample_data(sample_data, sample_data_model): + assert set(sample_data.keys()) == set( + [ + "content", + "content_metadata", + "skipped_content", + "person", + "directory", + "revision", + "release", + "snapshot", + "origin", + "fetcher", + "authority", + "origin_metadata", + ] + ) + for object_type, objs in sample_data.items(): + for obj in objs: + assert isinstance(obj, dict) + + if sample_data_model.get(object_type): + # metadata keys are missing because conversion is not possible yet + assert len(objs) == len(sample_data_model[object_type]) + + +def test_sample_data_model(sample_data, sample_data_model): + assert set(sample_data_model.keys()) == set( + [ + "content", + "content_metadata", + "skipped_content", + "person", + "directory", + "revision", + "release", + "snapshot", + "origin", + ] + ) + + for object_type, objs in sample_data_model.items(): + assert object_type in OBJECT_FACTORY + + for obj in objs: + assert isinstance(obj, BaseModel) + + assert len(objs) == len(sample_data[object_type])