update.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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('Update', function() {
  7. // Test Setup
  8. before(function(done) {
  9. Support.Setup('test_update', function(err) {
  10. if (err) {
  11. return done(err);
  12. }
  13. // Seed the database with two simple records.
  14. Support.Seed('test_update', done);
  15. });
  16. });
  17. after(function(done) {
  18. Support.Teardown('test_update', done);
  19. });
  20. it('should update the correct record', function(done) {
  21. var query = {
  22. using: 'test_update',
  23. criteria: {
  24. where: {
  25. fieldA: 'foo'
  26. }
  27. },
  28. valuesToSet: {
  29. fieldA: 'foobar'
  30. },
  31. meta: {
  32. fetch: true
  33. }
  34. };
  35. Adapter.update('test', query, function(err, results) {
  36. if (err) {
  37. return done(err);
  38. }
  39. assert(_.isArray(results));
  40. assert.equal(results.length, 1);
  41. assert.equal(_.first(results).fieldA, 'foobar');
  42. assert.equal(_.first(results).fieldB, 'bar');
  43. return done();
  44. });
  45. });
  46. it('should be case in-sensitive', function(done) {
  47. var query = {
  48. using: 'test_update',
  49. criteria: {
  50. where: {
  51. fieldB: 'bAr_2'
  52. }
  53. },
  54. valuesToSet: {
  55. fieldA: 'FooBar'
  56. },
  57. meta: {
  58. fetch: true
  59. }
  60. };
  61. Adapter.update('test', query, function(err, results) {
  62. if (err) {
  63. return done(err);
  64. }
  65. assert(_.isArray(results));
  66. assert.equal(results.length, 1);
  67. assert.equal(_.first(results).fieldA, 'FooBar');
  68. assert.equal(_.first(results).fieldB, 'bAr_2');
  69. return done();
  70. });
  71. });
  72. // Look into the bowels of the PG Driver and ensure the Create function handles
  73. // it's connections properly.
  74. it('should release it\'s connection when completed', function(done) {
  75. var manager = Adapter.datastores.test.manager;
  76. var preConnectionsAvailable = manager.pool._allConnections.length;
  77. var query = {
  78. using: 'test_update',
  79. criteria: {},
  80. valuesToSet: {}
  81. };
  82. Adapter.update('test', query, function(err) {
  83. if (err) {
  84. return done(err);
  85. }
  86. var postConnectionsAvailable = manager.pool._allConnections.length;
  87. assert.equal(preConnectionsAvailable, postConnectionsAvailable);
  88. return done();
  89. });
  90. });
  91. });
  92. });