summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorDaniel Rocco <drocco@gmail.com>2015-02-28 20:54:03 -0500
committerDaniel Rocco <drocco@gmail.com>2015-03-01 00:40:46 -0500
commit8e919f1b6c74859414fe472ffe4ede24c0a3f632 (patch)
treebf2ab61871818b69323c268188107af4d67792ea /tests
parent072d6053472cdc7fa1da404ac4af9a6c2d7705b5 (diff)
Interpret incoming JSON as a string instead of via json.loads
Closes #163
Diffstat (limited to 'tests')
-rw-r--r--tests/test_pgexecute.py21
-rw-r--r--tests/utils.py23
2 files changed, 41 insertions, 3 deletions
diff --git a/tests/test_pgexecute.py b/tests/test_pgexecute.py
index 94f98627..25e577c5 100644
--- a/tests/test_pgexecute.py
+++ b/tests/test_pgexecute.py
@@ -3,7 +3,7 @@
import pytest
import psycopg2
from textwrap import dedent
-from utils import run, dbtest
+from utils import run, dbtest, requires_json, requires_jsonb
@dbtest
def test_conn(executor):
@@ -125,6 +125,25 @@ def test_bytea_field_support_in_output(executor):
assert u'\\xdeadbeef' in run(executor, "select * from binarydata", join=True)
+
@dbtest
def test_unicode_support_in_unknown_type(executor):
assert u'日本語' in run(executor, "SELECT '日本語' AS japanese;", join=True)
+
+
+@requires_json
+def test_json_renders_without_u_prefix(executor, expanded):
+ run(executor, "create table jsontest(d json)")
+ run(executor, """insert into jsontest (d) values ('{"name": "Éowyn"}')""")
+ result = run(executor, "SELECT d FROM jsontest LIMIT 1", join=True)
+
+ assert u'{"name": "Éowyn"}' in result
+
+
+@requires_jsonb
+def test_jsonb_renders_without_u_prefix(executor, expanded):
+ run(executor, "create table jsonbtest(d jsonb)")
+ run(executor, """insert into jsonbtest (d) values ('{"name": "Éowyn"}')""")
+ result = run(executor, "SELECT d FROM jsonbtest LIMIT 1", join=True)
+
+ assert u'{"name": "Éowyn"}' in result
diff --git a/tests/utils.py b/tests/utils.py
index 9b7376f1..b755f78e 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -1,6 +1,8 @@
import pytest
import psycopg2
+import psycopg2.extras
from pgcli.main import format_output
+from pgcli.pgexecute import register_json_typecasters
# TODO: should this be somehow be divined from environment?
POSTGRES_USER, POSTGRES_HOST = 'postgres', 'localhost'
@@ -11,17 +13,34 @@ def db_connection(dbname=None):
conn.autocommit = True
return conn
+
try:
- db_connection()
+ conn = db_connection()
CAN_CONNECT_TO_DB = True
+ SERVER_VERSION = conn.server_version
+ json_types = register_json_typecasters(conn, lambda x: x)
+ JSON_AVAILABLE = 'json' in json_types
+ JSONB_AVAILABLE = 'jsonb' in json_types
except:
- CAN_CONNECT_TO_DB = False
+ CAN_CONNECT_TO_DB = JSON_AVAILABLE = JSONB_AVAILABLE = False
+ SERVER_VERSION = 0
+
dbtest = pytest.mark.skipif(
not CAN_CONNECT_TO_DB,
reason="Need a postgres instance at localhost accessible by user 'postgres'")
+requires_json = pytest.mark.skipif(
+ not JSON_AVAILABLE,
+ reason='Postgres server unavailable or json type not defined')
+
+
+requires_jsonb = pytest.mark.skipif(
+ not JSONB_AVAILABLE,
+ reason='Postgres server unavailable or jsonb type not defined')
+
+
def create_db(dbname):
with db_connection().cursor() as cur:
try: