@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,174 @@
1
+ var assert = require('assert'),
2
+ _ = require('lodash');
3
+
4
+ describe('Association Interface', function() {
5
+
6
+ describe('Belongs To Association', function() {
7
+ describe('update nested associations()', function() {
8
+ describe('with single level depth', function() {
9
+
10
+ describe('when association doesn\'t exist', function() {
11
+
12
+ /////////////////////////////////////////////////////
13
+ // TEST SETUP
14
+ ////////////////////////////////////////////////////
15
+
16
+ var Payment;
17
+
18
+ before(function(done) {
19
+ // Check Payment belongsTo Customer
20
+ assert.strictEqual(Associations.Payment.attributes.a_customer.model, 'customer');
21
+ assert.strictEqual(Associations.Customer.attributes.payments.collection, 'payment');
22
+
23
+ Associations.Payment.create({ amount: 1 }).exec(function(err, values) {
24
+ assert.ifError(err);
25
+ Payment = values;
26
+ done();
27
+ });
28
+
29
+ });
30
+
31
+
32
+ /////////////////////////////////////////////////////
33
+ // TEST METHODS
34
+ ////////////////////////////////////////////////////
35
+
36
+ it('should create a new customer', function(done) {
37
+
38
+ var data = {
39
+ amount: 200,
40
+ a_customer: {
41
+ name: 'belongsTo nested update'
42
+ }
43
+ };
44
+
45
+ Associations.Payment.update({ id: Payment.id }, data).exec(function(err, payment) {
46
+ assert.ifError(err);
47
+
48
+ Associations.Payment.findOne(payment[0].id)
49
+ .populate('a_customer')
50
+ .exec(function(err, paymnt) {
51
+ assert.ifError(err);
52
+ assert.equal(paymnt.a_customer.name, 'belongsTo nested update');
53
+ done();
54
+ });
55
+ });
56
+ });
57
+ });
58
+
59
+
60
+ describe('when association already exist', function() {
61
+
62
+ /////////////////////////////////////////////////////
63
+ // TEST SETUP
64
+ ////////////////////////////////////////////////////
65
+
66
+ var Payment;
67
+
68
+ before(function(done) {
69
+
70
+ var data = {
71
+ amount: 200,
72
+ a_customer: {
73
+ name: 'belongsTo nested update'
74
+ }
75
+ };
76
+
77
+ Associations.Payment.create(data).exec(function(err, payment) {
78
+ assert.ifError(err);
79
+ Payment = payment;
80
+ done();
81
+ });
82
+
83
+ });
84
+
85
+
86
+ /////////////////////////////////////////////////////
87
+ // TEST METHODS
88
+ ////////////////////////////////////////////////////
89
+
90
+ it('should reset associations with the updated associations', function(done) {
91
+
92
+ var data = {
93
+ amount: 100,
94
+ a_customer: {
95
+ name: 'belongsTo nested update - updated'
96
+ }
97
+ };
98
+
99
+ Associations.Payment.update({ id: Payment.id }, data).exec(function(err, values) {
100
+ assert.ifError(err);
101
+
102
+ // Look up the payment again to be sure the new customer was added
103
+ Associations.Payment.findOne(Payment.id)
104
+ .populate('a_customer')
105
+ .exec(function(err, model) {
106
+ assert.ifError(err);
107
+ assert.strictEqual(model.amount, 100);
108
+ assert(model.a_customer);
109
+ assert.equal(model.a_customer.name, 'belongsTo nested update - updated');
110
+ done();
111
+ });
112
+
113
+ });
114
+ });
115
+ });
116
+
117
+ describe('when association have primary keys', function() {
118
+
119
+ /////////////////////////////////////////////////////
120
+ // TEST SETUP
121
+ ////////////////////////////////////////////////////
122
+
123
+ var Customers, Payment;
124
+
125
+ before(function(done) {
126
+
127
+ Associations.Customer.create([{ name: 'foo' }, { name: 'bar' }]).exec(function(err, customers) {
128
+ assert.ifError(err);
129
+ Customers = customers;
130
+
131
+ Associations.Payment.create({ amount: 100, a_customer: customers[0].id })
132
+ .exec(function(err, payment) {
133
+ assert.ifError(err);
134
+ Payment = payment;
135
+ done();
136
+ });
137
+ });
138
+ });
139
+
140
+
141
+ /////////////////////////////////////////////////////
142
+ // TEST METHODS
143
+ ////////////////////////////////////////////////////
144
+
145
+ it('should update association values', function(done) {
146
+
147
+ var data = {
148
+ amount: 200,
149
+ a_customer: Customers[1]
150
+ };
151
+
152
+ Associations.Payment.update({ id: Payment.id }, data).exec(function(err, values) {
153
+ assert(!err, err);
154
+
155
+ // Look up the payment again to be sure the customer was linked
156
+ Associations.Payment.findOne(values[0].id)
157
+ .populate('a_customer')
158
+ .exec(function(err, model) {
159
+ assert.ifError(err);
160
+
161
+ assert.strictEqual(model.amount, 200);
162
+ assert.equal(model.a_customer.name, 'bar');
163
+
164
+ done();
165
+ });
166
+
167
+ });
168
+ });
169
+ });
170
+
171
+ });
172
+ });
173
+ });
174
+ });
@@ -0,0 +1,437 @@
1
+ var _ = require('lodash');
2
+ var assert = require('assert');
3
+ var async = require('async');
4
+
5
+ describe('Association Interface', function() {
6
+
7
+ describe('Deep Associations', function() {
8
+ var nestedSellers;
9
+ var nestedCompanies;
10
+ before(function(done) {
11
+ Associations.Companydeep.createEach([
12
+ {
13
+ name: 'company 1',
14
+ drivers: [
15
+ {
16
+ name: 'driver 1',
17
+ address: {city: 'city 1'}
18
+ },
19
+ {
20
+ name: 'driver 3',
21
+ address: {city: 'city 3'}
22
+ }
23
+ ]
24
+ },
25
+ {
26
+ name: 'company 2',
27
+ drivers: [
28
+ {
29
+ name: 'driver 2',
30
+ address: {city: 'city 2'}
31
+ },
32
+ {
33
+ name: 'driver 4',
34
+ address: {city: 'city 4'}
35
+ }
36
+ ]
37
+ }
38
+ ]).exec(function(err, companies) {
39
+ if(err) {
40
+ return done(err);
41
+ }
42
+ _.forEach(companies, function(company, companyIndex) {
43
+ companies[companyIndex].drivers = _.sortBy(companies[companyIndex].drivers, 'name');
44
+ });
45
+ nestedCompanies = companies;
46
+ Associations.Countrydeep.createEach([
47
+ {name: 'france'},
48
+ {name: 'germany'}
49
+ ]).exec(function(err, countries) {
50
+ nestedCreates = [
51
+ {
52
+ name: 'seller 1',
53
+ countries: [
54
+ countries[0].id,
55
+ countries[1].id
56
+ ],
57
+ departments: [
58
+ {name: 'dep 1'},
59
+ {name: 'dep 2'},
60
+ {name: 'dep 4'}
61
+ ],
62
+ taxis: [
63
+ {
64
+ matricule: 'taxi_1',
65
+ company: companies[0].id,
66
+ breakdowns: [
67
+ {level: 9},
68
+ {level: 9}
69
+ ],
70
+ drivers: [
71
+ companies[0].drivers[0].id,
72
+ companies[1].drivers[0].id
73
+ ]
74
+ },
75
+ {
76
+ matricule: 'taxi_4',
77
+ company: companies[0].id,
78
+ breakdowns: [
79
+ {level: 10}
80
+ ],
81
+ drivers: [
82
+ companies[0].drivers[0].id
83
+ ]
84
+ },
85
+ {
86
+ matricule: 'taxi_5',
87
+ company: companies[0].id,
88
+ breakdowns: [
89
+ {level: 11}
90
+ ],
91
+ drivers: [
92
+ companies[0].drivers[0].id
93
+ ]
94
+ }
95
+ ]
96
+ },
97
+ {
98
+ name: 'seller 2',
99
+ countries: [
100
+ countries[0].id
101
+ ],
102
+ departments: [
103
+ {name: 'dep 3'},
104
+ {name: 'dep 5'}
105
+ ],
106
+ taxis: [
107
+ {
108
+ matricule: 'taxi_2',
109
+ company: companies[1].id,
110
+ breakdowns: [
111
+ {level: 7}
112
+ ],
113
+ drivers: [
114
+ companies[1].drivers[0].id,
115
+ companies[0].drivers[1].id
116
+ ]
117
+ },
118
+ {
119
+ matricule: 'taxi_3',
120
+ company: companies[1].id,
121
+ breakdowns: [
122
+ {level: 5},
123
+ {level: 1},
124
+ {level: 8}
125
+ ],
126
+ drivers: [
127
+ companies[0].drivers[1].id
128
+ ]
129
+ }
130
+ ]
131
+ }
132
+ ];
133
+
134
+ Associations.Sellerdeep.createEach(nestedCreates).exec(function(err, sellers) {
135
+ if(err) {
136
+ return done(err);
137
+ }
138
+ nestedSellers = sellers;
139
+ done();
140
+ });
141
+ });
142
+ });
143
+ });
144
+
145
+ describe('Populate', function() {
146
+ it('should deeply populate a branch', function(done) {
147
+ Associations.Companydeep.find().sort('name asc')
148
+ .populate('drivers', {sort: {name: 1}})
149
+ .populate('drivers.taxis', {sort: {matricule: 1}})
150
+ .populate('drivers.taxis.seller', {sort: {name: 1}})
151
+ .populate('drivers.taxis.seller.departments', {sort: {name: 1}})
152
+ .exec(function(err, companies) {
153
+ assert.ifError(err);
154
+ // Root Level
155
+ assert.equal(companies.length, 2, 'Root criteria not applied.');
156
+ assert.equal(companies[1].name, 'company 2', 'Root criteria not applied.');
157
+ //Level 1
158
+ assert.equal(companies[1].drivers.length, 2, 'Could not populate first level oneToMany collection.');
159
+ assert.equal(companies[1].drivers[0].name, 'driver 2', 'First level not correctly populated.');
160
+
161
+ //Level 2
162
+ assert.equal(companies[1].drivers[0].taxis.length, 2, 'Could not populate second level manyToMany Through collection.');
163
+ var taxi1 = companies[1].drivers[0].taxis[0];
164
+ var taxi2 = companies[1].drivers[0].taxis[1];
165
+ assert.equal(taxi1.matricule, 'taxi_1', 'Second level not correctly populated.');
166
+ assert.equal(taxi2.matricule, 'taxi_2', 'Second level not correctly populated.');
167
+ //Level 3
168
+ assert(taxi1.seller, 'Could not populate third level manyToOne model.');
169
+ var seller1 = taxi1.seller;
170
+ var seller2 = taxi2.seller;
171
+ assert(seller1.name === 'seller 1' && seller2.name === 'seller 2',
172
+ 'Third level not correctly populated.');
173
+ //Level 4
174
+ assert(seller1.departments.length === 3, 'Could not populate fourth level oneToMany collection.');
175
+ assert(seller1.departments[0].name === 'dep 1' && seller1.departments[1].name === 'dep 2'
176
+ && seller1.departments[2].name === 'dep 4', 'Fourth level not correctly populated.');
177
+ assert(seller2.departments.length === 2, 'Could not populate fourth level oneToMany collection.');
178
+ assert(seller2.departments[0].name === 'dep 3',
179
+ 'Fourth level not correctly populated.');
180
+ done();
181
+ });
182
+ });
183
+
184
+ it('should deeply populate multiple branchs', function(done) {
185
+ Associations.Companydeep.find().where({name: 'company 2'})
186
+ .populate('drivers', {sort: {name: 1}})
187
+ .populate('drivers.taxis', {sort: {matricule: 1}})
188
+ .populate('drivers.address', {sort: {city: 1}})
189
+ .populate('taxis')
190
+ .exec(function(err, companies) {
191
+ assert.ifError(err);
192
+ // Root Level
193
+ assert(companies.length === 1 && companies[0].name === 'company 2', 'Root criteria not applied.');
194
+ //Level 1
195
+ assert(companies[0].drivers.length === 2, 'Could not populate first level oneToMany collection.');
196
+ assert(companies[0].drivers[0].name === 'driver 2', 'First level not correctly populated.');
197
+ assert(companies[0].taxis.length === 2, 'First level not correctly populated.');
198
+ //Level 2 A
199
+ assert(companies[0].drivers[0].taxis.length === 2, 'Could not populate second level manyToMany Through collection.');
200
+ var taxi1 = companies[0].drivers[0].taxis[0];
201
+ assert(taxi1.matricule === 'taxi_1', 'Second level (A) not correctly populated.');
202
+ //Level 2 B
203
+ assert(companies[0].drivers[0].address.city === 'city 2', 'Second level (B) criteria not populated.');
204
+ done();
205
+ });
206
+ });
207
+
208
+ it('should apply criteria to current populate path last alias', function(done) {
209
+ Associations.Companydeep.find().where({name: 'company 1'})
210
+ .populate('drivers', {name: 'driver 3'})
211
+ .populate('drivers.taxis', {matricule: 'taxi_3'})
212
+ .populate('drivers.taxis.breakdowns', {where: {level: {'>': 2}}, sort: {level: 1}})
213
+ .exec(function(err, companies) {
214
+ assert.ifError(err);
215
+ // Root Level
216
+ assert(companies.length === 1 && companies[0].name === 'company 1', 'Root criteria not applied.');
217
+ //Level 1
218
+ assert(companies[0].drivers.length === 1, 'Could not populate first level oneToMany collection.');
219
+ assert(companies[0].drivers[0].name === 'driver 3', 'First level criteria not applied.');
220
+ //Level 2
221
+ assert(companies[0].drivers[0].taxis.length === 1, 'Could not populate second level manyToMany Through collection.');
222
+ var taxi = companies[0].drivers[0].taxis[0];
223
+ assert(taxi.matricule === 'taxi_3', 'Second level criteria not applied.');
224
+ //Level 3
225
+ assert(taxi.breakdowns.length === 2, 'Could not populate third level oneToMany collection.');
226
+ assert(taxi.breakdowns[0].level === 5 && taxi.breakdowns[1].level === 8, 'Third level criteria not applied.');
227
+
228
+ done();
229
+ });
230
+ });
231
+
232
+ it('should deeply populate nested collections', function(done) {
233
+ Associations.Companydeep.find().where({name: 'company 2'})
234
+ .populate('taxis', {sort: 'matricule asc'})
235
+ .populate('taxis.breakdowns')
236
+ .exec(function(err, company) {
237
+ assert.ifError(err);
238
+ assert.strictEqual(company[0].taxis[0].breakdowns.length, 1);
239
+ assert.strictEqual(company[0].taxis[1].breakdowns.length, 3);
240
+ done();
241
+ });
242
+ });
243
+
244
+ it('findOne with populate deep should return undefined if there is no results', function(done) {
245
+ Associations.Companydeep.findOne().where({id: 999})
246
+ .populate('taxis')
247
+ .populate('taxis.breakdowns')
248
+ .exec(function(err, company) {
249
+ assert.ifError(err);
250
+ assert(company === void(0));
251
+ done();
252
+ });
253
+ });
254
+ });
255
+ describe('Criteria', function() {
256
+ it('should find model using deep criteria on belongsTo', function(done) {
257
+ Associations.Taxideep.find({where: {seller: {name: 'seller 1'}}}).populate('seller').exec(function(err, taxis) {
258
+ assert.ifError(err);
259
+ assert.strictEqual(taxis.length, 3);
260
+ assert.strictEqual(taxis[0].seller.name, 'seller 1');
261
+ assert.strictEqual(taxis[1].seller.name, 'seller 1');
262
+ assert.strictEqual(taxis[2].seller.name, 'seller 1');
263
+ done();
264
+ });
265
+ });
266
+
267
+ it('should find model using deep criteria on hasManyToOne', function(done) {
268
+ Associations.Taxideep.find({where: {breakdowns: {level: 11}}, sort: {matricule: 1}}).populate('breakdowns', {sort: {level: 1}}).exec(function(err, taxis) {
269
+ assert.ifError(err);
270
+ assert.strictEqual(taxis[0].matricule, 'taxi_5');
271
+ assert.strictEqual(taxis[0].breakdowns[0].level, 11);
272
+ done();
273
+ });
274
+ });
275
+
276
+ it('should find model using deep criteria on hasManyToMany', function(done) {
277
+ Associations.Sellerdeep.find({where: {countries: {name: 'france'}}, sort: {name: 1}}).populate('countries', {sort: {name: 1}}).exec(function(err, sellers) {
278
+ assert.ifError(err);
279
+ assert(sellers.length === 2);
280
+ assert.strictEqual(sellers[0].countries[0].name, 'france');
281
+ assert.strictEqual(sellers[1].countries[0].name, 'france');
282
+ done();
283
+ });
284
+ });
285
+
286
+ it('should find model using deep criteria on hasManyToMany through', function(done) {
287
+ Associations.Driverdeep.find({where: {taxis: {matricule: 'taxi_4'}}, sort: {name: 1}}).populate('taxis', {sort: 'matricule'}).exec(function(err, drivers) {
288
+ assert.ifError(err);
289
+ assert.strictEqual(drivers.length, 1);
290
+ assert.strictEqual(drivers[0].name, 'driver 1');
291
+ assert.strictEqual(drivers[0].taxis.length, 3);
292
+ assert.strictEqual(drivers[0].taxis[0].matricule, 'taxi_1');
293
+ assert.strictEqual(drivers[0].taxis[1].matricule, 'taxi_4');
294
+ assert.strictEqual(drivers[0].taxis[2].matricule, 'taxi_5');
295
+ done();
296
+ });
297
+ });
298
+
299
+ it('should find model using deep criteria with operator', function(done) {
300
+ Associations.Driverdeep.find({sort: 'name', where: {taxis: {or: [{matricule: 'taxi_1'}, {matricule: 'taxi_2'}]}}}).populate('taxis', {sort: 'matricule'}).exec(function(err, drivers) {
301
+ assert.ifError(err);
302
+ assert.strictEqual(drivers.length, 3);
303
+ assert.strictEqual(drivers[0].name, 'driver 1');
304
+ assert.strictEqual(drivers[0].taxis.length, 3);
305
+ assert.strictEqual(drivers[0].taxis[0].matricule, 'taxi_1');
306
+ assert.strictEqual(drivers[0].taxis[1].matricule, 'taxi_4');
307
+ assert.strictEqual(drivers[0].taxis[2].matricule, 'taxi_5');
308
+ assert.strictEqual(drivers[1].name, 'driver 2');
309
+ assert.strictEqual(drivers[1].taxis.length, 2);
310
+ assert.strictEqual(drivers[1].taxis[0].matricule, 'taxi_1');
311
+ assert.strictEqual(drivers[1].taxis[1].matricule, 'taxi_2');
312
+ assert.strictEqual(drivers[2].name, 'driver 3');
313
+ assert.strictEqual(drivers[2].taxis.length, 2);
314
+ assert.strictEqual(drivers[2].taxis[0].matricule, 'taxi_2');
315
+ assert.strictEqual(drivers[2].taxis[1].matricule, 'taxi_3');
316
+ done();
317
+ });
318
+ });
319
+
320
+ it('should find model using deep criteria in operator', function(done) {
321
+ Associations.Driverdeep.find({sort: 'name', where: {or: [{taxis: {matricule: 'taxi_3'}}, {address: {city: 'city 4'}}]}}).populate('taxis', {sort: 'matricule'}).exec(function(err, drivers) {
322
+ assert.ifError(err);
323
+ assert.strictEqual(drivers.length, 2);
324
+ assert.strictEqual(drivers[0].name, 'driver 3');
325
+ assert.strictEqual(drivers[0].taxis.length, 2);
326
+ assert.strictEqual(drivers[0].taxis[0].matricule, 'taxi_2');
327
+ assert.strictEqual(drivers[0].taxis[1].matricule, 'taxi_3');
328
+ assert.strictEqual(drivers[1].name, 'driver 4');
329
+ assert.strictEqual(drivers[1].taxis.length, 0);
330
+ done();
331
+ });
332
+ });
333
+
334
+ it('should populate using deep criteria', function(done) {
335
+ Associations.Driverdeep.findOne({name: 'driver 3'}).populate('taxis', {sort: 'matricule', where: {breakdowns: {level: 5}}}).exec(function(err, driver) {
336
+ assert.ifError(err);
337
+ assert.strictEqual(driver.name, 'driver 3');
338
+ assert.strictEqual(driver.taxis.length, 1);
339
+ assert.strictEqual(driver.taxis[0].matricule, 'taxi_3');
340
+ done();
341
+ });
342
+ });
343
+
344
+ });
345
+
346
+ describe('Associations', function() {
347
+
348
+ it('should deeply populate and apply criteria on associations (One-to-One)', function(done) {
349
+ Associations.Taxideep.findOne({where: {matricule: 'taxi_1'}})
350
+ .populate('seller', {where: {name: 'seller 1'}})
351
+ .populate('seller.departments', {name: {contains: '4'}})
352
+ .exec(function(err, taxi) {
353
+ assert.ifError(err);
354
+ // Root Level
355
+ assert(taxi.matricule === 'taxi_1', 'Root criteria not applied.');
356
+ //Level 1
357
+ assert(taxi.seller, 'Could not populate first level with criteria.');
358
+ assert(taxi.seller.name === 'seller 1', 'First level criteria not applied.');
359
+ //Level 2
360
+ assert(taxi.seller.departments, 'Second level not populated.');
361
+ assert(taxi.seller.departments[0].name === 'dep 4', 'Second level criteria not applied.');
362
+ done();
363
+ });
364
+ });
365
+
366
+ it('should deeply populate and apply criteria on associations (One-to-Many)', function(done) {
367
+ Associations.Companydeep.findOne({where: {name: 'company 1'}})
368
+ .populate('taxis', {matricule: 'taxi_4'})
369
+ .populate('taxis.breakdowns', {level: 10})
370
+ .exec(function(err, company) {
371
+ assert.ifError(err);
372
+ // Root Level
373
+ assert(company.name === 'company 1', 'Root criteria not applied.');
374
+ //Level 1
375
+ assert(company.taxis, 'Could not populate first level');
376
+ assert(company.taxis[0].matricule === 'taxi_4', 'First level criteria not applied.');
377
+ //Level 2
378
+ assert(company.taxis[0].breakdowns, 'Second level not populated.');
379
+ assert(company.taxis[0].breakdowns[0].level === 10, 'Second level criteria not applied.');
380
+ done();
381
+ });
382
+ });
383
+
384
+ it('should deeply populate and apply criteria on associations (Many-to-Many Through)', function(done) {
385
+ Associations.Driverdeep.findOne({where: {name: 'driver 1'}})
386
+ .populate('taxis', {matricule: 'taxi_4'})
387
+ .populate('taxis.breakdowns', {level: 10})
388
+ .exec(function(err, driver) {
389
+ assert.ifError(err);
390
+ // Root Level
391
+ assert(driver.name === 'driver 1', 'Root criteria not applied.');
392
+ //Level 1
393
+ assert(driver.taxis, 'Could not populate first level with criteria.');
394
+ assert(driver.taxis[0].matricule === 'taxi_4', 'first level criteria not applied.');
395
+ //Level 2
396
+ assert(driver.taxis[0].breakdowns, 'Second level not populated.');
397
+ assert(driver.taxis[0].breakdowns[0].level === 10, 'Second level criteria not applied.');
398
+ done();
399
+ });
400
+ });
401
+
402
+ it('should deeply populate and apply criteria on associations (Many-to-Many)', function(done) {
403
+ Associations.Countrydeep.findOne({name: 'france'})
404
+ .populate('sellers', {name: 'seller 1'})
405
+ .populate('sellers.departments', {name: 'dep 4'})
406
+ .exec(function(err, country) {
407
+ assert.ifError(err);
408
+ assert(country.name === 'france', 'Root criteria not applied.');
409
+ assert(country.sellers, 'Could not populate first level with criteria.');
410
+ assert(country.sellers[0].name === 'seller 1', 'first level criteria not applied.');
411
+ assert(country.sellers[0].departments, 'Second level not populated.');
412
+ assert(country.sellers[0].departments[0].name === 'dep 4', 'Second level criteria not applied.');
413
+ done();
414
+ });
415
+ });
416
+
417
+ it('should deeply populate and apply criteria on associations (Many-to-One)', function(done) {
418
+ Associations.Taxideep.findOne({where: {matricule: 'taxi_1'}})
419
+ .populate('seller', {where: {name: 'seller 1'}})
420
+ .populate('seller.departments', {name: {contains: '4'}})
421
+ .exec(function(err, taxi) {
422
+ assert.ifError(err);
423
+ // Root Level
424
+ assert(taxi.matricule === 'taxi_1', 'Root criteria not applied.');
425
+ //Level 1
426
+ assert(taxi.seller, 'Could not populate first level with criteria.');
427
+ assert(taxi.seller.name === 'seller 1', 'First level criteria not applied.');
428
+ //Level 2
429
+ assert(taxi.seller.departments, 'Second level not populated.');
430
+ assert(taxi.seller.departments[0].name === 'dep 4', 'Second level criteria not applied.');
431
+ done();
432
+ });
433
+ });
434
+
435
+ });
436
+ });
437
+ });