PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/twistedvisions/node-postgres
JavaScript | 181 lines | 143 code | 33 blank | 5 comment | 1 complexity | 9bb4c666e8f00beb5c55f8ae5d420892 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. //this test fails about 30% on travis and only on travis...
  91. //I'm not sure what the cause could be
  92. if(process.env.TRAVIS) return false;
  93. var client = new Client({
  94. user: 'aslkdjfsdf',
  95. password: '1234',
  96. host: 'asldkfjasdf!!#1308140.com'
  97. });
  98. var delay = 5000;
  99. var tid = setTimeout(function() {
  100. assert(false, "When connecting to an invalid host the error event should be emitted but it has been " + delay + " and still no error event.");
  101. }, delay);
  102. client.on('error', function() {
  103. clearTimeout(tid);
  104. })
  105. client.connect();
  106. });
  107. test('when connecting to invalid host with callback', function() {
  108. var client = new Client({
  109. user: 'brian',
  110. password: '1234',
  111. host: 'asldkfjasdf!!#1308140.com'
  112. });
  113. client.connect(function(error, client) {
  114. assert.ok(error);
  115. });
  116. });
  117. test('multiple connection errors (gh#31)', function() {
  118. return false;
  119. test('with single client', function() {
  120. //don't run yet...this test fails...need to think of fix
  121. var client = new Client({
  122. user: 'blaksdjf',
  123. password: 'omfsadfas',
  124. host: helper.args.host,
  125. port: helper.args.port,
  126. database: helper.args.database
  127. });
  128. client.connect();
  129. assert.emits(client, 'error', function(e) {
  130. client.connect();
  131. assert.emits(client, 'error');
  132. });
  133. });
  134. test('with callback method', function() {
  135. var badConString = "postgres://aslkdfj:oi14081@"+helper.args.host+":"+helper.args.port+"/"+helper.args.database;
  136. return false;
  137. });
  138. });
  139. test('query receives error on client shutdown', function() {
  140. var client = new Client(helper.config);
  141. client.connect(assert.calls(function() {
  142. client.query('SELECT pg_sleep(5)', assert.calls(function(err, res) {
  143. assert(err);
  144. }));
  145. client.end();
  146. assert.emits(client, 'end');
  147. }));
  148. });