- Notifications
You must be signed in to change notification settings - Fork126
Test parameter escaping#46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
10 commits Select commitHold shift + click to select a range
49d5b85 Refactor so we can unit test `inject_parameters`
bce1598 Add unit tests for inject_parameters
dd25478 Remove inaccurate description. Per #51, spark sql does not support
29bab86 Fixes #51 and adds unit tests.
aafbdfa Working change which passes all written tests and most from #51
6750633 Implement suggest fix from #51 which now passes the integration test …
82556df Incorporate e2e test suggested by #56
1f9b78c Black utils.py
ba4fed6 Fix one typo
9c8eb6e Fixed test.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
6 changes: 4 additions & 2 deletionssrc/databricks/sql/client.py
This file contains hidden or 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
8 changes: 6 additions & 2 deletionssrc/databricks/sql/utils.py
This file contains hidden or 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
14 changes: 14 additions & 0 deletionstests/e2e/driver_tests.py
This file contains hidden or 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
150 changes: 150 additions & 0 deletionstests/unit/test_param_escaper.py
This file contains hidden or 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,150 @@ | ||
| from datetime import date, datetime | ||
| import unittest, pytest | ||
| from databricks.sql.utils import ParamEscaper, inject_parameters | ||
| pe = ParamEscaper() | ||
| class TestIndividualFormatters(object): | ||
susodapop marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| # Test individual type escapers | ||
| def test_escape_number_integer(self): | ||
| """This behaviour falls back to Python's default string formatting of numbers | ||
| """ | ||
| assert pe.escape_number(100) == 100 | ||
| def test_escape_number_float(self): | ||
| """This behaviour falls back to Python's default string formatting of numbers | ||
| """ | ||
| assert pe.escape_number(100.1234) == 100.1234 | ||
| def test_escape_string_normal(self): | ||
| """ | ||
| """ | ||
| assert pe.escape_string("golly bob howdy") == "'golly bob howdy'" | ||
| def test_escape_string_that_includes_special_characters(self): | ||
| """Tests for how special characters are treated. | ||
| When passed a string, the `escape_string` method wraps it in single quotes | ||
| and escapes any special characters with a back stroke (\) | ||
| Example: | ||
| IN : his name was 'robert palmer' | ||
| OUT: 'his name was \'robert palmer\'' | ||
| """ | ||
| # Testing for the presence of these characters: '"/\😂 | ||
| assert pe.escape_string("his name was 'robert palmer'") == r"'his name was \'robert palmer\''" | ||
| # These tests represent the same user input in the several ways it can be written in Python | ||
| # Each argument to `escape_string` evaluates to the same bytes. But Python lets us write it differently. | ||
| assert pe.escape_string("his name was \"robert palmer\"") == "'his name was \"robert palmer\"'" | ||
| assert pe.escape_string('his name was "robert palmer"') == "'his name was \"robert palmer\"'" | ||
| assert pe.escape_string('his name was {}'.format('"robert palmer"')) == "'his name was \"robert palmer\"'" | ||
| assert pe.escape_string("his name was robert / palmer") == r"'his name was robert / palmer'" | ||
| # If you need to include a single backslash, use an r-string to prevent Python from raising a | ||
| # DeprecationWarning for an invalid escape sequence | ||
| assert pe.escape_string("his name was robert \\/ palmer") == r"'his name was robert \\/ palmer'" | ||
| assert pe.escape_string("his name was robert \\ palmer") == r"'his name was robert \\ palmer'" | ||
| assert pe.escape_string("his name was robert \\\\ palmer") == r"'his name was robert \\\\ palmer'" | ||
| assert pe.escape_string("his name was robert palmer 😂") == r"'his name was robert palmer 😂'" | ||
| # Adding the test from PR #56 to prove escape behaviour | ||
| assert pe.escape_string("you're") == r"'you\'re'" | ||
| # Adding this test from #51 to prove escape behaviour when the target string involves repeated SQL escape chars | ||
| assert pe.escape_string("cat\\'s meow") == r"'cat\\\'s meow'" | ||
| # Tests from the docs: https://docs.databricks.com/sql/language-manual/data-types/string-type.html | ||
| assert pe.escape_string('Spark') == "'Spark'" | ||
| assert pe.escape_string("O'Connell") == r"'O\'Connell'" | ||
| assert pe.escape_string("Some\\nText") == r"'Some\\nText'" | ||
| assert pe.escape_string("Some\\\\nText") == r"'Some\\\\nText'" | ||
| assert pe.escape_string("서울시") == "'서울시'" | ||
| assert pe.escape_string("\\\\") == r"'\\\\'" | ||
| def test_escape_date_time(self): | ||
| INPUT = datetime(1991,8,3,21,55) | ||
| FORMAT = "%Y-%m-%d %H:%M:%S" | ||
| OUTPUT = "'1991-08-03 21:55:00'" | ||
| assert pe.escape_datetime(INPUT, FORMAT) == OUTPUT | ||
| def test_escape_date(self): | ||
| INPUT = date(1991,8,3) | ||
| FORMAT = "%Y-%m-%d" | ||
| OUTPUT = "'1991-08-03'" | ||
| assert pe.escape_datetime(INPUT, FORMAT) == OUTPUT | ||
| def test_escape_sequence_integer(self): | ||
| assert pe.escape_sequence([1,2,3,4]) == "(1,2,3,4)" | ||
| def test_escape_sequence_float(self): | ||
| assert pe.escape_sequence([1.1,2.2,3.3,4.4]) == "(1.1,2.2,3.3,4.4)" | ||
| def test_escape_sequence_string(self): | ||
| assert pe.escape_sequence( | ||
| ["his", "name", "was", "robert", "palmer"]) == \ | ||
| "('his','name','was','robert','palmer')" | ||
| def test_escape_sequence_sequence_of_strings(self): | ||
| # This is not valid SQL. | ||
| INPUT = [["his", "name"], ["was", "robert"], ["palmer"]] | ||
| OUTPUT = "(('his','name'),('was','robert'),('palmer'))" | ||
| assert pe.escape_sequence(INPUT) == OUTPUT | ||
| class TestFullQueryEscaping(object): | ||
| def test_simple(self): | ||
| INPUT = """ | ||
| SELECT | ||
| field1, | ||
| field2, | ||
| field3 | ||
| FROM | ||
| table | ||
| WHERE | ||
| field1 = %(param1)s | ||
| """ | ||
| OUTPUT = """ | ||
| SELECT | ||
| field1, | ||
| field2, | ||
| field3 | ||
| FROM | ||
| table | ||
| WHERE | ||
| field1 = ';DROP ALL TABLES' | ||
| """ | ||
| args = {"param1": ";DROP ALL TABLES"} | ||
| assert inject_parameters(INPUT, pe.escape_args(args)) == OUTPUT | ||
| @unittest.skipUnless(False, "Thrift server supports native parameter binding.") | ||
| def test_only_bind_in_where_clause(self): | ||
| INPUT = """ | ||
| SELECT | ||
| %(field)s, | ||
| field2, | ||
| field3 | ||
| FROM table | ||
| """ | ||
| args = {"field": "Some Value"} | ||
| with pytest.raises(Exception): | ||
| inject_parameters(INPUT, pe.escape_args(args)) | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.