From 00e005b77ddfe2155f88a903e1521a205d97c77f Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sun, 26 Sep 2010 23:03:55 +0100 Subject: [PATCH] Added test to verify dict roundtrip with hstore. --- tests/types_extras.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/types_extras.py b/tests/types_extras.py index 1671fe2b..4171fd75 100644 --- a/tests/types_extras.py +++ b/tests/types_extras.py @@ -209,6 +209,30 @@ class HstoreTestCase(unittest.TestCase): self.assertEqual(t[1], {}) self.assertEqual(t[2], {'a': 'b'}) + def test_roundtrip(self): + from psycopg2.extras import register_hstore + register_hstore(self.conn) + cur = self.conn.cursor() + + def ok(d): + cur.execute("select %s", (d,)) + d1 = cur.fetchone()[0] + self.assertEqual(len(d), len(d1)) + for k in d: + self.assert_(k in d1, k) + self.assertEqual(d[k], d1[k]) + + ok({}) + ok({'a': 'b', 'c': None}) + + ab = map(chr, range(32, 128)) + ok(dict(zip(ab, ab))) + ok({''.join(ab): ''.join(ab)}) + + self.conn.set_client_encoding('latin1') + ab = map(chr, range(1, 256)) + ok({''.join(ab): ''.join(ab)}) + ok(dict(zip(ab, ab))) def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__)