@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,82 @@
1
+ /**
2
+ * Module Dependencies
3
+ */
4
+
5
+ var Offshore = require(process.env.offshorePath || 'offshore');
6
+ var _ = require('lodash');
7
+ var asynk = require('asynk');
8
+
9
+ // Require Fixtures
10
+ var fixtures = {
11
+ UserFixture: require('./fixtures/crud.fixture')
12
+ };
13
+
14
+
15
+ /////////////////////////////////////////////////////
16
+ // TEST SETUP
17
+ ////////////////////////////////////////////////////
18
+
19
+ var offshore, ontology;
20
+
21
+ before(function(done) {
22
+
23
+ offshore = new Offshore();
24
+
25
+ Object.keys(fixtures).forEach(function(key) {
26
+ var collection = fixtures[key];
27
+ _.keys(collection.attributes).forEach(function(attr) {
28
+ // skip collection
29
+ if (collection.attributes[attr].collection) {
30
+ return;
31
+ }
32
+ // skip functions
33
+ if (_.isFunction(collection.attributes[attr])) {
34
+ return;
35
+ }
36
+ // add columnName
37
+ collection.attributes[attr].columnName = collection.identity + _.capitalize(attr);
38
+ });
39
+ offshore.loadCollection(Offshore.Collection.extend(collection));
40
+ });
41
+
42
+ var connections = { sql: _.clone(Connections.test) };
43
+
44
+ offshore.initialize({ adapters: { wl_tests: Adapter }, connections: connections }, function(err, _ontology) {
45
+ if (err) {
46
+ return done(err);
47
+ }
48
+
49
+ ontology = _ontology;
50
+
51
+ Object.keys(_ontology.collections).forEach(function(key) {
52
+ var globalName = key.charAt(0).toUpperCase() + key.slice(1);
53
+ global.Sql[globalName] = _ontology.collections[key];
54
+ });
55
+
56
+ done();
57
+ });
58
+ });
59
+
60
+ after(function(done) {
61
+
62
+ function dropCollection(item, next) {
63
+ if (!Adapter.hasOwnProperty('drop')) {
64
+ return next();
65
+ }
66
+
67
+ ontology.collections[item].drop(function(err) {
68
+ if (err) {
69
+ return next(err);
70
+ }
71
+ next();
72
+ });
73
+ }
74
+
75
+ asynk.each(Object.keys(ontology.collections), dropCollection).serie().asCallback(function(err) {
76
+ if (err) {
77
+ return done(err);
78
+ }
79
+ offshore.teardown(done);
80
+ });
81
+
82
+ });
@@ -0,0 +1,57 @@
1
+ module.exports = {
2
+
3
+ identity: 'user',
4
+ tableName: 'usertablesql',
5
+ connection: 'sql',
6
+
7
+ attributes: {
8
+ first_name: {
9
+ type: 'string'
10
+ },
11
+ last_name: {
12
+ type: 'string'
13
+ },
14
+ email: {
15
+ type: 'string'
16
+ },
17
+ avatar: {
18
+ type: 'binary'
19
+ },
20
+ title: {
21
+ type: 'string'
22
+ },
23
+ phone: {
24
+ type: 'string'
25
+ },
26
+ type: {
27
+ type: 'string'
28
+ },
29
+ favoriteFruit: {
30
+ defaultsTo: 'blueberry',
31
+ type: 'string'
32
+ },
33
+ age: {
34
+ type: 'integer'
35
+ }, // integer field that's not auto-incrementable
36
+ dob: {
37
+ type: 'datetime'
38
+ },
39
+ status: {
40
+ type: 'boolean',
41
+ defaultsTo: false
42
+ },
43
+ percent: {
44
+ type: 'float'
45
+ },
46
+ list: {
47
+ type: 'array'
48
+ },
49
+ obj: {
50
+ type: 'json'
51
+ },
52
+ fullName: function() {
53
+ return this.first_name + ' ' + this.last_name;
54
+ }
55
+ }
56
+
57
+ });
@@ -0,0 +1,438 @@
1
+ var Offshore = require(process.env.offshorePath || 'offshore');
2
+ var assert = require('assert');
3
+ var _ = require('lodash');
4
+
5
+ describe('Transaction commit', function() {
6
+
7
+ it('should commit created data', function(done) {
8
+ var id;
9
+ var customerName = 'Test create transaction';
10
+
11
+ Offshore.Transaction(Transactable.Customer, function(trx, cb) {
12
+ trx.customer.create({name: customerName}).exec(function(err, createResult) {
13
+ if (err) {
14
+ return done(err);
15
+ }
16
+
17
+ id = createResult.id;
18
+ cb(null, createResult);
19
+ });
20
+ }).exec(function(err, result) {
21
+ if (err) {
22
+ done(err);
23
+ }
24
+ assert(result, 'Exec callback should have returned the updated entry');
25
+ assert.equal(result.id, id);
26
+ Transactable.Customer.find({name: customerName}, function(err, test) {
27
+ if (err) {
28
+ return done(err);
29
+ }
30
+ assert.equal(test.length, 1, 'Customer should be commited');
31
+ done();
32
+ });
33
+ });
34
+ });
35
+
36
+ it('should commit updated data', function(done) {
37
+ var id;
38
+ var customerName = 'Test update transaction';
39
+ var updatedName = 'Updated test transaction';
40
+
41
+ Offshore.Transaction(Transactable.Customer, function(trx, cb) {
42
+ trx.customer.create({name: customerName}).exec(function(err, createResult) {
43
+ if (err) {
44
+ return done(err);
45
+ }
46
+ id = createResult.id;
47
+ trx.customer.update({id: id}, {name: updatedName}).exec(function(err, updateResult) {
48
+ if (err) {
49
+ return done(err);
50
+ }
51
+ cb(null, updateResult);
52
+ });
53
+ });
54
+ }).exec(function(err, result) {
55
+ if (err) {
56
+ done(err);
57
+ }
58
+ assert(result, 'Exec callback should have returned the updated entry');
59
+ assert.equal(result.length, 1);
60
+ result = result[0];
61
+ assert.equal(result.id, id);
62
+ Transactable.Customer.find({name: customerName}, function(err, test) {
63
+ if (err) {
64
+ return done(err);
65
+ }
66
+ assert.equal(test.length, 0, 'Original customer should be updated');
67
+ Transactable.Customer.find({name: updatedName}, function(err, test) {
68
+ if (err) {
69
+ return done(err);
70
+ }
71
+ assert.equal(test.length, 1, 'Customer should be updated');
72
+ done();
73
+ });
74
+ });
75
+ });
76
+ });
77
+
78
+ it('should destroy data', function(done) {
79
+ var id;
80
+ var customerName = 'Test destroy transaction';
81
+
82
+ Transactable.Customer.create({name: customerName}, function(err, customer) {
83
+ if (err) {
84
+ return done(err);
85
+ }
86
+ id = customer.id;
87
+ Offshore.Transaction(Transactable.Customer, function(trx, cb) {
88
+ trx.customer.destroy({name: customerName}).exec(function(err, destroyResult) {
89
+ if (err) {
90
+ return done(err);
91
+ }
92
+ cb(null, destroyResult);
93
+ });
94
+
95
+ }).exec(function(err, result) {
96
+ if (err) {
97
+ done(err);
98
+ }
99
+ assert(result[0], 'Transaction callback should return the destroyed customer');
100
+ assert.equal(result[0].id, id);
101
+ Transactable.Customer.find({name: customerName}, function(err, test) {
102
+ if (err) {
103
+ return done(err);
104
+ }
105
+ assert.equal(test.length, 0, 'Customer should be destroyed');
106
+ done();
107
+ });
108
+ });
109
+ });
110
+ });
111
+
112
+ it('should destroy data in transaction', function(done) {
113
+ var id;
114
+ var customerName = 'Test destroy in transaction';
115
+
116
+ Offshore.Transaction(Transactable.Customer, function(trx, cb) {
117
+ trx.customer.create({name: customerName}).exec(function(err, createResult) {
118
+ if (err) {
119
+ return done(err);
120
+ }
121
+ id = createResult.id;
122
+ trx.customer.destroy({name: customerName}).exec(function(err, destroyResult) {
123
+ if (err) {
124
+ return done(err);
125
+ }
126
+ cb(null, destroyResult);
127
+ });
128
+ });
129
+ }).exec(function(err, result) {
130
+ if (err) {
131
+ done(err);
132
+ }
133
+ assert(result[0], 'Transaction callback should return the destroyed customer');
134
+ assert.equal(result[0].id, id);
135
+ Transactable.Customer.find({name: customerName}, function(err, test) {
136
+ if (err) {
137
+ return done(err);
138
+ }
139
+ assert.equal(test.length, 0, 'Customer should be destroyed');
140
+ done();
141
+ });
142
+ });
143
+ });
144
+
145
+ it('original model should not interact with transaction', function(done) {
146
+ var id;
147
+ var customerName = 'Test atomicity transaction';
148
+
149
+ Offshore.Transaction(Transactable.Customer, function(trx, cb) {
150
+ trx.customer.create({name: customerName}).exec(function(err, createResult) {
151
+ if (err) {
152
+ return done(err);
153
+ }
154
+ id = createResult.id;
155
+ trx.customer.find({id: id}).exec(function(err, findInTrx) {
156
+ if (err) {
157
+ return done(err);
158
+ }
159
+ assert.equal(findInTrx.length, 1, 'Customer should exist inside the transaction');
160
+ Transactable.Customer.find({name: customerName}, function(err, findOutTrx) {
161
+ if (err) {
162
+ return done(err);
163
+ }
164
+ assert.equal(findOutTrx.length, 0, 'Customer should not exist outside the transaction');
165
+ Transactable.Customer.update({id: id}, {name: 'updated atomicity transaction'}).exec(function(err, updateOutTrx) {
166
+ if (err) {
167
+ return done(err);
168
+ }
169
+ assert.equal(updateOutTrx.length, 0, 'Should not update customer before commit without using transaction object');
170
+ Transactable.Customer.destroy({id: id}).exec(function(err, destroyOutTrx) {
171
+ if (err) {
172
+ return done(err);
173
+ }
174
+ assert.equal(destroyOutTrx.length, 0, 'Should not destroy customer before commit without using transaction object');
175
+ cb(null, createResult);
176
+ });
177
+ });
178
+ });
179
+ });
180
+ });
181
+ }).exec(function(err, result) {
182
+ if (err) {
183
+ done(err);
184
+ }
185
+ assert(result);
186
+ assert.equal(result.id, id);
187
+ Transactable.Customer.find({id: id}, function(err, customer) {
188
+ if (err) {
189
+ return done(err);
190
+ }
191
+ assert.equal(customer.length, 1, 'Should not alter customer');
192
+ assert.equal(customer[0].name, customerName, 'Should not alter customer');
193
+ done();
194
+ });
195
+ });
196
+ });
197
+
198
+ it('should commit without doing anything', function(done) {
199
+ Offshore.Transaction(Transactable.Customer, function(trx, cb) {
200
+ cb(null, 'ok');
201
+ }).exec(function(err, result) {
202
+ if (err) {
203
+ done(err);
204
+ }
205
+ assert(result === 'ok', 'Should commit');
206
+ done();
207
+ });
208
+ });
209
+
210
+ it('should perform nested transactions', function(done) {
211
+ var customerName = 'Test transaception';
212
+
213
+ Offshore.Transaction(Transactable.Customer, function(trx1, cb1) {
214
+ trx1.customer.create({name: customerName}).exec(function(err, customer1) {
215
+ if (err) {
216
+ return done(err);
217
+ }
218
+ Offshore.Transaction(Transactable.Customer, function(trx2, cb2) {
219
+ trx2.customer.create({name: customerName}).exec(function(err, customer2) {
220
+ if (err) {
221
+ return done(err);
222
+ }
223
+ // Commit second transaction
224
+ cb2(null, customer2);
225
+ });
226
+ }).exec(function(err, result) {
227
+ if (err) {
228
+ done(err);
229
+ }
230
+ // Commit first transaction
231
+ cb1(null, customer1);
232
+ });
233
+ });
234
+ }).exec(function(err, result) {
235
+ if (err) {
236
+ done(err);
237
+ }
238
+ Transactable.Customer.find({name: customerName}, function(err, customers) {
239
+ if (err) {
240
+ return done(err);
241
+ }
242
+ assert.equal(customers.length, 2, 'All transactions should be commited');
243
+ assert.equal(customers[0].name, customerName);
244
+ assert.equal(customers[1].name, customerName);
245
+ done();
246
+ });
247
+ });
248
+ });
249
+
250
+
251
+ it('should populate', function(done) {
252
+ var customerName = 'Parent customer';
253
+ var paymentName = 'Child payment';
254
+
255
+ Transactable.Customer.create({name: customerName}, function(err, customer) {
256
+ if (err) {
257
+ done(err);
258
+ }
259
+ assert.equal(customer.name, customerName);
260
+ Transactable.Payment.createEach([{type: paymentName, a_customer: customer.id},
261
+ {type: paymentName, a_customer: customer.id}], function(err, payments) {
262
+ if (err) {
263
+ done(err);
264
+ }
265
+ assert.equal(payments.length, 2);
266
+ Offshore.Transaction([Transactable.Customer, Transactable.Payment], function(trx, cb) {
267
+ trx.customer.findOne({name: customerName}).populate('payments').exec(function(err, populatedCustomer) {
268
+ if (err) {
269
+ return done(err);
270
+ }
271
+ assert.equal(populatedCustomer.name, customerName);
272
+ assert.equal(populatedCustomer.payments.length, 2, 'Should populate 2 payments');
273
+ return cb(null, populatedCustomer);
274
+ });
275
+ }).exec(function(err, result) {
276
+ if (err) {
277
+ done(err);
278
+ }
279
+ done();
280
+ });
281
+ });
282
+ });
283
+ });
284
+
285
+ it('should populate through association', function(done) {
286
+ var customerName = 'Through customer';
287
+ var storeName1 = 'Through store 1';
288
+ var storeName2 = 'Through store 2';
289
+
290
+ Transactable.Customer.create({name: customerName, stores: [{name: storeName1}, {name: storeName2}]}, function(err, customer) {
291
+ if (err) {
292
+ done(err);
293
+ }
294
+ assert.equal(customer.name, customerName);
295
+ Offshore.Transaction([Transactable.Customer, Transactable.Store], function(trx, cb) {
296
+ trx.customer.findOne({name: customerName}).populate('stores').exec(function(err, populatedCustomer) {
297
+ if (err) {
298
+ return done(err);
299
+ }
300
+ assert.equal(populatedCustomer.name, customerName);
301
+ assert.equal(populatedCustomer.stores.length, 2, 'Should populate 2 stores');
302
+ return cb(null, populatedCustomer);
303
+ });
304
+ }).exec(function(err, result) {
305
+ if (err) {
306
+ done(err);
307
+ }
308
+ done();
309
+ });
310
+ });
311
+ });
312
+
313
+ it('should populate undeclared child collections', function(done) {
314
+ var customerName = 'Parent customer 2';
315
+ var paymentName = 'Child payment 2';
316
+
317
+ Transactable.Customer.create({name: customerName}, function(err, customer) {
318
+ if (err) {
319
+ done(err);
320
+ }
321
+ assert.equal(customer.name, customerName);
322
+ Transactable.Payment.createEach([{type: paymentName, a_customer: customer.id},
323
+ {type: paymentName, a_customer: customer.id}], function(err, payments) {
324
+ if (err) {
325
+ done(err);
326
+ }
327
+ assert.equal(payments.length, 2);
328
+ Offshore.Transaction(Transactable.Customer, function(trx, cb) {
329
+ trx.customer.findOne({name: customerName}).populate('payments').exec(function(err, populatedCustomer) {
330
+ if (err) {
331
+ return done(err);
332
+ }
333
+ assert.equal(populatedCustomer.name, customerName);
334
+ assert.equal(populatedCustomer.payments.length, 2, 'Should populate payments');
335
+ return cb(null, populatedCustomer);
336
+ });
337
+ }).exec(function(err, result) {
338
+ if (err) {
339
+ done(err);
340
+ }
341
+ done();
342
+ });
343
+ });
344
+ });
345
+ });
346
+
347
+ it('should perform nested transactions and commit only one', function(done) {
348
+ var close;
349
+ var customerName = 'Test transaception2';
350
+
351
+ Offshore.Transaction(Transactable.Customer, function(trx1, cb1) {
352
+ trx1.customer.create({name: customerName}).exec(function(err, customer1) {
353
+ if (err) {
354
+ return done(err);
355
+ }
356
+ Offshore.Transaction(Transactable.Customer, function(trx2, cb2) {
357
+ trx2.customer.create({name: customerName}).exec(function(err, customer2) {
358
+ if (err) {
359
+ return done(err);
360
+ }
361
+ // Commit only the first transaction and keep second transaction callback
362
+ close = cb2;
363
+ cb1(null, customer1);
364
+
365
+ });
366
+ });
367
+ });
368
+ }).exec(function(err, result) {
369
+ if (err) {
370
+ done(err);
371
+ }
372
+ Transactable.Customer.find({name: customerName}, function(err, test) {
373
+ // Close second transaction to free connection
374
+ close(null, 'ok');
375
+ if (err) {
376
+ return done(err);
377
+ }
378
+ assert.equal(test.length, 1, 'Only one customer should be commited');
379
+ assert.equal(test[0].name, customerName);
380
+
381
+ done();
382
+ });
383
+ });
384
+ });
385
+
386
+ it('should commit through table', function(done) {
387
+ var customerName = 'Through commit customer';
388
+ var storeName1 = 'Through commit store 1';
389
+ var storeName2 = 'Through commit 2';
390
+
391
+ Offshore.Transaction([Transactable.Customer, Transactable.Store], function(trx, cb) {
392
+ trx.customer.create({name: customerName, stores: [{name: storeName1}, {name: storeName2}]}).exec(function(err, customer) {
393
+ if (err) {
394
+ done(err);
395
+ }
396
+ assert.equal(customer.name, customerName);
397
+ trx.customer.findOne({name: customerName}).populate('stores').exec(function(err, populatedCustomer) {
398
+ if (err) {
399
+ return done(err);
400
+ }
401
+ assert.equal(populatedCustomer.name, customerName);
402
+ assert.equal(populatedCustomer.stores.length, 2, 'Should populate 2 stores');
403
+ return cb(null, populatedCustomer);
404
+ });
405
+ });
406
+ }).exec(function(err, result) {
407
+ if (err) {
408
+ done(err);
409
+ }
410
+ Transactable.Customer.find({name: customerName}).populate('stores').exec(function(err, customer) {
411
+ if (err) {
412
+ done(err);
413
+ }
414
+ assert.equal(customer.length, 1);
415
+ assert.equal(customer[0].name, customerName);
416
+ assert.equal(customer[0].stores.length, 2, 'Should populate 2 stores');
417
+ Transactable.Store.find({name: storeName1}).populate('customers').exec(function(err, store1) {
418
+ if (err) {
419
+ done(err);
420
+ }
421
+ assert.equal(store1.length, 1);
422
+ assert.equal(store1[0].name, storeName1);
423
+ assert.equal(store1[0].customers.length, 1, 'Should populate 1 customer');
424
+ Transactable.Store.find({name: storeName2}).populate('customers').exec(function(err, store2) {
425
+ if (err) {
426
+ done(err);
427
+ }
428
+ assert.equal(store2.length, 1);
429
+ assert.equal(store2[0].name, storeName2);
430
+ assert.equal(store2[0].customers.length, 1, 'Should populate 1 customer');
431
+ done();
432
+ });
433
+ });
434
+ });
435
+ });
436
+ });
437
+
438
+ });