epgsql_copy_SUITE.erl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. -module(epgsql_copy_SUITE).
  2. -include_lib("common_test/include/ct.hrl").
  3. -include_lib("stdlib/include/assert.hrl").
  4. -include("epgsql.hrl").
  5. -export([
  6. init_per_suite/1,
  7. all/0,
  8. end_per_suite/1,
  9. from_stdin_text/1,
  10. from_stdin_csv/1,
  11. from_stdin_binary/1,
  12. from_stdin_io_apis/1,
  13. from_stdin_with_terminator/1,
  14. from_stdin_corrupt_data/1
  15. ]).
  16. init_per_suite(Config) ->
  17. [{module, epgsql}|Config].
  18. end_per_suite(_Config) ->
  19. ok.
  20. all() ->
  21. [
  22. from_stdin_text,
  23. from_stdin_csv,
  24. from_stdin_binary,
  25. from_stdin_io_apis,
  26. from_stdin_with_terminator,
  27. from_stdin_corrupt_data
  28. ].
  29. %% @doc Test that COPY in text format works
  30. from_stdin_text(Config) ->
  31. Module = ?config(module, Config),
  32. epgsql_ct:with_connection(
  33. Config,
  34. fun(C) ->
  35. ?assertEqual(
  36. {ok, [text, text]},
  37. Module:copy_from_stdin(
  38. C, "COPY test_table1 (id, value) FROM STDIN WITH (FORMAT text)")),
  39. ?assertEqual(
  40. ok,
  41. io:put_chars(C,
  42. "10\thello world\n"
  43. "11\t\\N\n"
  44. "12\tline 12\n")),
  45. ?assertEqual(
  46. ok,
  47. io:put_chars(C, "13\tline 13\n")),
  48. ?assertEqual(
  49. ok,
  50. io:put_chars(C, "14\tli")),
  51. ?assertEqual(
  52. ok,
  53. io:put_chars(C, "ne 14\n")),
  54. ?assertEqual(
  55. {ok, 5},
  56. Module:copy_done(C)),
  57. ?assertMatch(
  58. {ok, _, [{10, <<"hello world">>},
  59. {11, null},
  60. {12, <<"line 12">>},
  61. {13, <<"line 13">>},
  62. {14, <<"line 14">>}]},
  63. Module:equery(C,
  64. "SELECT id, value FROM test_table1"
  65. " WHERE id IN (10, 11, 12, 13, 14) ORDER BY id"))
  66. end).
  67. %% @doc Test that COPY in CSV format works
  68. from_stdin_csv(Config) ->
  69. Module = ?config(module, Config),
  70. epgsql_ct:with_connection(
  71. Config,
  72. fun(C) ->
  73. ?assertEqual(
  74. {ok, [text, text]},
  75. Module:copy_from_stdin(
  76. C, "COPY test_table1 (id, value) FROM STDIN WITH (FORMAT csv, QUOTE '''')")),
  77. ?assertEqual(
  78. ok,
  79. io:put_chars(C,
  80. "20,'hello world'\n"
  81. "21,\n"
  82. "22,line 22\n")),
  83. ?assertEqual(
  84. ok,
  85. io:put_chars(C, "23,'line 23'\n")),
  86. ?assertEqual(
  87. ok,
  88. io:put_chars(C, "24,'li")),
  89. ?assertEqual(
  90. ok,
  91. io:put_chars(C, "ne 24'\n")),
  92. ?assertEqual(
  93. {ok, 5},
  94. Module:copy_done(C)),
  95. ?assertMatch(
  96. {ok, _, [{20, <<"hello world">>},
  97. {21, null},
  98. {22, <<"line 22">>},
  99. {23, <<"line 23">>},
  100. {24, <<"line 24">>}]},
  101. Module:equery(C,
  102. "SELECT id, value FROM test_table1"
  103. " WHERE id IN (20, 21, 22, 23, 24) ORDER BY id"))
  104. end).
  105. %% @doc Test that COPY in binary format works
  106. from_stdin_binary(Config) ->
  107. Module = ?config(module, Config),
  108. epgsql_ct:with_connection(
  109. Config,
  110. fun(C) ->
  111. ?assertEqual(
  112. {ok, [binary, binary]},
  113. Module:copy_from_stdin(
  114. C, "COPY test_table1 (id, value) FROM STDIN WITH (FORMAT binary)",
  115. {binary, [int4, text]})),
  116. %% Batch of rows
  117. ?assertEqual(
  118. ok,
  119. Module:copy_send_rows(
  120. C,
  121. [{60, <<"hello world">>},
  122. {61, null},
  123. {62, "line 62"}],
  124. 5000)),
  125. %% Single row
  126. ?assertEqual(
  127. ok,
  128. Module:copy_send_rows(
  129. C,
  130. [{63, <<"line 63">>}],
  131. 1000)),
  132. %% Rows as lists
  133. ?assertEqual(
  134. ok,
  135. Module:copy_send_rows(
  136. C,
  137. [
  138. [64, <<"line 64">>],
  139. [65, <<"line 65">>]
  140. ],
  141. infinity)),
  142. ?assertEqual({ok, 6}, Module:copy_done(C)),
  143. ?assertMatch(
  144. {ok, _, [{60, <<"hello world">>},
  145. {61, null},
  146. {62, <<"line 62">>},
  147. {63, <<"line 63">>},
  148. {64, <<"line 64">>},
  149. {65, <<"line 65">>}]},
  150. Module:equery(C,
  151. "SELECT id, value FROM test_table1"
  152. " WHERE id IN (60, 61, 62, 63, 64, 65) ORDER BY id"))
  153. end).
  154. %% @doc Tests that different IO-protocol APIs work
  155. from_stdin_io_apis(Config) ->
  156. Module = ?config(module, Config),
  157. epgsql_ct:with_connection(
  158. Config,
  159. fun(C) ->
  160. ?assertEqual(
  161. {ok, [text, text]},
  162. Module:copy_from_stdin(
  163. C, "COPY test_table1 (id, value) FROM STDIN WITH (FORMAT text)")),
  164. ?assertEqual(ok, io:format(C, "30\thello world\n", [])),
  165. ?assertEqual(ok, io:format(C, "~b\t~s\n", [31, "line 31"])),
  166. %% Output "32\thello\n" in multiple calls
  167. ?assertEqual(ok, io:write(C, 32)),
  168. ?assertEqual(ok, io:put_chars(C, "\t")),
  169. ?assertEqual(ok, io:write(C, hello)),
  170. ?assertEqual(ok, io:nl(C)),
  171. %% Using `file` API
  172. ?assertEqual(ok, file:write(C, "33\tline 33\n34\tline 34\n")),
  173. %% Binary
  174. ?assertEqual(ok, io:put_chars(C, <<"35\tline 35\n">>)),
  175. ?assertEqual(ok, file:write(C, <<"36\tline 36\n">>)),
  176. %% IoData
  177. ?assertEqual(ok, io:put_chars(C, [<<"37">>, $\t, <<"line 37">>, <<$\n>>])),
  178. ?assertEqual(ok, file:write(C, [["38", <<$\t>>], [<<"line 38">>, $\n]])),
  179. %% Raw IO-protocol message-passing
  180. Ref = erlang:make_ref(),
  181. C ! {io_request, self(), Ref, {put_chars, unicode, "39\tline 39\n"}},
  182. ?assertEqual(ok, receive {io_reply, Ref, Resp} -> Resp
  183. after 5000 ->
  184. timeout
  185. end),
  186. %% Not documented!
  187. ?assertEqual(ok, io:requests(
  188. C,
  189. [{put_chars, unicode, "40\tline 40\n"},
  190. {put_chars, latin1, "41\tline 41\n"},
  191. {format, "~w\t~s", [42, "line 42"]},
  192. nl])),
  193. ?assertEqual(
  194. {ok, 13},
  195. Module:copy_done(C)),
  196. ?assertMatch(
  197. {ok, _, [{30, <<"hello world">>},
  198. {31, <<"line 31">>},
  199. {32, <<"hello">>},
  200. {33, <<"line 33">>},
  201. {34, <<"line 34">>},
  202. {35, <<"line 35">>},
  203. {36, <<"line 36">>},
  204. {37, <<"line 37">>},
  205. {38, <<"line 38">>},
  206. {39, <<"line 39">>},
  207. {40, <<"line 40">>},
  208. {41, <<"line 41">>},
  209. {42, <<"line 42">>}
  210. ]},
  211. Module:equery(
  212. C,
  213. "SELECT id, value FROM test_table1"
  214. " WHERE id IN (30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42)"
  215. " ORDER BY id"))
  216. end).
  217. %% @doc Tests that "end-of-data" terminator is successfully ignored
  218. from_stdin_with_terminator(Config) ->
  219. Module = ?config(module, Config),
  220. epgsql_ct:with_connection(
  221. Config,
  222. fun(C) ->
  223. %% TEXT
  224. ?assertEqual(
  225. {ok, [text, text]},
  226. Module:copy_from_stdin(
  227. C, "COPY test_table1 (id, value) FROM STDIN WITH (FORMAT text)")),
  228. ?assertEqual(ok, io:put_chars(
  229. C,
  230. "50\tline 50\n"
  231. "51\tline 51\n"
  232. "\\.\n")),
  233. ?assertEqual({ok, 2}, Module:copy_done(C)),
  234. %% CSV
  235. ?assertEqual(
  236. {ok, [text, text]},
  237. Module:copy_from_stdin(
  238. C, "COPY test_table1 (id, value) FROM STDIN WITH (FORMAT csv)")),
  239. ?assertEqual(ok, io:put_chars(
  240. C,
  241. "52,line 52\n"
  242. "53,line 53\n"
  243. "\\.\n")),
  244. ?assertEqual({ok, 2}, Module:copy_done(C)),
  245. ?assertMatch(
  246. {ok, _, [{50, <<"line 50">>},
  247. {51, <<"line 51">>},
  248. {52, <<"line 52">>},
  249. {53, <<"line 53">>}
  250. ]},
  251. Module:equery(C,
  252. "SELECT id, value FROM test_table1"
  253. " WHERE id IN (50, 51, 52, 53) ORDER BY id"))
  254. end).
  255. from_stdin_corrupt_data(Config) ->
  256. Module = ?config(module, Config),
  257. epgsql_ct:with_connection(
  258. Config,
  259. fun(C) ->
  260. ?assertEqual(
  261. {ok, [text, text]},
  262. Module:copy_from_stdin(
  263. C, "COPY test_table1 (id, value) FROM STDIN WITH (FORMAT text)")),
  264. %% Wrong number of arguments to io:format
  265. Fmt = "~w\t~s\n",
  266. ?assertMatch({error, {fun_exception, {error, badarg, _Stack}}},
  267. io:request(C, {format, Fmt, []})),
  268. ?assertError(badarg, io:format(C, Fmt, [])),
  269. %% Wrong return value from IO function
  270. ?assertEqual({error, {fun_return_not_characters, node()}},
  271. io:request(C, {put_chars, unicode, erlang, node, []})),
  272. ?assertEqual({ok, 0}, Module:copy_done(C)),
  273. %%
  274. %% Corrupt text format
  275. ?assertEqual(
  276. {ok, [text, text]},
  277. Module:copy_from_stdin(
  278. C, "COPY test_table1 (id, value) FROM STDIN WITH (FORMAT text)")),
  279. ?assertEqual(ok, io:put_chars(
  280. C,
  281. "42\n43\nwasd\n")),
  282. ?assertMatch(
  283. #error{codename = bad_copy_file_format,
  284. severity = error},
  285. receive
  286. {epgsql, C, {error, Err}} ->
  287. Err
  288. after 5000 ->
  289. timeout
  290. end),
  291. ?assertEqual({error, not_in_copy_mode},
  292. io:request(C, {put_chars, unicode, "queque\n"})),
  293. ?assertError(badarg, io:format(C, "~w\n~s\n", [60, "wasd"])),
  294. %%
  295. %% Corrupt CSV format
  296. ?assertEqual(
  297. {ok, [text, text]},
  298. Module:copy_from_stdin(
  299. C, "COPY test_table1 (id, value) FROM STDIN WITH (FORMAT csv)")),
  300. ?assertEqual(ok, io:put_chars(
  301. C,
  302. "42\n43\nwasd\n")),
  303. ?assertMatch(
  304. #error{codename = bad_copy_file_format,
  305. severity = error},
  306. receive
  307. {epgsql, C, {error, Err}} ->
  308. Err
  309. after 5000 ->
  310. timeout
  311. end),
  312. %%
  313. %% Corrupt binary format
  314. ?assertEqual(
  315. {ok, [binary, binary]},
  316. Module:copy_from_stdin(
  317. C, "COPY test_table1 (id, value) FROM STDIN WITH (FORMAT binary)",
  318. {binary, [int4, text]})),
  319. ?assertEqual(
  320. ok,
  321. Module:copy_send_rows(C, [{44, <<"line 44">>}], 1000)),
  322. ?assertEqual(ok, io:put_chars(C, "45\tThis is not ok!\n")),
  323. ?assertMatch(
  324. #error{codename = bad_copy_file_format,
  325. severity = error},
  326. receive
  327. {epgsql, C, {error, Err}} ->
  328. Err
  329. after 5000 ->
  330. timeout
  331. end),
  332. %% Connection is still usable
  333. ?assertMatch(
  334. {ok, _, [{1}]},
  335. Module:equery(C, "SELECT 1", []))
  336. end).