Skip to content

Commit

Permalink
Test "unable" exception, add help
Browse files Browse the repository at this point in the history
  • Loading branch information
dmiwell committed Jul 6, 2020
1 parent 2ac6d0f commit 36a33b0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
15 changes: 11 additions & 4 deletions peewee_migrate/cli.py
Expand Up @@ -86,14 +86,21 @@ def create(name, database=None, auto=False, auto_source=False, directory=None, v

@cli.command()
@click.argument('name', required=False)
@click.option('--count', required=False, default=1, type=int)
@click.option('--count',
required=False,
default=1,
type=int,
help="Number of last migrations to be rolled back."
"Ignored in case of non-empty name")
@click.option('--database', default=None, help="Database connection")
@click.option('--directory', default='migrations', help="Directory where migrations are stored")
@click.option('--directory',
default='migrations',
help="Directory where migrations are stored")
@click.option('-v', '--verbose', count=True)
def rollback(name, count, database=None, directory=None, verbose=None):
"""
Rollback a migration with given name or number of migrations with given
name as integer number
Rollback a migration with given name or number of last migrations
with given --count option as integer number
"""
router = get_router(directory, database, verbose)
if not name:
Expand Down
10 changes: 8 additions & 2 deletions tests/test_cli.py
@@ -1,8 +1,7 @@
from click.testing import CliRunner
import pytest

from peewee_migrate.cli import cli
from peewee_migrate.cli import get_router
from peewee_migrate.cli import cli, get_router

runner = CliRunner()

Expand Down Expand Up @@ -72,6 +71,12 @@ def test_list(dir_option, db_option, migrations):
def test_rollback(dir_option, db_option, router, migrations):
router().run()

count_overflow = len(migrations) + 1
result = runner.invoke(cli, ['rollback', dir_option, db_option, '--count=%s' % count_overflow])
assert result.exception
assert 'Unable to rollback %s migrations' % count_overflow in result.exception.args[0]
assert router().done == migrations

result = runner.invoke(cli, ['rollback', dir_option, db_option])
assert not result.exception
assert router().done == migrations[:-1]
Expand All @@ -87,6 +92,7 @@ def test_rollback(dir_option, db_option, router, migrations):
result = runner.invoke(cli, ['rollback', dir_option, db_option, '005_test'])
assert result.exception
assert result.exception.args[0] == 'Only last migration can be canceled.'
assert router().done == migrations[:-4]


def test_fake(dir_option, db_option, migrations_str, router):
Expand Down

0 comments on commit 36a33b0

Please sign in to comment.