afterCreate.findOrCreate.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. var Waterline = require('../../../lib/waterline');
  2. var assert = require('assert');
  3. describe('.afterCreate()', function() {
  4. describe('basic function', function() {
  5. describe('.findOrCreate()', function() {
  6. describe('without a record', function() {
  7. var person;
  8. before(function(done) {
  9. var waterline = new Waterline();
  10. var Model = Waterline.Model.extend({
  11. identity: 'user',
  12. datastore: 'foo',
  13. primaryKey: 'id',
  14. fetchRecordsOnCreate: true,
  15. fetchRecordsOnCreateEach: true,
  16. attributes: {
  17. id: {
  18. type: 'number'
  19. },
  20. name: {
  21. type: 'string'
  22. }
  23. },
  24. afterCreate: function(values, cb) {
  25. values.name = values.name + ' updated';
  26. return cb();
  27. }
  28. });
  29. waterline.registerModel(Model);
  30. // Fixture Adapter Def
  31. var adapterDef = {
  32. find: function(con, query, cb) { return cb(null, null); },
  33. create: function(con, query, cb) { return cb(null, query.newRecord); }
  34. };
  35. var connections = {
  36. 'foo': {
  37. adapter: 'foobar'
  38. }
  39. };
  40. waterline.initialize({ adapters: { foobar: adapterDef }, datastores: connections }, function(err, orm) {
  41. if (err) {
  42. return done(err);
  43. }
  44. person = orm.collections.user;
  45. return done();
  46. });
  47. });
  48. it('should run afterCreate and mutate values on create', function(done) {
  49. person.findOrCreate({ name: 'test' }, { name: 'test', id: 1 }, function(err, user) {
  50. if (err) {
  51. return done(err);
  52. }
  53. assert.equal(user.name, 'test updated');
  54. return done();
  55. });
  56. });
  57. });
  58. describe('with a record', function() {
  59. var person;
  60. before(function(done) {
  61. var waterline = new Waterline();
  62. var Model = Waterline.Model.extend({
  63. identity: 'user',
  64. datastore: 'foo',
  65. primaryKey: 'id',
  66. attributes: {
  67. id: {
  68. type: 'number'
  69. },
  70. name: {
  71. type: 'string'
  72. }
  73. },
  74. afterCreate: function(values, cb) {
  75. values.name = values.name + ' updated';
  76. return cb();
  77. }
  78. });
  79. waterline.registerModel(Model);
  80. // Fixture Adapter Def
  81. var adapterDef = {
  82. find: function(con, query, cb) { return cb(null, [{ name: 'test', id: 1 }]); },
  83. create: function(con, query, cb) { return cb(null, query.newRecord); }
  84. };
  85. var connections = {
  86. 'foo': {
  87. adapter: 'foobar'
  88. }
  89. };
  90. waterline.initialize({ adapters: { foobar: adapterDef }, datastores: connections }, function(err, orm) {
  91. if (err) {
  92. return done(err);
  93. }
  94. person = orm.collections.user;
  95. return done();
  96. });
  97. });
  98. it('should not run afterCreate and mutate values on find', function(done) {
  99. person.findOrCreate({ name: 'test' }, { name: 'test', id: 1 }, function(err, user) {
  100. if (err) {
  101. return done(err);
  102. }
  103. assert.equal(user.name, 'test');
  104. return done();
  105. });
  106. });
  107. });
  108. });
  109. });
  110. });