Skip to content

Commit

Permalink
Unit test fixture for cli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
heikomuller committed Jun 25, 2020
1 parent 3f7efea commit e3dbad7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 57 deletions.
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Add managers for maintaining sets of archives


### 0.1.2 - ???
### 0.1.2 - 06-25-2020

* Proper handling of date/time objects by the default archive reader and writer
* Optional arguments for Json encoder and decoder for persistent archives
Expand Down
110 changes: 54 additions & 56 deletions tests/cli/test_cli_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,110 +8,108 @@
"""Unit tests for the command-line archive commands."""

import os
import pytest

from click.testing import CliRunner

from histore.cli.base import cli

import histore.config as config

def test_create_archive(tmpdir):
"""Test creating a new archive."""

# -- Fixtures and Helper Functions --------------------------------------------

@pytest.fixture
def test_runner(tmpdir):
"""Initialize archive manager in temporary directory and set basedir
environment variable.
"""
basedir = os.path.abspath(str(tmpdir))
runner = CliRunner()
runner.invoke(cli, ['init', '--basedir', basedir])
result = runner.invoke(cli, ['create', '--basedir', basedir, 'myarchive'])
os.environ[config.ENV_HISTORE_BASEDIR] = basedir
runner = CliRunner()
runner.invoke(cli, ['init'])
return runner


def cleanup():
"""Clear base dire environment variable."""
del os.environ[config.ENV_HISTORE_BASEDIR]


# -- Unit tests ---------------------------------------------------------------

def test_create_archive(test_runner):
"""Test creating a new archive."""
result = test_runner.invoke(cli, ['create', 'myarchive'])
assert result.exit_code == 0
assert result.output == 'Archive created!\n'
result = runner.invoke(cli, ['create', '--basedir', basedir, 'myarchive'])
result = test_runner.invoke(cli, ['create', 'myarchive'])
assert result.exit_code == -1
assert result.output == "archive 'myarchive' already exists\n"
# Cleanup environment
cleanup()


def test_delete_archive(tmpdir):
def test_delete_archive(test_runner):
"""Test deleting an existing archive."""
basedir = os.path.abspath(str(tmpdir))
runner = CliRunner()
runner.invoke(cli, ['init', '--basedir', basedir])
runner.invoke(cli, ['create', '--basedir', basedir, 'a_archive'])
runner.invoke(cli, ['create', '--basedir', basedir, 'myarchive'])
test_runner.invoke(cli, ['create', 'a_archive'])
test_runner.invoke(cli, ['create', 'myarchive'])
# Abort deletion.
result = runner.invoke(
cli,
['delete', '--basedir', basedir, 'myarchive'],
input='n'
)
result = test_runner.invoke(cli, ['delete', 'myarchive'], input='n')
assert result.exit_code == 1
assert 'Aborted!\n' in result.output
result = runner.invoke(
cli,
['delete', '--basedir', basedir, 'myarchive'],
input='Y'
)
result = test_runner.invoke(cli, ['delete', 'myarchive'], input='Y')
assert result.exit_code == 0
assert "Archive 'myarchive' deleted!\n" in result.output
# Delete with force option
result = runner.invoke(
cli,
['delete', '--basedir', basedir, '--force', 'a_archive']
)
result = test_runner.invoke(cli, ['delete', '--force', 'a_archive'])
assert result.exit_code == 0
assert "Archive 'a_archive' deleted!\n" in result.output
result = runner.invoke(
cli,
['delete', '--basedir', basedir, '--force', 'a_archive']
)
result = test_runner.invoke(cli, ['delete', '--force', 'a_archive'])
assert result.exit_code == -1
assert result.output == "Unknown archive 'a_archive'.\n"
# Cleanup environment
cleanup()


def test_list_archives(tmpdir):
def test_list_archives(test_runner):
"""Test listing an existing archive."""
basedir = os.path.abspath(str(tmpdir))
runner = CliRunner()
runner.invoke(cli, ['init', '--basedir', basedir])
runner.invoke(cli, ['create', '--basedir', basedir, 'archive B'])
runner.invoke(cli, ['create', '--basedir', basedir, 'archive A'])
test_runner.invoke(cli, ['create', 'archive B'])
test_runner.invoke(cli, ['create', 'archive A'])
# Order by name
result = runner.invoke(cli, ['list', '--basedir', basedir])
result = test_runner.invoke(cli, ['list'])
assert result.exit_code == 0
lines = result.output.split('\n')
assert lines[3].startswith('archive A')
assert lines[4].startswith('archive B')
# Order by created_at timestamp
result = runner.invoke(cli, ['list', '--basedir', basedir, '-d'])
result = test_runner.invoke(cli, ['list', '-d'])
assert result.exit_code == 0
lines = result.output.split('\n')
assert lines[3].startswith('archive B')
assert lines[4].startswith('archive A')
# Cleanup environment
cleanup()


def test_rename_archive(tmpdir):
def test_rename_archive(test_runner):
"""Test renaming an existing archive."""
basedir = os.path.abspath(str(tmpdir))
runner = CliRunner()
runner.invoke(cli, ['init', '--basedir', basedir])
runner.invoke(cli, ['create', '--basedir', basedir, 'archive B'])
runner.invoke(cli, ['create', '--basedir', basedir, 'archive A'])
test_runner.invoke(cli, ['create', 'archive B'])
test_runner.invoke(cli, ['create', 'archive A'])
# Rename archive B to C
result = runner.invoke(
cli,
['rename', '--basedir', basedir, 'archive B', 'archive C']
)
result = test_runner.invoke(cli, ['rename', 'archive B', 'archive C'])
assert result.exit_code == 0
result = runner.invoke(cli, ['list', '--basedir', basedir])
result = test_runner.invoke(cli, ['list'])
lines = result.output.split('\n')
assert lines[3].startswith('archive A')
assert lines[4].startswith('archive C')
# Error cases
result = runner.invoke(
cli,
['rename', '--basedir', basedir, 'archive B', 'archive C']
)
result = test_runner.invoke(cli, ['rename', 'archive B', 'archive C'])
assert result.exit_code == -1
assert result.output == "Unknown archive 'archive B'\n"
result = runner.invoke(
cli,
['rename', '--basedir', basedir, 'archive C', 'archive A']
)
result = test_runner.invoke(cli, ['rename', 'archive C', 'archive A'])
assert result.exit_code == -1
assert result.output == "archive 'archive A' already exists\n"
# Cleanup environment
cleanup()

0 comments on commit e3dbad7

Please sign in to comment.