pgsql_tests.erl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. -module(pgsql_tests).
  2. -export([run_tests/0]).
  3. -include_lib("eunit/include/eunit.hrl").
  4. -include_lib("ssl/include/OTP-PKIX.hrl").
  5. -include("pgsql.hrl").
  6. -define(host, "localhost").
  7. -define(port, 5432).
  8. connect_test() ->
  9. connect_only([[]]).
  10. connect_to_db_test() ->
  11. connect_only([[{database, "epgsql_test_db1"}]]).
  12. connect_as_test() ->
  13. connect_only(["epgsql_test", [{database, "epgsql_test_db1"}]]).
  14. connect_with_cleartext_test() ->
  15. connect_only(["epgsql_test_cleartext",
  16. "epgsql_test_cleartext",
  17. [{database, "epgsql_test_db1"}]]).
  18. connect_with_md5_test() ->
  19. connect_only(["epgsql_test_md5",
  20. "epgsql_test_md5",
  21. [{database, "epgsql_test_db1"}]]).
  22. connect_with_invalid_password_test() ->
  23. {error, invalid_authorization_specification} =
  24. pgsql:connect(?host,
  25. "epgsql_test_md5",
  26. "epgsql_test_sha1",
  27. [{port, ?port}, {database, "epgsql_test_db1"}]).
  28. connect_with_ssl_test() ->
  29. lists:foreach(fun application:start/1, [crypto, ssl]),
  30. with_connection(
  31. fun(C) ->
  32. {ok, _Cols, [{true}]} = pgsql:equery(C, "select ssl_is_used()")
  33. end,
  34. "epgsql_test",
  35. [{ssl, true}]).
  36. connect_with_client_cert_test() ->
  37. lists:foreach(fun application:start/1, [crypto, ssl]),
  38. Dir = filename:join(filename:dirname(code:which(pgsql_tests)), "../test_data"),
  39. File = fun(Name) -> filename:join(Dir, Name) end,
  40. {ok, [{cert, Der, _}]} = public_key:pem_to_der(File("epgsql.crt")),
  41. {ok, Cert} = public_key:pkix_decode_cert(Der, plain),
  42. #'TBSCertificate'{serialNumber = Serial} = Cert#'Certificate'.tbsCertificate,
  43. Serial2 = list_to_binary(integer_to_list(Serial)),
  44. with_connection(
  45. fun(C) ->
  46. {ok, _, [{true}]} = pgsql:equery(C, "select ssl_is_used()"),
  47. {ok, _, [{Serial2}]} = pgsql:equery(C, "select ssl_client_serial()")
  48. end,
  49. "epgsql_test_cert",
  50. [{ssl, true}, {keyfile, File("epgsql.key")}, {certfile, File("epgsql.crt")}]).
  51. select_test() ->
  52. with_connection(
  53. fun(C) ->
  54. {ok, Cols, Rows} = pgsql:squery(C, "select * from test_table1"),
  55. [#column{name = <<"id">>, type = int4, size = 4},
  56. #column{name = <<"value">>, type = text, size = -1}] = Cols,
  57. [{<<"1">>, <<"one">>}, {<<"2">>, <<"two">>}] = Rows
  58. end).
  59. insert_test() ->
  60. with_rollback(
  61. fun(C) ->
  62. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (3, 'three')")
  63. end).
  64. update_test() ->
  65. with_rollback(
  66. fun(C) ->
  67. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (3, 'three')"),
  68. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (4, 'four')"),
  69. {ok, 2} = pgsql:squery(C, "update test_table1 set value = 'foo' where id > 2"),
  70. {ok, _, [{<<"2">>}]} = pgsql:squery(C, "select count(*) from test_table1 where value = 'foo'")
  71. end).
  72. delete_test() ->
  73. with_rollback(
  74. fun(C) ->
  75. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (3, 'three')"),
  76. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (4, 'four')"),
  77. {ok, 2} = pgsql:squery(C, "delete from test_table1 where id > 2"),
  78. {ok, _, [{<<"2">>}]} = pgsql:squery(C, "select count(*) from test_table1")
  79. end).
  80. create_and_drop_table_test() ->
  81. with_rollback(
  82. fun(C) ->
  83. {ok, [], []} = pgsql:squery(C, "create table test_table3 (id int4)"),
  84. {ok, [#column{type = int4}], []} = pgsql:squery(C, "select * from test_table3"),
  85. {ok, [], []} = pgsql:squery(C, "drop table test_table3")
  86. end).
  87. cursor_test() ->
  88. with_connection(
  89. fun(C) ->
  90. {ok, [], []} = pgsql:squery(C, "begin"),
  91. {ok, [], []} = pgsql:squery(C, "declare c cursor for select id from test_table1"),
  92. {ok, 2} = pgsql:squery(C, "move forward 2 from c"),
  93. {ok, 1} = pgsql:squery(C, "move backward 1 from c"),
  94. {ok, 1, _Cols, [{<<"2">>}]} = pgsql:squery(C, "fetch next from c"),
  95. {ok, [], []} = pgsql:squery(C, "close c")
  96. end).
  97. multiple_result_test() ->
  98. with_connection(
  99. fun(C) ->
  100. [{ok, _, [{<<"1">>}]}, {ok, _, [{<<"2">>}]}] = pgsql:squery(C, "select 1; select 2"),
  101. [{ok, _, [{<<"1">>}]}, {error, #error{}}] = pgsql:squery(C, "select 1; select foo;")
  102. end).
  103. extended_select_test() ->
  104. with_connection(
  105. fun(C) ->
  106. {ok, Cols, Rows} = pgsql:equery(C, "select * from test_table1", []),
  107. [#column{name = <<"id">>, type = int4, size = 4},
  108. #column{name = <<"value">>, type = text, size = -1}] = Cols,
  109. [{1, <<"one">>}, {2, <<"two">>}] = Rows
  110. end).
  111. extended_sync_ok_test() ->
  112. with_connection(
  113. fun(C) ->
  114. {ok, _Cols, [{<<"one">>}]} = pgsql:equery(C, "select value from test_table1 where id = $1", [1]),
  115. {ok, _Cols, [{<<"two">>}]} = pgsql:equery(C, "select value from test_table1 where id = $1", [2])
  116. end).
  117. extended_sync_error_test() ->
  118. with_connection(
  119. fun(C) ->
  120. {error, #error{}} = pgsql:equery(C, "select _alue from test_table1 where id = $1", [1]),
  121. {ok, _Cols, [{<<"one">>}]} = pgsql:equery(C, "select value from test_table1 where id = $1", [1])
  122. end).
  123. returning_from_insert_test() ->
  124. with_rollback(
  125. fun(C) ->
  126. {ok, 1, _Cols, [{3}]} = pgsql:equery(C, "insert into test_table1 (id) values (3) returning id")
  127. end).
  128. returning_from_update_test() ->
  129. with_rollback(
  130. fun(C) ->
  131. {ok, 2, _Cols, [{1}, {2}]} = pgsql:equery(C, "update test_table1 set value = 'hi' returning id")
  132. end).
  133. returning_from_delete_test() ->
  134. with_rollback(
  135. fun(C) ->
  136. {ok, 2, _Cols, [{1}, {2}]} = pgsql:equery(C, "delete from test_table1 returning id")
  137. end).
  138. parse_test() ->
  139. with_connection(
  140. fun(C) ->
  141. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  142. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  143. ok = pgsql:close(C, S),
  144. ok = pgsql:sync(C)
  145. end).
  146. parse_column_format_test() ->
  147. with_connection(
  148. fun(C) ->
  149. {ok, S} = pgsql:parse(C, "select 1::int4, false::bool, 2.0::float4"),
  150. [#column{type = int4},
  151. #column{type = bool},
  152. #column{type = float4}] = S#statement.columns,
  153. ok = pgsql:bind(C, S, []),
  154. {ok, [{1, false, 2.0}]} = pgsql:execute(C, S, 0),
  155. ok = pgsql:close(C, S),
  156. ok = pgsql:sync(C)
  157. end).
  158. parse_error_test() ->
  159. with_connection(
  160. fun(C) ->
  161. {error, #error{}} = pgsql:parse(C, "select _ from test_table1"),
  162. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  163. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  164. ok = pgsql:close(C, S),
  165. ok = pgsql:sync(C)
  166. end).
  167. parse_and_close_test() ->
  168. with_connection(
  169. fun(C) ->
  170. Parse = fun() -> pgsql:parse(C, "test", "select * from test_table1", []) end,
  171. {ok, S} = Parse(),
  172. {error, #error{code = <<"42P05">>}} = Parse(),
  173. pgsql:close(C, S),
  174. {ok, S} = Parse(),
  175. ok = pgsql:sync(C)
  176. end).
  177. bind_test() ->
  178. with_connection(
  179. fun(C) ->
  180. {ok, S} = pgsql:parse(C, "select value from test_table1 where id = $1"),
  181. ok = pgsql:bind(C, S, [1]),
  182. ok = pgsql:close(C, S),
  183. ok = pgsql:sync(C)
  184. end).
  185. bind_parameter_format_test() ->
  186. with_connection(
  187. fun(C) ->
  188. {ok, S} = pgsql:parse(C, "select $1, $2, $3", [int2, text, bool]),
  189. [int2, text, bool] = S#statement.types,
  190. ok = pgsql:bind(C, S, [1, "hi", true]),
  191. {ok, [{1, <<"hi">>, true}]} = pgsql:execute(C, S, 0),
  192. ok = pgsql:close(C, S),
  193. ok = pgsql:sync(C)
  194. end).
  195. bind_error_test() ->
  196. with_connection(
  197. fun(C) ->
  198. {ok, S} = pgsql:parse(C, "select $1::char"),
  199. {error, #error{}} = pgsql:bind(C, S, [0]),
  200. ok = pgsql:bind(C, S, [$A]),
  201. ok = pgsql:close(C, S),
  202. ok = pgsql:sync(C)
  203. end).
  204. bind_and_close_test() ->
  205. with_connection(
  206. fun(C) ->
  207. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  208. ok = pgsql:bind(C, S, "one", []),
  209. {error, #error{code = <<"42P03">>}} = pgsql:bind(C, S, "one", []),
  210. ok = pgsql:close(C, portal, "one"),
  211. ok = pgsql:bind(C, S, "one", []),
  212. ok = pgsql:sync(C)
  213. end).
  214. describe_test() ->
  215. with_connection(
  216. fun(C) ->
  217. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  218. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  219. {ok, S} = pgsql:describe(C, S),
  220. ok = pgsql:close(C, S),
  221. ok = pgsql:sync(C)
  222. end).
  223. describe_with_param_test() ->
  224. with_connection(
  225. fun(C) ->
  226. {ok, S} = pgsql:parse(C, "select id from test_table1 where id = $1"),
  227. [int4] = S#statement.types,
  228. [#column{name = <<"id">>}] = S#statement.columns,
  229. {ok, S} = pgsql:describe(C, S),
  230. ok = pgsql:close(C, S),
  231. ok = pgsql:sync(C)
  232. end).
  233. describe_named_test() ->
  234. with_connection(
  235. fun(C) ->
  236. {ok, S} = pgsql:parse(C, "name", "select * from test_table1", []),
  237. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  238. {ok, S} = pgsql:describe(C, S),
  239. ok = pgsql:close(C, S),
  240. ok = pgsql:sync(C)
  241. end).
  242. describe_error_test() ->
  243. with_connection(
  244. fun(C) ->
  245. {error, #error{}} = pgsql:describe(C, statement, ""),
  246. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  247. {ok, S} = pgsql:describe(C, statement, ""),
  248. ok = pgsql:sync(C)
  249. end).
  250. portal_test() ->
  251. with_connection(
  252. fun(C) ->
  253. {ok, S} = pgsql:parse(C, "select value from test_table1"),
  254. ok = pgsql:bind(C, S, []),
  255. {partial, [{<<"one">>}]} = pgsql:execute(C, S, 1),
  256. {partial, [{<<"two">>}]} = pgsql:execute(C, S, 1),
  257. {ok, []} = pgsql:execute(C, S,1),
  258. ok = pgsql:close(C, S),
  259. ok = pgsql:sync(C)
  260. end).
  261. returning_test() ->
  262. with_rollback(
  263. fun(C) ->
  264. {ok, S} = pgsql:parse(C, "update test_table1 set value = $1 returning id"),
  265. ok = pgsql:bind(C, S, ["foo"]),
  266. {ok, 2, [{1}, {2}]} = pgsql:execute(C, S),
  267. ok = pgsql:sync(C)
  268. end).
  269. multiple_statement_test() ->
  270. with_connection(
  271. fun(C) ->
  272. {ok, S1} = pgsql:parse(C, "one", "select value from test_table1 where id = 1", []),
  273. ok = pgsql:bind(C, S1, []),
  274. {partial, [{<<"one">>}]} = pgsql:execute(C, S1, 1),
  275. {ok, S2} = pgsql:parse(C, "two", "select value from test_table1 where id = 2", []),
  276. ok = pgsql:bind(C, S2, []),
  277. {partial, [{<<"two">>}]} = pgsql:execute(C, S2, 1),
  278. {ok, []} = pgsql:execute(C, S1, 1),
  279. {ok, []} = pgsql:execute(C, S2, 1),
  280. ok = pgsql:close(C, S1),
  281. ok = pgsql:close(C, S2),
  282. ok = pgsql:sync(C)
  283. end).
  284. multiple_portal_test() ->
  285. with_connection(
  286. fun(C) ->
  287. {ok, S} = pgsql:parse(C, "select value from test_table1 where id = $1"),
  288. ok = pgsql:bind(C, S, "one", [1]),
  289. ok = pgsql:bind(C, S, "two", [2]),
  290. {ok, [{<<"one">>}]} = pgsql:execute(C, S, "one", 0),
  291. {ok, [{<<"two">>}]} = pgsql:execute(C, S, "two", 0),
  292. ok = pgsql:close(C, S),
  293. ok = pgsql:sync(C)
  294. end).
  295. execute_function_test() ->
  296. with_rollback(
  297. fun(C) ->
  298. {ok, _Cols1, [{3}]} = pgsql:equery(C, "select insert_test1(3, 'three')"),
  299. {ok, _Cols2, [{<<>>}]} = pgsql:equery(C, "select do_nothing()")
  300. end).
  301. parameter_get_test() ->
  302. with_connection(
  303. fun(C) ->
  304. {ok, <<"off">>} = pgsql:get_parameter(C, "is_superuser")
  305. end).
  306. parameter_set_test() ->
  307. with_connection(
  308. fun(C) ->
  309. {ok, [], []} = pgsql:squery(C, "set DateStyle = 'ISO, MDY'"),
  310. {ok, <<"ISO, MDY">>} = pgsql:get_parameter(C, "DateStyle"),
  311. {ok, _Cols, [{<<"2000-01-02">>}]} = pgsql:squery(C, "select '2000-01-02'::date"),
  312. {ok, [], []} = pgsql:squery(C, "set DateStyle = 'German'"),
  313. {ok, <<"German, DMY">>} = pgsql:get_parameter(C, "DateStyle"),
  314. {ok, _Cols, [{<<"02.01.2000">>}]} = pgsql:squery(C, "select '2000-01-02'::date")
  315. end).
  316. numeric_type_test() ->
  317. check_type(int2, "1", 1, [0, 256, -32768, +32767]),
  318. check_type(int4, "1", 1, [0, 512, -2147483648, +2147483647]),
  319. check_type(int8, "1", 1, [0, 1024, -9223372036854775808, +9223372036854775807]),
  320. check_type(float4, "1.0", 1.0, [0.0, 1.23456, -1.23456]),
  321. check_type(float8, "1.0", 1.0, [0.0, 1.23456789012345, -1.23456789012345]).
  322. character_type_test() ->
  323. Alpha = unicode:characters_to_binary([16#03B1]),
  324. Ka = unicode:characters_to_binary([16#304B]),
  325. One = unicode:characters_to_binary([16#10D360]),
  326. check_type(bpchar, "'A'", $A, [1, $1, 16#7F, Alpha, Ka, One], "c_char"),
  327. check_type(text, "'hi'", <<"hi">>, [<<"">>, <<"hi">>]),
  328. check_type(varchar, "'hi'", <<"hi">>, [<<"">>, <<"hi">>]).
  329. date_time_type_test() ->
  330. with_connection(
  331. fun(C) ->
  332. case pgsql:get_parameter(C, "integer_datetimes") of
  333. {ok, <<"on">>} -> MaxTsDate = 294276;
  334. {ok, <<"off">>} -> MaxTsDate = 5874897
  335. end,
  336. check_type(date, "'2008-01-02'", {2008,1,2}, [{-4712,1,1}, {5874897,1,1}]),
  337. check_type(time, "'00:01:02'", {0,1,2.0}, [{0,0,0.0}, {24,0,0.0}]),
  338. check_type(timetz, "'00:01:02-01'", {{0,1,2.0},1*60*60},
  339. [{{0,0,0.0},0}, {{24,0,0.0},-13*60*60}]),
  340. check_type(timestamp, "'2008-01-02 03:04:05'", {{2008,1,2},{3,4,5.0}},
  341. [{{-4712,1,1},{0,0,0.0}}, {{MaxTsDate,12,31}, {23,59,59.0}}]),
  342. check_type(interval, "'1 hour 2 minutes 3.1 seconds'", {{1,2,3.1},0,0},
  343. [{{0,0,0.0},0,-178000000 * 12}, {{0,0,0.0},0,178000000 * 12}])
  344. end).
  345. misc_type_test() ->
  346. check_type(bool, "true", true, [true, false]),
  347. check_type(bytea, "E'\001\002'", <<1,2>>, [<<>>, <<0,128,255>>]).
  348. text_format_test() ->
  349. with_connection(
  350. fun(C) ->
  351. Select = fun(Type, V) ->
  352. V2 = list_to_binary(V),
  353. Query = "select $1::" ++ Type,
  354. {ok, _Cols, [{V2}]} = pgsql:equery(C, Query, [V]),
  355. {ok, _Cols, [{V2}]} = pgsql:equery(C, Query, [V2])
  356. end,
  357. Select("inet", "127.0.0.1"),
  358. Select("numeric", "123456")
  359. end).
  360. connect_timeout_test() ->
  361. {error, timeout} = pgsql:connect(?host, [{port, ?port}, {timeout, 0}]).
  362. query_timeout_test() ->
  363. with_connection(
  364. fun(C) ->
  365. {error, timeout} = pgsql:squery(C, "select pg_sleep(1)"),
  366. {error, timeout} = pgsql:equery(C, "select pg_sleep(2)"),
  367. {ok, _Cols, [{1}]} = pgsql:equery(C, "select 1")
  368. end,
  369. [{timeout, 10}]).
  370. execute_timeout_test() ->
  371. with_connection(
  372. fun(C) ->
  373. {ok, S} = pgsql:parse(C, "select pg_sleep($1)"),
  374. ok = pgsql:bind(C, S, [2]),
  375. {error, timeout} = pgsql:execute(C, S, 0),
  376. ok = pgsql:bind(C, S, [0]),
  377. {ok, [{<<>>}]} = pgsql:execute(C, S, 0),
  378. ok = pgsql:close(C, S),
  379. ok = pgsql:sync(C)
  380. end,
  381. [{timeout, 10}]).
  382. connection_closed_test() ->
  383. P = self(),
  384. F = fun() ->
  385. process_flag(trap_exit, true),
  386. {ok, C} = pgsql:connect(?host, [{port, ?port}]),
  387. P ! {connected, C},
  388. receive
  389. Any -> P ! Any
  390. end
  391. end,
  392. spawn_link(F),
  393. receive
  394. {connected, C} ->
  395. timer:sleep(100),
  396. pgsql:close(C),
  397. {'EXIT', C, _} = receive R -> R end
  398. end,
  399. flush().
  400. active_connection_closed_test() ->
  401. P = self(),
  402. F = fun() ->
  403. process_flag(trap_exit, true),
  404. {ok, C} = pgsql:connect(?host, [{port, ?port}]),
  405. P ! {connected, C},
  406. R = pgsql:squery(C, "select pg_sleep(10)"),
  407. P ! R
  408. end,
  409. spawn_link(F),
  410. receive
  411. {connected, C} ->
  412. timer:sleep(100),
  413. pgsql:close(C),
  414. {error, closed} = receive R -> R end
  415. end,
  416. flush().
  417. %% -- run all tests --
  418. run_tests() ->
  419. Files = filelib:wildcard("test_ebin/*tests.beam"),
  420. Mods = [list_to_atom(filename:basename(F, ".beam")) || F <- Files],
  421. eunit:test(Mods, []).
  422. %% -- internal functions --
  423. connect_only(Args) ->
  424. {ok, C} = apply(pgsql, connect, [?host, [{port, ?port} | Args]]),
  425. pgsql:close(C),
  426. flush().
  427. with_connection(F) ->
  428. with_connection(F, "epgsql_test", []).
  429. with_connection(F, Args) ->
  430. with_connection(F, "epgsql_test", Args).
  431. with_connection(F, Username, Args) ->
  432. Args2 = [{port, ?port}, {database, "epgsql_test_db1"} | Args],
  433. {ok, C} = pgsql:connect(?host, Username, Args2),
  434. try
  435. F(C)
  436. after
  437. pgsql:close(C)
  438. end,
  439. flush().
  440. with_rollback(F) ->
  441. with_connection(
  442. fun(C) ->
  443. try
  444. pgsql:squery(C, "begin"),
  445. F(C)
  446. after
  447. pgsql:squery(C, "rollback")
  448. end
  449. end).
  450. check_type(Type, In, Out, Values) ->
  451. Column = "c_" ++ atom_to_list(Type),
  452. check_type(Type, In, Out, Values, Column).
  453. check_type(Type, In, Out, Values, Column) ->
  454. with_connection(
  455. fun(C) ->
  456. Select = io_lib:format("select ~s::~w", [In, Type]),
  457. {ok, [#column{type = Type}], [{Out}]} = pgsql:equery(C, Select),
  458. Sql = io_lib:format("insert into test_table2 (~s) values ($1) returning ~s", [Column, Column]),
  459. {ok, #statement{columns = [#column{type = Type}]} = S} = pgsql:parse(C, Sql),
  460. Insert = fun(V) ->
  461. pgsql:bind(C, S, [V]),
  462. {ok, 1, [{V2}]} = pgsql:execute(C, S),
  463. case compare(Type, V, V2) of
  464. true -> ok;
  465. false -> ?debugFmt("~p =/= ~p~n", [V, V2]), ?assert(false)
  466. end,
  467. ok = pgsql:sync(C)
  468. end,
  469. lists:foreach(Insert, [null | Values])
  470. end).
  471. compare(_Type, null, null) -> true;
  472. compare(float4, V1, V2) -> abs(V2 - V1) < 0.000001;
  473. compare(float8, V1, V2) -> abs(V2 - V1) < 0.000000000000001;
  474. compare(_Type, V1, V2) -> V1 =:= V2.
  475. %% flush mailbox
  476. flush() ->
  477. ?assertEqual([], flush([])).
  478. flush(Acc) ->
  479. receive
  480. {'EXIT', _Pid, normal} -> flush(Acc);
  481. M -> flush([M | Acc])
  482. after
  483. 0 -> lists:reverse(Acc)
  484. end.