foreignKeys.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. var assert = require('assert');
  2. var _ = require('@sailshq/lodash');
  3. var SchemaBuilder = require('../lib/waterline-schema/schema');
  4. var ForeignKeyMapper = require('../lib/waterline-schema/foreignKeys');
  5. describe('Foreign Key Mapper :: ', function() {
  6. describe('With automatic column name properties', function() {
  7. var schema;
  8. before(function() {
  9. var fixtures = [
  10. {
  11. identity: 'foo',
  12. primaryKey: 'id',
  13. attributes: {
  14. id: {
  15. type: 'string'
  16. }
  17. }
  18. },
  19. {
  20. identity: 'bar',
  21. primaryKey: 'id',
  22. attributes: {
  23. id: {
  24. type: 'number'
  25. },
  26. foo: {
  27. model: 'foo'
  28. }
  29. }
  30. }
  31. ];
  32. var collections = _.map(fixtures, function(obj) {
  33. var collection = function() {};
  34. collection.prototype = obj;
  35. return collection;
  36. });
  37. // Build the schema
  38. schema = SchemaBuilder(collections);
  39. });
  40. /**
  41. * Test that a foreign key gets built for the bar table in the following structure:
  42. *
  43. * attributes: {
  44. * foo: {
  45. * columnName: 'foo',
  46. * type: 'number',
  47. * foreignKey: true,
  48. * references: 'foo',
  49. * on: 'id'
  50. * }
  51. * }
  52. */
  53. it('should add a foreign key mapping to the bar collection', function() {
  54. // Map out the foreign keys
  55. ForeignKeyMapper(schema);
  56. var barSchema = schema.bar.schema;
  57. assert.equal(barSchema.foo.columnName, 'foo');
  58. assert.equal(barSchema.foo.type, 'string');
  59. assert.equal(barSchema.foo.foreignKey, true);
  60. assert.equal(barSchema.foo.references, 'foo');
  61. assert.equal(barSchema.foo.on, 'id');
  62. });
  63. });
  64. describe('with custom column names', function() {
  65. var schema;
  66. before(function() {
  67. var fixtures = [
  68. {
  69. identity: 'foo',
  70. primaryKey: 'id',
  71. attributes: {
  72. id: {
  73. type: 'string'
  74. }
  75. }
  76. },
  77. {
  78. identity: 'bar',
  79. primaryKey: 'id',
  80. attributes: {
  81. id: {
  82. type: 'number'
  83. },
  84. foo: {
  85. model: 'foo',
  86. columnName: 'xyz_foo_id'
  87. }
  88. }
  89. }
  90. ];
  91. var collections = _.map(fixtures, function(obj) {
  92. var collection = function() {};
  93. collection.prototype = obj;
  94. return collection;
  95. });
  96. // Build the schema
  97. schema = SchemaBuilder(collections);
  98. });
  99. /**
  100. * Test that a foreign key gets built for the bar table in the following structure:
  101. *
  102. * attributes: {
  103. * foo: {
  104. * columnName: 'xyz_foo_id',
  105. * type: 'integer',
  106. * foreignKey: true,
  107. * references: 'foo',
  108. * on: 'id'
  109. * }
  110. * }
  111. */
  112. it('should add a foreign key mapping to the custom column name', function() {
  113. // Map out the foreign keys
  114. ForeignKeyMapper(schema);
  115. var barSchema = schema.bar.schema;
  116. assert.equal(barSchema.foo.columnName, 'xyz_foo_id');
  117. assert.equal(barSchema.foo.type, 'string');
  118. assert.equal(barSchema.foo.foreignKey, true);
  119. assert.equal(barSchema.foo.references, 'foo');
  120. assert.equal(barSchema.foo.on, 'id');
  121. });
  122. });
  123. describe('With custom column type', function() {
  124. var schema;
  125. before(function() {
  126. var fixtures = [
  127. {
  128. identity: 'foo',
  129. primaryKey: 'id',
  130. attributes: {
  131. id: {
  132. type: 'string'
  133. }
  134. }
  135. },
  136. {
  137. identity: 'bar',
  138. primaryKey: 'id',
  139. attributes: {
  140. id: {
  141. type: 'number'
  142. },
  143. foo: {
  144. model: 'foo',
  145. autoMigrations: {
  146. columnType: 'foobar'
  147. }
  148. }
  149. }
  150. }
  151. ];
  152. var collections = _.map(fixtures, function(obj) {
  153. var collection = function() {};
  154. collection.prototype = obj;
  155. return collection;
  156. });
  157. // Build the schema
  158. schema = SchemaBuilder(collections);
  159. });
  160. /**
  161. * Test that a foreign key gets built for the bar table in the following structure:
  162. *
  163. * attributes: {
  164. * foo: {
  165. * columnName: 'foo',
  166. * type: 'number',
  167. * foreignKey: true,
  168. * references: 'foo',
  169. * on: 'id'
  170. * }
  171. * }
  172. */
  173. it('should add a foreign key mapping to the bar collection', function() {
  174. // Map out the foreign keys
  175. ForeignKeyMapper(schema);
  176. var barSchema = schema.bar.schema;
  177. assert.equal(barSchema.foo.columnName, 'foo');
  178. assert.equal(barSchema.foo.type, 'foobar');
  179. assert.equal(barSchema.foo.foreignKey, true);
  180. assert.equal(barSchema.foo.references, 'foo');
  181. assert.equal(barSchema.foo.on, 'id');
  182. });
  183. });
  184. });