|
5 | 5 | import pytest
|
6 | 6 |
|
7 | 7 | from database_sanitizer import dump
|
| 8 | +from database_sanitizer.config import Configuration |
8 | 9 |
|
9 | 10 | EXPECTED_POPEN_KWARGS = {
|
10 | 11 | 'mysql://User:Pass@HostName/Db': {
|
11 | 12 | 'args': (
|
12 | 13 | 'mysqldump --complete-insert --extended-insert'
|
13 |
| - ' --net_buffer_length=10240 -h hostname -u User Db').split(), |
| 14 | + ' --net_buffer_length=10240 -h hostname -u User Db' |
| 15 | + ' --single-transaction' |
| 16 | + ).split(), |
14 | 17 | 'env': {'MYSQL_PWD': 'Pass'},
|
15 | 18 | 'stdout': subprocess.PIPE,
|
16 | 19 | },
|
@@ -45,6 +48,73 @@ def test_run(mocked_popen, url):
|
45 | 48 | assert popen_kwargs == expected_popen_kwargs
|
46 | 49 |
|
47 | 50 |
|
| 51 | +@mock.patch('subprocess.Popen') |
| 52 | +def test_run_with_mysql_extra_params(mocked_popen): |
| 53 | + mocked_popen.return_value.stdout = BytesIO(b'INPUT DUMP') |
| 54 | + output = StringIO() |
| 55 | + |
| 56 | + url = "mysql://User:Pass@HostName/Db" |
| 57 | + config = Configuration() |
| 58 | + config.load({ |
| 59 | + "config": { |
| 60 | + "extra_parameters": { |
| 61 | + "mysqldump": ["--double-transaction"] |
| 62 | + } |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + dump.run(url, output, config) |
| 67 | + |
| 68 | + expected = { |
| 69 | + 'args': ( |
| 70 | + 'mysqldump --complete-insert --extended-insert' |
| 71 | + ' --net_buffer_length=10240 -h hostname -u User Db' |
| 72 | + ' --double-transaction' |
| 73 | + ).split(), |
| 74 | + 'env': {'MYSQL_PWD': 'Pass'}, |
| 75 | + 'stdout': subprocess.PIPE, |
| 76 | + } |
| 77 | + |
| 78 | + (popen_args, popen_kwargs) = mocked_popen.call_args |
| 79 | + expected_popen_args = ( |
| 80 | + (expected.pop('args'),) if popen_args else ()) |
| 81 | + assert popen_args == expected_popen_args |
| 82 | + assert popen_kwargs == expected |
| 83 | + |
| 84 | + |
| 85 | +@mock.patch('subprocess.Popen') |
| 86 | +def test_run_with_pg_dump_extra_params(mocked_popen): |
| 87 | + mocked_popen.return_value.stdout = BytesIO(b'INPUT DUMP') |
| 88 | + output = StringIO() |
| 89 | + |
| 90 | + url = "postgres:///Db" |
| 91 | + config = Configuration() |
| 92 | + config.load({ |
| 93 | + "config": { |
| 94 | + "extra_parameters": { |
| 95 | + "pg_dump": ["--exclude-table=something"] |
| 96 | + } |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + dump.run(url, output, config) |
| 101 | + |
| 102 | + expected = { |
| 103 | + 'args': tuple(( |
| 104 | + 'pg_dump --encoding=utf-8 --quote-all-identifiers' |
| 105 | + ' --dbname postgres:///Db' |
| 106 | + ' --exclude-table=something' |
| 107 | + ).split()), |
| 108 | + 'stdout': subprocess.PIPE, |
| 109 | + } |
| 110 | + |
| 111 | + (popen_args, popen_kwargs) = mocked_popen.call_args |
| 112 | + expected_popen_args = ( |
| 113 | + (expected.pop('args'),) if popen_args else ()) |
| 114 | + assert popen_args == expected_popen_args |
| 115 | + assert popen_kwargs == expected |
| 116 | + |
| 117 | + |
48 | 118 | @mock.patch('subprocess.Popen')
|
49 | 119 | def test_run_unknown_scheme(mocked_popen):
|
50 | 120 | with pytest.raises(ValueError) as excinfo:
|
|
0 commit comments