Unverified Commit 3b9c7b5a authored by David Runge's avatar David Runge
Browse files

Add fixture helper for json files

tests/fixtures.py:
Add `create_empty_json_files()` which creates a set of empty .json files
in a temporary directory and can be used by fixtures (which remove the
temporary directory afterwards).
Add `create_json_files()` which creates a set of valid JSON files in a
temporary directory using the `models.OutputPackageBase` model.
parent 9cff77d7
......@@ -5,6 +5,10 @@ import tempfile
from os.path import dirname, join, realpath
from pathlib import Path
import orjson
from repo_management import models
RESOURCES = join(dirname(realpath(__file__)), "resources", "repo_db")
......@@ -24,3 +28,44 @@ def create_db_file(compression: str = "gz", remove_db: bool = False) -> Path:
os.remove(db_file)
return Path(db_file)
def create_empty_json_files() -> Path:
temp_dir = tempfile.mkdtemp()
for i in range(5):
tempfile.NamedTemporaryFile(suffix=".json", dir=temp_dir, delete=False)
return Path(temp_dir)
def create_json_files() -> Path:
temp_dir = tempfile.mkdtemp()
for name in ["foo", "bar", "baz"]:
model = models.OutputPackageBase(
base=name,
packager="someone",
version="1.0.0-1",
packages=[
models.OutputPackage(
arch="foo",
builddate=1,
csize=0,
desc="description",
filename="foo.pkg.tar.zst",
files=["foo", "bar", "baz"],
isize=1,
license=["foo"],
md5sum="foo",
name=name,
pgpsig="foo",
sha256sum="foo",
url="foo",
)
],
)
output_file = tempfile.NamedTemporaryFile(mode="wb", suffix=".json", dir=temp_dir, delete=False)
output_file.write(
orjson.dumps(model.dict(), option=orjson.OPT_INDENT_2 | orjson.OPT_APPEND_NEWLINE | orjson.OPT_SORT_KEYS)
)
return Path(temp_dir)
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