def test_init(self): self.assert_(isinstance(sql.Literal('foo'), sql.Literal)) self.assert_(isinstance(sql.Literal(u'foo'), sql.Literal)) self.assert_(isinstance(sql.Literal(b'foo'), sql.Literal)) self.assert_(isinstance(sql.Literal(42), sql.Literal)) self.assert_( isinstance(sql.Literal(dt.date(2016, 12, 31)), sql.Literal))
def test_repr(self): self.assertEqual(repr(sql.Literal("foo")), "Literal('foo')") self.assertEqual(str(sql.Literal("foo")), "Literal('foo')") self.assertQuotedEqual( sql.Literal("foo").as_string(self.conn), "'foo'") self.assertEqual(sql.Literal(42).as_string(self.conn), "42") self.assertEqual( sql.Literal(dt.date(2017, 1, 1)).as_string(self.conn), "'2017-01-01'::date")
def test_sum_inplace(self): obj = sql.Composed([sql.SQL("foo ")]) obj += sql.Literal("bar") self.assert_(isinstance(obj, sql.Composed)) self.assertQuotedEqual(obj.as_string(self.conn), "foo 'bar'") obj = sql.Composed([sql.SQL("foo ")]) obj += sql.Composed([sql.Literal("bar")]) self.assert_(isinstance(obj, sql.Composed)) self.assertQuotedEqual(obj.as_string(self.conn), "foo 'bar'")
def test_eq(self): self.assert_(sql.Placeholder('foo') == sql.Placeholder('foo')) self.assert_(sql.Placeholder('foo') != sql.Placeholder('bar')) self.assert_(sql.Placeholder('foo') != 'foo') self.assert_(sql.Placeholder() == sql.Placeholder()) self.assert_(sql.Placeholder('foo') != sql.Placeholder()) self.assert_(sql.Placeholder('foo') != sql.Literal('foo'))
def test_must_be_adaptable(self): class Foo(object): pass self.assertRaises( uxdb.ProgrammingError, sql.SQL("select {0};").format(sql.Literal(Foo())).as_string, self.conn)
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_braces_escape(self): s = sql.SQL("{{{0}}}").format(sql.Literal(7)) self.assertEqual(s.as_string(self.conn), "{7}") s = sql.SQL("{{1,{0}}}").format(sql.Literal(7)) self.assertEqual(s.as_string(self.conn), "{1,7}")
def test_percent_escape(self): s = sql.SQL("42 % {0}").format(sql.Literal(7)) s1 = s.as_string(self.conn) self.assertEqual(s1, "42 % 7")
def test_compose_literal(self): s = sql.SQL("select {0};").format(sql.Literal(dt.date(2016, 12, 31))) s1 = s.as_string(self.conn) self.assertEqual(s1, "select '2016-12-31'::date;")
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_eq(self): self.assert_(sql.SQL('foo') == sql.SQL('foo')) self.assert_(sql.SQL('foo') != sql.SQL('bar')) self.assert_(sql.SQL('foo') != 'foo') self.assert_(sql.SQL('foo') != sql.Literal('foo'))
def test_wrapped(self): self.assertEqual(sql.Literal('foo').wrapped, 'foo')