create.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. var assert = require('assert');
  2. var _ = require('@sailshq/lodash');
  3. var Adapter = require('../../../lib/adapter');
  4. var Support = require('../../support/bootstrap');
  5. describe('Unit Tests ::', function() {
  6. describe('Create', function() {
  7. // Test Setup
  8. before(function(done) {
  9. Support.Setup('test_create', done);
  10. });
  11. after(function(done) {
  12. Support.Teardown('test_create', done);
  13. });
  14. // Attributes for the test table
  15. var attributes = {
  16. fieldA: 'foo',
  17. fieldB: 'bar'
  18. };
  19. it('should insert a record into the database and return it\'s fields', function(done) {
  20. var query = {
  21. using: 'test_create',
  22. newRecord: attributes,
  23. meta: {
  24. fetch: true
  25. }
  26. };
  27. Adapter.create('test', query, function(err, result) {
  28. if (err) {
  29. return done(err);
  30. }
  31. assert(_.isObject(result));
  32. assert(!_.isFunction(result));
  33. assert(!_.isArray(result));
  34. assert.equal(result.fieldA, 'foo');
  35. assert.equal(result.fieldB, 'bar');
  36. assert(result.id);
  37. return done();
  38. });
  39. });
  40. // Create Auto-Incremented ID
  41. it('should create an auto-incremented id field', function(done) {
  42. var query = {
  43. using: 'test_create',
  44. newRecord: attributes,
  45. meta: {
  46. fetch: true
  47. }
  48. };
  49. Adapter.create('test', query, function(err, result) {
  50. if (err) {
  51. return done(err);
  52. }
  53. assert(_.isObject(result));
  54. assert(!_.isFunction(result));
  55. assert(!_.isArray(result));
  56. assert(result.id);
  57. return done();
  58. });
  59. });
  60. it('should keep case', function(done) {
  61. var query = {
  62. using: 'test_create',
  63. newRecord: {
  64. fieldA: 'Foo',
  65. fieldB: 'bAr'
  66. },
  67. meta: {
  68. fetch: true
  69. }
  70. };
  71. Adapter.create('test', query, function(err, result) {
  72. if (err) {
  73. return done(err);
  74. }
  75. assert.equal(result.fieldA, 'Foo');
  76. assert.equal(result.fieldB, 'bAr');
  77. return done();
  78. });
  79. });
  80. it('should pass through buffers for `ref` type attributes', function(done) {
  81. var query = {
  82. using: 'test_create',
  83. newRecord: {
  84. fieldC: new Buffer([1,2,3])
  85. },
  86. meta: {
  87. fetch: true
  88. }
  89. };
  90. Adapter.create('test', query, function(err, record) {
  91. if (err) { return done(err); }
  92. assert(record.fieldC instanceof Buffer);
  93. assert.equal(record.fieldC.length, 3);
  94. return done();
  95. });
  96. });
  97. it('should pass through date objects for `ref` type attributes', function(done) {
  98. var query = {
  99. using: 'test_create',
  100. newRecord: {
  101. fieldD: new Date('2001-06-15 12:00:00')
  102. },
  103. meta: {
  104. fetch: true
  105. }
  106. };
  107. Adapter.create('test', query, function(err, record) {
  108. if (err) { return done(err); }
  109. assert(record.fieldD instanceof Date);
  110. assert.equal(record.fieldD.getFullYear(), '2001');
  111. return done();
  112. });
  113. });
  114. // Look into the bowels of the PG Driver and ensure the Create function handles
  115. // it's connections properly.
  116. it('should release it\'s connection when completed', function(done) {
  117. var manager = Adapter.datastores.test.manager;
  118. var preConnectionsAvailable = manager.pool._allConnections.length;
  119. var query = {
  120. using: 'test_create',
  121. newRecord: attributes,
  122. meta: {
  123. fetch: true
  124. }
  125. };
  126. Adapter.create('test', query, function(err) {
  127. if (err) {
  128. return done(err);
  129. }
  130. var postConnectionsAvailable = manager.pool._allConnections.length;
  131. assert.equal(preConnectionsAvailable, postConnectionsAvailable);
  132. return done();
  133. });
  134. });
  135. });
  136. });