~cjwatson/storm/py3-strings

« back to all changes in this revision

Viewing changes to tests/expr.py

  • Committer: Colin Watson
  • Date: 2019-08-11 16:57:24 UTC
  • Revision ID: cjwatson@canonical.com-20190811165724-y99252clnw75z4ut
Update string handling for Python 3.

This is inspired by work done by Thiago Bellini.  I've taken a different
approach in a few areas which are useful to discuss briefly here (and some
of this should end up in release notes):

 * I generally wrote "bytes" rather than "six.binary_type", since it's
   shorter and works in all supported versions of Python.

 * In the SQLite backend, there's no need to use memoryview, because the
   sqlite3 module in Python 3 automatically converts between the SQLite BLOB
   type and bytes.

 * Some exception messages have changed slightly for clarity.

 * On Python 3, raw=True and token=True in storm.expr.Compile.__call__ only
   treats str specially, not bytes and str, because ultimately the compiler
   is assembling a text string to send to the database.

 * On Python 3, storm.tracer.BaseStatementTracer.connection_raw_execute
   renders text parameters using ascii() rather than by encoding to bytes
   and then calling repr().  While this does result in slightly different
   output from Python 2, it's normally more useful since the encoding is in
   terms of Unicode codepoints rather than UTF-8.

 * storm.sqlobject.AutoUnicodeVariable (and hence StringCol) explicitly
   documents that it only accepts text on Python 3, since native strings are
   already Unicode there so there's much less need for the porting
   affordance.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2168
2168
 
2169
2169
    def test_bytes(self):
2170
2170
        py_expr = compile_python(b"str")
2171
 
        self.assertEquals(py_expr, "'str'")
 
2171
        self.assertEquals(py_expr, "b'str'" if six.PY3 else "'str'")
2172
2172
 
2173
2173
    def test_unicode(self):
2174
2174
        py_expr = compile_python(u"str")
2175
 
        self.assertEquals(py_expr, "u'str'")
 
2175
        self.assertEquals(py_expr, "'str'" if six.PY3 else "u'str'")
2176
2176
 
2177
2177
    def test_int(self):
2178
2178
        py_expr = compile_python(1)