PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/test/integration/error.test.js

https://github.com/db6edr/library-sequelize
JavaScript | 192 lines | 168 code | 23 blank | 1 comment | 0 complexity | ee8cc456037a2ea492457f6e3218e658 MD5 | raw file
  1. 'use strict';
  2. var chai = require('chai')
  3. , sinon = require('sinon')
  4. , expect = chai.expect
  5. , errors = require('../../lib/errors')
  6. , Support = require(__dirname + '/support')
  7. , Sequelize = Support.Sequelize
  8. , Promise = Sequelize.Promise;
  9. chai.config.includeStack = true;
  10. describe(Support.getTestDialectTeaser('Sequelize Errors'), function () {
  11. describe('API Surface', function() {
  12. it('Should have the Error constructors exposed', function() {
  13. expect(Sequelize).to.have.property('Error');
  14. expect(Sequelize).to.have.property('ValidationError');
  15. var sequelize = new Sequelize();
  16. expect(sequelize).to.have.property('Error');
  17. expect(sequelize).to.have.property('ValidationError');
  18. });
  19. it('Sequelize Errors instances should be instances of Error', function() {
  20. var error = new Sequelize.Error();
  21. var validationError = new Sequelize.ValidationError('Validation Error', [
  22. new errors.ValidationErrorItem('<field name> cannot be null', 'notNull Violation', '<field name>', null)
  23. , new errors.ValidationErrorItem('<field name> cannot be an array or an object', 'string violation', '<field name>', null)
  24. ]);
  25. var sequelize = new Sequelize();
  26. var instError = new sequelize.Error();
  27. var instValidationError = new sequelize.ValidationError();
  28. expect(error).to.be.instanceOf(Sequelize.Error);
  29. expect(error).to.be.instanceOf(Error);
  30. expect(error).to.have.property('name', 'SequelizeBaseError');
  31. expect(validationError).to.be.instanceOf(Sequelize.ValidationError);
  32. expect(validationError).to.be.instanceOf(Error);
  33. expect(validationError).to.have.property('name', 'SequelizeValidationError');
  34. expect(validationError.message).to.match(/notNull Violation: <field name> cannot be null,\nstring violation: <field name> cannot be an array or an object/);
  35. expect(instError).to.be.instanceOf(Sequelize.Error);
  36. expect(instError).to.be.instanceOf(Error);
  37. expect(instValidationError).to.be.instanceOf(Sequelize.ValidationError);
  38. expect(instValidationError).to.be.instanceOf(Error);
  39. });
  40. it('SequelizeValidationError should find errors by path', function() {
  41. var errorItems = [
  42. new Sequelize.ValidationErrorItem('invalid', 'type', 'first_name', null),
  43. new Sequelize.ValidationErrorItem('invalid', 'type', 'last_name', null)
  44. ];
  45. var validationError = new Sequelize.ValidationError('Validation error', errorItems);
  46. expect(validationError).to.have.property('get');
  47. expect(validationError.get).to.be.a('function');
  48. var matches = validationError.get('first_name');
  49. expect(matches).to.be.instanceOf(Array);
  50. expect(matches).to.have.lengthOf(1);
  51. expect(matches[0]).to.have.property('message', 'invalid');
  52. });
  53. it('SequelizeDatabaseError should keep original message', function() {
  54. var orig = new Error('original database error message');
  55. var databaseError = new Sequelize.DatabaseError(orig);
  56. expect(databaseError).to.have.property('parent');
  57. expect(databaseError).to.have.property('original');
  58. expect(databaseError.name).to.equal('SequelizeDatabaseError');
  59. expect(databaseError.message).to.equal('original database error message');
  60. });
  61. it('ConnectionError should keep original message', function() {
  62. var orig = new Error('original connection error message');
  63. var connectionError = new Sequelize.ConnectionError(orig);
  64. expect(connectionError).to.have.property('parent');
  65. expect(connectionError).to.have.property('original');
  66. expect(connectionError.name).to.equal('SequelizeConnectionError');
  67. expect(connectionError.message).to.equal('original connection error message');
  68. });
  69. it('ConnectionRefusedError should keep original message', function() {
  70. var orig = new Error('original connection error message');
  71. var connectionError = new Sequelize.ConnectionRefusedError(orig);
  72. expect(connectionError).to.have.property('parent');
  73. expect(connectionError).to.have.property('original');
  74. expect(connectionError.name).to.equal('SequelizeConnectionRefusedError');
  75. expect(connectionError.message).to.equal('original connection error message');
  76. });
  77. it('AccessDeniedError should keep original message', function() {
  78. var orig = new Error('original connection error message');
  79. var connectionError = new Sequelize.AccessDeniedError(orig);
  80. expect(connectionError).to.have.property('parent');
  81. expect(connectionError).to.have.property('original');
  82. expect(connectionError.name).to.equal('SequelizeAccessDeniedError');
  83. expect(connectionError.message).to.equal('original connection error message');
  84. });
  85. it('HostNotFoundError should keep original message', function() {
  86. var orig = new Error('original connection error message');
  87. var connectionError = new Sequelize.HostNotFoundError(orig);
  88. expect(connectionError).to.have.property('parent');
  89. expect(connectionError).to.have.property('original');
  90. expect(connectionError.name).to.equal('SequelizeHostNotFoundError');
  91. expect(connectionError.message).to.equal('original connection error message');
  92. });
  93. it('HostNotReachableError should keep original message', function() {
  94. var orig = new Error('original connection error message');
  95. var connectionError = new Sequelize.HostNotReachableError(orig);
  96. expect(connectionError).to.have.property('parent');
  97. expect(connectionError).to.have.property('original');
  98. expect(connectionError.name).to.equal('SequelizeHostNotReachableError');
  99. expect(connectionError.message).to.equal('original connection error message');
  100. });
  101. it('InvalidConnectionError should keep original message', function() {
  102. var orig = new Error('original connection error message');
  103. var connectionError = new Sequelize.InvalidConnectionError(orig);
  104. expect(connectionError).to.have.property('parent');
  105. expect(connectionError).to.have.property('original');
  106. expect(connectionError.name).to.equal('SequelizeInvalidConnectionError');
  107. expect(connectionError.message).to.equal('original connection error message');
  108. });
  109. it('ConnectionTimedOutError should keep original message', function() {
  110. var orig = new Error('original connection error message');
  111. var connectionError = new Sequelize.ConnectionTimedOutError(orig);
  112. expect(connectionError).to.have.property('parent');
  113. expect(connectionError).to.have.property('original');
  114. expect(connectionError.name).to.equal('SequelizeConnectionTimedOutError');
  115. expect(connectionError.message).to.equal('original connection error message');
  116. });
  117. });
  118. describe('Constraint error', function () {
  119. [
  120. {
  121. type: 'UniqueConstraintError',
  122. exception: Sequelize.UniqueConstraintError
  123. },
  124. {
  125. type: 'ValidationError',
  126. exception: Sequelize.ValidationError
  127. }
  128. ].forEach(function(constraintTest) {
  129. it('Can be intercepted as ' + constraintTest.type + ' using .catch', function () {
  130. var spy = sinon.spy()
  131. , User = this.sequelize.define('user', {
  132. first_name: {
  133. type: Sequelize.STRING,
  134. unique: 'unique_name'
  135. },
  136. last_name: {
  137. type: Sequelize.STRING,
  138. unique: 'unique_name'
  139. }
  140. });
  141. var record = { first_name: 'jan', last_name: 'meier' };
  142. return this.sequelize.sync({ force: true }).bind(this).then(function () {
  143. return User.create(record);
  144. }).then(function () {
  145. return User.create(record).catch(constraintTest.exception, spy);
  146. }).then(function () {
  147. expect(spy).to.have.been.calledOnce;
  148. });
  149. });
  150. });
  151. it('Supports newlines in keys', function () {
  152. var spy = sinon.spy()
  153. , User = this.sequelize.define('user', {
  154. name: {
  155. type: Sequelize.STRING,
  156. unique: 'unique \n unique',
  157. }
  158. });
  159. return this.sequelize.sync({ force: true }).bind(this).then(function () {
  160. return User.create({ name: 'jan' });
  161. }).then(function () {
  162. // If the error was successfully parsed, we can catch it!
  163. return User.create({ name: 'jan' }).catch(this.sequelize.UniqueConstraintError, spy);
  164. }).then(function () {
  165. expect(spy).to.have.been.calledOnce;
  166. });
  167. });
  168. });
  169. });