epgsql_tests.erl 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. -module(epgsql_tests).
  2. -export([run_tests/0]).
  3. -compile([export_all]).
  4. -include_lib("eunit/include/eunit.hrl").
  5. -include_lib("public_key/include/public_key.hrl").
  6. -include("epgsql.hrl").
  7. -define(host, "localhost").
  8. -define(port, 10432).
  9. -define(ssl_apps, [crypto, asn1, public_key, ssl]).
  10. -define(UUID1,
  11. <<163,189,240,40,149,151,17,227,141,6,112,24,139,130,16,73>>).
  12. -define(UUID2,
  13. <<183,55,22,52,149,151,17,227,187,167,112,24,139,130,16,73>>).
  14. -define(UUID3,
  15. <<198,188,155,66,149,151,17,227,138,98,112,24,139,130,16,73>>).
  16. -define(TIMEOUT_ERROR, {error, #error{
  17. severity = error,
  18. code = <<"57014">>,
  19. codename = query_canceled,
  20. message = <<"canceling statement due to statement timeout">>,
  21. extra = []
  22. }}).
  23. %% From uuid.erl in http://gitorious.org/avtobiff/erlang-uuid
  24. uuid_to_string(<<U0:32, U1:16, U2:16, U3:16, U4:48>>) ->
  25. lists:flatten(io_lib:format(
  26. "~8.16.0b-~4.16.0b-~4.16.0b-~4.16.0b-~12.16.0b",
  27. [U0, U1, U2, U3, U4])).
  28. connect_test(Module) ->
  29. connect_only(Module, []).
  30. connect_to_db_test(Module) ->
  31. connect_only(Module, [{database, "epgsql_test_db1"}]).
  32. connect_as_test(Module) ->
  33. connect_only(Module, ["epgsql_test", [{database, "epgsql_test_db1"}]]).
  34. connect_with_cleartext_test(Module) ->
  35. connect_only(Module, ["epgsql_test_cleartext",
  36. "epgsql_test_cleartext",
  37. [{database, "epgsql_test_db1"}]]).
  38. connect_with_md5_test(Module) ->
  39. connect_only(Module, ["epgsql_test_md5",
  40. "epgsql_test_md5",
  41. [{database, "epgsql_test_db1"}]]).
  42. connect_with_invalid_user_test(Module) ->
  43. {error, Why} =
  44. Module:connect(?host,
  45. "epgsql_test_invalid",
  46. "epgsql_test_invalid",
  47. [{port, ?port}, {database, "epgsql_test_db1"}]),
  48. case Why of
  49. invalid_authorization_specification -> ok; % =< 8.4
  50. invalid_password -> ok % >= 9.0
  51. end.
  52. connect_with_invalid_password_test(Module) ->
  53. {error, Why} =
  54. Module:connect(?host,
  55. "epgsql_test_md5",
  56. "epgsql_test_invalid",
  57. [{port, ?port}, {database, "epgsql_test_db1"}]),
  58. case Why of
  59. invalid_authorization_specification -> ok; % =< 8.4
  60. invalid_password -> ok % >= 9.0
  61. end.
  62. connect_with_ssl_test(Module) ->
  63. lists:foreach(fun application:start/1, ?ssl_apps),
  64. with_connection(
  65. Module,
  66. fun(C) ->
  67. {ok, _Cols, [{true}]} = Module:equery(C, "select ssl_is_used()")
  68. end,
  69. "epgsql_test",
  70. [{ssl, true}]).
  71. connect_with_client_cert_test(Module) ->
  72. lists:foreach(fun application:start/1, ?ssl_apps),
  73. Dir = filename:join(code:lib_dir(epgsql), "test/data"),
  74. File = fun(Name) -> filename:join(Dir, Name) end,
  75. {ok, Pem} = file:read_file(File("epgsql.crt")),
  76. [{'Certificate', Der, not_encrypted}] = public_key:pem_decode(Pem),
  77. Cert = public_key:pkix_decode_cert(Der, plain),
  78. #'TBSCertificate'{serialNumber = Serial} = Cert#'Certificate'.tbsCertificate,
  79. Serial2 = list_to_binary(integer_to_list(Serial)),
  80. with_connection(
  81. Module,
  82. fun(C) ->
  83. {ok, _, [{true}]} = Module:equery(C, "select ssl_is_used()"),
  84. {ok, _, [{Serial2}]} = Module:equery(C, "select ssl_client_serial()")
  85. end,
  86. "epgsql_test_cert",
  87. [{ssl, true}, {keyfile, File("epgsql.key")}, {certfile, File("epgsql.crt")}]).
  88. -ifdef(have_maps).
  89. connect_map_test(Module) ->
  90. Opts = #{host => ?host,
  91. port => ?port,
  92. database => "epgsql_test_db1",
  93. username => "epgsql_test_md5",
  94. password => "epgsql_test_md5"
  95. },
  96. {ok, C} = Module:connect(Opts),
  97. Module:close(C),
  98. flush(),
  99. ok.
  100. -endif.
  101. prepared_query_test(Module) ->
  102. with_connection(
  103. Module,
  104. fun(C) ->
  105. {ok, _} = Module:parse(C, "inc", "select $1+1", []),
  106. {ok, Cols, [{5}]} = Module:prepared_query(C, "inc", [4]),
  107. {ok, Cols, [{2}]} = Module:prepared_query(C, "inc", [1]),
  108. {ok, Cols, [{23}]} = Module:prepared_query(C, "inc", [22]),
  109. {error, _} = Module:prepared_query(C, "non_existent_query", [4])
  110. end).
  111. select_test(Module) ->
  112. with_connection(
  113. Module,
  114. fun(C) ->
  115. {ok, Cols, Rows} = Module:squery(C, "select * from test_table1"),
  116. [#column{name = <<"id">>, type = int4, size = 4},
  117. #column{name = <<"value">>, type = text, size = -1}] = Cols,
  118. [{<<"1">>, <<"one">>}, {<<"2">>, <<"two">>}] = Rows
  119. end).
  120. insert_test(Module) ->
  121. with_rollback(
  122. Module,
  123. fun(C) ->
  124. {ok, 1} = Module:squery(C, "insert into test_table1 (id, value) values (3, 'three')")
  125. end).
  126. update_test(Module) ->
  127. with_rollback(
  128. Module,
  129. fun(C) ->
  130. {ok, 1} = Module:squery(C, "insert into test_table1 (id, value) values (3, 'three')"),
  131. {ok, 1} = Module:squery(C, "insert into test_table1 (id, value) values (4, 'four')"),
  132. {ok, 2} = Module:squery(C, "update test_table1 set value = 'foo' where id > 2"),
  133. {ok, _, [{<<"2">>}]} = Module:squery(C, "select count(*) from test_table1 where value = 'foo'")
  134. end).
  135. delete_test(Module) ->
  136. with_rollback(
  137. Module,
  138. fun(C) ->
  139. {ok, 1} = Module:squery(C, "insert into test_table1 (id, value) values (3, 'three')"),
  140. {ok, 1} = Module:squery(C, "insert into test_table1 (id, value) values (4, 'four')"),
  141. {ok, 2} = Module:squery(C, "delete from test_table1 where id > 2"),
  142. {ok, _, [{<<"2">>}]} = Module:squery(C, "select count(*) from test_table1")
  143. end).
  144. create_and_drop_table_test(Module) ->
  145. with_rollback(
  146. Module,
  147. fun(C) ->
  148. {ok, [], []} = Module:squery(C, "create table test_table3 (id int4)"),
  149. {ok, [#column{type = int4}], []} = Module:squery(C, "select * from test_table3"),
  150. {ok, [], []} = Module:squery(C, "drop table test_table3")
  151. end).
  152. cursor_test(Module) ->
  153. with_connection(
  154. Module,
  155. fun(C) ->
  156. {ok, [], []} = Module:squery(C, "begin"),
  157. {ok, [], []} = Module:squery(C, "declare c cursor for select id from test_table1"),
  158. {ok, 2} = Module:squery(C, "move forward 2 from c"),
  159. {ok, 1} = Module:squery(C, "move backward 1 from c"),
  160. {ok, 1, _Cols, [{<<"2">>}]} = Module:squery(C, "fetch next from c"),
  161. {ok, [], []} = Module:squery(C, "close c")
  162. end).
  163. multiple_result_test(Module) ->
  164. with_connection(
  165. Module,
  166. fun(C) ->
  167. [{ok, _, [{<<"1">>}]}, {ok, _, [{<<"2">>}]}] = Module:squery(C, "select 1; select 2"),
  168. [{ok, _, [{<<"1">>}]}, {error, #error{}}] = Module:squery(C, "select 1; select foo;")
  169. end).
  170. execute_batch_test(Module) ->
  171. with_connection(
  172. Module,
  173. fun(C) ->
  174. {ok, S1} = Module:parse(C, "one", "select $1", [int4]),
  175. {ok, S2} = Module:parse(C, "two", "select $1 + $2", [int4, int4]),
  176. [{ok, [{1}]}, {ok, [{3}]}] =
  177. Module:execute_batch(C, [{S1, [1]}, {S2, [1, 2]}])
  178. end).
  179. batch_error_test(Module) ->
  180. with_rollback(
  181. Module,
  182. fun(C) ->
  183. {ok, S} = Module:parse(C, "insert into test_table1(id, value) values($1, $2)"),
  184. [{ok, 1}, {error, _}] =
  185. Module:execute_batch(
  186. C,
  187. [{S, [3, "batch_error 3"]},
  188. {S, [2, "batch_error 2"]}, % duplicate key error
  189. {S, [5, "batch_error 5"]} % won't be executed
  190. ])
  191. end).
  192. single_batch_test(Module) ->
  193. with_connection(
  194. Module,
  195. fun(C) ->
  196. {ok, S1} = Module:parse(C, "one", "select $1", [int4]),
  197. [{ok, [{1}]}] = Module:execute_batch(C, [{S1, [1]}])
  198. end).
  199. extended_select_test(Module) ->
  200. with_connection(
  201. Module,
  202. fun(C) ->
  203. {ok, Cols, Rows} = Module:equery(C, "select * from test_table1", []),
  204. [#column{name = <<"id">>, type = int4, size = 4},
  205. #column{name = <<"value">>, type = text, size = -1}] = Cols,
  206. [{1, <<"one">>}, {2, <<"two">>}] = Rows
  207. end).
  208. extended_sync_ok_test(Module) ->
  209. with_connection(
  210. Module,
  211. fun(C) ->
  212. {ok, _Cols, [{<<"one">>}]} = Module:equery(C, "select value from test_table1 where id = $1", [1]),
  213. {ok, _Cols, [{<<"two">>}]} = Module:equery(C, "select value from test_table1 where id = $1", [2])
  214. end).
  215. extended_sync_error_test(Module) ->
  216. with_connection(
  217. Module,
  218. fun(C) ->
  219. {error, #error{}} = Module:equery(C, "select _alue from test_table1 where id = $1", [1]),
  220. {ok, _Cols, [{<<"one">>}]} = Module:equery(C, "select value from test_table1 where id = $1", [1])
  221. end).
  222. returning_from_insert_test(Module) ->
  223. with_rollback(
  224. Module,
  225. fun(C) ->
  226. {ok, 1, _Cols, [{3}]} = Module:equery(C, "insert into test_table1 (id) values (3) returning id")
  227. end).
  228. returning_from_update_test(Module) ->
  229. with_rollback(
  230. Module,
  231. fun(C) ->
  232. {ok, 2, _Cols, [{1}, {2}]} = Module:equery(C, "update test_table1 set value = 'hi' returning id")
  233. end).
  234. returning_from_delete_test(Module) ->
  235. with_rollback(
  236. Module,
  237. fun(C) ->
  238. {ok, 2, _Cols, [{1}, {2}]} = Module:equery(C, "delete from test_table1 returning id")
  239. end).
  240. parse_test(Module) ->
  241. with_connection(
  242. Module,
  243. fun(C) ->
  244. {ok, S} = Module:parse(C, "select * from test_table1"),
  245. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  246. ok = Module:close(C, S),
  247. ok = Module:sync(C)
  248. end).
  249. parse_column_format_test(Module) ->
  250. with_connection(
  251. Module,
  252. fun(C) ->
  253. {ok, S} = Module:parse(C, "select 1::int4, false::bool, 2.0::float4"),
  254. [#column{type = int4},
  255. #column{type = bool},
  256. #column{type = float4}] = S#statement.columns,
  257. ok = Module:bind(C, S, []),
  258. {ok, [{1, false, 2.0}]} = Module:execute(C, S, 0),
  259. ok = Module:close(C, S),
  260. ok = Module:sync(C)
  261. end).
  262. parse_error_test(Module) ->
  263. with_connection(
  264. Module,
  265. fun(C) ->
  266. {error, #error{}} = Module:parse(C, "select _ from test_table1"),
  267. {ok, S} = Module:parse(C, "select * from test_table1"),
  268. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  269. ok = Module:close(C, S),
  270. ok = Module:sync(C)
  271. end).
  272. parse_and_close_test(Module) ->
  273. with_connection(
  274. Module,
  275. fun(C) ->
  276. Parse = fun() -> Module:parse(C, "test", "select * from test_table1", []) end,
  277. {ok, S} = Parse(),
  278. {error, #error{code = <<"42P05">>, codename = duplicate_prepared_statement}} = Parse(),
  279. Module:close(C, S),
  280. {ok, S} = Parse(),
  281. ok = Module:sync(C)
  282. end).
  283. bind_test(Module) ->
  284. with_connection(
  285. Module,
  286. fun(C) ->
  287. {ok, S} = Module:parse(C, "select value from test_table1 where id = $1"),
  288. ok = Module:bind(C, S, [1]),
  289. ok = Module:close(C, S),
  290. ok = Module:sync(C)
  291. end).
  292. bind_parameter_format_test(Module) ->
  293. with_connection(
  294. Module,
  295. fun(C) ->
  296. {ok, S} = Module:parse(C, "select $1, $2, $3", [int2, text, bool]),
  297. [int2, text, bool] = S#statement.types,
  298. ok = Module:bind(C, S, [1, "hi", true]),
  299. {ok, [{1, <<"hi">>, true}]} = Module:execute(C, S, 0),
  300. ok = Module:close(C, S),
  301. ok = Module:sync(C)
  302. end).
  303. bind_error_test(Module) ->
  304. with_connection(
  305. Module,
  306. fun(C) ->
  307. {ok, S} = Module:parse(C, "select $1::char"),
  308. {error, #error{}} = Module:bind(C, S, [0]),
  309. ok = Module:bind(C, S, [$A]),
  310. ok = Module:close(C, S),
  311. ok = Module:sync(C)
  312. end).
  313. bind_and_close_test(Module) ->
  314. with_connection(
  315. Module,
  316. fun(C) ->
  317. {ok, S} = Module:parse(C, "select * from test_table1"),
  318. ok = Module:bind(C, S, "one", []),
  319. {error, #error{code = <<"42P03">>, codename = duplicate_cursor}} = Module:bind(C, S, "one", []),
  320. ok = Module:close(C, portal, "one"),
  321. ok = Module:bind(C, S, "one", []),
  322. ok = Module:sync(C)
  323. end).
  324. execute_error_test(Module) ->
  325. with_connection(
  326. Module,
  327. fun(C) ->
  328. {ok, S} = Module:parse(C, "insert into test_table1 (id, value) values ($1, $2)"),
  329. ok = Module:bind(C, S, [1, <<"foo">>]),
  330. {error, #error{code = <<"23505">>, codename = unique_violation}} = Module:execute(C, S, 0),
  331. {error, sync_required} = Module:bind(C, S, [3, <<"quux">>]),
  332. ok = Module:sync(C),
  333. ok = Module:bind(C, S, [3, <<"quux">>]),
  334. {ok, _} = Module:execute(C, S, 0),
  335. {ok, 1} = Module:squery(C, "delete from test_table1 where id = 3")
  336. end).
  337. describe_test(Module) ->
  338. with_connection(
  339. Module,
  340. fun(C) ->
  341. {ok, S} = Module:parse(C, "select * from test_table1"),
  342. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  343. {ok, S} = Module:describe(C, S),
  344. ok = Module:close(C, S),
  345. ok = Module:sync(C)
  346. end).
  347. describe_with_param_test(Module) ->
  348. with_connection(
  349. Module,
  350. fun(C) ->
  351. {ok, S} = Module:parse(C, "select id from test_table1 where id = $1"),
  352. [int4] = S#statement.types,
  353. [#column{name = <<"id">>}] = S#statement.columns,
  354. {ok, S} = Module:describe(C, S),
  355. ok = Module:close(C, S),
  356. ok = Module:sync(C)
  357. end).
  358. describe_named_test(Module) ->
  359. with_connection(
  360. Module,
  361. fun(C) ->
  362. {ok, S} = Module:parse(C, "name", "select * from test_table1", []),
  363. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  364. {ok, S} = Module:describe(C, S),
  365. ok = Module:close(C, S),
  366. ok = Module:sync(C)
  367. end).
  368. describe_error_test(Module) ->
  369. with_connection(
  370. Module,
  371. fun(C) ->
  372. {error, #error{}} = Module:describe(C, statement, ""),
  373. {ok, S} = Module:parse(C, "select * from test_table1"),
  374. {ok, S} = Module:describe(C, statement, ""),
  375. ok = Module:sync(C)
  376. end).
  377. portal_test(Module) ->
  378. with_connection(
  379. Module,
  380. fun(C) ->
  381. {ok, S} = Module:parse(C, "select value from test_table1"),
  382. ok = Module:bind(C, S, []),
  383. {partial, [{<<"one">>}]} = Module:execute(C, S, 1),
  384. {partial, [{<<"two">>}]} = Module:execute(C, S, 1),
  385. {ok, []} = Module:execute(C, S,1),
  386. ok = Module:close(C, S),
  387. ok = Module:sync(C)
  388. end).
  389. returning_test(Module) ->
  390. with_rollback(
  391. Module,
  392. fun(C) ->
  393. {ok, S} = Module:parse(C, "update test_table1 set value = $1 returning id"),
  394. ok = Module:bind(C, S, ["foo"]),
  395. {ok, 2, [{1}, {2}]} = Module:execute(C, S),
  396. ok = Module:sync(C)
  397. end).
  398. multiple_statement_test(Module) ->
  399. with_connection(
  400. Module,
  401. fun(C) ->
  402. {ok, S1} = Module:parse(C, "one", "select value from test_table1 where id = 1", []),
  403. ok = Module:bind(C, S1, []),
  404. {partial, [{<<"one">>}]} = Module:execute(C, S1, 1),
  405. {ok, S2} = Module:parse(C, "two", "select value from test_table1 where id = 2", []),
  406. ok = Module:bind(C, S2, []),
  407. {partial, [{<<"two">>}]} = Module:execute(C, S2, 1),
  408. {ok, []} = Module:execute(C, S1, 1),
  409. {ok, []} = Module:execute(C, S2, 1),
  410. ok = Module:close(C, S1),
  411. ok = Module:close(C, S2),
  412. ok = Module:sync(C)
  413. end).
  414. multiple_portal_test(Module) ->
  415. with_connection(
  416. Module,
  417. fun(C) ->
  418. {ok, S} = Module:parse(C, "select value from test_table1 where id = $1"),
  419. ok = Module:bind(C, S, "one", [1]),
  420. ok = Module:bind(C, S, "two", [2]),
  421. {ok, [{<<"one">>}]} = Module:execute(C, S, "one", 0),
  422. {ok, [{<<"two">>}]} = Module:execute(C, S, "two", 0),
  423. ok = Module:close(C, S),
  424. ok = Module:sync(C)
  425. end).
  426. execute_function_test(Module) ->
  427. with_rollback(
  428. Module,
  429. fun(C) ->
  430. {ok, _Cols1, [{3}]} = Module:equery(C, "select insert_test1(3, 'three')"),
  431. {ok, _Cols2, [{<<>>}]} = Module:equery(C, "select do_nothing()")
  432. end).
  433. parameter_get_test(Module) ->
  434. with_connection(
  435. Module,
  436. fun(C) ->
  437. {ok, <<"off">>} = Module:get_parameter(C, "is_superuser")
  438. end).
  439. parameter_set_test(Module) ->
  440. with_connection(
  441. Module,
  442. fun(C) ->
  443. {ok, [], []} = Module:squery(C, "set DateStyle = 'ISO, MDY'"),
  444. {ok, <<"ISO, MDY">>} = Module:get_parameter(C, "DateStyle"),
  445. {ok, _Cols, [{<<"2000-01-02">>}]} = Module:squery(C, "select '2000-01-02'::date"),
  446. {ok, [], []} = Module:squery(C, "set DateStyle = 'German'"),
  447. {ok, <<"German, DMY">>} = Module:get_parameter(C, "DateStyle"),
  448. {ok, _Cols, [{<<"02.01.2000">>}]} = Module:squery(C, "select '2000-01-02'::date")
  449. end).
  450. numeric_type_test(Module) ->
  451. check_type(Module, int2, "1", 1, [0, 256, -32768, +32767]),
  452. check_type(Module, int4, "1", 1, [0, 512, -2147483648, +2147483647]),
  453. check_type(Module, int8, "1", 1, [0, 1024, -9223372036854775808, +9223372036854775807]),
  454. check_type(Module, float4, "1.0", 1.0, [0.0, 1.23456, -1.23456]),
  455. check_type(Module, float8, "1.0", 1.0, [0.0, 1.23456789012345, -1.23456789012345]).
  456. character_type_test(Module) ->
  457. Alpha = unicode:characters_to_binary([16#03B1]),
  458. Ka = unicode:characters_to_binary([16#304B]),
  459. One = unicode:characters_to_binary([16#10D360]),
  460. check_type(Module, bpchar, "'A'", $A, [1, $1, 16#7F, Alpha, Ka, One], "c_char"),
  461. check_type(Module, text, "'hi'", <<"hi">>, [<<"">>, <<"hi">>]),
  462. check_type(Module, varchar, "'hi'", <<"hi">>, [<<"">>, <<"hi">>]).
  463. uuid_type_test(Module) ->
  464. check_type(Module, uuid,
  465. io_lib:format("'~s'", [uuid_to_string(?UUID1)]),
  466. list_to_binary(uuid_to_string(?UUID1)), []).
  467. point_type_test(Module) ->
  468. check_type(Module, point, "'(23.15, 100)'", {23.15, 100.0}, []).
  469. geometry_type_test(Module) ->
  470. check_type(Module, geometry, "'COMPOUNDCURVE(CIRCULARSTRING(0 0,1 1,1 0),(1 0,0 1))'",
  471. {compound_curve,'2d',
  472. [{circular_string,'2d',
  473. [{point,'2d',0.0,0.0,undefined,undefined},
  474. {point,'2d',1.0,1.0,undefined,undefined},
  475. {point,'2d',1.0,0.0,undefined,undefined}]},
  476. {line_string,'2d',
  477. [{point,'2d',1.0,0.0,undefined,undefined},
  478. {point,'2d',0.0,1.0,undefined,undefined}]}]},
  479. []).
  480. uuid_select_test(Module) ->
  481. with_rollback(
  482. Module,
  483. fun(C) ->
  484. U1 = uuid_to_string(?UUID1),
  485. U2 = uuid_to_string(?UUID2),
  486. U3 = uuid_to_string(?UUID3),
  487. {ok, 1} =
  488. Module:equery(C, "insert into test_table2 (c_varchar, c_uuid) values ('UUID1', $1)",
  489. [U1]),
  490. {ok, 1} =
  491. Module:equery(C, "insert into test_table2 (c_varchar, c_uuid) values ('UUID2', $1)",
  492. [U2]),
  493. {ok, 1} =
  494. Module:equery(C, "insert into test_table2 (c_varchar, c_uuid) values ('UUID3', $1)",
  495. [U3]),
  496. Res = Module:equery(C, "select c_varchar, c_uuid from test_table2 where c_uuid = any($1)",
  497. [[U1, U2]]),
  498. U1Bin = list_to_binary(U1),
  499. U2Bin = list_to_binary(U2),
  500. {ok,[{column,<<"c_varchar">>,varchar,_,_,_},
  501. {column,<<"c_uuid">>,uuid,_,_,_}],
  502. [{<<"UUID1">>, U1Bin},
  503. {<<"UUID2">>, U2Bin}]} = Res
  504. end).
  505. date_time_type_test(Module) ->
  506. with_connection(
  507. Module,
  508. fun(C) ->
  509. case Module:get_parameter(C, "integer_datetimes") of
  510. {ok, <<"on">>} -> MaxTsDate = 294276;
  511. {ok, <<"off">>} -> MaxTsDate = 5874897
  512. end,
  513. check_type(Module, date, "'2008-01-02'", {2008,1,2}, [{-4712,1,1}, {5874897,1,1}]),
  514. check_type(Module, time, "'00:01:02'", {0,1,2.0}, [{0,0,0.0}, {24,0,0.0}]),
  515. check_type(Module, timetz, "'00:01:02-01'", {{0,1,2.0},1*60*60},
  516. [{{0,0,0.0},0}, {{24,0,0.0},-13*60*60}]),
  517. check_type(Module, timestamp, "'2008-01-02 03:04:05'", {{2008,1,2},{3,4,5.0}},
  518. [{{-4712,1,1},{0,0,0.0}}, {{MaxTsDate,12,31}, {23,59,59.0}}, {1322,334285,440966}]),
  519. check_type(Module, timestamptz, "'2011-01-02 03:04:05+3'", {{2011, 1, 2}, {0, 4, 5.0}}, [{1324,875970,286983}]),
  520. check_type(Module, interval, "'1 hour 2 minutes 3.1 seconds'", {{1,2,3.1},0,0},
  521. [{{0,0,0.0},0,-178000000 * 12}, {{0,0,0.0},0,178000000 * 12}])
  522. end).
  523. json_type_test(Module) ->
  524. with_connection(
  525. Module,
  526. fun(C) ->
  527. check_type(Module, json, "'{}'", <<"{}">>,
  528. [<<"{}">>, <<"[]">>, <<"1">>, <<"1.0">>, <<"true">>, <<"\"string\"">>, <<"{\"key\": []}">>]),
  529. check_type(Module, jsonb, "'{}'", <<"{}">>,
  530. [<<"{}">>, <<"[]">>, <<"1">>, <<"1.0">>, <<"true">>, <<"\"string\"">>, <<"{\"key\": []}">>])
  531. end
  532. ).
  533. misc_type_test(Module) ->
  534. check_type(Module, bool, "true", true, [true, false]),
  535. check_type(Module, bytea, "E'\001\002'", <<1,2>>, [<<>>, <<0,128,255>>]).
  536. hstore_type_test(Module) ->
  537. Values = [
  538. {[]},
  539. {[{null, null}]},
  540. {[{null, undefined}]},
  541. {[{1, null}]},
  542. {[{1.0, null}]},
  543. {[{1, undefined}]},
  544. {[{1.0, undefined}]},
  545. {[{<<"a">>, <<"c">>}, {<<"c">>, <<"d">>}]},
  546. {[{<<"a">>, <<"c">>}, {<<"c">>, null}]},
  547. {[{<<"a">>, <<"c">>}, {<<"c">>, undefined}]}
  548. ],
  549. with_connection(
  550. Module,
  551. fun(_C) ->
  552. check_type(Module, hstore, "''", {[]}, []),
  553. check_type(Module, hstore,
  554. "'a => 1, b => 2.0, c => null'",
  555. {[{<<"c">>, null}, {<<"b">>, <<"2.0">>}, {<<"a">>, <<"1">>}]}, Values)
  556. end).
  557. net_type_test(Module) ->
  558. check_type(Module, cidr, "'127.0.0.1/32'", {{127,0,0,1}, 32}, [{{127,0,0,1}, 32}, {{0,0,0,0,0,0,0,1}, 128}]),
  559. check_type(Module, inet, "'127.0.0.1'", {127,0,0,1}, [{127,0,0,1}, {0,0,0,0,0,0,0,1}]).
  560. array_type_test(Module) ->
  561. with_connection(
  562. Module,
  563. fun(C) ->
  564. {ok, _, [{[1, 2]}]} = Module:equery(C, "select ($1::int[])[1:2]", [[1, 2, 3]]),
  565. {ok, _, [{[{1, <<"one">>}, {2, <<"two">>}]}]} =
  566. Module:equery(C, "select Array(select (id, value) from test_table1)", []),
  567. Select = fun(Type, A) ->
  568. Query = "select $1::" ++ atom_to_list(Type) ++ "[]",
  569. {ok, _Cols, [{A2}]} = Module:equery(C, Query, [A]),
  570. case lists:all(fun({V, V2}) -> compare(Type, V, V2) end, lists:zip(A, A2)) of
  571. true -> ok;
  572. false -> ?assertMatch(A, A2)
  573. end
  574. end,
  575. Select(int2, []),
  576. Select(int2, [1, 2, 3, 4]),
  577. Select(int2, [[1], [2], [3], [4]]),
  578. Select(int2, [[[[[[1, 2]]]]]]),
  579. Select(bool, [true]),
  580. Select(char, [$a, $b, $c]),
  581. Select(int4, [[1, 2]]),
  582. Select(int8, [[[[1, 2]], [[3, 4]]]]),
  583. Select(text, [<<"one">>, <<"two>">>]),
  584. Select(varchar, [<<"one">>, <<"two>">>]),
  585. Select(float4, [0.0, 1.0, 0.123]),
  586. Select(float8, [0.0, 1.0, 0.123]),
  587. Select(date, [{2008,1,2}, {2008,1,3}]),
  588. Select(time, [{0,1,2.0}, {0,1,3.0}]),
  589. Select(timetz, [{{0,1,2.0},1*60*60}, {{0,1,3.0},1*60*60}]),
  590. Select(timestamp, [{{2008,1,2},{3,4,5.0}}, {{2008,1,2},{3,4,6.0}}]),
  591. Select(timestamptz, [{{2008,1,2},{3,4,5.0}}, {{2008,1,2},{3,4,6.0}}]),
  592. Select(interval, [{{1,2,3.1},0,0}, {{1,2,3.2},0,0}]),
  593. Select(hstore, [{[{null, null}, {a, 1}, {1, 2}, {b, undefined}]}]),
  594. Select(hstore, [[{[{null, null}, {a, 1}, {1, 2}, {b, undefined}]}, {[]}], [{[{a, 1}]}, {[{null, 2}]}]]),
  595. Select(cidr, [{{127,0,0,1}, 32}, {{0,0,0,0,0,0,0,1}, 128}]),
  596. Select(inet, [{127,0,0,1}, {0,0,0,0,0,0,0,1}]),
  597. Select(json, [<<"{}">>, <<"[]">>, <<"1">>, <<"1.0">>, <<"true">>, <<"\"string\"">>, <<"{\"key\": []}">>]),
  598. Select(jsonb, [<<"{}">>, <<"[]">>, <<"1">>, <<"1.0">>, <<"true">>, <<"\"string\"">>, <<"{\"key\": []}">>])
  599. end).
  600. custom_types_test(Module) ->
  601. with_connection(
  602. Module,
  603. fun(C) ->
  604. Module:squery(C, "drop table if exists t_foo;"),
  605. Module:squery(C, "drop type foo;"),
  606. {ok, [], []} = Module:squery(C, "create type foo as enum('foo', 'bar');"),
  607. ok = epgsql:update_type_cache(C, [<<"foo">>]),
  608. {ok, [], []} = Module:squery(C, "create table t_foo (col foo);"),
  609. {ok, S} = Module:parse(C, "insert_foo", "insert into t_foo values ($1)", [foo]),
  610. ok = Module:bind(C, S, ["bar"]),
  611. {ok, 1} = Module:execute(C, S)
  612. end).
  613. text_format_test(Module) ->
  614. with_connection(
  615. Module,
  616. fun(C) ->
  617. Select = fun(Type, V) ->
  618. V2 = list_to_binary(V),
  619. Query = "select $1::" ++ Type,
  620. {ok, _Cols, [{V2}]} = Module:equery(C, Query, [V]),
  621. {ok, _Cols, [{V2}]} = Module:equery(C, Query, [V2])
  622. end,
  623. Select("numeric", "123456")
  624. end).
  625. query_timeout_test(Module) ->
  626. with_connection(
  627. Module,
  628. fun(C) ->
  629. {ok, _, _} = Module:squery(C, "SET statement_timeout = 500"),
  630. ?TIMEOUT_ERROR = Module:squery(C, "SELECT pg_sleep(1)"),
  631. ?TIMEOUT_ERROR = Module:equery(C, "SELECT pg_sleep(2)"),
  632. {ok, _Cols, [{1}]} = Module:equery(C, "SELECT 1")
  633. end,
  634. []).
  635. execute_timeout_test(Module) ->
  636. with_connection(
  637. Module,
  638. fun(C) ->
  639. {ok, _, _} = Module:squery(C, "SET statement_timeout = 500"),
  640. {ok, S} = Module:parse(C, "select pg_sleep($1)"),
  641. ok = Module:bind(C, S, [2]),
  642. ?TIMEOUT_ERROR = Module:execute(C, S, 0),
  643. ok = Module:sync(C),
  644. ok = Module:bind(C, S, [0]),
  645. {ok, [{<<>>}]} = Module:execute(C, S, 0),
  646. ok = Module:close(C, S),
  647. ok = Module:sync(C)
  648. end,
  649. []).
  650. connection_closed_test(Module) ->
  651. P = self(),
  652. F = fun() ->
  653. process_flag(trap_exit, true),
  654. {ok, C} = Module:connect(?host, "epgsql_test",
  655. [{port, ?port}, {database, "epgsql_test_db1"}]),
  656. P ! {connected, C},
  657. receive
  658. Any -> P ! Any
  659. end
  660. end,
  661. spawn_link(F),
  662. receive
  663. {connected, C} ->
  664. timer:sleep(100),
  665. Module:close(C),
  666. {'EXIT', C, _} = receive R -> R end
  667. end,
  668. flush().
  669. connection_closed_by_server_test(Module) ->
  670. with_connection(Module,
  671. fun(C1) ->
  672. P = self(),
  673. spawn_link(fun() ->
  674. process_flag(trap_exit, true),
  675. with_connection(Module,
  676. fun(C2) ->
  677. {ok, _, [{Pid}]} = Module:equery(C2, "select pg_backend_pid()"),
  678. % emulate of disconnection
  679. {ok, _, [{true}]} = Module:equery(C1,
  680. "select pg_terminate_backend($1)", [Pid]),
  681. receive
  682. {'EXIT', C2, {shutdown, #error{code = <<"57P01">>}}} ->
  683. P ! ok;
  684. Other ->
  685. ?debugFmt("Unexpected msg: ~p~n", [Other]),
  686. P ! error
  687. end
  688. end)
  689. end),
  690. receive ok -> ok end
  691. end).
  692. active_connection_closed_test(Module) ->
  693. P = self(),
  694. F = fun() ->
  695. process_flag(trap_exit, true),
  696. {ok, C} = Module:connect(?host, [{database,
  697. "epgsql_test_db1"}, {port, ?port}]),
  698. P ! {connected, C},
  699. R = Module:squery(C, "select pg_sleep(10)"),
  700. P ! R
  701. end,
  702. spawn_link(F),
  703. receive
  704. {connected, C} ->
  705. timer:sleep(100),
  706. Module:close(C),
  707. {error, closed} = receive R -> R end
  708. end,
  709. flush().
  710. warning_notice_test(Module) ->
  711. with_connection(
  712. Module,
  713. fun(C) ->
  714. Q = "create function pg_temp.raise() returns void as $$
  715. begin
  716. raise warning 'oops';
  717. end;
  718. $$ language plpgsql;
  719. select pg_temp.raise()",
  720. [{ok, _, _}, _] = Module:squery(C, Q),
  721. receive
  722. {epgsql, C, {notice, #error{message = <<"oops">>}}} -> ok
  723. after
  724. 100 -> erlang:error(didnt_receive_notice)
  725. end
  726. end,
  727. [{async, self()}]).
  728. listen_notify_test(Module) ->
  729. with_connection(
  730. Module,
  731. fun(C) ->
  732. {ok, [], []} = Module:squery(C, "listen epgsql_test"),
  733. {ok, _, [{Pid}]} = Module:equery(C, "select pg_backend_pid()"),
  734. {ok, [], []} = Module:squery(C, "notify epgsql_test"),
  735. receive
  736. {epgsql, C, {notification, <<"epgsql_test">>, Pid, <<>>}} -> ok
  737. after
  738. 100 -> erlang:error(didnt_receive_notification)
  739. end
  740. end,
  741. [{async, self()}]).
  742. listen_notify_payload_test(Module) ->
  743. with_min_version(
  744. Module,
  745. 9.0,
  746. fun(C) ->
  747. {ok, [], []} = Module:squery(C, "listen epgsql_test"),
  748. {ok, _, [{Pid}]} = Module:equery(C, "select pg_backend_pid()"),
  749. {ok, [], []} = Module:squery(C, "notify epgsql_test, 'test!'"),
  750. receive
  751. {epgsql, C, {notification, <<"epgsql_test">>, Pid, <<"test!">>}} -> ok
  752. after
  753. 100 -> erlang:error(didnt_receive_notification)
  754. end
  755. end,
  756. [{async, self()}]).
  757. set_notice_receiver_test(Module) ->
  758. with_min_version(
  759. Module,
  760. 9.0,
  761. fun(C) ->
  762. {ok, [], []} = Module:squery(C, "listen epgsql_test"),
  763. {ok, _, [{Pid}]} = Module:equery(C, "select pg_backend_pid()"),
  764. EnsureNoNotification = fun(Payload) ->
  765. {ok, [], []} = Module:squery(C, ["notify epgsql_test, '", Payload, "'"]),
  766. receive
  767. {epgsql, _, _} -> erlang:error(got_unexpected_notification)
  768. after
  769. 10 -> ok
  770. end
  771. end,
  772. EnsureNotification = fun(Payload) ->
  773. {ok, [], []} = Module:squery(C, ["notify epgsql_test, '", Payload, "'"]),
  774. receive
  775. {epgsql, C, {notification, <<"epgsql_test">>, Pid, Payload}} -> ok
  776. after
  777. 100 -> erlang:error(didnt_receive_notification)
  778. end
  779. end,
  780. Self = self(),
  781. EnsureNoNotification(<<"test1">>),
  782. % Set pid()
  783. {ok, undefined} = Module:set_notice_receiver(C, Self),
  784. EnsureNotification(<<"test2">>),
  785. %% test PL/PgSQL NOTICE
  786. {ok, [], []} = Module:squery(
  787. C, ["DO $$ BEGIN RAISE WARNING 'test notice'; END $$;"]),
  788. receive
  789. {epgsql, C, {notice, #error{severity = warning,
  790. code = <<"01000">>,
  791. message = <<"test notice">>}}} -> ok
  792. after
  793. 100 -> erlang:error(didnt_receive_notice)
  794. end,
  795. % set registered pid
  796. Receiver = pg_notification_receiver,
  797. register(Receiver, Self),
  798. {ok, Self} = Module:set_notice_receiver(C, Receiver),
  799. EnsureNotification(<<"test3">>),
  800. % make registered name invalid
  801. unregister(Receiver),
  802. EnsureNoNotification(<<"test4">>),
  803. % disable
  804. {ok, Receiver} = Module:set_notice_receiver(C, undefined),
  805. EnsureNoNotification(<<"test5">>)
  806. end,
  807. []).
  808. application_test(_Module) ->
  809. lists:foreach(fun application:start/1, ?ssl_apps),
  810. ok = application:start(epgsql),
  811. ok = application:stop(epgsql).
  812. range_type_test(Module) ->
  813. with_min_version(
  814. Module,
  815. 9.2,
  816. fun(_C) ->
  817. check_type(Module, int4range, "int4range(10, 20)", <<"[10,20)">>,
  818. [{1, 58}, {-1, 12}, {-985521, 5412687}, {minus_infinity, 0},
  819. {984655, plus_infinity}, {minus_infinity, plus_infinity}])
  820. end,
  821. []).
  822. %% -- run all tests --
  823. run_tests() ->
  824. Files = filelib:wildcard(filename:dirname(code:which(epgsql_tests))
  825. ++ "/*tests.beam"),
  826. Mods = [list_to_atom(filename:basename(F, ".beam")) || F <- Files],
  827. eunit:test(Mods, []).
  828. all_test_() ->
  829. Version =
  830. erlang:list_to_binary(
  831. re:replace(os:cmd("git rev-parse HEAD"), "\\s+", "")),
  832. with_connection(
  833. epgsql,
  834. fun(C) ->
  835. {ok, _Cols, [{DBVersion}]} = epgsql:squery(C, "SELECT version FROM schema_version"),
  836. case DBVersion == Version of
  837. false ->
  838. error_logger:info_msg("Git version of test schema does not match: ~p ~p~nPlease run make create_testdbs to update your test databases", [Version, DBVersion]),
  839. erlang:exit(1);
  840. _ ->
  841. undefined
  842. end
  843. end),
  844. Tests =
  845. lists:map(
  846. fun({Name, _}) ->
  847. {Name, fun(X) -> ?MODULE:Name(X) end}
  848. end,
  849. lists:filter(
  850. fun({Name, Arity}) ->
  851. case {lists:suffix("_test", atom_to_list(Name)), Arity} of
  852. {true, 1} -> true;
  853. _ -> false
  854. end
  855. end,
  856. ?MODULE:module_info(functions))),
  857. WithModule =
  858. fun(Module) ->
  859. lists:map(
  860. fun({Name, Test}) ->
  861. {lists:flatten(
  862. io_lib:format("~s(~s)", [Name, Module])),
  863. fun() -> Test(Module) end}
  864. end,
  865. Tests)
  866. end,
  867. [WithModule(epgsql),
  868. WithModule(epgsql_cast),
  869. WithModule(epgsql_incremental)].
  870. %% -- internal functions --
  871. connect_only(Module, Args) ->
  872. TestOpts = [{port, ?port}],
  873. case Args of
  874. [User, Opts] -> Args2 = [User, TestOpts ++ Opts];
  875. [User, Pass, Opts] -> Args2 = [User, Pass, TestOpts ++ Opts];
  876. Opts -> Args2 = [TestOpts ++ Opts]
  877. end,
  878. {ok, C} = apply(Module, connect, [?host | Args2]),
  879. Module:close(C),
  880. flush().
  881. with_connection(Module, F) ->
  882. with_connection(Module, F, "epgsql_test", []).
  883. with_connection(Module, F, Args) ->
  884. with_connection(Module, F, "epgsql_test", Args).
  885. with_connection(Module, F, Username, Args) ->
  886. Args2 = [{port, ?port}, {database, "epgsql_test_db1"} | Args],
  887. {ok, C} = Module:connect(?host, Username, Args2),
  888. try
  889. F(C)
  890. after
  891. Module:close(C)
  892. end,
  893. flush().
  894. with_rollback(Module, F) ->
  895. with_connection(
  896. Module,
  897. fun(C) ->
  898. try
  899. Module:squery(C, "begin"),
  900. F(C)
  901. after
  902. Module:squery(C, "rollback")
  903. end
  904. end).
  905. with_min_version(Module, Min, F, Args) ->
  906. with_connection(
  907. Module,
  908. fun(C) ->
  909. {ok, Bin} = Module:get_parameter(C, <<"server_version">>),
  910. {ok, [{float, 1, Ver} | _], _} = erl_scan:string(binary_to_list(Bin)),
  911. case Ver >= Min of
  912. true -> F(C);
  913. false -> ?debugFmt("skipping test requiring PostgreSQL >= ~.2f~n", [Min])
  914. end
  915. end,
  916. Args).
  917. check_type(Module, Type, In, Out, Values) ->
  918. Column = "c_" ++ atom_to_list(Type),
  919. check_type(Module, Type, In, Out, Values, Column).
  920. check_type(Module, Type, In, Out, Values, Column) ->
  921. with_connection(
  922. Module,
  923. fun(C) ->
  924. Select = io_lib:format("select ~s::~w", [In, Type]),
  925. Res = Module:equery(C, Select),
  926. {ok, [#column{type = Type}], [{Out}]} = Res,
  927. Sql = io_lib:format("insert into test_table2 (~s) values ($1) returning ~s", [Column, Column]),
  928. {ok, #statement{columns = [#column{type = Type}]} = S} = Module:parse(C, Sql),
  929. Insert = fun(V) ->
  930. ok = Module:bind(C, S, [V]),
  931. {ok, 1, [{V2}]} = Module:execute(C, S),
  932. case compare(Type, V, V2) of
  933. true -> ok;
  934. false -> ?debugFmt("~p =/= ~p~n", [V, V2]), ?assert(false)
  935. end,
  936. ok = Module:sync(C)
  937. end,
  938. lists:foreach(Insert, [null, undefined | Values])
  939. end).
  940. compare(_Type, null, null) -> true;
  941. compare(_Type, undefined, null) -> true;
  942. compare(float4, V1, V2) -> abs(V2 - V1) < 0.000001;
  943. compare(float8, V1, V2) -> abs(V2 - V1) < 0.000000000000001;
  944. compare(hstore, {V1}, V2) -> compare(hstore, V1, V2);
  945. compare(hstore, V1, {V2}) -> compare(hstore, V1, V2);
  946. compare(hstore, V1, V2) ->
  947. orddict:from_list(format_hstore(V1)) =:= orddict:from_list(format_hstore(V2));
  948. compare(Type, V1 = {_, _, MS}, {D2, {H2, M2, S2}}) when Type == timestamp;
  949. Type == timestamptz ->
  950. {D1, {H1, M1, S1}} = calendar:now_to_universal_time(V1),
  951. ({D1, H1, M1} =:= {D2, H2, M2}) and (abs(S1 + MS/1000000 - S2) < 0.000000000000001);
  952. compare(int4range, {Lower, Upper}, Result) ->
  953. translate_infinities(Lower, Upper) =:= Result;
  954. compare(_Type, V1, V2) -> V1 =:= V2.
  955. translate_infinities(Lower, Upper) ->
  956. iolist_to_binary([lower(Lower), [","], upper(Upper)]).
  957. lower(minus_infinity) ->
  958. "(";
  959. lower(Val) ->
  960. io_lib:format("[~p", [Val]).
  961. upper(plus_infinity) ->
  962. ")";
  963. upper(Val) ->
  964. io_lib:format("~p)", [Val]).
  965. format_hstore({Hstore}) -> Hstore;
  966. format_hstore(Hstore) ->
  967. [{format_hstore_key(Key), format_hstore_value(Value)} || {Key, Value} <- Hstore].
  968. format_hstore_key(Key) -> format_hstore_string(Key).
  969. format_hstore_value(null) -> null;
  970. format_hstore_value(undefined) -> null;
  971. format_hstore_value(Value) -> format_hstore_string(Value).
  972. format_hstore_string(Num) when is_number(Num) -> iolist_to_binary(io_lib:format("~w", [Num]));
  973. format_hstore_string(Str) -> iolist_to_binary(io_lib:format("~s", [Str])).
  974. %% flush mailbox
  975. flush() ->
  976. ?assertEqual([], flush([])).
  977. flush(Acc) ->
  978. receive
  979. {'EXIT', _Pid, normal} -> flush(Acc);
  980. M -> flush([M | Acc])
  981. after
  982. 0 -> lists:reverse(Acc)
  983. end.