|
| 1 | +import pickle |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from psycopg.postgres import types as builtins |
| 6 | +from .fix_crdb import is_crdb, crdb_encoding, crdb_time_precision |
| 7 | + |
| 8 | + |
| 9 | +def test_description_attribs(conn): |
| 10 | + curs = conn.cursor() |
| 11 | + curs.execute( |
| 12 | + """select |
| 13 | + 3.14::decimal(10,2) as pi, |
| 14 | + 'hello'::text as hi, |
| 15 | + '2010-02-18'::date as now |
| 16 | + """ |
| 17 | + ) |
| 18 | + assert len(curs.description) == 3 |
| 19 | + for c in curs.description: |
| 20 | + len(c) == 7 # DBAPI happy |
| 21 | + for i, a in enumerate( |
| 22 | + """ |
| 23 | + name type_code display_size internal_size precision scale null_ok |
| 24 | + """.split() |
| 25 | + ): |
| 26 | + assert c[i] == getattr(c, a) |
| 27 | + |
| 28 | + # Won't fill them up |
| 29 | + assert c.null_ok is None |
| 30 | + |
| 31 | + c = curs.description[0] |
| 32 | + assert c.name == "pi" |
| 33 | + assert c.type_code == builtins["numeric"].oid |
| 34 | + assert c.display_size is None |
| 35 | + assert c.internal_size is None |
| 36 | + assert c.precision == 10 |
| 37 | + assert c.scale == 2 |
| 38 | + |
| 39 | + c = curs.description[1] |
| 40 | + assert c.name == "hi" |
| 41 | + assert c.type_code == builtins["text"].oid |
| 42 | + assert c.display_size is None |
| 43 | + assert c.internal_size is None |
| 44 | + assert c.precision is None |
| 45 | + assert c.scale is None |
| 46 | + |
| 47 | + c = curs.description[2] |
| 48 | + assert c.name == "now" |
| 49 | + assert c.type_code == builtins["date"].oid |
| 50 | + assert c.display_size is None |
| 51 | + if is_crdb(conn) and conn.info.server_version < 230000: |
| 52 | + assert c.internal_size == 16 |
| 53 | + else: |
| 54 | + assert c.internal_size == 4 |
| 55 | + assert c.precision is None |
| 56 | + assert c.scale is None |
| 57 | + |
| 58 | + |
| 59 | +def test_description_slice(conn): |
| 60 | + curs = conn.cursor() |
| 61 | + curs.execute("select 1::int as a") |
| 62 | + curs.description[0][0:2] == ("a", 23) |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize( |
| 66 | + "type, precision, scale, dsize, isize", |
| 67 | + [ |
| 68 | + ("text", None, None, None, None), |
| 69 | + ("varchar", None, None, None, None), |
| 70 | + ("varchar(42)", None, None, 42, None), |
| 71 | + ("int4", None, None, None, 4), |
| 72 | + ("numeric", None, None, None, None), |
| 73 | + ("numeric(10)", 10, 0, None, None), |
| 74 | + ("numeric(10, 3)", 10, 3, None, None), |
| 75 | + ("time", None, None, None, 8), |
| 76 | + crdb_time_precision("time(4)", 4, None, None, 8), |
| 77 | + crdb_time_precision("time(10)", 6, None, None, 8), |
| 78 | + ], |
| 79 | +) |
| 80 | +def test_details(conn, type, precision, scale, dsize, isize): |
| 81 | + cur = conn.cursor() |
| 82 | + cur.execute(f"select null::{type}") |
| 83 | + col = cur.description[0] |
| 84 | + repr(col) |
| 85 | + assert col.precision == precision |
| 86 | + assert col.scale == scale |
| 87 | + assert col.display_size == dsize |
| 88 | + assert col.internal_size == isize |
| 89 | + |
| 90 | + |
| 91 | +def test_pickle(conn): |
| 92 | + curs = conn.cursor() |
| 93 | + curs.execute( |
| 94 | + """select |
| 95 | + 3.14::decimal(10,2) as pi, |
| 96 | + 'hello'::text as hi, |
| 97 | + '2010-02-18'::date as now |
| 98 | + """ |
| 99 | + ) |
| 100 | + description = curs.description |
| 101 | + pickled = pickle.dumps(description, pickle.HIGHEST_PROTOCOL) |
| 102 | + unpickled = pickle.loads(pickled) |
| 103 | + assert [tuple(d) for d in description] == [tuple(d) for d in unpickled] |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.crdb_skip("no col query") |
| 107 | +def test_no_col_query(conn): |
| 108 | + cur = conn.execute("select") |
| 109 | + assert cur.description == [] |
| 110 | + assert cur.fetchall() == [()] |
| 111 | + |
| 112 | + |
| 113 | +def test_description_closed_connection(conn): |
| 114 | + # If we have reasons to break this test we will (e.g. we really need |
| 115 | + # the connection). In #172 it fails just by accident. |
| 116 | + cur = conn.execute("select 1::int4 as foo") |
| 117 | + conn.close() |
| 118 | + assert len(cur.description) == 1 |
| 119 | + col = cur.description[0] |
| 120 | + assert col.name == "foo" |
| 121 | + assert col.type_code == 23 |
| 122 | + |
| 123 | + |
| 124 | +def test_name_not_a_name(conn): |
| 125 | + cur = conn.cursor() |
| 126 | + (res,) = cur.execute("""select 'x' as "foo-bar" """).fetchone() |
| 127 | + assert res == "x" |
| 128 | + assert cur.description[0].name == "foo-bar" |
| 129 | + |
| 130 | + |
| 131 | +@pytest.mark.parametrize("encoding", ["utf8", crdb_encoding("latin9")]) |
| 132 | +def test_name_encode(conn, encoding): |
| 133 | + conn.execute(f"set client_encoding to {encoding}") |
| 134 | + cur = conn.cursor() |
| 135 | + (res,) = cur.execute("""select 'x' as "\u20ac" """).fetchone() |
| 136 | + assert res == "x" |
| 137 | + assert cur.description[0].name == "\u20ac" |
0 commit comments