Beispiel #1
0
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',))
Beispiel #2
0
class SQLInterpEscTest(unittest.TestCase):
    """
    Unit tests for ``SQLInterp``'s ``esc`` method.
    """
    def setUp(self):
        "Create a SQLInterp instance for testing."
        self.sqli = SQLInterp()

    def test_esc_int(self):
        "An integer should be wrapped by an ``Esc`` instance."
        obj = self.sqli.esc(1)
        self.assertEquals(type(obj), Esc)

    def test_esc_str(self):
        "A string should be wrapped by an ``Esc`` instance."
        obj = self.sqli.esc('')
        self.assertEquals(type(obj), Esc)

    def test_esc_dict(self):
        "A dictionary should be wrapped by a ``DictEsc`` instance."
        obj = self.sqli.esc({})
        self.assertEquals(type(obj), DictEsc)

    def test_esc_list(self):
        "A list should be wrapped by a ``ListEsc`` instance."
        obj = self.sqli.esc([])
        self.assertEquals(type(obj), ListEsc)

    def test_esc_tuple(self):
        "A tuple should be wrapped by a ``ListEsc`` instance."
        # Tuples are treated identically to lists.
        obj = self.sqli.esc((1,))
        self.assertEquals(type(obj), ListEsc)
Beispiel #3
0
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, ())
Beispiel #4
0
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',))
Beispiel #5
0
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, ())
Beispiel #6
0
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', ))
Beispiel #7
0
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', ))
Beispiel #8
0
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'))
Beispiel #9
0
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'))
Beispiel #10
0
def test_add_types_custom_constructor():
    class MyClass(object): pass
    class MyClassEsc(Esc): pass

    sqli = SQLInterp({ MyClass: MyClassEsc })

    obj = sqli.esc(MyClass())
    eq_(type(obj), MyClassEsc)
Beispiel #11
0
 def test_add_types_custom_constructor(self):
     """
     Test ``SQLInterp``'s ability to add custom types via its constructor.
     """
     sqli = SQLInterp({ self.custom_cls: self.custom_cls_esc })
     
     obj = sqli.esc(self.custom_cls())
     self.assertEquals(type(obj), self.custom_cls_esc)
Beispiel #12
0
    def test_add_types_custom(self):
        """
        Test ``SQLInterp``'s ``add_types`` method.
        """
        sqli = SQLInterp()

        sqli.add_types({ self.custom_cls: self.custom_cls_esc })
        
        obj = sqli.esc(self.custom_cls())
        self.assertEquals(type(obj), self.custom_cls_esc)
Beispiel #13
0
def test_add_types_custom():
    sqli = SQLInterp()

    class MyClass(object): pass
    class MyClassEsc(Esc): pass

    sqli.add_types({ MyClass: MyClassEsc })
    
    obj = sqli.esc(MyClass())
    eq_(type(obj), MyClassEsc)
Beispiel #14
0
def test_add_types_custom_constructor():
    class MyClass(object):
        pass

    class MyClassEsc(Esc):
        pass

    sqli = SQLInterp({MyClass: MyClassEsc})

    obj = sqli.esc(MyClass())
    eq_(type(obj), MyClassEsc)
Beispiel #15
0
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'))
Beispiel #16
0
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'))
Beispiel #17
0
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'))
Beispiel #18
0
def test_add_types_custom():
    sqli = SQLInterp()

    class MyClass(object):
        pass

    class MyClassEsc(Esc):
        pass

    sqli.add_types({MyClass: MyClassEsc})

    obj = sqli.esc(MyClass())
    eq_(type(obj), MyClassEsc)
Beispiel #19
0
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'))
Beispiel #20
0
def test_esc_list():
    sqli = SQLInterp()
    obj = sqli.esc([])
    eq_(type(obj), ListEsc)
Beispiel #21
0
def test_esc_dict():
    sqli = SQLInterp()
    obj = sqli.esc({})
    eq_(type(obj), DictEsc)
Beispiel #22
0
def test_esc_int():
    sqli = SQLInterp()
    obj = sqli.esc(1)
    eq_(type(obj), Esc)
Beispiel #23
0
def test_esc_tuple():
    # Tuples are treated identically to lists.
    sqli = SQLInterp()
    obj = sqli.esc((1,))
    eq_(type(obj), ListEsc)
Beispiel #24
0
def test_esc_dict():
    sqli = SQLInterp()
    obj = sqli.esc({})
    eq_(type(obj), DictEsc)
Beispiel #25
0
def test_esc_list():
    sqli = SQLInterp()
    obj = sqli.esc([])
    eq_(type(obj), ListEsc)
Beispiel #26
0
def test_esc_int():
    sqli = SQLInterp()
    obj = sqli.esc(1)
    eq_(type(obj), Esc)
Beispiel #27
0
def test_esc_str():
    sqli = SQLInterp()
    obj = sqli.esc('')
    eq_(type(obj), Esc)
Beispiel #28
0
 def setUp(self):
     "Create a SQLInterp instance for testing."
     self.sqli = SQLInterp()
Beispiel #29
0
 def setUp(self):
     "Create custom ``SQLInterp`` instance for testing."
     self.sqli = SQLInterp()
Beispiel #30
0
def test_esc_tuple():
    # Tuples are treated identically to lists.
    sqli = SQLInterp()
    obj = sqli.esc((1, ))
    eq_(type(obj), ListEsc)
Beispiel #31
0
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',))
Beispiel #32
0
def test_esc_str():
    sqli = SQLInterp()
    obj = sqli.esc('')
    eq_(type(obj), Esc)