|
3 | 3 | from __future__ import unicode_literals
|
4 | 4 |
|
5 | 5 | import io
|
6 |
| -import pytest |
7 | 6 |
|
| 7 | +import pytest |
8 | 8 | from six.moves.urllib import parse as urlparse
|
9 | 9 |
|
10 | 10 | from ..config import Configuration
|
|
15 | 15 | sanitize_from_stream,
|
16 | 16 | )
|
17 | 17 |
|
18 |
| - |
19 | 18 | MOCK_MYSQLDUMP_OUTPUT = b"""
|
20 | 19 | --- Fake MySQL database dump
|
21 | 20 |
|
@@ -89,7 +88,43 @@ def test_parse_column_names(text, expected_column_names):
|
89 | 88 | ("('test'),('test')", (("test",), ("test",))),
|
90 | 89 | ("(1,2),(3,4),", ((1, 2), (3, 4))),
|
91 | 90 | ("(TRUE),(FALSE),(NULL)", ((True,), (False,), (None,))),
|
| 91 | + ("(x')", ()), # Invalid data |
92 | 92 | ),
|
93 | 93 | )
|
94 | 94 | def test_parse_values(text, expected_values):
|
95 | 95 | assert tuple(parse_values(text)) == expected_values
|
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.parametrize('config_type', [ |
| 99 | + 'no-config', 'empty-config', 'single-column-config']) |
| 100 | +@pytest.mark.parametrize('data_label', ['ok', 'invalid']) |
| 101 | +def test_optimizations(config_type, data_label): |
| 102 | + if config_type == 'no-config': |
| 103 | + config = None |
| 104 | + decoder_call_count = 0 |
| 105 | + else: |
| 106 | + config = Configuration() |
| 107 | + if config_type == 'empty-config': |
| 108 | + decoder_call_count = 0 |
| 109 | + else: |
| 110 | + assert config_type == 'single-column-config' |
| 111 | + config.sanitizers["test.notes"] = (lambda x: x) |
| 112 | + decoder_call_count = 3 # Number of rows in test table |
| 113 | + |
| 114 | + data = { |
| 115 | + 'ok': MOCK_MYSQLDUMP_OUTPUT, |
| 116 | + 'invalid': INVALID_MOCK_MYSQLDUMP_OUTPUT, |
| 117 | + }[data_label] |
| 118 | + |
| 119 | + should_raise = ( |
| 120 | + config_type == 'single-column-config' |
| 121 | + and data_label == 'invalid') |
| 122 | + |
| 123 | + dump_stream = io.BytesIO(data) |
| 124 | + if should_raise: |
| 125 | + with pytest.raises(ValueError): |
| 126 | + list(sanitize_from_stream(dump_stream, config)) |
| 127 | + else: |
| 128 | + expected_output = data.decode('utf-8').splitlines() |
| 129 | + result = list(sanitize_from_stream(dump_stream, config)) |
| 130 | + assert result == expected_output |
0 commit comments