Add conversion method for PackageDesc to OutputPackage

repo_management/convert.py:
Add private method `_transform_package_desc_to_output_package()` which
can be used to construct a `models.OutputPackage` from a
`models.PackageDesc` (and optionally a `models.PackageFiles`).
The method drops attributes from a dict representation of the
`models.PackageDesc` instance, that are not relevant for the
`models.OutputPackage`.

tests/test_convert.py:
Add parametrized test for `_transform_package_desc_to_output_package()`.
parent f549d51a
import io
from typing import Dict, List, Union
from typing import Dict, List, Optional, Union
from repo_management import defaults, models
......@@ -94,3 +94,34 @@ def _desc_data_to_model(data: io.StringIO) -> models.PackageDesc:
data.close()
merged_dict: Dict[str, Union[int, str, List[str]]] = {**int_types, **string_types, **string_list_types}
return models.PackageDesc(**merged_dict)
def _transform_package_desc_to_output_package(
desc: models.PackageDesc, files: Optional[models.Files]
) -> models.OutputPackage:
"""Transform a PackageDesc model and an accompanying Files model to an OutputPackage model
Parameters
----------
desc: models.PackageDesc
A pydantic model, that has all required attributes (apart from the list of files) to create an OutputPackage
model
files: models.Files:
A pydantic model, that represents the list of files, that belong to the package described by desc
Returns
-------
models.OutputPackage
A pydantic model, that describes a package and its list of files
"""
desc_dict = desc.dict()
# remove attributes, that are represented on the pkgbase level
for name in ["base", "makedepends", "packager", "version"]:
if desc_dict.get(name):
del desc_dict[name]
if files:
return models.OutputPackage(**desc_dict, **files.dict())
else:
return models.OutputPackage(**desc_dict)
......@@ -6,7 +6,7 @@ from typing import ContextManager
from pydantic.error_wrappers import ValidationError
from pytest import mark, raises
from repo_management import convert
from repo_management import convert, models
RESOURCES = join(dirname(realpath(__file__)), "resources")
......@@ -106,3 +106,60 @@ def test__desc_data_to_dict(
) -> None:
with expectation:
assert convert._desc_data_to_model(data=io.StringIO(file_data))
@mark.parametrize(
"desc, files",
[
(
models.PackageDesc(
arch="foo",
base="foo",
builddate=1,
csize=1,
desc="foo",
filename="foo",
isize=1,
licenses=["foo"],
md5sum="foo",
name="foo",
packager="foo",
pgpsig="foo",
sha256sum="foo",
url="foo",
version="foo",
),
models.Files(files=["foo", "bar"]),
),
(
models.PackageDesc(
arch="foo",
base="foo",
builddate=1,
csize=1,
desc="foo",
filename="foo",
isize=1,
licenses=["foo"],
md5sum="foo",
name="foo",
packager="foo",
pgpsig="foo",
sha256sum="foo",
url="foo",
version="foo",
),
None,
),
],
)
def test__transform_package_desc_to_output_package(
desc: models.PackageDesc,
files: models.Files,
) -> None:
output = convert._transform_package_desc_to_output_package(desc=desc, files=files)
assert isinstance(output, models.OutputPackage)
if files:
assert output.files
else:
assert not output.files
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment