PostgreSQL native library for Tarantool programming language
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pg.test.lua 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env tarantool
  2. package.path = "../?/init.lua;./?/init.lua"
  3. package.cpath = "../?.so;../?.dylib;./?.so;./?.dylib"
  4. local pg = require('pg')
  5. local json = require('json')
  6. local tap = require('tap')
  7. local f = require('fiber')
  8. local host, port, user, pass, db = string.match(os.getenv('PG') or '',
  9. "([^:]*):([^:]*):([^:]*):([^:]*):([^:]*)")
  10. local p, msg = pg.pool_create({ host = host, port = port, user = user, pass = pass,
  11. db = db, raise = false, size = 2 })
  12. if p == nil then error(msg) end
  13. local conn, msg = pg.connect({ host = host, port = port, user = user, pass = pass,
  14. db = db, raise = false})
  15. if conn == nil then error(msg) end
  16. function test_old_api(t, c)
  17. t:plan(14)
  18. t:ok(c ~= nil, "connection")
  19. -- Add an extension to 'tap' module
  20. getmetatable(t).__index.q = function(test, stmt, result, ...)
  21. test:is_deeply({c:execute(stmt, ...)}, {{result}, true},
  22. ... ~= nil and stmt..' % '..json.encode({...}) or stmt)
  23. end
  24. t:ok(c:ping(), "ping")
  25. if p == nil then
  26. return
  27. end
  28. t:q('SELECT 123::text AS bla, 345', {{ bla = '123', ['?column?'] = 345 }})
  29. t:q('SELECT -1 AS neg, NULL AS abc', {{ neg = -1 }})
  30. t:q('SELECT -1.1 AS neg, 1.2 AS pos', {{ neg = -1.1, pos = 1.2 }})
  31. t:q('SELECT ARRAY[1,2] AS arr, 1.2 AS pos', {{ arr = '{1,2}', pos = 1.2}})
  32. t:q('SELECT $1 AS val', {{ val = 'abc' }}, 'abc')
  33. t:q('SELECT $1 AS val', {{ val = 123 }}, 123)
  34. t:q('SELECT $1 AS val', {{ val = true }}, true)
  35. t:q('SELECT $1 AS val', {{ val = false }}, false)
  36. t:q('SELECT $1 AS val, $2 AS num, $3 AS str',
  37. {{ val = false, num = 123, str = 'abc'}}, false, 123, 'abc')
  38. t:q('SELECT * FROM (VALUES (1,2), (2,3)) t', {
  39. { column1 = 1, column2 = 2}, { column1 = 2, column2 = 3}})
  40. t:test("tx", function(t)
  41. t:plan(7)
  42. if not c:execute("CREATE TABLE _tx_test (a int)") then
  43. return
  44. end
  45. t:ok(c:begin(), "begin")
  46. c:execute("INSERT INTO _tx_test VALUES(10)");
  47. t:q('SELECT * FROM _tx_test', {{ a = 10 }})
  48. t:ok(c:rollback(), "roolback")
  49. t:q('SELECT * FROM _tx_test', {})
  50. t:ok(c:begin(), "begin")
  51. c:execute("INSERT INTO _tx_test VALUES(10)");
  52. t:ok(c:commit(), "commit")
  53. t:q('SELECT * FROM _tx_test', {{ a = 10 }})
  54. c:execute("DROP TABLE _tx_test")
  55. end)
  56. local status, reason = pcall(c.execute, c, 'DROP TABLE unknown_table')
  57. t:like(reason, 'unknown_table', 'error')
  58. end
  59. function test_gc(t, p)
  60. t:plan(1)
  61. p:get()
  62. local c = p:get()
  63. c = nil
  64. collectgarbage('collect')
  65. t:is(p.queue:count(), p.size, 'gc connections')
  66. end
  67. function test_conn_fiber1(c, q)
  68. for i = 1, 10 do
  69. c:execute('SELECT pg_sleep(0.05)')
  70. end
  71. q:put(true)
  72. end
  73. function test_conn_fiber2(c, q)
  74. for i = 1, 25 do
  75. c:execute('SELECT pg_sleep(0.02)')
  76. end
  77. q:put(true)
  78. end
  79. function test_conn_concurrent(t, p)
  80. t:plan(1)
  81. local c = p:get()
  82. local q = f.channel(2)
  83. local t1 = f.time()
  84. f.create(test_conn_fiber1, c, q)
  85. f.create(test_conn_fiber2, c, q)
  86. q:get()
  87. q:get()
  88. p:put(c)
  89. t:ok(f.time() - t1 >= 0.95, 'concurrent connections')
  90. end
  91. function test_pg_int64(t, p)
  92. t:plan(1)
  93. conn = p:get()
  94. conn:execute('create table int64test (id bigint)')
  95. conn:execute('insert into int64test values(1234567890123456789)')
  96. local r, m = conn:execute('select id from int64test')
  97. conn:execute('drop table int64test')
  98. t:ok(r[1][1]['id'] == 1234567890123456789LL, 'int64 test')
  99. p:put(conn)
  100. end
  101. tap.test('connection old api', test_old_api, conn)
  102. local pool_conn = p:get()
  103. tap.test('connection old api via pool', test_old_api, pool_conn)
  104. p:put(pool_conn)
  105. tap.test('test collection connections', test_gc, p)
  106. tap.test('connection concurrent', test_conn_concurrent, p)
  107. tap.test('int64', test_pg_int64, p)
  108. p:close()