Skip to content

Commit bcb442e

Browse files
HemangChothanilarkeeAVaksman
authored
test: fix string compliance tests (#23)
* test: fix string compliance tests * test: nit * fix: nit * fix: remove override methods and add default value * fix: add default value MAX * test: fix nit * test: add logic for additional escape ti fox the test * test: add condition check for other data type * test: remove override tests Co-authored-by: larkee <[email protected]> Co-authored-by: Alex <[email protected]>
1 parent e6e136a commit bcb442e

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,32 @@ def visit_empty_set_expr(self, type_):
137137
_type_map_inv[type(type_[0])]
138138
)
139139

140+
def render_literal_value(self, value, type_):
141+
"""Render the value of a bind parameter as a quoted literal.
142+
143+
This is used for statement sections that do not accept bind parameters
144+
on the target driver/database.
145+
146+
This should be implemented by subclasses using the quoting services
147+
of the DBAPI.
148+
149+
Cloud spanner supports prefixed backslash to escape non-alphanumeric characters
150+
in string. Override the method to add additional escape before using it to
151+
generate a SQL statement.
152+
"""
153+
raw = ["\\", "'", '"', "\n", "\t", "\r"]
154+
if type(value) == str and any(single in value for single in raw):
155+
value = 'r"""{}"""'.format(value)
156+
return value
157+
else:
158+
processor = type_._cached_literal_processor(self.dialect)
159+
if processor:
160+
return processor(value)
161+
else:
162+
raise NotImplementedError(
163+
"Don't know how to literal-quote value %r" % value
164+
)
165+
140166

141167
class SpannerDDLCompiler(DDLCompiler):
142168
"""Spanner DDL statements compiler."""
@@ -183,7 +209,7 @@ def visit_primary_key_constraint(self, constraint):
183209
return None
184210

185211
def visit_unique_constraint(self, constraint):
186-
"""Unique contraints in Spanner are defined with indexes:
212+
"""Unique constraints in Spanner are defined with indexes:
187213
https://cloud.google.com/spanner/docs/secondary-indexes#unique-indexes
188214
189215
The method throws an exception to notify user that in

test/test_suite.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@
8686
from sqlalchemy.testing.suite.test_select import OrderByLabelTest as _OrderByLabelTest
8787
from sqlalchemy.testing.suite.test_types import BooleanTest as _BooleanTest
8888
from sqlalchemy.testing.suite.test_types import IntegerTest as _IntegerTest
89+
from sqlalchemy.testing.suite.test_types import StringTest as _StringTest
90+
8991
from sqlalchemy.testing.suite.test_types import _LiteralRoundTripFixture
9092

9193
from sqlalchemy.testing.suite.test_types import ( # noqa: F401, F403
@@ -535,7 +537,6 @@ def _literal_round_trip(self, type_, input_, output, filter_=None):
535537
)
536538
)
537539
conn.execute(ins)
538-
conn.execute("SELECT 1")
539540

540541
if self.supports_whereclause:
541542
stmt = t.select().where(t.c.x == literal(value))
@@ -843,3 +844,9 @@ def test_nolength_binary(self):
843844
@pytest.mark.skip("Spanner doesn't support quotes in table names.")
844845
class QuotedNameArgumentTest(_QuotedNameArgumentTest):
845846
pass
847+
848+
849+
class StringTest(_StringTest):
850+
@pytest.mark.skip("Spanner doesn't support non-ascii characters")
851+
def test_literal_non_ascii(self):
852+
pass

0 commit comments

Comments
 (0)