@na1647/offshore-adapter-tests 0.0.0-stage → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (224) hide show
  1. package/.eslintignore +1 -0
  2. package/.eslintrc +35 -0
  3. package/.travis.yml +32 -0
  4. package/CHANGELOG.md +24 -0
  5. package/Dockerfile +7 -0
  6. package/LICENSE.md +11 -0
  7. package/README.md +71 -2
  8. package/docker-compose.yml +32 -0
  9. package/features/autoIncrement/README.md +37 -0
  10. package/features/autoIncrement/core/autoIncrement.test.js +151 -0
  11. package/features/autoIncrement/sequential/autoIncrementSequential.test.js +104 -0
  12. package/features/autoIncrement/support/autoInc.fixture.js +25 -0
  13. package/features/compositePrimaryKey/core/compositePrimaryKey.test.js +67 -0
  14. package/features/compositePrimaryKey/support/fixture.js +24 -0
  15. package/features/compositeUnique/core/compositeUnique.test.js +67 -0
  16. package/features/compositeUnique/support/fixture.js +30 -0
  17. package/features/crossAdapter/core/crossAdapter.js +119 -0
  18. package/features/crossAdapter/support/bootstrap.js +106 -0
  19. package/features/sequences/core/sequences.test.js +110 -0
  20. package/features/sequences/support/mariadb.js +23 -0
  21. package/features/sequences/support/oracle.js +23 -0
  22. package/features/sequences/support/postgres.js +23 -0
  23. package/features/spatial/core/geojson.test.js +68 -0
  24. package/features/spatial/support/fixtures.js +82 -0
  25. package/features/spatial/support/models.js +45 -0
  26. package/features/unique/README.md +18 -0
  27. package/features/unique/core/unique.test.js +107 -0
  28. package/features/unique/support/unique.fixture.js +26 -0
  29. package/interfaces/associations/belongsTo/create.js +41 -0
  30. package/interfaces/associations/belongsTo/create.nested.customPK.js +61 -0
  31. package/interfaces/associations/belongsTo/create.nested.js +52 -0
  32. package/interfaces/associations/belongsTo/dynamicFinder.js +213 -0
  33. package/interfaces/associations/belongsTo/find.populate.js +145 -0
  34. package/interfaces/associations/belongsTo/findOne.populate.js +84 -0
  35. package/interfaces/associations/belongsTo/modifiers.js +275 -0
  36. package/interfaces/associations/belongsTo/multipleForeignKeys.js +70 -0
  37. package/interfaces/associations/belongsTo/update.nested.customPK.js +141 -0
  38. package/interfaces/associations/belongsTo/update.nested.js +174 -0
  39. package/interfaces/associations/deep.associations.test.js +437 -0
  40. package/interfaces/associations/find.associations.test.js +92 -0
  41. package/interfaces/associations/hasMany/add.js +106 -0
  42. package/interfaces/associations/hasMany/create.nested.customPK.js +113 -0
  43. package/interfaces/associations/hasMany/create.nested.js +96 -0
  44. package/interfaces/associations/hasMany/find.populate.customPK.js +75 -0
  45. package/interfaces/associations/hasMany/find.populate.js +219 -0
  46. package/interfaces/associations/hasMany/find.populate.where.js +148 -0
  47. package/interfaces/associations/hasMany/findOne.populate.js +88 -0
  48. package/interfaces/associations/hasMany/modifiers.js +560 -0
  49. package/interfaces/associations/hasMany/multipleForeignKeys.js +113 -0
  50. package/interfaces/associations/hasMany/remove.js +96 -0
  51. package/interfaces/associations/hasMany/update.nested.customPK.js +148 -0
  52. package/interfaces/associations/hasMany/update.nested.js +202 -0
  53. package/interfaces/associations/manyToMany/add.js +136 -0
  54. package/interfaces/associations/manyToMany/create.nested.customPK.js +109 -0
  55. package/interfaces/associations/manyToMany/create.nested.js +96 -0
  56. package/interfaces/associations/manyToMany/destroy.js +128 -0
  57. package/interfaces/associations/manyToMany/find.populate.js +109 -0
  58. package/interfaces/associations/manyToMany/find.populate.where.js +75 -0
  59. package/interfaces/associations/manyToMany/find.populate.withSchema.js +91 -0
  60. package/interfaces/associations/manyToMany/findOne.populate.js +86 -0
  61. package/interfaces/associations/manyToMany/modifiers.js +558 -0
  62. package/interfaces/associations/manyToMany/remove.js +103 -0
  63. package/interfaces/associations/manyToMany/update.js +105 -0
  64. package/interfaces/associations/manyToMany/update.nested.customPK.js +315 -0
  65. package/interfaces/associations/manyToMany/update.nested.js +355 -0
  66. package/interfaces/associations/manyToManyThrough/add.js +109 -0
  67. package/interfaces/associations/manyToManyThrough/create.nested.customPK.js +118 -0
  68. package/interfaces/associations/manyToManyThrough/create.nested.js +114 -0
  69. package/interfaces/associations/manyToManyThrough/find.populate.customPK.js +76 -0
  70. package/interfaces/associations/manyToManyThrough/find.populate.js +120 -0
  71. package/interfaces/associations/manyToManyThrough/find.populate.where.js +157 -0
  72. package/interfaces/associations/manyToManyThrough/findOne.populate.js +93 -0
  73. package/interfaces/associations/manyToManyThrough/modifiers.js +560 -0
  74. package/interfaces/associations/manyToManyThrough/multipleForeignKeys.js +118 -0
  75. package/interfaces/associations/manyToManyThrough/remove.js +98 -0
  76. package/interfaces/associations/manyToManyThrough/update.nested.customPK.js +150 -0
  77. package/interfaces/associations/manyToManyThrough/update.nested.js +202 -0
  78. package/interfaces/associations/oneToOne/create.js +41 -0
  79. package/interfaces/associations/oneToOne/create.nested.customPK.js +61 -0
  80. package/interfaces/associations/oneToOne/create.nested.js +52 -0
  81. package/interfaces/associations/oneToOne/dynamicFinder.js +68 -0
  82. package/interfaces/associations/oneToOne/find.populate.js +127 -0
  83. package/interfaces/associations/oneToOne/findOne.populate.js +83 -0
  84. package/interfaces/associations/oneToOne/modifiers.js +275 -0
  85. package/interfaces/associations/oneToOne/multipleForeignKeys.js +69 -0
  86. package/interfaces/associations/oneToOne/update.nested.customPK.js +141 -0
  87. package/interfaces/associations/oneToOne/update.nested.js +174 -0
  88. package/interfaces/associations/support/bootstrap.js +82 -0
  89. package/interfaces/associations/support/fixtures/belongsTo.child.customPK.fixture.js +25 -0
  90. package/interfaces/associations/support/fixtures/belongsTo.child.fixture.js +25 -0
  91. package/interfaces/associations/support/fixtures/belongsTo.parent.customPK.fixture.js +26 -0
  92. package/interfaces/associations/support/fixtures/belongsTo.parent.fixture.js +28 -0
  93. package/interfaces/associations/support/fixtures/crud.fixture.js +53 -0
  94. package/interfaces/associations/support/fixtures/deep.address.fixture.js +21 -0
  95. package/interfaces/associations/support/fixtures/deep.breakdown.fixture.js +23 -0
  96. package/interfaces/associations/support/fixtures/deep.company.fixture.js +29 -0
  97. package/interfaces/associations/support/fixtures/deep.country.fixture.js +25 -0
  98. package/interfaces/associations/support/fixtures/deep.departement.fixture.js +24 -0
  99. package/interfaces/associations/support/fixtures/deep.driver.fixture.js +32 -0
  100. package/interfaces/associations/support/fixtures/deep.ride.fixture.js +23 -0
  101. package/interfaces/associations/support/fixtures/deep.seller.fixture.js +33 -0
  102. package/interfaces/associations/support/fixtures/deep.taxi.fixture.js +36 -0
  103. package/interfaces/associations/support/fixtures/hasMany.child.fixture.js +33 -0
  104. package/interfaces/associations/support/fixtures/hasMany.customPK.fixture.js +22 -0
  105. package/interfaces/associations/support/fixtures/hasMany.parent.fixture.js +28 -0
  106. package/interfaces/associations/support/fixtures/hasManyThrough.child.customPK.fixture.js +32 -0
  107. package/interfaces/associations/support/fixtures/hasManyThrough.parent.customPK.fixture.js +23 -0
  108. package/interfaces/associations/support/fixtures/hasManyThrough.stadium.fixture.js +20 -0
  109. package/interfaces/associations/support/fixtures/hasManyThrough.team.fixture.js +35 -0
  110. package/interfaces/associations/support/fixtures/hasManyThrough.through.customPK.fixture.js +18 -0
  111. package/interfaces/associations/support/fixtures/hasManyThrough.venue.fixture.js +18 -0
  112. package/interfaces/associations/support/fixtures/manyToMany.driver.customPK.fixture.js +22 -0
  113. package/interfaces/associations/support/fixtures/manyToMany.driver.fixture.js +25 -0
  114. package/interfaces/associations/support/fixtures/manyToMany.driver.withSchema.fixture.js +25 -0
  115. package/interfaces/associations/support/fixtures/manyToMany.taxi.customPK.fixture.js +21 -0
  116. package/interfaces/associations/support/fixtures/manyToMany.taxi.fixture.js +30 -0
  117. package/interfaces/associations/support/fixtures/manyToMany.taxi.withSchema.fixture.js +24 -0
  118. package/interfaces/associations/support/fixtures/multipleAssociations.fixture.js +54 -0
  119. package/interfaces/associations/support/fixtures/multipleOneToOneAssociations.fixture.js +40 -0
  120. package/interfaces/associations/support/fixtures/multipleThroughAssociations.fixture.js +79 -0
  121. package/interfaces/associations/support/fixtures/oneToOne.customPK.fixture.js +47 -0
  122. package/interfaces/associations/support/fixtures/oneToOne.fixture.js +52 -0
  123. package/interfaces/associations/support/fixtures.js +41 -0
  124. package/interfaces/datastore/datastore.test.js +63 -0
  125. package/interfaces/migratable/definition.test.js +65 -0
  126. package/interfaces/migratable/migrate.alter.test.js +55 -0
  127. package/interfaces/migratable/migrate.create.test.js +120 -0
  128. package/interfaces/migratable/migrate.drop.test.js +47 -0
  129. package/interfaces/migratable/migrate.safe.test.js +25 -0
  130. package/interfaces/migratable/schema.test.js +117 -0
  131. package/interfaces/migratable/support/bootstrap.js +55 -0
  132. package/interfaces/migratable/support/bootstrapFn.js +54 -0
  133. package/interfaces/migratable/support/fixtures/alter.fixture.js +12 -0
  134. package/interfaces/migratable/support/fixtures/create.fixture.js +12 -0
  135. package/interfaces/migratable/support/fixtures/crud.fixture.js +53 -0
  136. package/interfaces/migratable/support/fixtures/custom.fixture.js +17 -0
  137. package/interfaces/migratable/support/fixtures/drop.fixture.js +12 -0
  138. package/interfaces/migratable/support/fixtures/safe.fixture.js +12 -0
  139. package/interfaces/migratable/support/fixtures/schema.fixture.js +23 -0
  140. package/interfaces/queryable/caseInsensitive.test.js +196 -0
  141. package/interfaces/queryable/count.test.js +51 -0
  142. package/interfaces/queryable/dynamicFinders.test.js +179 -0
  143. package/interfaces/queryable/findLike.test.js +34 -0
  144. package/interfaces/queryable/findOneLike.test.js +28 -0
  145. package/interfaces/queryable/modifiers/average.modifier.test.js +55 -0
  146. package/interfaces/queryable/modifiers/contains.modifier.test.js +82 -0
  147. package/interfaces/queryable/modifiers/endsWith.modifier.test.js +82 -0
  148. package/interfaces/queryable/modifiers/greaterThan.modifier.test.js +335 -0
  149. package/interfaces/queryable/modifiers/groupBy.modifier.test.js +123 -0
  150. package/interfaces/queryable/modifiers/in.modifier.test.js +76 -0
  151. package/interfaces/queryable/modifiers/lessThan.modifier.test.js +223 -0
  152. package/interfaces/queryable/modifiers/like.modifier.test.js +50 -0
  153. package/interfaces/queryable/modifiers/limit.modifier.test.js +60 -0
  154. package/interfaces/queryable/modifiers/max.modifier.test.js +68 -0
  155. package/interfaces/queryable/modifiers/min.modifier.test.js +68 -0
  156. package/interfaces/queryable/modifiers/not.modifier.test.js +80 -0
  157. package/interfaces/queryable/modifiers/notIn.modifier.test.js +69 -0
  158. package/interfaces/queryable/modifiers/or.modifier.test.js +142 -0
  159. package/interfaces/queryable/modifiers/select.modifier.test.js +118 -0
  160. package/interfaces/queryable/modifiers/skip.modifier.test.js +59 -0
  161. package/interfaces/queryable/modifiers/sort.modifier.test.js +160 -0
  162. package/interfaces/queryable/modifiers/startsWith.modifier.test.js +83 -0
  163. package/interfaces/queryable/modifiers/sum.modifier.test.js +68 -0
  164. package/interfaces/queryable/support/bootstrap.js +80 -0
  165. package/interfaces/queryable/support/fixtures/crud.fixture.js +54 -0
  166. package/interfaces/scalable/README.md +19 -0
  167. package/interfaces/scalable/crud.test.js +73 -0
  168. package/interfaces/scalable/queries.test.js +9 -0
  169. package/interfaces/scalable/support/crud.fixture.js +60 -0
  170. package/interfaces/semantic/create.test.js +110 -0
  171. package/interfaces/semantic/createEach.test.js +50 -0
  172. package/interfaces/semantic/destroy.js +147 -0
  173. package/interfaces/semantic/find.test.js +91 -0
  174. package/interfaces/semantic/findOne.test.js +60 -0
  175. package/interfaces/semantic/findOrCreate.test.js +91 -0
  176. package/interfaces/semantic/findOrCreateEach.test.js +75 -0
  177. package/interfaces/semantic/support/bootstrap.js +85 -0
  178. package/interfaces/semantic/support/fixtures/crud.fixture.js +66 -0
  179. package/interfaces/semantic/support/fixtures/validations.fixture.js +23 -0
  180. package/interfaces/semantic/types/array.type.js +28 -0
  181. package/interfaces/semantic/types/binary.type.js +34 -0
  182. package/interfaces/semantic/types/boolean.type.js +80 -0
  183. package/interfaces/semantic/types/date.type.js +53 -0
  184. package/interfaces/semantic/types/datetime.type.js +48 -0
  185. package/interfaces/semantic/types/float.type.js +40 -0
  186. package/interfaces/semantic/types/integer.type.js +40 -0
  187. package/interfaces/semantic/types/json.type.js +29 -0
  188. package/interfaces/semantic/types/string.type.js +40 -0
  189. package/interfaces/semantic/types/text.type.js +34 -0
  190. package/interfaces/semantic/types/time.type.js +52 -0
  191. package/interfaces/semantic/update.test.js +157 -0
  192. package/interfaces/sql/find.test.js +73 -0
  193. package/interfaces/sql/query.test.js +40 -0
  194. package/interfaces/sql/support/bootstrap.js +82 -0
  195. package/interfaces/sql/support/fixtures/crud.fixture.js +57 -0
  196. package/interfaces/transactable/commit.test.js +438 -0
  197. package/interfaces/transactable/crossAdapter.test.js +346 -0
  198. package/interfaces/transactable/deep.test.js +100 -0
  199. package/interfaces/transactable/rollback.test.js +221 -0
  200. package/interfaces/transactable/support/bootstrap.js +55 -0
  201. package/interfaces/transactable/support/bootstrapFn.js +51 -0
  202. package/interfaces/transactable/support/fixtures/customer.fixture.js +29 -0
  203. package/interfaces/transactable/support/fixtures/payment.fixture.js +24 -0
  204. package/interfaces/transactable/support/fixtures/receipt.fixture.js +20 -0
  205. package/interfaces/transactable/support/fixtures/store.fixture.js +22 -0
  206. package/interfaces/transactable/support/fixtures/storecustomer.fixture.js +14 -0
  207. package/lib/index.js +155 -0
  208. package/lib/utils.js +39 -0
  209. package/package.json +58 -4
  210. package/test/integration/README.md +26 -0
  211. package/test/integration/config/offshore-memory.js +8 -0
  212. package/test/integration/config/offshore-sql-mariadb.js +15 -0
  213. package/test/integration/config/offshore-sql-mysql.js +21 -0
  214. package/test/integration/config/offshore-sql-oracle.js +19 -0
  215. package/test/integration/config/offshore-sql-postgres.js +27 -0
  216. package/test/integration/config/offshore-sql-sqlite3.js +19 -0
  217. package/test/integration/runner.js +83 -0
  218. package/test/integration/runnerDispatcher.js +163 -0
  219. package/test/load/README.md +20 -0
  220. package/test/load/index.js +33 -0
  221. package/test/load/runner.js +160 -0
  222. package/test/load/support/bootstrap.js +69 -0
  223. package/test/load/support/fixtures/pet.js +8 -0
  224. package/test/load/support/fixtures/user.js +9 -0
@@ -0,0 +1,355 @@
1
+ var assert = require('assert'),
2
+ Promise = require('bluebird'),
3
+ _ = require('lodash');
4
+
5
+ describe('Association Interface', function() {
6
+
7
+ describe('n:m association :: .update()', function() {
8
+ describe('update nested associations()', function() {
9
+
10
+ describe('with single level depth', function() {
11
+
12
+ describe('when association doesn\'t exist', function() {
13
+
14
+ /////////////////////////////////////////////////////
15
+ // TEST SETUP
16
+ ////////////////////////////////////////////////////
17
+
18
+ var Driver;
19
+
20
+ before(function(done) {
21
+ // Check Driver hasManytoMany Taxis
22
+ assert.strictEqual(Associations.Driver.attributes.taxis.collection, 'taxi');
23
+ assert.strictEqual(Associations.Taxi.attributes.drivers.collection, 'driver');
24
+
25
+ Associations.Driver.create({ name: 'm:m update nested' }).exec(function(err, driver) {
26
+ assert.ifError(err);
27
+ Driver = driver;
28
+ done();
29
+ });
30
+
31
+ });
32
+
33
+
34
+ /////////////////////////////////////////////////////
35
+ // TEST METHODS
36
+ ////////////////////////////////////////////////////
37
+
38
+ it('should create a new taxi and associate it with a driver', function(done) {
39
+
40
+ var taxiData = {
41
+ name: 'm:m update nested - updated',
42
+ taxis: [
43
+ { medallion: 1 }
44
+ ]
45
+ };
46
+
47
+ Associations.Driver.update({ id: Driver.id }, taxiData).exec(function(err, values) {
48
+ assert.ifError(err);
49
+
50
+ // Look up the driver again to be sure the taxis were added
51
+ Associations.Driver.findOne(values[0].id)
52
+ .populate('taxis')
53
+ .exec(function(err, model) {
54
+ assert.ifError(err);
55
+ assert.equal(model.name, 'm:m update nested - updated');
56
+ assert.strictEqual(model.taxis.length, 1);
57
+ assert.strictEqual(model.taxis[0].medallion, 1);
58
+ done();
59
+ });
60
+
61
+ });
62
+ });
63
+ });
64
+
65
+
66
+ describe('when associations already exist', function() {
67
+
68
+ /////////////////////////////////////////////////////
69
+ // TEST SETUP
70
+ ////////////////////////////////////////////////////
71
+
72
+ var Driver;
73
+
74
+ before(function(done) {
75
+
76
+ var data = {
77
+ name: 'm:m update nested',
78
+ taxis: [
79
+ { medallion: 1 },
80
+ { medallion: 2 }
81
+ ]
82
+ };
83
+
84
+ Associations.Driver.create(data).exec(function(err, driver) {
85
+ assert.ifError(err);
86
+ Driver = driver;
87
+ done();
88
+ });
89
+
90
+ });
91
+
92
+
93
+ /////////////////////////////////////////////////////
94
+ // TEST METHODS
95
+ ////////////////////////////////////////////////////
96
+
97
+ it('should reset associations with the updated associations', function(done) {
98
+
99
+ var taxiData = {
100
+ name: 'm:m update nested - updated',
101
+ taxis: [
102
+ { medallion: 3 },
103
+ { medallion: 4 },
104
+ { medallion: 5 }
105
+ ]
106
+ };
107
+
108
+ Associations.Driver.update({ id: Driver.id }, taxiData).exec(function(err, values) {
109
+ assert.ifError(err);
110
+
111
+ // Look up the driver again to be sure the taxis were added
112
+ Associations.Driver.findOne(values[0].id)
113
+ .populate('taxis',{sort : {medallion : 1}})
114
+ .exec(function(err, model) {
115
+ assert(!err, 'Error: ' + err);
116
+ assert.equal(model.name, 'm:m update nested - updated');
117
+ assert.strictEqual(model.taxis.length, 3);
118
+ assert.strictEqual(model.taxis[0].medallion, 3);
119
+ assert.strictEqual(model.taxis[1].medallion, 4);
120
+ assert.strictEqual(model.taxis[2].medallion, 5);
121
+ done();
122
+ });
123
+
124
+ });
125
+ });
126
+ });
127
+
128
+ describe('when associations have primary keys', function() {
129
+
130
+ /////////////////////////////////////////////////////
131
+ // TEST SETUP
132
+ ////////////////////////////////////////////////////
133
+
134
+ var Driver, Taxis;
135
+
136
+ before(function(done) {
137
+
138
+ var taxiData = [
139
+ { medallion: 1 },
140
+ { medallion: 2 }
141
+ ];
142
+
143
+ var data = {
144
+ name: 'm:m update nested',
145
+ taxis: [
146
+ { medallion: 100 },
147
+ { medallion: 200 }
148
+ ]
149
+ };
150
+
151
+ Associations.Taxi.create(taxiData).exec(function(err, taxis) {
152
+ assert.ifError(err);
153
+ Taxis = taxis;
154
+
155
+ Associations.Driver.create(data).exec(function(err, driver) {
156
+ assert.ifError(err);
157
+ Driver = driver;
158
+ done();
159
+ });
160
+ });
161
+ });
162
+
163
+
164
+ /////////////////////////////////////////////////////
165
+ // TEST METHODS
166
+ ////////////////////////////////////////////////////
167
+
168
+ it('should update association values', function(done) {
169
+
170
+ var taxiData = {
171
+ name: 'm:m update nested - updated',
172
+ taxis: Taxis.map(function(taxi) { return taxi.toObject(); })
173
+ };
174
+
175
+ Associations.Driver.update({ id: Driver.id }, taxiData).exec(function(err, values) {
176
+ assert.ifError(err);
177
+
178
+ // Look up the driver again to be sure the taxis were added
179
+ Associations.Driver.findOne(values[0].id)
180
+ .populate('taxis',{sort : {medallion : 1}})
181
+ .exec(function(err, model) {
182
+ assert.ifError(err);
183
+ assert.equal(model.name, 'm:m update nested - updated');
184
+ assert.strictEqual(model.taxis.length, 2);
185
+
186
+ // Ensure association values were updated
187
+ assert.strictEqual(model.taxis[0].medallion, 1);
188
+ assert.strictEqual(model.taxis[1].medallion, 2);
189
+
190
+ done();
191
+ });
192
+
193
+ });
194
+ });
195
+
196
+ });
197
+
198
+
199
+ describe('when associations already exist', function() {
200
+
201
+ /////////////////////////////////////////////////////
202
+ // TEST SETUP
203
+ ////////////////////////////////////////////////////
204
+
205
+ var Driver;
206
+
207
+ before(function(done) {
208
+
209
+ var data = {
210
+ name: 'm:m update nested save',
211
+ taxis: [ { medallion: 1000 } ]
212
+ };
213
+
214
+ Associations.Driver.create(data).exec(function(err, driver) {
215
+ assert.ifError(err);
216
+ Driver = driver;
217
+ done();
218
+ });
219
+
220
+ });
221
+
222
+
223
+ /////////////////////////////////////////////////////
224
+ // TEST METHODS
225
+ ////////////////////////////////////////////////////
226
+
227
+ it('should update association values with save()', function(done) {
228
+
229
+ Associations.Driver.findOne({ id: Driver.id })
230
+ .populate('taxis')
231
+ .exec(function(err, model) {
232
+ var taxi = model.taxis[0];
233
+ taxi.medallion = 1001;
234
+ taxi.save(function(err){
235
+ assert.ifError(err);
236
+
237
+ Associations.Driver.findOne({ id: Driver.id })
238
+ .populate('taxis')
239
+ .exec(function(err, model) {
240
+ assert.ifError(err);
241
+ assert.strictEqual(model.taxis.length, 1);
242
+ assert.strictEqual(model.taxis[0].medallion, 1001);
243
+ done();
244
+ });
245
+ });
246
+
247
+ });
248
+ });
249
+ });
250
+
251
+
252
+ describe('when associations are sync\'ed rapidly', function() {
253
+
254
+ /////////////////////////////////////////////////////
255
+ // TEST SETUP
256
+ ////////////////////////////////////////////////////
257
+
258
+ var Drivers;
259
+ var Taxis;
260
+
261
+ before(function(done) {
262
+
263
+ var driversData = [
264
+ {name: "Rapid 0"},
265
+ {name: "Rapid 1"},
266
+ {name: "Rapid 2"},
267
+ {name: "Rapid 3"}
268
+ ];
269
+
270
+ var taxisData = [
271
+ {medallion: 200},
272
+ {medallion: 201},
273
+ {medallion: 202},
274
+ {medallion: 203}
275
+ ];
276
+
277
+ Associations.Driver.createEach(driversData).exec(function(err, drivers) {
278
+ assert.ifError(err);
279
+ Drivers = drivers;
280
+
281
+ Associations.Taxi.createEach(taxisData).exec(function(err, taxis) {
282
+ assert.ifError(err);
283
+ Taxis = taxis;
284
+
285
+ done();
286
+ });
287
+
288
+ });
289
+
290
+ });
291
+
292
+
293
+ /////////////////////////////////////////////////////
294
+ // TEST METHODS
295
+ ////////////////////////////////////////////////////
296
+
297
+ it('should create the correct associations', function(done) {
298
+
299
+ // Construct promises
300
+ var associationMap = {};
301
+ var associationPromises = [];
302
+
303
+ var driverId, taxiId;
304
+ for (var i = 0, il = Drivers.length; i < il; ++i) {
305
+
306
+ driverId = _.find(Drivers, {name: "Rapid " + i}).id;
307
+ taxiId = _.find(Taxis, {medallion: 200 + i}).id;
308
+
309
+ // Set map for testing later. Maps driver to taxi being associated.
310
+ associationMap[driverId] = taxiId;
311
+
312
+ associationPromises.push(
313
+ Associations.Driver.update(driverId, {taxis: [{id: taxiId}]})
314
+ );
315
+ }
316
+
317
+ // Perform all updates at once
318
+ Promise.all(associationPromises)
319
+ .then(function(results){
320
+
321
+ // Check to see if associations were created as expected
322
+ Associations.Driver.find({id: _.map(Drivers, 'id')})
323
+ .populate('taxis')
324
+ .exec(function(err, drivers) {
325
+
326
+ if (err) return done(err);
327
+
328
+ assert.equal(drivers.length, Drivers.length);
329
+
330
+ // Ensure the correct operations happened per record using associationMap
331
+ var driver;
332
+ for (var i = 0, il = drivers.length; i < il; ++i) {
333
+
334
+ driver = drivers[i];
335
+
336
+ assert(typeof driver === "object" && driver !== null);
337
+ assert(Array.isArray(driver.taxis));
338
+ assert.equal(driver.taxis.length, 1);
339
+ assert.equal(driver.taxis[0].id, associationMap[driver.id]);
340
+ }
341
+
342
+ done();
343
+
344
+ });
345
+
346
+ })
347
+ .catch(done);
348
+
349
+ });
350
+ });
351
+
352
+ });
353
+ });
354
+ });
355
+ });
@@ -0,0 +1,109 @@
1
+ var assert = require('assert'),
2
+ _ = require('lodash');
3
+
4
+ describe('Association Interface', function() {
5
+
6
+ describe('Has Many Through Association', function() {
7
+ describe('association .add()', function() {
8
+
9
+ describe('with an object', function() {
10
+
11
+ /////////////////////////////////////////////////////
12
+ // TEST SETUP
13
+ ////////////////////////////////////////////////////
14
+
15
+ var stadiumRecord;
16
+
17
+ before(function(done) {
18
+ // Check Stadium hasManytoMany Team through Venue
19
+ assert.strictEqual(Associations.Stadium.attributes.teams.collection, 'team');
20
+ assert.strictEqual(Associations.Team.attributes.stadiums.collection, 'stadium');
21
+ assert.strictEqual(Associations.Venue.attributes.stadium.model, 'stadium');
22
+ assert.strictEqual(Associations.Venue.attributes.team.model, 'team');
23
+
24
+ Associations.Stadium.create({ name: 'hasMany through add stadium 1' }, function(err, model) {
25
+ assert.ifError(err);
26
+
27
+ stadiumRecord = model;
28
+ Associations.Team.create({ name: 'hasMany through add team 1', stadiums: stadiumRecord.id }, done);
29
+ });
30
+ });
31
+
32
+ /////////////////////////////////////////////////////
33
+ // TEST METHODS
34
+ ////////////////////////////////////////////////////
35
+
36
+ it('should create a new team association', function(done) {
37
+ stadiumRecord.teams.add({ name: 'hasMany through add team 2' });
38
+ stadiumRecord.save(function(err) {
39
+ assert.ifError(err);
40
+
41
+ // Look up the stadium again to be sure the team was added
42
+ Associations.Stadium.findOne(stadiumRecord.id)
43
+ .populate('teams', {sort: 'id asc'})
44
+ .exec(function(err, model) {
45
+ assert.ifError(err);
46
+
47
+ assert.strictEqual(model.teams.length, 2);
48
+ assert.strictEqual(model.teams[0].name, 'hasMany through add team 1');
49
+ assert.strictEqual(model.teams[1].name, 'hasMany through add team 2');
50
+ done();
51
+ });
52
+ });
53
+ });
54
+ });
55
+
56
+ describe('with an id', function() {
57
+
58
+ /////////////////////////////////////////////////////
59
+ // TEST SETUP
60
+ ////////////////////////////////////////////////////
61
+
62
+ var stadiumRecord, teamRecord;
63
+
64
+ before(function(done) {
65
+
66
+ var records = [
67
+ { name: 'hasMany through add stadium 2' },
68
+ { name: 'hasMany through add stadium 3' }
69
+ ];
70
+
71
+ Associations.Stadium.createEach(records, function(err, models) {
72
+ assert.ifError(err);
73
+
74
+ stadiumRecord = models[0];
75
+ Associations.Team.create({ name: 'hasMany through add team 3', stadiums: models[1].id }, function(err, teamModel) {
76
+ assert.ifError(err);
77
+
78
+ teamRecord = teamModel;
79
+ done();
80
+ });
81
+ });
82
+ });
83
+
84
+ /////////////////////////////////////////////////////
85
+ // TEST METHODS
86
+ ////////////////////////////////////////////////////
87
+
88
+ it('should link the team to another association', function(done) {
89
+ stadiumRecord.teams.add(teamRecord.id);
90
+ stadiumRecord.save(function(err) {
91
+ assert.ifError(err);
92
+
93
+ // Look up the stadium again to be sure the team was added
94
+ Associations.Stadium.findOne(stadiumRecord.id)
95
+ .populate('teams')
96
+ .exec(function(err, data) {
97
+ assert.ifError(err);
98
+
99
+ assert.strictEqual(data.teams.length, 1);
100
+ assert.strictEqual(data.teams[0].name, 'hasMany through add team 3');
101
+ done();
102
+ });
103
+ });
104
+ });
105
+ });
106
+
107
+ });
108
+ });
109
+ });
@@ -0,0 +1,118 @@
1
+ var assert = require('assert');
2
+ var _ = require('lodash');
3
+
4
+ describe('Association Interface', function() {
5
+
6
+ describe('Has Many Through Association', function() {
7
+ describe('create nested through associations when a custom primary key is used()', function() {
8
+ describe('with single level depth', function() {
9
+
10
+ describe('and objects', function() {
11
+
12
+ before(function(done) {
13
+ // Check Stadium hasManytoMany Team through Venue
14
+ assert.strictEqual(Associations.Stadiumcustom.attributes.teams.collection, 'teamcustom');
15
+ assert.strictEqual(Associations.Teamcustom.attributes.stadiums.collection, 'stadiumcustom');
16
+ assert.strictEqual(Associations.Venuecustom.attributes.stadium.model, 'stadiumcustom');
17
+ assert.strictEqual(Associations.Venuecustom.attributes.team.model, 'teamcustom');
18
+ done();
19
+ });
20
+
21
+ /////////////////////////////////////////////////////
22
+ // TEST METHODS
23
+ ////////////////////////////////////////////////////
24
+
25
+ it('should create a new stadium and team through association', function(done) {
26
+
27
+ var data = {
28
+ address: 'address 1',
29
+ name: 'has many through nested',
30
+ teams: [
31
+ { location: 'Stark tower', name: 'Avengers', mascot: 'Hulk' },
32
+ { location: 'Space station', name: 'Justice League', mascot: 'Batman' }
33
+ ]
34
+ };
35
+ Associations.Stadiumcustom.create(data)
36
+ .exec(function(err, values) {
37
+ assert.ifError(err);
38
+ // Look up the stadium again to be sure the teams were added
39
+ Associations.Stadiumcustom.findOne({address: values.address})
40
+ .populate('teams', { sort: 'name ASC' })
41
+ .exec(function(err, model) {
42
+ assert.ifError(err);
43
+
44
+ assert(model, 'should find the parent');
45
+ assert.equal(model.teams.length, 2);
46
+
47
+ assert.equal(model.teams[0].location, 'Stark tower');
48
+ assert.equal(model.teams[0].name, 'Avengers');
49
+ assert.equal(model.teams[0].mascot, 'Hulk');
50
+
51
+ assert.equal(model.teams[1].location, 'Space station');
52
+ assert.equal(model.teams[1].name, 'Justice League');
53
+ assert.equal(model.teams[1].mascot, 'Batman');
54
+
55
+ done();
56
+ });
57
+
58
+ });
59
+ });
60
+ });
61
+
62
+ describe('and objects mixed with ids', function() {
63
+ var idTeam;
64
+ before(function(done) {
65
+ Associations.Teamcustom.create({ location: 'Xavier institute', name: 'X-Men', mascot: 'Wolverine' })
66
+ .exec(function(err, team) {
67
+ assert.ifError(err);
68
+ idTeam = team.location;
69
+ done();
70
+ });
71
+ });
72
+
73
+ /////////////////////////////////////////////////////
74
+ // TEST METHODS
75
+ ////////////////////////////////////////////////////
76
+
77
+ it('should create a new stadium and team through association', function(done) {
78
+
79
+ var data = {
80
+ address: 'address 2',
81
+ name: 'mixed custom through create',
82
+ teams: [
83
+ idTeam,
84
+ { location: 'Copyright hell', name : 'Fantastic Four', mascot: 'Dr Doom' }
85
+ ]
86
+ };
87
+
88
+ Associations.Stadiumcustom.create(data)
89
+ .exec(function(err, values) {
90
+ assert.ifError(err);
91
+
92
+ // Look up the stadium again to be sure the teams were added
93
+ Associations.Stadiumcustom.findOne({address: values.address})
94
+ .populate('teams', { sort: 'name ASC' })
95
+ .exec(function(err, model) {
96
+ assert.ifError(err);
97
+ assert(model, 'should find the parent');
98
+ assert.equal(model.teams.length, 2);
99
+
100
+ assert.equal(model.teams[0].location, 'Copyright hell');
101
+ assert.equal(model.teams[0].name, 'Fantastic Four');
102
+ assert.equal(model.teams[0].mascot, 'Dr Doom');
103
+
104
+ assert.equal(model.teams[1].location, 'Xavier institute');
105
+ assert.equal(model.teams[1].name, 'X-Men');
106
+ assert.equal(model.teams[1].mascot, 'Wolverine');
107
+
108
+ done();
109
+ });
110
+
111
+ });
112
+ });
113
+ });
114
+
115
+ });
116
+ });
117
+ });
118
+ });