Skip to content

Commit 94f77f3

Browse files
committed
fix tests
1 parent 7e340b3 commit 94f77f3

File tree

2 files changed

+113
-15
lines changed

2 files changed

+113
-15
lines changed

lib/req_ch.ex

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,67 @@ defmodule ReqCH do
177177
end
178178

179179
defp prepare_params(params) do
180-
Enum.map(params, fn {key, value} -> {"param_#{key}", value} end)
180+
Enum.map(params, fn {key, value} -> {"param_#{key}", prepare_param_value(value)} end)
181+
end
182+
183+
defp prepare_param_value(text) when is_binary(text) do
184+
escapes = [{"\\", "\\\\"}, {"\t", "\\\t"}, {"\n", "\\\n"}]
185+
186+
Enum.reduce(escapes, text, fn {pattern, replacement}, text ->
187+
String.replace(text, pattern, replacement)
188+
end)
189+
end
190+
191+
defp prepare_param_value(%DateTime{} = datetime) do
192+
unix_microseconds =
193+
datetime
194+
|> DateTime.shift_zone!("Etc/UTC")
195+
|> DateTime.to_unix(:microsecond)
196+
197+
unix_seconds = unix_microseconds / 1_000_000
198+
unix_seconds_trunc = trunc(unix_seconds)
199+
200+
if unix_seconds_trunc == unix_seconds do
201+
unix_seconds_trunc
202+
else
203+
:erlang.float_to_binary(unix_seconds, decimals: 6)
204+
end
205+
end
206+
207+
defp prepare_param_value(array) when is_list(array) do
208+
elements = Enum.map(array, &prepare_array_param_value/1)
209+
IO.iodata_to_binary([?[, Enum.intersperse(elements, ?,), ?]])
210+
end
211+
212+
defp prepare_param_value(tuple) when is_tuple(tuple) do
213+
elements = Enum.map(Tuple.to_list(tuple), &prepare_array_param_value/1)
214+
IO.iodata_to_binary([?(, Enum.intersperse(elements, ?,), ?)])
215+
end
216+
217+
defp prepare_param_value(struct) when is_struct(struct), do: to_string(struct)
218+
219+
defp prepare_param_value(map) when is_map(map) do
220+
elements = Enum.map(Map.to_list(map), &prepare_map_param_value/1)
221+
IO.iodata_to_binary([?{, Enum.intersperse(elements, ?,), ?}])
222+
end
223+
224+
defp prepare_param_value(other), do: to_string(other)
225+
226+
defp prepare_array_param_value(text) when is_binary(text) do
227+
text = prepare_param_value(text)
228+
[?', String.replace(text, "'", "''"), ?']
229+
end
230+
231+
defp prepare_array_param_value(%s{} = param) when s in [Date, NaiveDateTime] do
232+
[?', to_string(param), ?']
233+
end
234+
235+
defp prepare_array_param_value(other), do: prepare_param_value(other)
236+
237+
defp prepare_map_param_value({key, value}) do
238+
key = prepare_array_param_value(key)
239+
value = prepare_array_param_value(value)
240+
[key, ?:, value]
181241
end
182242

183243
@valid_formats [:tsv, :csv, :json, :explorer]

test/req_ch_test.exs

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,12 @@ defmodule ReqCHTest do
215215

216216
test "a query with Date(Time) params" do
217217
utc_now = ~U[2024-12-20 05:44:53.855679Z]
218-
naive_now = DateTime.to_naive(utc_now)
218+
utc_now_seconds = DateTime.truncate(utc_now, :second)
219219
utc_today = DateTime.to_date(utc_now)
220220

221221
params = [
222222
utc_now: utc_now,
223-
naive_now: naive_now,
223+
utc_now_seconds: utc_now_seconds,
224224
utc_today: utc_today
225225
]
226226

@@ -229,17 +229,24 @@ defmodule ReqCHTest do
229229
ReqCH.new(),
230230
"""
231231
SELECT
232-
{utc_now:DateTime},
233232
{utc_now:DateTime64(6)},
234-
{naive_now:DateTime},
235-
{naive_now:DateTime64(6)},
236-
{date:Date}
233+
{utc_now_seconds:DateTime},
234+
{utc_today:Date}
235+
FORMAT
236+
JSONCompact
237237
""",
238238
params
239239
)
240240

241241
assert response.status == 200
242-
assert response.body == ""
242+
243+
assert response.body["data"] == [
244+
[
245+
"2024-12-20 05:44:53.855679",
246+
"2024-12-20 05:44:53",
247+
"2024-12-20"
248+
]
249+
]
243250
end
244251

245252
# https://clickhouse.com/docs/en/interfaces/http#tabs-in-url-parameters
@@ -252,17 +259,21 @@ defmodule ReqCHTest do
252259
]
253260

254261
response =
255-
ReqCH.query(ReqCH.new(), "SELECT {tab:String}, {newline:String}, {both:String}", params)
262+
ReqCH.query!(
263+
ReqCH.new(),
264+
"SELECT {tab:String}, {newline:String}, {both:String} FORMAT JSONCompact",
265+
params
266+
)
256267

257268
assert response.status == 200
258-
assert response.body == "a\tb\nc\n"
269+
assert response.body["data"] == [["a\tb", "c\nd", "a\tb\nc\t\nd"]]
259270
end
260271

261272
test "a query with arrays" do
262273
params = [
263-
array: ["a", "b", "c"],
274+
array: ["a", "b", "c", "a\tb\nc\t\nd"],
264275
empty_array: [],
265-
nested_array: [["a", "b"], ["c", "d"]],
276+
nested_array: [["a", "b"], ["c", "d"], ["a\tb\nc\t\nd"]],
266277
date_array: [~D[2024-12-20], ~D[2024-12-21]]
267278
]
268279

@@ -275,12 +286,22 @@ defmodule ReqCHTest do
275286
{empty_array:Array(String)},
276287
{nested_array:Array(Array(String))},
277288
{date_array:Array(Date)}
289+
FORMAT
290+
JSONCompact
278291
""",
279292
params
280293
)
281294

282295
assert response.status == 200
283-
assert response.body == ""
296+
297+
assert response.body["data"] == [
298+
[
299+
["a", "b", "c", "a\tb\nc\t\nd"],
300+
[],
301+
[["a", "b"], ["c", "d"], ["a\tb\nc\t\nd"]],
302+
["2024-12-20", "2024-12-21"]
303+
]
304+
]
284305
end
285306

286307
test "a query with tuples" do
@@ -305,7 +326,14 @@ defmodule ReqCHTest do
305326
)
306327

307328
assert response.status == 200
308-
assert response.body == ""
329+
330+
assert response.body ==
331+
"""
332+
(1,'a','2024-12-20')\t\
333+
()\t\
334+
((1,'a'),(2,'b'))\t\
335+
('2024-12-20','2024-12-21')
336+
"""
309337
end
310338

311339
test "a query with maps" do
@@ -325,12 +353,22 @@ defmodule ReqCHTest do
325353
{empty_map:Map(String, UInt8)},
326354
{nested_map:Map(String, Map(String, UInt8))},
327355
{date_map:Map(String, Date)}
356+
FORMAT
357+
JSONCompact
328358
""",
329359
params
330360
)
331361

332362
assert response.status == 200
333-
assert response.body == ""
363+
364+
assert response.body["data"] == [
365+
[
366+
%{"a" => 1, "b" => 2},
367+
%{},
368+
%{"a" => %{"b" => 1}, "c" => %{"d" => 2}},
369+
%{"a" => "2024-12-20", "b" => "2024-12-21"}
370+
]
371+
]
334372
end
335373

336374
test "a query with unknown database" do

0 commit comments

Comments
 (0)