epgsql_tests.erl 40 KB

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