Skip to content

Commit

Permalink
add json as string example test (#246)
Browse files Browse the repository at this point in the history
* add json as string example test

* fix test for older ch
  • Loading branch information
ruslandoga authored Feb 22, 2025
1 parent 388570e commit a653833
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions test/ch/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,48 @@ defmodule Ch.ConnectionTest do
assert_raise ArgumentError, fn -> Ch.query!(conn, "SELECT o FROM json") end
end

test "json as string", %{conn: conn} do
# after v25 ClickHouse started rendering numbers in JSON as strings
[[version]] = Ch.query!(conn, "select version()").rows
numbers_as_strings? = version >= "25"

[expected1, expected2] =
if numbers_as_strings? do
[
[[~s|{"answer":"42"}|]],
[[~s|{"a":"42"}|], [~s|{"b":"10"}|]]
]
else
[
[[~s|{"answer":42}|]],
[[~s|{"a":"42"}|], [~s|{"b":"10"}|]]
]
end

assert Ch.query!(conn, ~s|select '{"answer":42}'::JSON::String|, [],
settings: [enable_json_type: 1]
).rows == expected1

Ch.query!(conn, "CREATE TABLE test_json_as_string(json JSON) ENGINE = Memory", [],
settings: [enable_json_type: 1]
)

Ch.query!(
conn,
"INSERT INTO test_json_as_string(json) FORMAT RowBinary",
_rows = [[Jason.encode_to_iodata!(%{"a" => 42})], [Jason.encode_to_iodata!(%{"b" => 10})]],
types: [:string],
settings: [
enable_json_type: 1,
input_format_binary_read_json_as_string: 1
]
)

assert Ch.query!(conn, "select json::String from test_json_as_string", [],
settings: [enable_json_type: 1]
).rows == expected2
end

# TODO enum16

test "enum8", %{conn: conn} do
Expand Down

0 comments on commit a653833

Please sign in to comment.