find.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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('Find', function() {
  7. // Test Setup
  8. before(function(done) {
  9. Support.Setup('test_find', function(err) {
  10. if (err) {
  11. return done(err);
  12. }
  13. // Seed the database with two simple records.
  14. Support.Seed('test_find', done);
  15. });
  16. });
  17. after(function(done) {
  18. Support.Teardown('test_find', done);
  19. });
  20. it('should select the correct record', function(done) {
  21. var query = {
  22. using: 'test_find',
  23. criteria: {
  24. where: {
  25. fieldA: 'foo'
  26. }
  27. }
  28. };
  29. Adapter.find('test', query, function(err, results) {
  30. if (err) {
  31. return done(err);
  32. }
  33. assert(_.isArray(results));
  34. assert.equal(results.length, 1);
  35. assert.equal(_.first(results).fieldA, 'foo');
  36. assert.equal(_.first(results).fieldB, 'bar');
  37. return done();
  38. });
  39. });
  40. it('should return all the records', function(done) {
  41. var query = {
  42. using: 'test_find',
  43. criteria: {}
  44. };
  45. Adapter.find('test', query, function(err, results) {
  46. if (err) {
  47. return done(err);
  48. }
  49. assert(_.isArray(results));
  50. assert.equal(results.length, 2);
  51. return done();
  52. });
  53. });
  54. it('should be case sensitive', function(done) {
  55. var query = {
  56. using: 'test_find',
  57. criteria: {
  58. where: {
  59. fieldB: 'bAr_2'
  60. }
  61. }
  62. };
  63. Adapter.find('test', query, function(err, results) {
  64. if (err) {
  65. return done(err);
  66. }
  67. assert(_.isArray(results));
  68. assert.equal(results.length, 1);
  69. assert.equal(_.first(results).fieldA, 'foo_2');
  70. assert.equal(_.first(results).fieldB, 'bAr_2');
  71. return done();
  72. });
  73. });
  74. it('should return `ref` type attributes unchanged', function(done) {
  75. var query = {
  76. using: 'test_find',
  77. criteria: {
  78. where: {
  79. fieldB: 'bAr_2'
  80. }
  81. }
  82. };
  83. Adapter.find('test', query, function(err, results) {
  84. if (err) {
  85. return done(err);
  86. }
  87. var record = results[0];
  88. assert(record.fieldC instanceof Buffer, 'fieldC was not a Buffer!');
  89. assert(record.fieldD instanceof Date, 'fieldD was not a Date!');
  90. assert.equal(record.fieldC.length, 3, 'fieldC was a Buffer, but not the right Buffer! (contained: ' + require('util').inspect(record.fieldC) + ')');
  91. assert.equal(record.fieldD.getFullYear(), '2001', 'fieldD was a Date, but not the right Date! (contained: ' + require('util').inspect(record.fieldD) + ')');
  92. return done();
  93. });
  94. });
  95. // Look into the bowels of the PG Driver and ensure the Create function handles
  96. // it's connections properly.
  97. it('should release it\'s connection when completed', function(done) {
  98. var manager = Adapter.datastores.test.manager;
  99. var preConnectionsAvailable = manager.pool._allConnections.length;
  100. var query = {
  101. using: 'test_find',
  102. criteria: {}
  103. };
  104. Adapter.find('test', query, function(err) {
  105. if (err) {
  106. return done(err);
  107. }
  108. var postConnectionsAvailable = manager.pool._allConnections.length;
  109. assert.equal(preConnectionsAvailable, postConnectionsAvailable);
  110. return done();
  111. });
  112. });
  113. });
  114. });