transformedPopulations.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. var assert = require('assert');
  2. var Waterline = require('../../../../lib/waterline');
  3. describe('Collection Query ::', function() {
  4. describe('populated associations ::', function() {
  5. var User;
  6. var Car;
  7. var generatedCriteria = {};
  8. before(function(done) {
  9. var waterline = new Waterline();
  10. var collections = {};
  11. collections.user = Waterline.Model.extend({
  12. identity: 'user',
  13. datastore: 'foo',
  14. primaryKey: 'id',
  15. attributes: {
  16. id: {
  17. type: 'number'
  18. },
  19. car: {
  20. model: 'car'
  21. },
  22. name: {
  23. columnName: 'my_name',
  24. type: 'string'
  25. }
  26. }
  27. });
  28. collections.car = Waterline.Model.extend({
  29. identity: 'car',
  30. datastore: 'foo',
  31. primaryKey: 'id',
  32. attributes: {
  33. id: {
  34. type: 'number'
  35. },
  36. driver: {
  37. model: 'user',
  38. columnName: 'foobar'
  39. }
  40. }
  41. });
  42. waterline.registerModel(collections.user);
  43. waterline.registerModel(collections.car);
  44. // Fixture Adapter Def
  45. var adapterDef = {
  46. identity: 'foo',
  47. find: function(con, query, cb) {
  48. generatedCriteria = query.criteria;
  49. if (query.using === 'user') {
  50. return cb(null, [{ id: 1, car: 1 }]);
  51. }
  52. if (query.using === 'car') {
  53. return cb(null, [{ id: 1, foobar: 1 }]);
  54. }
  55. return cb();
  56. }
  57. };
  58. var connections = {
  59. 'foo': {
  60. adapter: 'foobar'
  61. }
  62. };
  63. waterline.initialize({ adapters: { foobar: adapterDef }, datastores: connections }, function(err, orm) {
  64. if (err) {
  65. return done(err);
  66. }
  67. User = orm.collections.user;
  68. Car = orm.collections.car;
  69. return done();
  70. });
  71. });
  72. it('should transform populated values', function(done) {
  73. User.find().populate('car').exec(function(err, users) {
  74. if (err) {
  75. return done(err);
  76. }
  77. assert(users[0].car);
  78. assert(users[0].car.driver);
  79. assert(!users[0].car.foobar);
  80. return done();
  81. });
  82. });
  83. });
  84. });