def test_psql_style_params_warning(): warned = {} def warn(message, category=None, stacklevel=1): warned[message] = True with hook(warnings, 'warn', warn): assert_eq(encode_sql('SELECT $1'), 'SELECT $1') assert warned.get('PostgreSQL-style bind-parameters deprecated')
def test_unescaped_percentage_warning(): for string in ['%', '%e']: warned = {} def warn(message, category=None, stacklevel=1): warned[message] = True with hook(warnings, 'warn', warn): assert_eq(encode_sql(string), string) assert warned.get('Unescaped % in SQL')
def test_interval(): assert_eq(typecast_interval(None, '1 day'), interval(days=1)) assert_eq(typecast_interval(None, '12:34:56'), interval(hours=12, minutes=34, seconds=56)) assert_eq(typecast_interval(None, '12:34:56.789'), interval(hours=12, minutes=34, seconds=56, microseconds=789000)) assert_eq(typecast_interval(None, '123 days 12:34:56'), interval(days=123, hours=12, minutes=34, seconds=56)) assert_eq(typecast_interval(None, '1 year -3 days'), interval(years=1, days=-3))
def test_cursor_description(): assert cu.description is None cu.execute("INSERT INTO x(i) VALUES(%s)", [42]) assert cu.description is None cu.execute("SELECT * FROM x") assert len(cu.description) == 1 name, type_code, display_size, internal_size, precision, scale, null_ok = cu.description[0] assert_eq(name, "i") assert_eq(type_code, pgsql.NUMBER) assert null_ok is None or null_ok cu.execute("INSERT INTO x(i) VALUES(%s)", [42]) assert cu.description is None
def test_encode_placeholders(): assert_eq(encode_sql('SELECT blah FROM foo WHERE blah>%s'), 'SELECT blah FROM foo WHERE blah>$1') assert_eq(encode_sql('SELECT blah FROM foo WHERE blah BETWEEN %s AND %s'), 'SELECT blah FROM foo WHERE blah BETWEEN $1 AND $2') assert_eq(encode_sql('%s %s %s'), '$1 $2 $3') assert_eq(encode_sql('%%s %s %%'), '%s $1 %')
def test_interval_repr(): assert_eq(repr(interval()), 'interval()') assert_eq(repr(interval(seconds=42)), 'interval(seconds=42)') assert_eq(repr(interval(years=7, seconds=42)), 'interval(years=7, seconds=42)')
def test_interval_to_timedelta(): assert_eq(interval(days=1).to_timedelta(), timedelta(days=1)) assert_eq(interval(seconds=1).to_timedelta(), timedelta(seconds=1)) assert_eq(interval(days=1, minutes=3, seconds=1).to_timedelta(), timedelta(days=1, seconds=181)) try: interval(years=42).to_timedelta() raise AssertionError('Should fail converting year interval.') except ValueError: pass
def test_encode_sql(): assert_eq(encode_sql('SELECT blah FROM foo'), 'SELECT blah FROM foo')
def test_encode_literal(): raise SkipTest assert_eq(encode_sql("SELECT '%s'"), "SELECT '%s'")
def assert_executemany(): assert_eq(sorted(cnx.execute('SELECT * FROM x')), executemany_rows)
def test_copy_in_zero(): assert_eq(cnx.copy_in('copy x from stdin csv', []), 0) assert_eq(cnx.execute('select * from x').fetchall(), [])
def test_copy_in_many(): assert_eq(cnx.copy_in('copy x from stdin csv', ['42\n21\n117\n']), 3) assert_eq(cnx.execute('select * from x').fetchall(), [(42,), (21,), (117,)])
def test_copy_in_unicode(): # Try to insert one row with danish ae ligature. assert_eq(cnx.copy_in('copy y from stdin csv', ['\xc3\xa6\n']), 1) assert_eq(cnx.execute('select * from y').fetchall(), [(u'\xe6',)])
def test_copy_in_one(): assert_eq(cnx.copy_in('copy x from stdin csv', ['42\n']), 1) assert_eq(cnx.execute('select * from x').fetchall(), [(42,)])
def test_cursor_arraysize(): assert_eq(cu.arraysize, 1)
def test_module(): assert callable(pgsql.connect) assert_eq(pgsql.apilevel, "2.0") assert_eq(pgsql.threadsafety, 1) assert_eq(pgsql.paramstyle, "format")