Skip to content

Commit

Permalink
tests: cover cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jirikuncar committed Feb 16, 2018
1 parent 837a0a9 commit 982fb1b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
5 changes: 3 additions & 2 deletions git_json_tree.py
Expand Up @@ -126,6 +126,7 @@ def decode(repo, tree_id):
return []
elif b'.object' in tree:
return {}

raise TypeError('Unknown tree type.')

if all((isinstance(key[0], key_types) for key in items)):
Expand All @@ -151,7 +152,7 @@ def cli():


@cli.command(name='encode')
@click.option('--source', type=click.File('rb'), default='-')
@click.option('--source', type=click.File('r'), default='-')
@click.option('--git', type=click.Path(exists=True), default='.')
def cli_encode(source, git):
"""Encode a JSON object in a Git tree."""
Expand Down Expand Up @@ -187,7 +188,7 @@ def smudge(source, git):


@cli.command()
@click.option('--source', type=click.File('rb'), default='-')
@click.option('--source', type=click.File('r'), default='-')
@click.option('--git', type=click.Path(exists=True), default='.')
def clean(source, git):
"""Store a JSON file in Git repository."""
Expand Down
36 changes: 34 additions & 2 deletions test_git_json_tree.py
@@ -1,13 +1,15 @@
"""Test Git-JSON-Tree library."""

import json
from string import printable

import hypothesis.strategies as st
import pytest
from click.testing import CliRunner
from dulwich.repo import Repo
from hypothesis import assume, given, settings

from git_json_tree import decode, encode
from git_json_tree import cli, decode, encode


@pytest.fixture()
Expand All @@ -16,6 +18,12 @@ def repo(tmpdir):
yield Repo.init_bare(str(tmpdir))


@pytest.fixture()
def runner(repo):
"""Define a click runner."""
return CliRunner()


def json_data():
"""Generate JSON data."""
return st.recursive(
Expand All @@ -33,7 +41,7 @@ def json_data():
),
children,
),
max_leaves=500,
max_leaves=100,
)


Expand All @@ -43,3 +51,27 @@ def test_encode_decode(data, repo):
"""Test (d)encoding."""
assume(isinstance(data, (dict, list)))
assert decode(repo, encode(repo, data)) == data


@given(data=json_data())
@settings(max_examples=100, deadline=10000)
def test_cli_encoder(data, runner):
"""Test cli encoder."""
assume(isinstance(data, (dict, list)) and data)
encoded = runner.invoke(cli, ['encode'], input=json.dumps(data))
assert encoded.exit_code == 0
decoded = runner.invoke(cli, ['decode', encoded.output.strip()])
assert decoded.exit_code == 0
assert json.loads(decoded.output_bytes.decode('utf-8')) == data


@given(data=json_data())
@settings(max_examples=100, deadline=10000)
def test_smudge_clean(data, runner):
"""Test Git integration."""
assume(isinstance(data, (dict, list)) and data)
cleaned = runner.invoke(cli, ['clean'], input=json.dumps(data))
assert cleaned.exit_code == 0
smudged = runner.invoke(cli, ['smudge'], input=cleaned.output)
assert smudged.exit_code == 0
assert json.loads(smudged.output_bytes.decode('utf-8')) == data

0 comments on commit 982fb1b

Please sign in to comment.