@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,103 @@
1
+ var assert = require('assert'),
2
+ _ = require('lodash');
3
+
4
+ describe('Association Interface', function() {
5
+
6
+ describe('n:m association :: .remove()', function() {
7
+ describe('with an id', function() {
8
+
9
+ /////////////////////////////////////////////////////
10
+ // TEST SETUP
11
+ ////////////////////////////////////////////////////
12
+
13
+ var driverRecord, taxiRecords;
14
+
15
+ before(function(done) {
16
+ // Check Driver hasManytoMany Taxis
17
+ assert.strictEqual(Associations.Driver.attributes.taxis.collection, 'taxi');
18
+ assert.strictEqual(Associations.Taxi.attributes.drivers.collection, 'driver');
19
+
20
+ Associations.Driver.create({ name: 'manymany remove' })
21
+ .exec(function(err, model) {
22
+ assert.ifError(err);
23
+
24
+ driverRecord = model;
25
+
26
+ var taxis = [];
27
+ for(var i=0; i<2; i++) {
28
+ driverRecord.taxis.add({ medallion: i });
29
+ }
30
+
31
+ driverRecord.save(function(err) {
32
+ assert.ifError(err);
33
+
34
+ Associations.Driver.findOne(driverRecord.id)
35
+ .populate('taxis')
36
+ .exec(function(err, driver) {
37
+ assert.ifError(err);
38
+ taxiRecords = driver.toObject().taxis;
39
+ done();
40
+ });
41
+ });
42
+ });
43
+ });
44
+
45
+ /////////////////////////////////////////////////////
46
+ // TEST METHODS
47
+ ////////////////////////////////////////////////////
48
+
49
+ it('should remove the record from the join table', function(done) {
50
+ driverRecord.taxis.remove(taxiRecords[0].id);
51
+ driverRecord.save(function(err) {
52
+ assert.ifError(err);
53
+
54
+ // Look up the driver again to be sure the taxi was removed
55
+ Associations.Driver.findOne(driverRecord.id)
56
+ .populate('taxis')
57
+ .exec(function(err, data) {
58
+ assert.ifError(err);
59
+
60
+ assert.strictEqual(data.taxis.length, 1);
61
+ done();
62
+ });
63
+ });
64
+ });
65
+ });
66
+
67
+ describe('with an object', function() {
68
+
69
+ /////////////////////////////////////////////////////
70
+ // TEST SETUP
71
+ ////////////////////////////////////////////////////
72
+
73
+ var driverRecord;
74
+
75
+ before(function(done) {
76
+ Associations.Driver.create({ name: 'manymany remove' })
77
+ .exec(function(err, model) {
78
+ assert.ifError(err);
79
+ driverRecord = model;
80
+ done();
81
+ });
82
+ });
83
+
84
+ /////////////////////////////////////////////////////
85
+ // TEST METHODS
86
+ ////////////////////////////////////////////////////
87
+
88
+ it('should error when an object is passed in', function(done) {
89
+ driverRecord.taxis.remove({ medallion: 1337 });
90
+ driverRecord.save(function(err) {
91
+ assert(err);
92
+ assert(err.failedTransactions);
93
+ assert(Array.isArray(err.failedTransactions));
94
+ assert.strictEqual(err.failedTransactions.length, 1);
95
+ assert.equal(err.failedTransactions[0].type, 'remove');
96
+
97
+ done();
98
+ });
99
+ });
100
+ });
101
+
102
+ });
103
+ });
@@ -0,0 +1,105 @@
1
+ var assert = require('assert'),
2
+ _ = require('lodash');
3
+
4
+ describe('Association Interface', function() {
5
+
6
+ describe('n:m association :: .update()', function() {
7
+
8
+ describe('update parent association()', function() {
9
+
10
+ /////////////////////////////////////////////////////
11
+ // TEST SETUP
12
+ ////////////////////////////////////////////////////
13
+
14
+ var Driver;
15
+
16
+ before(function(done) {
17
+ // Check Driver hasManytoMany Taxis
18
+ assert.strictEqual(Associations.Driver.attributes.taxis.collection, 'taxi');
19
+ assert.strictEqual(Associations.Taxi.attributes.drivers.collection, 'driver');
20
+
21
+ Associations.Driver.create({ name: 'm:m update', taxis: [ { medallion: 1 } ] }).exec(function(err, driver) {
22
+ assert.ifError(err);
23
+ Driver = driver;
24
+ done();
25
+ });
26
+
27
+ });
28
+
29
+
30
+ /////////////////////////////////////////////////////
31
+ // TEST METHODS
32
+ ////////////////////////////////////////////////////
33
+
34
+ it('should update model attributes without modifying nested association', function(done) {
35
+ Associations.Driver.update({ name: 'm:m update' }, { name: 'm:m updated' }, function(err, drivers) {
36
+ assert.ifError(err);
37
+ assert(Array.isArray(drivers));
38
+ assert.equal(drivers.length, 1);
39
+ assert.equal(drivers[0].name, 'm:m updated');
40
+
41
+ Associations.Driver.findOne({ name: 'm:m updated' })
42
+ .populate('taxis')
43
+ .then(function(driver){
44
+ assert(Array.isArray(driver.taxis));
45
+ assert.equal(driver.taxis[0].medallion, 1);
46
+ done();
47
+ })
48
+ .catch(done);
49
+
50
+ });
51
+ });
52
+ });
53
+
54
+
55
+ describe('update parent association() using save()', function() {
56
+
57
+ /////////////////////////////////////////////////////
58
+ // TEST SETUP
59
+ ////////////////////////////////////////////////////
60
+
61
+ var Driver;
62
+
63
+ before(function(done) {
64
+
65
+ Associations.Driver.create({ name: 'm:m update add', taxis: [ { medallion: 2 } ] }).exec(function(err, driver) {
66
+ assert.ifError(err);
67
+ Driver = driver;
68
+ done();
69
+ });
70
+
71
+ });
72
+
73
+
74
+ /////////////////////////////////////////////////////
75
+ // TEST METHODS
76
+ ////////////////////////////////////////////////////
77
+
78
+ it('should update model attributes after find.populate using save()', function(done) {
79
+ Associations.Driver.findOne({ name: 'm:m update add' })
80
+ .populate('taxis')
81
+ .then(function(driver){
82
+
83
+ driver.name = 'm:m updated add';
84
+ driver.save(function(err){
85
+ assert.ifError(err);
86
+
87
+ Associations.Driver.findOne({ name: 'm:m updated add' })
88
+ .populate('taxis')
89
+ .then(function(driver){
90
+ assert(driver);
91
+ assert(Array.isArray(driver.taxis));
92
+ assert.equal(driver.taxis[0].medallion, 2);
93
+ done();
94
+ })
95
+ .catch(done);
96
+
97
+ });
98
+ })
99
+ .catch(done);
100
+
101
+ });
102
+ });
103
+
104
+ });
105
+ });
@@ -0,0 +1,315 @@
1
+ var assert = require('assert');
2
+ var Promise = require('bluebird');
3
+ var _ = require('lodash');
4
+
5
+ describe('Association Interface', function() {
6
+
7
+ describe('n:m association :: .update()', function() {
8
+ describe('update nested associations() when a custom primary key is used', 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.Drivercustom.attributes.taxis.collection, 'taxicustom');
23
+ assert.strictEqual(Associations.Taxicustom.attributes.drivers.collection, 'drivercustom');
24
+
25
+ Associations.Drivercustom.create({ number: 1000, 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
+ { vin: 'b001', medallion: 1 }
44
+ ]
45
+ };
46
+
47
+ Associations.Drivercustom.update({ number: Driver.number }, 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.Drivercustom.findOne(values[0].number)
52
+ .populate('taxis')
53
+ .exec(function(err, model) {
54
+ assert.ifError(err);
55
+ assert.equal(model.name, 'm:m update nested - updated');
56
+ assert.equal(model.taxis.length, 1);
57
+
58
+ assert.equal(model.taxis[0].vin, 'b001');
59
+ assert.equal(model.taxis[0].medallion, 1);
60
+
61
+ done();
62
+ });
63
+
64
+ });
65
+ });
66
+ });
67
+
68
+
69
+ describe('when associations already exist', function() {
70
+
71
+ /////////////////////////////////////////////////////
72
+ // TEST SETUP
73
+ ////////////////////////////////////////////////////
74
+
75
+ var Driver;
76
+
77
+ before(function(done) {
78
+
79
+ var data = {
80
+ number: 2000,
81
+ name: 'm:m update nested',
82
+ taxis: [
83
+ { vin: 'b003', medallion: 1 },
84
+ { vin: 'b004', medallion: 2 }
85
+ ]
86
+ };
87
+
88
+ Associations.Drivercustom.create(data).exec(function(err, driver) {
89
+ assert.ifError(err);
90
+ Driver = driver;
91
+ done();
92
+ });
93
+
94
+ });
95
+
96
+
97
+ /////////////////////////////////////////////////////
98
+ // TEST METHODS
99
+ ////////////////////////////////////////////////////
100
+
101
+ it('should reset associations with the updated associations', function(done) {
102
+
103
+ var taxiData = {
104
+ name: 'm:m update nested - updated',
105
+ taxis: [
106
+ { vin: 'b005', medallion: 3 },
107
+ { vin: 'b006', medallion: 4 },
108
+ { vin: 'b007', medallion: 5 }
109
+ ]
110
+ };
111
+
112
+ Associations.Drivercustom.update({ number: Driver.number }, taxiData).exec(function(err, values) {
113
+ assert.ifError(err);
114
+
115
+ // Look up the driver again to be sure the taxis were added
116
+ Associations.Drivercustom.findOne(values[0].number)
117
+ .populate('taxis',{sort : {medallion : 1}})
118
+ .exec(function(err, model) {
119
+ assert(!err, 'Error: ' + err);
120
+
121
+ assert.equal(model.name, 'm:m update nested - updated');
122
+ assert.equal(model.taxis.length, 3);
123
+
124
+ assert.equal(model.taxis[0].vin, 'b005');
125
+ assert.equal(model.taxis[0].medallion, 3);
126
+
127
+ assert.equal(model.taxis[1].vin, 'b006');
128
+ assert.equal(model.taxis[1].medallion, 4);
129
+
130
+ assert.equal(model.taxis[2].vin, 'b007');
131
+ assert.equal(model.taxis[2].medallion, 5);
132
+
133
+ done();
134
+ });
135
+
136
+ });
137
+ });
138
+ });
139
+
140
+ describe('when associations have primary keys', function() {
141
+
142
+ /////////////////////////////////////////////////////
143
+ // TEST SETUP
144
+ ////////////////////////////////////////////////////
145
+
146
+ var Driver, Taxis;
147
+
148
+ before(function(done) {
149
+
150
+ var taxiData = [
151
+ { vin: 'b008', medallion: 1 },
152
+ { vin: 'b009', medallion: 2 }
153
+ ];
154
+
155
+ var data = {
156
+ number: 600,
157
+ name: 'm:m update nested',
158
+ taxis: [
159
+ { vin: 'b010', medallion: 100 },
160
+ { vin: 'b011', medallion: 200 }
161
+ ]
162
+ };
163
+
164
+ Associations.Taxicustom.create(taxiData).exec(function(err, taxis) {
165
+ assert.ifError(err);
166
+ Taxis = taxis;
167
+
168
+ Associations.Drivercustom.create(data).exec(function(err, driver) {
169
+ assert.ifError(err);
170
+ Driver = driver;
171
+ done();
172
+ });
173
+ });
174
+ });
175
+
176
+
177
+ /////////////////////////////////////////////////////
178
+ // TEST METHODS
179
+ ////////////////////////////////////////////////////
180
+
181
+ it('should update association values', function(done) {
182
+
183
+ var taxiData = {
184
+ name: 'm:m update nested - updated',
185
+ taxis: Taxis.map(function(taxi) { return taxi.vin; })
186
+ };
187
+
188
+ Associations.Drivercustom.update({ number: Driver.number }, taxiData).exec(function(err, values) {
189
+ assert.ifError(err);
190
+
191
+ // Look up the driver again to be sure the taxis were added
192
+ Associations.Drivercustom.findOne(values[0].number)
193
+ .populate('taxis',{sort : {medallion : 1}})
194
+ .exec(function(err, model) {
195
+ assert.ifError(err);
196
+ assert.equal(model.name, 'm:m update nested - updated');
197
+ assert.equal(model.taxis.length, 2);
198
+
199
+ // Ensure association values were updated
200
+ assert.equal(model.taxis[0].vin, 'b008');
201
+ assert.equal(model.taxis[0].medallion, 1);
202
+
203
+ assert.equal(model.taxis[1].vin, 'b009');
204
+ assert.equal(model.taxis[1].medallion, 2);
205
+
206
+ done();
207
+ });
208
+
209
+ });
210
+ });
211
+
212
+ });
213
+
214
+
215
+ describe('when associations already exist', function() {
216
+
217
+ /////////////////////////////////////////////////////
218
+ // TEST SETUP
219
+ ////////////////////////////////////////////////////
220
+
221
+ var Driver;
222
+
223
+ before(function(done) {
224
+
225
+ var data = {
226
+ number: 700,
227
+ name: 'm:m update nested save',
228
+ taxis: [ { vin: 'b012', medallion: 1000 } ]
229
+ };
230
+
231
+ Associations.Drivercustom.create(data).exec(function(err, driver) {
232
+ assert.ifError(err);
233
+ Driver = driver;
234
+ done();
235
+ });
236
+
237
+ });
238
+
239
+
240
+ /////////////////////////////////////////////////////
241
+ // TEST METHODS
242
+ ////////////////////////////////////////////////////
243
+
244
+ it('should update association values with save()', function(done) {
245
+
246
+ Associations.Drivercustom.findOne({ number: Driver.number })
247
+ .populate('taxis')
248
+ .exec(function(err, model) {
249
+ var taxi = model.taxis[0];
250
+ taxi.medallion = 1001;
251
+ taxi.save(function(err){
252
+ assert.ifError(err);
253
+
254
+ Associations.Drivercustom.findOne({ number: Driver.number })
255
+ .populate('taxis')
256
+ .exec(function(err, model) {
257
+ assert.ifError(err);
258
+ assert.equal(model.taxis.length, 1);
259
+ assert.equal(model.taxis[0].vin, 'b012');
260
+ assert.equal(model.taxis[0].medallion, 1001);
261
+ done();
262
+ });
263
+ });
264
+
265
+ });
266
+ });
267
+ });
268
+
269
+
270
+ describe('when associations are sync\'ed rapidly', function() {
271
+
272
+ /////////////////////////////////////////////////////
273
+ // TEST SETUP
274
+ ////////////////////////////////////////////////////
275
+
276
+ var Drivers;
277
+ var Taxis;
278
+
279
+ before(function(done) {
280
+
281
+ var driversData = [
282
+ {number: 8, name: "Rapid 0"},
283
+ {number: 9, name: "Rapid 1"},
284
+ {number: 10, name: "Rapid 2"},
285
+ {number: 11, name: "Rapid 3"}
286
+ ];
287
+
288
+ var taxisData = [
289
+ {vin: 'b013', medallion: 200},
290
+ {vin: 'b014', medallion: 201},
291
+ {vin: 'b015', medallion: 202},
292
+ {vin: 'b016', medallion: 203}
293
+ ];
294
+
295
+ Associations.Drivercustom.createEach(driversData).exec(function(err, drivers) {
296
+ assert.ifError(err);
297
+ Drivers = drivers;
298
+
299
+ Associations.Taxicustom.createEach(taxisData).exec(function(err, taxis) {
300
+ assert.ifError(err);
301
+ Taxis = taxis;
302
+
303
+ done();
304
+ });
305
+
306
+ });
307
+
308
+ });
309
+
310
+ });
311
+
312
+ });
313
+ });
314
+ });
315
+ });