epgsql_copy_SUITE.erl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. C ! {io_request, self(), ?FUNCTION_NAME, {put_chars, unicode, "39\tline 39\n"}},
  181. ?assertEqual(ok, receive {io_reply, ?FUNCTION_NAME, Resp} -> Resp
  182. after 5000 ->
  183. timeout
  184. end),
  185. %% Not documented!
  186. ?assertEqual(ok, io:requests(
  187. C,
  188. [{put_chars, unicode, "40\tline 40\n"},
  189. {put_chars, latin1, "41\tline 41\n"},
  190. {format, "~w\t~s", [42, "line 42"]},
  191. nl])),
  192. ?assertEqual(
  193. {ok, 13},
  194. Module:copy_done(C)),
  195. ?assertMatch(
  196. {ok, _, [{30, <<"hello world">>},
  197. {31, <<"line 31">>},
  198. {32, <<"hello">>},
  199. {33, <<"line 33">>},
  200. {34, <<"line 34">>},
  201. {35, <<"line 35">>},
  202. {36, <<"line 36">>},
  203. {37, <<"line 37">>},
  204. {38, <<"line 38">>},
  205. {39, <<"line 39">>},
  206. {40, <<"line 40">>},
  207. {41, <<"line 41">>},
  208. {42, <<"line 42">>}
  209. ]},
  210. Module:equery(
  211. C,
  212. "SELECT id, value FROM test_table1"
  213. " WHERE id IN (30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42)"
  214. " ORDER BY id"))
  215. end).
  216. %% @doc Tests that "end-of-data" terminator is successfully ignored
  217. from_stdin_with_terminator(Config) ->
  218. Module = ?config(module, Config),
  219. epgsql_ct:with_connection(
  220. Config,
  221. fun(C) ->
  222. %% TEXT
  223. ?assertEqual(
  224. {ok, [text, text]},
  225. Module:copy_from_stdin(
  226. C, "COPY test_table1 (id, value) FROM STDIN WITH (FORMAT text)")),
  227. ?assertEqual(ok, io:put_chars(
  228. C,
  229. "50\tline 50\n"
  230. "51\tline 51\n"
  231. "\\.\n")),
  232. ?assertEqual({ok, 2}, Module:copy_done(C)),
  233. %% CSV
  234. ?assertEqual(
  235. {ok, [text, text]},
  236. Module:copy_from_stdin(
  237. C, "COPY test_table1 (id, value) FROM STDIN WITH (FORMAT csv)")),
  238. ?assertEqual(ok, io:put_chars(
  239. C,
  240. "52,line 52\n"
  241. "53,line 53\n"
  242. "\\.\n")),
  243. ?assertEqual({ok, 2}, Module:copy_done(C)),
  244. ?assertMatch(
  245. {ok, _, [{50, <<"line 50">>},
  246. {51, <<"line 51">>},
  247. {52, <<"line 52">>},
  248. {53, <<"line 53">>}
  249. ]},
  250. Module:equery(C,
  251. "SELECT id, value FROM test_table1"
  252. " WHERE id IN (50, 51, 52, 53) ORDER BY id"))
  253. end).
  254. from_stdin_corrupt_data(Config) ->
  255. Module = ?config(module, Config),
  256. epgsql_ct:with_connection(
  257. Config,
  258. fun(C) ->
  259. ?assertEqual(
  260. {ok, [text, text]},
  261. Module:copy_from_stdin(
  262. C, "COPY test_table1 (id, value) FROM STDIN WITH (FORMAT text)")),
  263. %% Wrong number of arguments to io:format
  264. Fmt = "~w\t~s\n",
  265. ?assertMatch({error, {fun_exception, {error, badarg, _Stack}}},
  266. io:request(C, {format, Fmt, []})),
  267. ?assertError(badarg, io:format(C, Fmt, [])),
  268. %% Wrong return value from IO function
  269. ?assertEqual({error, {fun_return_not_characters, node()}},
  270. io:request(C, {put_chars, unicode, erlang, node, []})),
  271. ?assertEqual({ok, 0}, Module:copy_done(C)),
  272. %%
  273. %% Corrupt text format
  274. ?assertEqual(
  275. {ok, [text, text]},
  276. Module:copy_from_stdin(
  277. C, "COPY test_table1 (id, value) FROM STDIN WITH (FORMAT text)")),
  278. ?assertEqual(ok, io:put_chars(
  279. C,
  280. "42\n43\nwasd\n")),
  281. ?assertMatch(
  282. #error{codename = bad_copy_file_format,
  283. severity = error},
  284. receive
  285. {epgsql, C, {error, Err}} ->
  286. Err
  287. after 5000 ->
  288. timeout
  289. end),
  290. ?assertEqual({error, not_in_copy_mode},
  291. io:request(C, {put_chars, unicode, "queque\n"})),
  292. ?assertError(badarg, io:format(C, "~w\n~s\n", [60, "wasd"])),
  293. %%
  294. %% Corrupt CSV format
  295. ?assertEqual(
  296. {ok, [text, text]},
  297. Module:copy_from_stdin(
  298. C, "COPY test_table1 (id, value) FROM STDIN WITH (FORMAT csv)")),
  299. ?assertEqual(ok, io:put_chars(
  300. C,
  301. "42\n43\nwasd\n")),
  302. ?assertMatch(
  303. #error{codename = bad_copy_file_format,
  304. severity = error},
  305. receive
  306. {epgsql, C, {error, Err}} ->
  307. Err
  308. after 5000 ->
  309. timeout
  310. end),
  311. %%
  312. %% Corrupt binary format
  313. ?assertEqual(
  314. {ok, [binary, binary]},
  315. Module:copy_from_stdin(
  316. C, "COPY test_table1 (id, value) FROM STDIN WITH (FORMAT binary)",
  317. {binary, [int4, text]})),
  318. ?assertEqual(
  319. ok,
  320. Module:copy_send_rows(C, [{44, <<"line 44">>}], 1000)),
  321. ?assertEqual(ok, io:put_chars(C, "45\tThis is not ok!\n")),
  322. ?assertMatch(
  323. #error{codename = bad_copy_file_format,
  324. severity = error},
  325. receive
  326. {epgsql, C, {error, Err}} ->
  327. Err
  328. after 5000 ->
  329. timeout
  330. end),
  331. %% Connection is still usable
  332. ?assertMatch(
  333. {ok, _, [{1}]},
  334. Module:equery(C, "SELECT 1", []))
  335. end).