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"')
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))
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')
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))
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, six.string_types)) 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, six.string_types)) self.assertEqual(s1, 'select "field" from "table"')
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'))
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_copy(self): cur = self.conn.cursor() cur.execute(""" create table test_compose ( id serial primary key, foo text, bar text, "ba'z" text) """) s = six.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 = six.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')
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')])
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"')
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, six.string_types)) self.assertEqual(s1, 'select "field" from "table"')
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\"")
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))
def test_seq(self): l = [sql.SQL('foo'), sql.Literal('bar'), sql.Identifier('baz')] self.assertEqual(sql.Composed(l).seq, l)
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))
def test_join(self): self.assert_(not hasattr(sql.Identifier('foo'), 'join'))