Verified Commit d542d1f4 authored by David Runge's avatar David Runge
Browse files

Add entrypoint for json2db

repo_management/cli.py:
Add `json2db()` as entrypoint for the `json2db` script.

tests/test_cli.py:
Add a test for `json2db()`.
parent 0f02fe86
from repo_management import argparse, operations from repo_management import argparse, defaults, operations
def db2json() -> None: def db2json() -> None:
...@@ -13,3 +13,18 @@ def db2json() -> None: ...@@ -13,3 +13,18 @@ def db2json() -> None:
input_path=args.db_file, input_path=args.db_file,
output_path=args.output_dir, output_path=args.output_dir,
) )
def json2db() -> None:
"""The entry point for the json2db script
The method calls operations.create_db_from_json_files() which creates a repository database from a set of JSON files
in a directory.
"""
args = argparse.ArgParseFactory.json2db().parse_args()
operations.create_db_from_json_files(
input_path=args.input_dir,
output_path=args.db_file,
db_type=defaults.RepoDbType.FILES if args.files else defaults.RepoDbType.DEFAULT,
)
from argparse import Namespace from argparse import Namespace
from mock import Mock, patch from mock import Mock, patch
from pytest import mark
from repo_management import cli from repo_management import cli, defaults
@patch("repo_management.operations.dump_db_to_json_files") @patch("repo_management.operations.dump_db_to_json_files")
...@@ -12,3 +13,19 @@ def test_db2json(argparsefactory_mock: Mock, dump_db_to_json_files_mock: Mock) - ...@@ -12,3 +13,19 @@ def test_db2json(argparsefactory_mock: Mock, dump_db_to_json_files_mock: Mock) -
argparsefactory_mock.db2json.return_value = Mock(parse_args=Mock(return_value=namespace)) argparsefactory_mock.db2json.return_value = Mock(parse_args=Mock(return_value=namespace))
cli.db2json() cli.db2json()
dump_db_to_json_files_mock.assert_called_once_with(input_path=namespace.db_file, output_path=namespace.output_dir) dump_db_to_json_files_mock.assert_called_once_with(input_path=namespace.db_file, output_path=namespace.output_dir)
@mark.parametrize("files, db_type", [(True, defaults.RepoDbType.FILES), (False, defaults.RepoDbType.DEFAULT)])
@patch("repo_management.operations.create_db_from_json_files")
@patch("repo_management.argparse.ArgParseFactory")
def test_json2db(
argparsefactory_mock: Mock, create_db_from_json_files: Mock, files: bool, db_type: defaults.RepoDbType
) -> None:
namespace = Namespace(db_file="db_file", input_dir="input_dir", files=files)
argparsefactory_mock.json2db.return_value = Mock(parse_args=Mock(return_value=namespace))
cli.json2db()
create_db_from_json_files.assert_called_once_with(
input_path=namespace.input_dir,
output_path=namespace.db_file,
db_type=db_type,
)
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