Skip to content

Commit

Permalink
Fixed and improve JSON tests and fixed JSON binary decoding.
Browse files Browse the repository at this point in the history
According to PR #42, JSON is supposed to be decoded as `{json, binary()}`
and `{jsonb, binary()}`.
  • Loading branch information
pguyot committed Mar 23, 2018
1 parent 70f8cf6 commit df9e384
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/pgsql_protocol.erl
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,8 @@ decode_array_text0(<<$\\, C, Rest/binary>>, true, Acc) ->
decode_array_text0(<<C, Rest/binary>>, Quoted, Acc) ->
decode_array_text0(Rest, Quoted, [C | Acc]).

decode_value_bin(?JSONBOID, <<?JSONB_VERSION_1:8, Value/binary>>, _OIDMap, _IntegerDateTimes) -> Value;
decode_value_bin(?JSONBOID, <<?JSONB_VERSION_1:8, Value/binary>>, _OIDMap, _DecodeOptions) -> {jsonb, Value};
decode_value_bin(?JSONOID, Value, _OIDMap, _DecodeOptions) -> {json, Value};
decode_value_bin(?BOOLOID, <<0>>, _OIDMap, _DecodeOptions) -> false;
decode_value_bin(?BOOLOID, <<1>>, _OIDMap, _DecodeOptions) -> true;
decode_value_bin(?BYTEAOID, Value, _OIDMap, _DecodeOptions) -> Value;
Expand Down
24 changes: 13 additions & 11 deletions test/pgsql_connection_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1256,21 +1256,23 @@ json_types_test_() ->
[?_test(begin
{updated, 1} = pgsql_connection:sql_query("create temporary table tmp (id integer primary key, a_json json, b_json json)", Conn),
{{insert,0,1}, []} = pgsql_connection:extended_query("insert into tmp (id, b_json) values ($1, $2)", [2, {json, <<"[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]">>}], Conn),
?_assertEqual({{select,1},[{{json,<<"[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]">>}}]}, pgsql_connection:simple_query("select '[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]'::json", Conn)),
?_assertEqual({{select,1},[{{json,<<"[{\"a\": \"foo\"}, {\"b\": \"bar\"}, {\"c\": \"baz\"}]">>}}]}, pgsql_connection:simple_query("select b_json from tmp where id = 2", Conn))
end),
?_test(begin
%% only run jsonb tests on 9.4 in travis
case os:getenv("TRAVIS_POSTGRESQL_VERSION") of
"9.4" ->
?assertEqual({{select,1},[{{json,<<"[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]">>}}]}, pgsql_connection:simple_query("select '[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]'::json", Conn)),
?assertEqual({{select,1},[{{json,<<"[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]">>}}]}, pgsql_connection:simple_query("select b_json from tmp where id = 2", Conn)),
?assertEqual({{select,1},[{{json,<<"[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]">>}}]}, pgsql_connection:extended_query("select b_json from tmp where id = $1", [2], Conn))
end),
?_test(begin
%% only run jsonb tests if type exists.
case pgsql_connection:simple_query("SELECT 1 FROM pg_type WHERE typname = 'jsonb'", Conn) of
{{select, 1}, [{1}]} ->
{updated, 1} = pgsql_connection:sql_query("create temporary table tmp_b (id integer primary key, a_json jsonb, b_json json)", Conn),
?_assertEqual({{select,1},[{{jsonb,<<"[{\"a\": \"foo\"}, {\"b\": \"bar\"}, {\"c\": \"baz\"}]">>}}]}, pgsql_connection:simple_query("select '[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]'::jsonb", Conn)),
?assertEqual({{select,1},[{{jsonb,<<"[{\"a\": \"foo\"}, {\"b\": \"bar\"}, {\"c\": \"baz\"}]">>}}]}, pgsql_connection:simple_query("select '[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]'::jsonb", Conn)),
{{insert,0,1}, []} = pgsql_connection:extended_query("insert into tmp_b (id, a_json) values ($1, $2)", [1, {jsonb, <<"[{\"a\":\"foo\"},{\"b\":\"bar\"},{\"c\":\"baz\"}]">>}], Conn),
?_assertEqual({{select,1},[{{jsonb,<<"[{\"a\": \"foo\"}, {\"b\": \"bar\"}, {\"c\": \"baz\"}]">>}}]}, pgsql_connection:simple_query("select a_json from tmp_b where id = 1", Conn));
_ ->
?assertEqual({{select,1},[{{jsonb,<<"[{\"a\": \"foo\"}, {\"b\": \"bar\"}, {\"c\": \"baz\"}]">>}}]}, pgsql_connection:simple_query("select a_json from tmp_b where id = 1", Conn)),
?assertEqual({{select,1},[{{jsonb,<<"[{\"a\": \"foo\"}, {\"b\": \"bar\"}, {\"c\": \"baz\"}]">>}}]}, pgsql_connection:extended_query("select a_json from tmp_b where id = $1", [1], Conn));
{{select, 0}, []} ->
ok
end
end)
end)
]
end
}.
Expand Down

0 comments on commit df9e384

Please sign in to comment.