PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/test/integration/client/error-handling-tests.js

https://github.com/mdcuesta/node-postgres
JavaScript | 162 lines | 126 code | 33 blank | 3 comment | 0 complexity | f27f7f39f44d98c363c8110bc21f98b7 MD5 | raw file
  1. var helper = require(__dirname + '/test-helper');
  2. var util = require('util');
  3. var createErorrClient = function() {
  4. var client = helper.client();
  5. client.on('error', function(err) {
  6. assert.ok(false, "client should not throw query error: " + util.inspect(err));
  7. });
  8. client.on('drain', client.end.bind(client));
  9. return client;
  10. };
  11. test('error handling', function(){
  12. test('within a simple query', function() {
  13. var client = createErorrClient();
  14. var query = client.query("select omfg from yodas_dsflsd where pixistix = 'zoiks!!!'");
  15. assert.emits(query, 'error', function(error) {
  16. test('error is a psql error', function() {
  17. assert.equal(error.severity, "ERROR");
  18. });
  19. });
  20. });
  21. test('within a prepared statement', function() {
  22. var client = createErorrClient();
  23. var q = client.query({text: "CREATE TEMP TABLE boom(age integer); INSERT INTO boom (age) VALUES (28);", binary: false});
  24. test("when query is parsing", function() {
  25. //this query wont parse since there ain't no table named bang
  26. var ensureFuture = function(testClient) {
  27. test("client can issue more queries successfully", function() {
  28. var goodQuery = testClient.query("select age from boom");
  29. assert.emits(goodQuery, 'row', function(row) {
  30. assert.equal(row.age, 28);
  31. });
  32. });
  33. };
  34. var query = client.query({
  35. text: "select * from bang where name = $1",
  36. values: ['0']
  37. });
  38. test("query emits the error", function() {
  39. assert.emits(query, 'error', function(err) {
  40. ensureFuture(client);
  41. });
  42. });
  43. test("when a query is binding", function() {
  44. var query = client.query({
  45. text: 'select * from boom where age = $1',
  46. values: ['asldkfjasdf']
  47. });
  48. test("query emits the error", function() {
  49. assert.emits(query, 'error', function(err) {
  50. test('error has right severity', function() {
  51. assert.equal(err.severity, "ERROR");
  52. })
  53. ensureFuture(client);
  54. });
  55. });
  56. //TODO how to test for errors during execution?
  57. });
  58. });
  59. });
  60. test('non-query error', function() {
  61. var client = new Client({
  62. user:'asldkfjsadlfkj'
  63. });
  64. assert.emits(client, 'error');
  65. client.connect();
  66. });
  67. test('non-query error with callback', function() {
  68. var client = new Client({
  69. user:'asldkfjsadlfkj'
  70. });
  71. client.connect(assert.calls(function(error, client) {
  72. assert.ok(error);
  73. }));
  74. });
  75. });
  76. test('non-error calls supplied callback', function() {
  77. var client = new Client({
  78. user: helper.args.user,
  79. password: helper.args.password,
  80. host: helper.args.host,
  81. port: helper.args.port,
  82. database: helper.args.database
  83. });
  84. client.connect(assert.calls(function(err) {
  85. assert.isNull(err);
  86. client.end();
  87. }))
  88. });
  89. test('when connecting to invalid host', function() {
  90. var client = new Client({
  91. user: 'brian',
  92. password: '1234',
  93. host: 'asldkfjasdf!!#1308140.com'
  94. });
  95. assert.emits(client, 'error');
  96. client.connect();
  97. });
  98. test('when connecting to invalid host with callback', function() {
  99. var client = new Client({
  100. user: 'brian',
  101. password: '1234',
  102. host: 'asldkfjasdf!!#1308140.com'
  103. });
  104. client.connect(function(error, client) {
  105. assert.ok(error);
  106. });
  107. });
  108. test('multiple connection errors (gh#31)', function() {
  109. return false;
  110. test('with single client', function() {
  111. //don't run yet...this test fails...need to think of fix
  112. var client = new Client({
  113. user: 'blaksdjf',
  114. password: 'omfsadfas',
  115. host: helper.args.host,
  116. port: helper.args.port,
  117. database: helper.args.database
  118. });
  119. client.connect();
  120. assert.emits(client, 'error', function(e) {
  121. client.connect();
  122. assert.emits(client, 'error');
  123. });
  124. });
  125. test('with callback method', function() {
  126. var badConString = "tcp://aslkdfj:oi14081@"+helper.args.host+":"+helper.args.port+"/"+helper.args.database;
  127. return false;
  128. });
  129. });