Skip to content

Commit

Permalink
Merge pull request #9 from FAIRmat-NFDI/8-add-cli-for-create-archive-…
Browse files Browse the repository at this point in the history
…functionality

8 add cli for create archive functionality
  • Loading branch information
ka-sarthak authored Aug 15, 2024
2 parents d8fca39 + fa0819a commit 64151db
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ dev = [
"structlog",
]

[project.scripts]
nomad-polymerization = "nomad_polymerization_reactions.cli:cli"

[tool.ruff]
# Exclude a variety of commonly ignored directories.
exclude = [
Expand Down
31 changes: 31 additions & 0 deletions src/nomad_polymerization_reactions/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import click

from nomad_polymerization_reactions.utils import generate_archive_from_json


@click.group(
help="""
This is the entry point to nomad-polymerization-reactions's command line interface
CLI. This CLI provides a set of commands to interact with the utility functions
of the package.
""",
)
def cli():
pass


@cli.command(
help='Create an archive from a JSON file containing polymerization reaction data.',
name='create-archive',
)
@click.argument(
'JSON_FILE_PATH',
required=True,
type=click.Path(exists=True),
)
def _create_archive(json_file_path):
try:
generate_archive_from_json(json_file_path)
click.echo('Archive created successfully.')
except Exception as e:
click.echo(f'Archive creation failed. Error: {e}')
32 changes: 32 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os

from click.testing import CliRunner
from nomad_polymerization_reactions.cli import cli


def invoke_cli(*args, **kwargs):
return CliRunner().invoke(*args, **kwargs)


def test_cli_help():
result = invoke_cli(cli, ['--help'])
assert result.exit_code == 0
assert 'Usage' in result.output
assert 'This is the entry point to nomad-polymerization-reactions' in result.output


def test_create_archive_help():
result = invoke_cli(cli, ['create-archive', '--help'])
assert result.exit_code == 0
assert 'Usage' in result.output
assert 'Create an archive from a JSON file' in result.output


def test_create_archive_success():
result = invoke_cli(
cli,
['create-archive', 'tests/data/processed_reactions/paper_0_reaction_1.json'],
)
assert result.exit_code == 0
assert 'Archive created successfully.' in result.output
os.remove('paper_0_reaction_1.archive.yaml')

0 comments on commit 64151db

Please sign in to comment.