MCPcopy Create free account
hub / github.com/MagicStack/asyncpg / test_array_with_custom_json_text_codec

Method test_array_with_custom_json_text_codec

tests/test_codecs.py:1987–2022  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1985 self.assertTupleEqual(st.get_attributes(), ())
1986
1987 async def test_array_with_custom_json_text_codec(self):
1988 import json
1989
1990 await self.con.execute('CREATE TABLE tab (id serial, val json[]);')
1991 insert_sql = 'INSERT INTO tab (val) VALUES (cast($1 AS json[]));'
1992 query_sql = 'SELECT val FROM tab ORDER BY id DESC;'
1993 try:
1994 for custom_codec in [False, True]:
1995 if custom_codec:
1996 await self.con.set_type_codec(
1997 'json',
1998 encoder=lambda v: v,
1999 decoder=json.loads,
2000 schema="pg_catalog",
2001 )
2002
2003 for val in ['"null"', '22', 'null', '[2]', '{"a": null}']:
2004 await self.con.execute(insert_sql, [val])
2005 result = await self.con.fetchval(query_sql)
2006 if custom_codec:
2007 self.assertEqual(result, [json.loads(val)])
2008 else:
2009 self.assertEqual(result, [val])
2010
2011 await self.con.execute(insert_sql, [None])
2012 result = await self.con.fetchval(query_sql)
2013 self.assertEqual(result, [None])
2014
2015 await self.con.execute(insert_sql, None)
2016 result = await self.con.fetchval(query_sql)
2017 self.assertEqual(result, None)
2018
2019 finally:
2020 await self.con.execute('''
2021 DROP TABLE tab;
2022 ''')
2023
2024
2025@unittest.skipIf(os.environ.get('PGHOST'), 'using remote cluster for testing')

Callers

nothing calls this directly

Calls 3

set_type_codecMethod · 0.80
executeMethod · 0.45
fetchvalMethod · 0.45

Tested by

no test coverage detected