strategy.alter.schema.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. var assert = require('assert');
  2. var _ = require('@sailshq/lodash');
  3. var Waterline = require('../../lib/waterline');
  4. var MigrateHelper = require('../support/migrate.helper');
  5. describe.skip('Alter Mode Recovery with an enforced schema', function () {
  6. var record;
  7. // Create an adapter and set some existing data
  8. before(function (done) {
  9. // Hold data to be used in tests
  10. var persistentData = [{
  11. id: 1,
  12. name: 'batman',
  13. car: 'batmobile',
  14. age: 50
  15. }];
  16. var adapter = {
  17. registerDatastore: function (connection, collections, cb) {
  18. cb(null, null);
  19. },
  20. define: function (connectionName, collectionName, definition, cb) {
  21. this.describe(connectionName, collectionName, function (err, schema) {
  22. cb(null, schema);
  23. });
  24. },
  25. describe: function (connectionName, collectionName, cb, connection) {
  26. var schema = {
  27. name: { type: 'string' },
  28. age: { type: 'number' }
  29. };
  30. cb(null, (persistentData.length === 1) ? schema : undefined);
  31. },
  32. find: function (connectionName, collectionName, options, cb, connection) {
  33. if(!options.select && !options.where) {
  34. return cb(null, persistentData);
  35. }
  36. var results;
  37. if(!options.where) {
  38. results = persistentData;
  39. }
  40. else {
  41. results = _.filter(persistentData, options.where);
  42. }
  43. // Psuedo support for select (needed to act like a real adapter)
  44. if(options.select && _.isArray(options.select) && options.select.length) {
  45. // Force ID in query
  46. options.select.push('id');
  47. results = _.map(results, function(result) {
  48. return _.pick(result, options.select);
  49. });
  50. }
  51. cb(null, results);
  52. },
  53. create: function (connectionName, collectionName, data, cb, connection) {
  54. var schemaData = _.pick(data, ['id', 'name', 'age']);
  55. persistentData.push(schemaData);
  56. cb(null, data);
  57. },
  58. drop: function (connectionName, collectionName, relations, cb, connection) {
  59. persistentData = [];
  60. cb(null);
  61. }
  62. };
  63. var waterline = new Waterline();
  64. // Build up a model to test
  65. var PersonModel = {
  66. identity: 'Person',
  67. tableName: 'person_table',
  68. datastore: 'test_alter',
  69. migrate: 'alter',
  70. adapter: 'fake',
  71. schema: true,
  72. attributes: {
  73. name: 'string',
  74. age: 'integer',
  75. id: 'integer'
  76. }
  77. };
  78. // Create a connection using the stub adapter we created above
  79. var connections = {
  80. 'test_alter': {
  81. adapter: 'fake'
  82. }
  83. };
  84. // Build up the named adapters object
  85. var adapters = {fake: adapter};
  86. // Build the collections and find the record
  87. var PersonCollection = Waterline.Model.extend(PersonModel);
  88. waterline.registerModel(PersonCollection);
  89. waterline.initialize({adapters: adapters, datastores: connections}, function (err, data) {
  90. if (err) return done(err);
  91. MigrateHelper(data, function(err) {
  92. data.collections.person.findOne({id: 1}, function (err, found) {
  93. if (err) return done(err);
  94. record = found;
  95. done();
  96. });
  97. });
  98. });
  99. });
  100. it('should keep data already in the data store', function() {
  101. assert(record);
  102. });
  103. it('should include the attributes in the schema', function() {
  104. assert.equal(record.id, 1);
  105. assert.equal(record.name, 'batman');
  106. assert.equal(record.age, 50);
  107. });
  108. it('should not include the attributes NOT in the schema', function() {
  109. assert(!record.car);
  110. });
  111. });