Skip to content

Commit

Permalink
added status cli option and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rlizzo committed Oct 23, 2019
1 parent 55db942 commit 2a421de
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 23 deletions.
14 changes: 14 additions & 0 deletions src/hangar/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,20 @@ def log(repo: Repository, startpoint):
click.echo(repo.log(commit=base_commit))


@main.command()
@pass_repo
def status(repo: Repository):
"""Display changes made in the staging area compared to it's base commit
"""
from hangar.records.summarize import status
co = repo.checkout(write=True)
try:
diff = co.diff.staged()
click.echo(status(co.branch_name, diff.diff).getvalue(), nl=False)
finally:
co.close()


# ------------------------------- Branching -----------------------------------


Expand Down
7 changes: 6 additions & 1 deletion src/hangar/records/summarize.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,13 @@ class which contains all of the lmdb environments pre-initialized for use.
return buf


def status(diff: DiffOut) -> StringIO:
def status(branch_name: str, diff: DiffOut) -> StringIO:
"""Format human readable string buffer of changes in a staging area
Parameters
----------
branch_name : str
Name of the branch the diff is from.
diff : DiffOut
diff struct tuple returned from standard diff tool.
Expand Down Expand Up @@ -230,6 +232,9 @@ def _diff_info(df: Changes) -> StringIO:
return buf

buf = StringIO()
buf.write('============ \n')
buf.write(f'| Branch: {branch_name} \n')
buf.write(' \n')
for changes, changeType in zip(diff, diff.__annotations__.keys()):
buf.write('============ \n')
buf.write(f'| {changeType.upper()} \n')
Expand Down
17 changes: 17 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ def repo_with_10000_samples(request, written_repo, array5by7):
yield written_repo


@pytest.fixture()
def dummy_repo(repo):
# for diff testing
dummyData = np.arange(50)
co1 = repo.checkout(write=True, branch='master')
co1.arraysets.init_arrayset(
name='dummy', prototype=dummyData, named_samples=True)
for idx in range(10):
dummyData[:] = idx
co1.arraysets['dummy'][idx] = dummyData
co1.metadata['hello'] = 'world'
co1.metadata['somemetadatakey'] = 'somemetadatavalue'
co1.commit('first commit adding dummy data and hello meta')
co1.close()
return repo


@pytest.fixture(params=backend_params)
def variable_shape_written_repo(request, repo):
co = repo.checkout(write=True)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
' push Upload local BRANCH commit history / data to REMOTE server.\n'\
' remote Operations for working with remote server references\n'\
' server Start a hangar server, initializing one if does not exist.\n'\
' status Display changes made in the staging area compared to it\'s base...\n'\
' summary Display content summary at STARTPOINT (short-digest or branch).\n'\
' view Use a plugin to view the data of some SAMPLE in ARRAYSET at...\n'

Expand Down Expand Up @@ -294,6 +295,26 @@ def test_log(written_two_cmt_server_repo, capsys):
assert res.stdout == f"{capsys.readouterr().out}\n"


def test_status(dummy_repo):
from hangar.records.summarize import status

dummyData = np.arange(50)
co2 = dummy_repo.checkout(write=True)
for idx in range(10, 20):
dummyData[:] = idx
co2.arraysets['dummy'][str(idx)] = dummyData
co2.arraysets['dummy'][idx] = dummyData
co2.metadata['foo'] = 'bar'
df = co2.diff.staged()
co2.close()
expected = status('master', df.diff).getvalue()

runner = CliRunner()
res = runner.invoke(cli.status, obj=dummy_repo)
assert res.exit_code == 0
assert res.stdout == expected


def test_branch_create_and_list(written_two_cmt_server_repo):
server, base_repo = written_two_cmt_server_repo

Expand Down
46 changes: 24 additions & 22 deletions tests/test_diff_staged_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,12 @@
import numpy as np


@pytest.fixture()
def dummy_repo(repo):
dummyData = np.arange(50)
co1 = repo.checkout(write=True, branch='master')
co1.arraysets.init_arrayset(
name='dummy', prototype=dummyData, named_samples=True)
for idx in range(10):
dummyData[:] = idx
co1.arraysets['dummy'][idx] = dummyData
co1.metadata['hello'] = 'world'
co1.metadata['somemetadatakey'] = 'somemetadatavalue'
co1.commit('first commit adding dummy data and hello meta')
co1.close()
return repo


def test_add_metadata_and_samples_to_existing_aset(dummy_repo):
from hangar.records.summarize import status
expected = '============ \n'\
'| Branch: master \n'\
' \n'\
'============ \n'\
'| ADDED \n'\
'|---------- \n'\
'| Schema: 0 \n'\
Expand Down Expand Up @@ -57,12 +44,15 @@ def test_add_metadata_and_samples_to_existing_aset(dummy_repo):
co2.metadata['foo'] = 'bar'
df = co2.diff.staged()
co2.close()
assert status(df.diff).getvalue() == expected
assert status('master', df.diff).getvalue() == expected


def test_mutate_metadata_and_sample_values(dummy_repo):
from hangar.records.summarize import status
expected = '============ \n'\
'| Branch: master \n'\
' \n'\
'============ \n'\
'| ADDED \n'\
'|---------- \n'\
'| Schema: 0 \n'\
Expand Down Expand Up @@ -98,12 +88,15 @@ def test_mutate_metadata_and_sample_values(dummy_repo):
co2.metadata['hello'] = 'bar'
df = co2.diff.staged()
co2.close()
assert status(df.diff).getvalue() == expected
assert status('master', df.diff).getvalue() == expected


def test_delete_metadata_and_samples(dummy_repo):
from hangar.records.summarize import status
expected = '============ \n'\
'| Branch: master \n'\
' \n'\
'============ \n'\
'| ADDED \n'\
'|---------- \n'\
'| Schema: 0 \n'\
Expand Down Expand Up @@ -137,12 +130,15 @@ def test_delete_metadata_and_samples(dummy_repo):
del co2.metadata['hello']
df = co2.diff.staged()
co2.close()
assert status(df.diff).getvalue() == expected
assert status('master', df.diff).getvalue() == expected


def test_add_new_aset_schema_and_samples(dummy_repo):
from hangar.records.summarize import status
expected = '============ \n'\
'| Branch: master \n'\
' \n'\
'============ \n'\
'| ADDED \n'\
'|---------- \n'\
'| Schema: 1 \n'\
Expand Down Expand Up @@ -184,12 +180,15 @@ def test_add_new_aset_schema_and_samples(dummy_repo):
co2.arraysets['new_aset'][idx] = dummyData
df = co2.diff.staged()
co2.close()
assert status(df.diff).getvalue() == expected
assert status('master', df.diff).getvalue() == expected


def test_add_new_aset_schema_and_sample_and_delete_old_aset(dummy_repo):
from hangar.records.summarize import status
expected = '============ \n'\
'| Branch: master \n'\
' \n'\
'============ \n'\
'| ADDED \n'\
'|---------- \n'\
'| Schema: 1 \n'\
Expand Down Expand Up @@ -240,12 +239,15 @@ def test_add_new_aset_schema_and_sample_and_delete_old_aset(dummy_repo):
del co2.arraysets['dummy']
df = co2.diff.staged()
co2.close()
assert status(df.diff).getvalue() == expected
assert status('master', df.diff).getvalue() == expected


def test_add_new_schema_and_samples_and_change_old_backend(dummy_repo):
from hangar.records.summarize import status
expected = '============ \n'\
'| Branch: master \n'\
' \n'\
'============ \n'\
'| ADDED \n'\
'|---------- \n'\
'| Schema: 1 \n'\
Expand Down Expand Up @@ -297,4 +299,4 @@ def test_add_new_schema_and_samples_and_change_old_backend(dummy_repo):
co2.arraysets['dummy'][idx] = np.arange(50) + idx
df = co2.diff.staged()
co2.close()
assert status(df.diff).getvalue() == expected
assert status('master', df.diff).getvalue() == expected

0 comments on commit 2a421de

Please sign in to comment.