-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial CLI draft * Fix lint * Fix lint * Initial xdump command for PG * Actually dump data * Test for multiple full tables * PyPy fix * partial tables specification * Update coverage config * Add compression option * Extra options * SQLite support * Refactoring * Use callback for parameter validation * Fix for Python 2 * Decompose decorators processing * Decompose CLI * Add tests for utils * Better boolean handling * Fix imports * Docstrings * Skip old SQLite * PyPy fix * Remove a test * Refactor cli fixture * Fix arg * Add xload command * Refactoring * Add an option for DB re-creation / truncation * Update README * Fix SQLite test
- Loading branch information
1 parent
827bca3
commit aa8dc61
Showing
18 changed files
with
456 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import sqlite3 | ||
|
||
import pytest | ||
|
||
from xdump.cli import dump, load | ||
|
||
from ..conftest import DATABASE, IS_POSTGRES, IS_SQLITE | ||
|
||
|
||
@pytest.fixture | ||
def cli(request, archive_filename, isolated_cli_runner): | ||
if IS_SQLITE and sqlite3.sqlite_version_info < (3, 8, 3): | ||
pytest.skip('Unsupported SQLite version') | ||
|
||
commands = { | ||
'sqlite': { | ||
'dump': dump.sqlite, | ||
'load': load.sqlite, | ||
}, | ||
'postgres': { | ||
'dump': dump.postgres, | ||
'load': load.postgres, | ||
} | ||
}[DATABASE] | ||
|
||
class CLI(object): | ||
|
||
def call(self, command, *args): | ||
default_args = () | ||
if IS_SQLITE: | ||
dbname = request.getfixturevalue('dbname') | ||
default_args = ( | ||
'-D', dbname, | ||
) | ||
elif IS_POSTGRES: | ||
dsn_parameters = request.getfixturevalue('dsn_parameters') | ||
default_args = ( | ||
'-U', dsn_parameters['user'], | ||
'-H', dsn_parameters['host'], | ||
'-P', dsn_parameters['port'], | ||
'-D', dsn_parameters['dbname'], | ||
) | ||
return isolated_cli_runner.invoke( | ||
command, | ||
default_args + args, | ||
catch_exceptions=False | ||
) | ||
|
||
def dump(self, *args): | ||
return self.call(commands['dump'], '-o', archive_filename, *args) | ||
|
||
def load(self, *args): | ||
return self.call(commands['load'], '-i', archive_filename, *args) | ||
|
||
return CLI() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import pytest | ||
|
||
from xdump import __version__ | ||
from xdump.cli import dump, load | ||
|
||
|
||
@pytest.mark.parametrize('command, name', ( | ||
(dump.dump, 'xdump'), (load.load, 'xload') | ||
)) | ||
def test_xdump_run(isolated_cli_runner, command, name): | ||
"""Smoke test for a click group.""" | ||
result = isolated_cli_runner.invoke(command, ('--version', )) | ||
assert not result.exception | ||
assert result.output == '{0}, version {1}\n'.format(name, __version__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import zipfile | ||
|
||
import pytest | ||
|
||
|
||
@pytest.mark.usefixtures('schema', 'data') | ||
def test_single_full_table(cli, archive_filename, db_helper): | ||
result = cli.dump('-f', 'groups') | ||
assert not result.exception | ||
assert result.output == 'Dumping ...\nOutput file: {0}\nDone!\n'.format(archive_filename) | ||
archive = zipfile.ZipFile(archive_filename) | ||
db_helper.assert_groups(archive) | ||
|
||
|
||
@pytest.mark.usefixtures('schema', 'data') | ||
def test_multiple_full_tables(cli, archive_filename, db_helper): | ||
result = cli.dump('-f', 'groups', '-f' 'tickets') | ||
assert not result.exception | ||
archive = zipfile.ZipFile(archive_filename) | ||
db_helper.assert_groups(archive) | ||
db_helper.assert_content( | ||
archive, | ||
'tickets', | ||
{ | ||
b'id,author_id,subject,message', | ||
b'1,1,Sub 1,Message 1', | ||
b'2,2,Sub 2,Message 2', | ||
b'3,2,Sub 3,Message 3', | ||
b'4,2,Sub 4,Message 4', | ||
b'5,3,Sub 5,Message 5', | ||
} | ||
) | ||
|
||
|
||
@pytest.mark.usefixtures('schema', 'data') | ||
def test_partial_tables(cli, archive_filename, db_helper): | ||
result = cli.dump('-p', 'employees:SELECT * FROM employees WHERE id = 1') | ||
assert not result.exception | ||
archive = zipfile.ZipFile(archive_filename) | ||
db_helper.assert_content(archive, 'groups', {b'id,name', b'1,Admin'}) | ||
db_helper.assert_content( | ||
archive, | ||
'employees', | ||
{ | ||
b'id,first_name,last_name,manager_id,referrer_id,group_id', | ||
b'1,John,Doe,,,1', | ||
} | ||
) | ||
|
||
|
||
@pytest.mark.usefixtures('schema', 'data') | ||
def test_partial_tables_invalid(cli): | ||
result = cli.dump('-p', 'shit') | ||
assert result.exception | ||
assert result.output.endswith( | ||
'Invalid value for "-p" / "--partial": partial table specification should be in ' | ||
'the following format: "table:select SQL"\n' | ||
) | ||
|
||
|
||
@pytest.mark.usefixtures('schema', 'data') | ||
def test_no_schema(cli, archive_filename): | ||
result = cli.dump('-f', 'groups', '--no-schema') | ||
assert not result.exception | ||
archive = zipfile.ZipFile(archive_filename) | ||
assert archive.namelist() == ['dump/data/groups.csv'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import sqlite3 | ||
|
||
import pytest | ||
|
||
from ..conftest import EMPLOYEES_SQL, IS_POSTGRES | ||
|
||
|
||
@pytest.mark.usefixtures('schema', 'data') | ||
def test_load(backend, cli, archive_filename, db_helper): | ||
backend.dump(archive_filename, ['groups'], {'employees': EMPLOYEES_SQL}) | ||
backend.recreate_database() | ||
if IS_POSTGRES: | ||
backend.run('COMMIT') | ||
assert db_helper.get_tables_count() == 0 | ||
result = cli.load('-i', archive_filename) | ||
assert not result.exception | ||
assert db_helper.get_tables_count() == 3 | ||
|
||
|
||
@pytest.mark.parametrize('cleanup_method, dump_kwargs', ( | ||
('truncate', {'dump_schema': False}), | ||
('recreate', {}), | ||
)) | ||
@pytest.mark.usefixtures('schema', 'data') | ||
def test_cleanup_methods(cli, archive_filename, backend, cleanup_method, dump_kwargs): | ||
backend.dump(archive_filename, ['groups'], {'employees': EMPLOYEES_SQL}, **dump_kwargs) | ||
try: | ||
backend.run('COMMIT') | ||
except sqlite3.OperationalError: | ||
pass | ||
result = cli.load('-i', archive_filename, '-m', cleanup_method) | ||
assert not result.exception | ||
assert backend.run('SELECT name FROM groups') == [{'name': 'Admin'}, {'name': 'User'}] | ||
assert backend.run('SELECT id, first_name, last_name FROM employees') == [ | ||
{'id': 5, 'last_name': 'Snow', 'first_name': 'John'}, | ||
{'id': 4, 'first_name': 'John', 'last_name': 'Brown'}, | ||
{'id': 3, 'first_name': 'John', 'last_name': 'Smith'}, | ||
{'id': 1, 'first_name': 'John', 'last_name': 'Doe'}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from xdump.base import BaseBackend | ||
from xdump.cli.utils import apply_decorators, import_backend | ||
|
||
|
||
def test_import_backend(): | ||
backend_class = import_backend('xdump.sqlite.SQLiteBackend') | ||
assert issubclass(backend_class, BaseBackend) | ||
|
||
|
||
def test_apply_decorators(): | ||
|
||
def dec1(func): | ||
func.foo = 1 | ||
return func | ||
|
||
def dec2(func): | ||
func.bar = 2 | ||
return func | ||
|
||
@apply_decorators([dec1, dec2]) | ||
def func(): | ||
pass | ||
|
||
assert func.foo == 1 | ||
assert func.bar == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ deps = | |
pytest | ||
postgres: pytest-postgresql | ||
pytest-django | ||
pytest-click | ||
django | ||
coverage | ||
py27,pypy: mock | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import click | ||
|
||
|
||
COMMON_DECORATORS = [ | ||
click.option('-D', '--dbname', required=True, help='database to work with'), | ||
click.option('-v', '--verbosity', help='verbosity level', default=0, count=True, type=click.IntRange(0, 2)), | ||
] | ||
|
||
|
||
PG_DECORATORS = [ | ||
click.option('-U', '--user', required=True, help='connect as specified database user'), | ||
click.option('-W', '--password', help='password for the DB connection'), | ||
click.option('-H', '--host', default='127.0.0.1', help='database server host or socket directory'), | ||
click.option('-P', '--port', default='5432', help='database server port number'), | ||
] |
Oops, something went wrong.