def test_register_conn(hstore, conn): info = TypeInfo.fetch(conn, "hstore") register_hstore(info, conn) assert conn.adapters.types[info.oid].name == "hstore" cur = conn.execute("select null::hstore, ''::hstore, 'a => b'::hstore") assert cur.fetchone() == (None, {}, {"a": "b"})
def test_register_globally(hstore, dsn, svcconn, global_adapters): info = TypeInfo.fetch(svcconn, "hstore") register_hstore(info) assert psycopg.adapters.types[info.oid].name == "hstore" assert svcconn.adapters.types.get(info.oid) is None conn = psycopg.connect(dsn) assert conn.adapters.types[info.oid].name == "hstore" cur = conn.execute("select null::hstore, ''::hstore, 'a => b'::hstore") assert cur.fetchone() == (None, {}, {"a": "b"})
def test_set_custom_type(conn, hstore): command = """copy (select '"a"=>"1", "b"=>"2"'::hstore) to stdout""" cur = conn.cursor() with cur.copy(command) as copy: rows = list(copy.rows()) assert rows == [('"a"=>"1", "b"=>"2"',)] register_hstore(TypeInfo.fetch(conn, "hstore"), cur) with cur.copy(command) as copy: copy.set_types(["hstore"]) rows = list(copy.rows()) assert rows == [({"a": "1", "b": "2"},)]
def initialize(self, connection): super().initialize(connection) # PGDialect.initialize() checks server version for <= 8.2 and sets # this flag to False if so if not self.full_returning: self.insert_executemany_returning = False # HSTORE can't be registered until we have a connection so that # we can look up its OID, so we set up this adapter in # initialize() if self.use_native_hstore: info = self._type_info_fetch(connection, "hstore") self._has_native_hstore = info is not None if self._has_native_hstore: from psycopg.types.hstore import register_hstore # register the adapter for connections made subsequent to # this one register_hstore(info, self._psycopg_adapters_map) # register the adapter for this connection register_hstore(info, connection.connection)
def test_no_info_error(conn): with pytest.raises(TypeError, match="hstore.*extension"): register_hstore(None, conn) # type: ignore[arg-type]
def test_roundtrip_array(hstore, conn): register_hstore(TypeInfo.fetch(conn, "hstore"), conn) samp1 = conn.execute("select %s", (samp, )).fetchone()[0] assert samp1 == samp
def test_roundtrip(hstore, conn, d): register_hstore(TypeInfo.fetch(conn, "hstore"), conn) d1 = conn.execute("select %s", [d]).fetchone()[0] assert d == d1