Example #1
0
 def test_as_str(self):
     self.assertEqual(sql.Identifier('foo').as_string(self.conn), '"foo"')
     self.assertEqual(
         sql.Identifier('foo', 'bar').as_string(self.conn), '"foo"."bar"')
     self.assertEqual(
         sql.Identifier("fo'o", 'ba"r').as_string(self.conn),
         '"fo\'o"."ba""r"')
Example #2
0
    def test_repr(self):
        obj = sql.Identifier("fo'o")
        self.assertEqual(repr(obj), 'Identifier("fo\'o")')
        self.assertEqual(repr(obj), str(obj))

        obj = sql.Identifier("fo'o", 'ba"r')
        self.assertEqual(repr(obj), 'Identifier("fo\'o", \'ba"r\')')
        self.assertEqual(repr(obj), str(obj))
Example #3
0
    def test_strings(self):
        self.assertEqual(sql.Identifier('foo').strings, ('foo', ))
        self.assertEqual(sql.Identifier('foo', 'bar').strings, ('foo', 'bar'))

        # Legacy method
        self.assertEqual(sql.Identifier('foo').string, 'foo')
        self.assertRaises(AttributeError, getattr,
                          sql.Identifier('foo', 'bar'), 'string')
Example #4
0
 def test_init(self):
     self.assert_(isinstance(sql.Identifier('foo'), sql.Identifier))
     self.assert_(isinstance(sql.Identifier(u'foo'), sql.Identifier))
     self.assert_(
         isinstance(sql.Identifier('foo', 'bar', 'baz'), sql.Identifier))
     self.assertRaises(TypeError, sql.Identifier)
     self.assertRaises(TypeError, sql.Identifier, 10)
     self.assertRaises(TypeError, sql.Identifier, dt.date(2016, 12, 31))
Example #5
0
    def test_pos_spec(self):
        s = sql.SQL("select {0} from {1}").format(sql.Identifier('field'),
                                                  sql.Identifier('table'))
        s1 = s.as_string(self.conn)
        self.assert_(isinstance(s1, str))
        self.assertEqual(s1, 'select "field" from "table"')

        s = sql.SQL("select {1} from {0}").format(sql.Identifier('table'),
                                                  sql.Identifier('field'))
        s1 = s.as_string(self.conn)
        self.assert_(isinstance(s1, str))
        self.assertEqual(s1, 'select "field" from "table"')
Example #6
0
 def test_eq(self):
     self.assert_(sql.Identifier('foo') == sql.Identifier('foo'))
     self.assert_(
         sql.Identifier('foo', 'bar') == sql.Identifier('foo', 'bar'))
     self.assert_(sql.Identifier('foo') != sql.Identifier('bar'))
     self.assert_(sql.Identifier('foo') != 'foo')
     self.assert_(sql.Identifier('foo') != sql.SQL('foo'))
Example #7
0
    def test_join(self):
        obj = sql.SQL(", ").join(
            [sql.Identifier('foo'),
             sql.SQL('bar'),
             sql.Literal(42)])
        self.assert_(isinstance(obj, sql.Composed))
        self.assertEqual(obj.as_string(self.conn), '"foo", bar, 42')

        obj = sql.SQL(", ").join(
            sql.Composed(
                [sql.Identifier('foo'),
                 sql.SQL('bar'),
                 sql.Literal(42)]))
        self.assert_(isinstance(obj, sql.Composed))
        self.assertEqual(obj.as_string(self.conn), '"foo", bar, 42')

        obj = sql.SQL(", ").join([])
        self.assertEqual(obj, sql.Composed([]))
 def test_composed(self):
     cur = self.conn.cursor()
     uxdb.extras.execute_batch(
         cur,
         sql.SQL("insert into {0} (id, val) values (%s, %s)").format(
             sql.Identifier('testfast')),
         ((i, i * 10) for i in range(1000)))
     cur.execute("select id, val from testfast order by id")
     self.assertEqual(cur.fetchall(), [(i, i * 10) for i in range(1000)])
Example #9
0
    def test_copy(self):
        cur = self.conn.cursor()
        cur.execute("""
            create table test_compose (
                id serial primary key,
                foo text, bar text, "ba'z" text)
            """)

        s = StringIO("10\ta\tb\tc\n20\td\te\tf\n")
        cur.copy_expert(
            sql.SQL("copy {t} (id, foo, bar, {f}) from stdin").format(
                t=sql.Identifier("test_compose"), f=sql.Identifier("ba'z")), s)

        s1 = StringIO()
        cur.copy_expert(
            sql.SQL("copy (select {f} from {t} order by id) to stdout").format(
                t=sql.Identifier("test_compose"), f=sql.Identifier("ba'z")),
            s1)
        s1.seek(0)
        self.assertEqual(s1.read(), 'c\nf\n')
Example #10
0
    def test_execute(self):
        cur = self.conn.cursor()
        cur.execute("""
            create table test_compose (
                id serial primary key,
                foo text, bar text, "ba'z" text)
            """)
        cur.execute(
            sql.SQL("insert into {0} (id, {1}) values (%s, {2})").format(
                sql.Identifier('test_compose'),
                sql.SQL(', ').join(map(sql.Identifier,
                                       ['foo', 'bar', "ba'z"])),
                (sql.Placeholder() * 3).join(', ')), (10, 'a', 'b', 'c'))

        cur.execute("select * from test_compose")
        self.assertEqual(cur.fetchall(), [(10, 'a', 'b', 'c')])
Example #11
0
 def test_unicode(self):
     s = sql.SQL(u"select {0} from {1}").format(sql.Identifier(u'field'),
                                                sql.Identifier('table'))
     s1 = s.as_string(self.conn)
     self.assert_(isinstance(s1, text_type))
     self.assertEqual(s1, u'select "field" from "table"')
Example #12
0
 def test_dict(self):
     s = sql.SQL("select {f} from {t}").format(f=sql.Identifier('field'),
                                               t=sql.Identifier('table'))
     s1 = s.as_string(self.conn)
     self.assert_(isinstance(s1, str))
     self.assertEqual(s1, 'select "field" from "table"')
Example #13
0
 def test_join(self):
     self.assert_(not hasattr(sql.Identifier('foo'), 'join'))
Example #14
0
 def test_join(self):
     obj = sql.Composed([sql.Literal("foo"), sql.Identifier("b'ar")])
     obj = obj.join(", ")
     self.assert_(isinstance(obj, sql.Composed))
     self.assertQuotedEqual(obj.as_string(self.conn), "'foo', \"b'ar\"")
Example #15
0
 def test_eq(self):
     l = [sql.Literal("foo"), sql.Identifier("b'ar")]
     l2 = [sql.Literal("foo"), sql.Literal("b'ar")]
     self.assert_(sql.Composed(l) == sql.Composed(list(l)))
     self.assert_(sql.Composed(l) != l)
     self.assert_(sql.Composed(l) != sql.Composed(l2))
Example #16
0
 def test_seq(self):
     l = [sql.SQL('foo'), sql.Literal('bar'), sql.Identifier('baz')]
     self.assertEqual(sql.Composed(l).seq, l)
Example #17
0
 def test_repr(self):
     obj = sql.Composed([sql.Literal("foo"), sql.Identifier("b'ar")])
     self.assertEqual(repr(obj),
                      """Composed([Literal('foo'), Identifier("b'ar")])""")
     self.assertEqual(str(obj), repr(obj))