def test_sql_interp_no_whitespace(): # Whitespace should be added between arguments. sqli = SQLInterp() sql, bind = sqli.interp("SELECT", "*", "FROM table") eq_(sql, "SELECT * FROM table") eq_(bind, ()) cols = ['one', 'two', 'three'] sql, bind = sqli.interp("SELECT", cols, "FROM table") eq_(sql, "SELECT (?, ?, ?) FROM table") eq_(bind, ('one', 'two', 'three'))
def test_sql_interp_dict(): sqli = SQLInterp() where = { 'first_name': 'John', 'last_name': 'Doe' } sql, bind = sqli.interp("SELECT * FROM users WHERE", where) eq_(sql, "SELECT * FROM users WHERE first_name = ? AND last_name = ?") eq_(bind, ('John', 'Doe')) where = { 'first_name': ['John', 'Jane'], 'last_name': 'Doe' } sql, bind = sqli.interp("SELECT * FROM users WHERE", where) eq_(sql, "SELECT * FROM users WHERE first_name IN (?, ?) AND last_name = ?") eq_(bind, ('John', 'Jane', 'Doe'))
def test_sql_interp_dict(): sqli = SQLInterp() where = {'first_name': 'John', 'last_name': 'Doe'} sql, bind = sqli.interp("SELECT * FROM users WHERE", where) eq_(sql, "SELECT * FROM users WHERE first_name = ? AND last_name = ?") eq_(bind, ('John', 'Doe')) where = {'first_name': ['John', 'Jane'], 'last_name': 'Doe'} sql, bind = sqli.interp("SELECT * FROM users WHERE", where) eq_(sql, "SELECT * FROM users WHERE first_name IN (?, ?) AND last_name = ?") eq_(bind, ('John', 'Jane', 'Doe'))
def test_sql_interp_dict_none(): sqli = SQLInterp() where = { 'first_name': None, 'last_name': 'Doe' } sql, bind = sqli.interp("SELECT * FROM users WHERE", where) eq_(sql, "SELECT * FROM users WHERE first_name IS NULL AND last_name = ?") eq_(bind, ('Doe',))
def test_sql_interp_extra_whitespace(): # Excess whitespace is fine. sqli = SQLInterp() sql, bind = sqli.interp("SELECT ", " *", " FROM table") eq_(sql, "SELECT * FROM table") eq_(bind, ())
def test_sql_interp_string(): sqli = SQLInterp() full_name = 'John Doe' sql, bind = sqli.interp("SELECT * FROM table WHERE full_name =", sqli.esc(full_name)) eq_(sql, "SELECT * FROM table WHERE full_name = ?") eq_(bind, ('John Doe',))
def test_sql_interp_dict_none(): sqli = SQLInterp() where = {'first_name': None, 'last_name': 'Doe'} sql, bind = sqli.interp("SELECT * FROM users WHERE", where) eq_(sql, "SELECT * FROM users WHERE first_name IS NULL AND last_name = ?") eq_(bind, ('Doe', ))
def test_sql_interp_tuple(): # Tuples are treated identically to lists. sqli = SQLInterp() cols = ('one', 'two', 'three') sql, bind = sqli.interp("SELECT", cols, "FROM table") eq_(sql, "SELECT (?, ?, ?) FROM table") eq_(bind, ('one', 'two', 'three'))
def test_sql_interp_string(): sqli = SQLInterp() full_name = 'John Doe' sql, bind = sqli.interp("SELECT * FROM table WHERE full_name =", sqli.esc(full_name)) eq_(sql, "SELECT * FROM table WHERE full_name = ?") eq_(bind, ('John Doe', ))
class SQLInterpTest(unittest.TestCase): """ Unit tests for ``SQLInterp``'s ``interp`` method. """ def setUp(self): "Create custom ``SQLInterp`` instance for testing." self.sqli = SQLInterp() def test_sql_interp_no_whitespace(self): "Whitespace should be added between arguments." sql, bind = self.sqli.interp("SELECT", "*", "FROM table") self.assertEquals(sql, "SELECT * FROM table") self.assertEquals(bind, ()) cols = ['one', 'two', 'three'] sql, bind = self.sqli.interp("SELECT", cols, "FROM table") self.assertEquals(sql, "SELECT (?, ?, ?) FROM table") self.assertEquals(bind, ('one', 'two', 'three')) def test_sql_interp_extra_whitespace(self): "Excess whitespace is fine." sql, bind = self.sqli.interp("SELECT ", " *", " FROM table") self.assertEquals(sql, "SELECT * FROM table") self.assertEquals(bind, ()) def test_sql_interp_dict(self): "Test interpolating a dictionary." where = { 'first_name': 'John', 'last_name': 'Doe' } sql, bind = self.sqli.interp("SELECT * FROM users WHERE", where) self.assertEquals(sql, "SELECT * FROM users WHERE first_name = ? AND last_name = ?") self.assertEquals(bind, ('John', 'Doe')) where = { 'first_name': ['John', 'Jane'], 'last_name': 'Doe' } sql, bind = self.sqli.interp("SELECT * FROM users WHERE", where) self.assertEquals(sql, "SELECT * FROM users WHERE first_name IN (?, ?) AND last_name = ?") self.assertEquals(bind, ('John', 'Jane', 'Doe')) def test_sql_interp_dict_none(self): "Test interpolating a dictionary containing a ``None`` value." where = { 'first_name': None, 'last_name': 'Doe' } sql, bind = self.sqli.interp("SELECT * FROM users WHERE", where) self.assertEquals(sql, "SELECT * FROM users WHERE first_name IS NULL AND last_name = ?") self.assertEquals(bind, ('Doe',)) def test_sql_interp_tuple(self): "Test interpolating a tuple." # Tuples are treated identically to lists. cols = ('one', 'two', 'three') sql, bind = self.sqli.interp("SELECT", cols, "FROM table") self.assertEquals(sql, "SELECT (?, ?, ?) FROM table") self.assertEquals(bind, ('one', 'two', 'three')) def test_sql_interp_string(self): "Test interpolating a string." full_name = 'John Doe' sql, bind = self.sqli.interp("SELECT * FROM table WHERE full_name =", self.sqli.esc(full_name)) self.assertEquals(sql, "SELECT * FROM table WHERE full_name = ?") self.assertEquals(bind, ('John Doe',)) def test_unicode(self): "Test unicode strings." full_name = u'John Doe' sql, bind = self.sqli.interp(u"SELECT * FROM table WHERE full_name =", self.sqli.esc(full_name)) self.assertEquals(sql, u"SELECT * FROM table WHERE full_name = ?") self.assertEquals(bind, (u'John Doe',))