@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.
- package/.eslintignore +1 -0
- package/.eslintrc +35 -0
- package/.travis.yml +32 -0
- package/CHANGELOG.md +24 -0
- package/Dockerfile +7 -0
- package/LICENSE.md +11 -0
- package/README.md +71 -2
- package/docker-compose.yml +32 -0
- package/features/autoIncrement/README.md +37 -0
- package/features/autoIncrement/core/autoIncrement.test.js +151 -0
- package/features/autoIncrement/sequential/autoIncrementSequential.test.js +104 -0
- package/features/autoIncrement/support/autoInc.fixture.js +25 -0
- package/features/compositePrimaryKey/core/compositePrimaryKey.test.js +67 -0
- package/features/compositePrimaryKey/support/fixture.js +24 -0
- package/features/compositeUnique/core/compositeUnique.test.js +67 -0
- package/features/compositeUnique/support/fixture.js +30 -0
- package/features/crossAdapter/core/crossAdapter.js +119 -0
- package/features/crossAdapter/support/bootstrap.js +106 -0
- package/features/sequences/core/sequences.test.js +110 -0
- package/features/sequences/support/mariadb.js +23 -0
- package/features/sequences/support/oracle.js +23 -0
- package/features/sequences/support/postgres.js +23 -0
- package/features/spatial/core/geojson.test.js +68 -0
- package/features/spatial/support/fixtures.js +82 -0
- package/features/spatial/support/models.js +45 -0
- package/features/unique/README.md +18 -0
- package/features/unique/core/unique.test.js +107 -0
- package/features/unique/support/unique.fixture.js +26 -0
- package/interfaces/associations/belongsTo/create.js +41 -0
- package/interfaces/associations/belongsTo/create.nested.customPK.js +61 -0
- package/interfaces/associations/belongsTo/create.nested.js +52 -0
- package/interfaces/associations/belongsTo/dynamicFinder.js +213 -0
- package/interfaces/associations/belongsTo/find.populate.js +145 -0
- package/interfaces/associations/belongsTo/findOne.populate.js +84 -0
- package/interfaces/associations/belongsTo/modifiers.js +275 -0
- package/interfaces/associations/belongsTo/multipleForeignKeys.js +70 -0
- package/interfaces/associations/belongsTo/update.nested.customPK.js +141 -0
- package/interfaces/associations/belongsTo/update.nested.js +174 -0
- package/interfaces/associations/deep.associations.test.js +437 -0
- package/interfaces/associations/find.associations.test.js +92 -0
- package/interfaces/associations/hasMany/add.js +106 -0
- package/interfaces/associations/hasMany/create.nested.customPK.js +113 -0
- package/interfaces/associations/hasMany/create.nested.js +96 -0
- package/interfaces/associations/hasMany/find.populate.customPK.js +75 -0
- package/interfaces/associations/hasMany/find.populate.js +219 -0
- package/interfaces/associations/hasMany/find.populate.where.js +148 -0
- package/interfaces/associations/hasMany/findOne.populate.js +88 -0
- package/interfaces/associations/hasMany/modifiers.js +560 -0
- package/interfaces/associations/hasMany/multipleForeignKeys.js +113 -0
- package/interfaces/associations/hasMany/remove.js +96 -0
- package/interfaces/associations/hasMany/update.nested.customPK.js +148 -0
- package/interfaces/associations/hasMany/update.nested.js +202 -0
- package/interfaces/associations/manyToMany/add.js +136 -0
- package/interfaces/associations/manyToMany/create.nested.customPK.js +109 -0
- package/interfaces/associations/manyToMany/create.nested.js +96 -0
- package/interfaces/associations/manyToMany/destroy.js +128 -0
- package/interfaces/associations/manyToMany/find.populate.js +109 -0
- package/interfaces/associations/manyToMany/find.populate.where.js +75 -0
- package/interfaces/associations/manyToMany/find.populate.withSchema.js +91 -0
- package/interfaces/associations/manyToMany/findOne.populate.js +86 -0
- package/interfaces/associations/manyToMany/modifiers.js +558 -0
- package/interfaces/associations/manyToMany/remove.js +103 -0
- package/interfaces/associations/manyToMany/update.js +105 -0
- package/interfaces/associations/manyToMany/update.nested.customPK.js +315 -0
- package/interfaces/associations/manyToMany/update.nested.js +355 -0
- package/interfaces/associations/manyToManyThrough/add.js +109 -0
- package/interfaces/associations/manyToManyThrough/create.nested.customPK.js +118 -0
- package/interfaces/associations/manyToManyThrough/create.nested.js +114 -0
- package/interfaces/associations/manyToManyThrough/find.populate.customPK.js +76 -0
- package/interfaces/associations/manyToManyThrough/find.populate.js +120 -0
- package/interfaces/associations/manyToManyThrough/find.populate.where.js +157 -0
- package/interfaces/associations/manyToManyThrough/findOne.populate.js +93 -0
- package/interfaces/associations/manyToManyThrough/modifiers.js +560 -0
- package/interfaces/associations/manyToManyThrough/multipleForeignKeys.js +118 -0
- package/interfaces/associations/manyToManyThrough/remove.js +98 -0
- package/interfaces/associations/manyToManyThrough/update.nested.customPK.js +150 -0
- package/interfaces/associations/manyToManyThrough/update.nested.js +202 -0
- package/interfaces/associations/oneToOne/create.js +41 -0
- package/interfaces/associations/oneToOne/create.nested.customPK.js +61 -0
- package/interfaces/associations/oneToOne/create.nested.js +52 -0
- package/interfaces/associations/oneToOne/dynamicFinder.js +68 -0
- package/interfaces/associations/oneToOne/find.populate.js +127 -0
- package/interfaces/associations/oneToOne/findOne.populate.js +83 -0
- package/interfaces/associations/oneToOne/modifiers.js +275 -0
- package/interfaces/associations/oneToOne/multipleForeignKeys.js +69 -0
- package/interfaces/associations/oneToOne/update.nested.customPK.js +141 -0
- package/interfaces/associations/oneToOne/update.nested.js +174 -0
- package/interfaces/associations/support/bootstrap.js +82 -0
- package/interfaces/associations/support/fixtures/belongsTo.child.customPK.fixture.js +25 -0
- package/interfaces/associations/support/fixtures/belongsTo.child.fixture.js +25 -0
- package/interfaces/associations/support/fixtures/belongsTo.parent.customPK.fixture.js +26 -0
- package/interfaces/associations/support/fixtures/belongsTo.parent.fixture.js +28 -0
- package/interfaces/associations/support/fixtures/crud.fixture.js +53 -0
- package/interfaces/associations/support/fixtures/deep.address.fixture.js +21 -0
- package/interfaces/associations/support/fixtures/deep.breakdown.fixture.js +23 -0
- package/interfaces/associations/support/fixtures/deep.company.fixture.js +29 -0
- package/interfaces/associations/support/fixtures/deep.country.fixture.js +25 -0
- package/interfaces/associations/support/fixtures/deep.departement.fixture.js +24 -0
- package/interfaces/associations/support/fixtures/deep.driver.fixture.js +32 -0
- package/interfaces/associations/support/fixtures/deep.ride.fixture.js +23 -0
- package/interfaces/associations/support/fixtures/deep.seller.fixture.js +33 -0
- package/interfaces/associations/support/fixtures/deep.taxi.fixture.js +36 -0
- package/interfaces/associations/support/fixtures/hasMany.child.fixture.js +33 -0
- package/interfaces/associations/support/fixtures/hasMany.customPK.fixture.js +22 -0
- package/interfaces/associations/support/fixtures/hasMany.parent.fixture.js +28 -0
- package/interfaces/associations/support/fixtures/hasManyThrough.child.customPK.fixture.js +32 -0
- package/interfaces/associations/support/fixtures/hasManyThrough.parent.customPK.fixture.js +23 -0
- package/interfaces/associations/support/fixtures/hasManyThrough.stadium.fixture.js +20 -0
- package/interfaces/associations/support/fixtures/hasManyThrough.team.fixture.js +35 -0
- package/interfaces/associations/support/fixtures/hasManyThrough.through.customPK.fixture.js +18 -0
- package/interfaces/associations/support/fixtures/hasManyThrough.venue.fixture.js +18 -0
- package/interfaces/associations/support/fixtures/manyToMany.driver.customPK.fixture.js +22 -0
- package/interfaces/associations/support/fixtures/manyToMany.driver.fixture.js +25 -0
- package/interfaces/associations/support/fixtures/manyToMany.driver.withSchema.fixture.js +25 -0
- package/interfaces/associations/support/fixtures/manyToMany.taxi.customPK.fixture.js +21 -0
- package/interfaces/associations/support/fixtures/manyToMany.taxi.fixture.js +30 -0
- package/interfaces/associations/support/fixtures/manyToMany.taxi.withSchema.fixture.js +24 -0
- package/interfaces/associations/support/fixtures/multipleAssociations.fixture.js +54 -0
- package/interfaces/associations/support/fixtures/multipleOneToOneAssociations.fixture.js +40 -0
- package/interfaces/associations/support/fixtures/multipleThroughAssociations.fixture.js +79 -0
- package/interfaces/associations/support/fixtures/oneToOne.customPK.fixture.js +47 -0
- package/interfaces/associations/support/fixtures/oneToOne.fixture.js +52 -0
- package/interfaces/associations/support/fixtures.js +41 -0
- package/interfaces/datastore/datastore.test.js +63 -0
- package/interfaces/migratable/definition.test.js +65 -0
- package/interfaces/migratable/migrate.alter.test.js +55 -0
- package/interfaces/migratable/migrate.create.test.js +120 -0
- package/interfaces/migratable/migrate.drop.test.js +47 -0
- package/interfaces/migratable/migrate.safe.test.js +25 -0
- package/interfaces/migratable/schema.test.js +117 -0
- package/interfaces/migratable/support/bootstrap.js +55 -0
- package/interfaces/migratable/support/bootstrapFn.js +54 -0
- package/interfaces/migratable/support/fixtures/alter.fixture.js +12 -0
- package/interfaces/migratable/support/fixtures/create.fixture.js +12 -0
- package/interfaces/migratable/support/fixtures/crud.fixture.js +53 -0
- package/interfaces/migratable/support/fixtures/custom.fixture.js +17 -0
- package/interfaces/migratable/support/fixtures/drop.fixture.js +12 -0
- package/interfaces/migratable/support/fixtures/safe.fixture.js +12 -0
- package/interfaces/migratable/support/fixtures/schema.fixture.js +23 -0
- package/interfaces/queryable/caseInsensitive.test.js +196 -0
- package/interfaces/queryable/count.test.js +51 -0
- package/interfaces/queryable/dynamicFinders.test.js +179 -0
- package/interfaces/queryable/findLike.test.js +34 -0
- package/interfaces/queryable/findOneLike.test.js +28 -0
- package/interfaces/queryable/modifiers/average.modifier.test.js +55 -0
- package/interfaces/queryable/modifiers/contains.modifier.test.js +82 -0
- package/interfaces/queryable/modifiers/endsWith.modifier.test.js +82 -0
- package/interfaces/queryable/modifiers/greaterThan.modifier.test.js +335 -0
- package/interfaces/queryable/modifiers/groupBy.modifier.test.js +123 -0
- package/interfaces/queryable/modifiers/in.modifier.test.js +76 -0
- package/interfaces/queryable/modifiers/lessThan.modifier.test.js +223 -0
- package/interfaces/queryable/modifiers/like.modifier.test.js +50 -0
- package/interfaces/queryable/modifiers/limit.modifier.test.js +60 -0
- package/interfaces/queryable/modifiers/max.modifier.test.js +68 -0
- package/interfaces/queryable/modifiers/min.modifier.test.js +68 -0
- package/interfaces/queryable/modifiers/not.modifier.test.js +80 -0
- package/interfaces/queryable/modifiers/notIn.modifier.test.js +69 -0
- package/interfaces/queryable/modifiers/or.modifier.test.js +142 -0
- package/interfaces/queryable/modifiers/select.modifier.test.js +118 -0
- package/interfaces/queryable/modifiers/skip.modifier.test.js +59 -0
- package/interfaces/queryable/modifiers/sort.modifier.test.js +160 -0
- package/interfaces/queryable/modifiers/startsWith.modifier.test.js +83 -0
- package/interfaces/queryable/modifiers/sum.modifier.test.js +68 -0
- package/interfaces/queryable/support/bootstrap.js +80 -0
- package/interfaces/queryable/support/fixtures/crud.fixture.js +54 -0
- package/interfaces/scalable/README.md +19 -0
- package/interfaces/scalable/crud.test.js +73 -0
- package/interfaces/scalable/queries.test.js +9 -0
- package/interfaces/scalable/support/crud.fixture.js +60 -0
- package/interfaces/semantic/create.test.js +110 -0
- package/interfaces/semantic/createEach.test.js +50 -0
- package/interfaces/semantic/destroy.js +147 -0
- package/interfaces/semantic/find.test.js +91 -0
- package/interfaces/semantic/findOne.test.js +60 -0
- package/interfaces/semantic/findOrCreate.test.js +91 -0
- package/interfaces/semantic/findOrCreateEach.test.js +75 -0
- package/interfaces/semantic/support/bootstrap.js +85 -0
- package/interfaces/semantic/support/fixtures/crud.fixture.js +66 -0
- package/interfaces/semantic/support/fixtures/validations.fixture.js +23 -0
- package/interfaces/semantic/types/array.type.js +28 -0
- package/interfaces/semantic/types/binary.type.js +34 -0
- package/interfaces/semantic/types/boolean.type.js +80 -0
- package/interfaces/semantic/types/date.type.js +53 -0
- package/interfaces/semantic/types/datetime.type.js +48 -0
- package/interfaces/semantic/types/float.type.js +40 -0
- package/interfaces/semantic/types/integer.type.js +40 -0
- package/interfaces/semantic/types/json.type.js +29 -0
- package/interfaces/semantic/types/string.type.js +40 -0
- package/interfaces/semantic/types/text.type.js +34 -0
- package/interfaces/semantic/types/time.type.js +52 -0
- package/interfaces/semantic/update.test.js +157 -0
- package/interfaces/sql/find.test.js +73 -0
- package/interfaces/sql/query.test.js +40 -0
- package/interfaces/sql/support/bootstrap.js +82 -0
- package/interfaces/sql/support/fixtures/crud.fixture.js +57 -0
- package/interfaces/transactable/commit.test.js +438 -0
- package/interfaces/transactable/crossAdapter.test.js +346 -0
- package/interfaces/transactable/deep.test.js +100 -0
- package/interfaces/transactable/rollback.test.js +221 -0
- package/interfaces/transactable/support/bootstrap.js +55 -0
- package/interfaces/transactable/support/bootstrapFn.js +51 -0
- package/interfaces/transactable/support/fixtures/customer.fixture.js +29 -0
- package/interfaces/transactable/support/fixtures/payment.fixture.js +24 -0
- package/interfaces/transactable/support/fixtures/receipt.fixture.js +20 -0
- package/interfaces/transactable/support/fixtures/store.fixture.js +22 -0
- package/interfaces/transactable/support/fixtures/storecustomer.fixture.js +14 -0
- package/lib/index.js +155 -0
- package/lib/utils.js +39 -0
- package/package.json +58 -4
- package/test/integration/README.md +26 -0
- package/test/integration/config/offshore-memory.js +8 -0
- package/test/integration/config/offshore-sql-mariadb.js +15 -0
- package/test/integration/config/offshore-sql-mysql.js +21 -0
- package/test/integration/config/offshore-sql-oracle.js +19 -0
- package/test/integration/config/offshore-sql-postgres.js +27 -0
- package/test/integration/config/offshore-sql-sqlite3.js +19 -0
- package/test/integration/runner.js +83 -0
- package/test/integration/runnerDispatcher.js +163 -0
- package/test/load/README.md +20 -0
- package/test/load/index.js +33 -0
- package/test/load/runner.js +160 -0
- package/test/load/support/bootstrap.js +69 -0
- package/test/load/support/fixtures/pet.js +8 -0
- package/test/load/support/fixtures/user.js +9 -0
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
var Offshore = require(process.env.offshorePath || 'offshore');
|
|
2
|
+
var assert = require('assert');
|
|
3
|
+
var _ = require('lodash');
|
|
4
|
+
|
|
5
|
+
describe('Transaction cross adapter', function() {
|
|
6
|
+
|
|
7
|
+
var rollbackError = new Error('rollback');
|
|
8
|
+
|
|
9
|
+
describe('Commit', function() {
|
|
10
|
+
|
|
11
|
+
it('should make a cross-adapter transaction', function(done) {
|
|
12
|
+
var customerName = 'Cross-adapter trx test';
|
|
13
|
+
var paymentName = 'Cross-adapter trx test 2';
|
|
14
|
+
|
|
15
|
+
Offshore.Transaction([Transactable.Customer, Transactable.Payment], function(trx, cb) {
|
|
16
|
+
trx.customer.create({name: customerName}).exec(function(err, item1) {
|
|
17
|
+
if (err) {
|
|
18
|
+
done(err);
|
|
19
|
+
}
|
|
20
|
+
assert(item1);
|
|
21
|
+
assert.equal(item1.name, customerName);
|
|
22
|
+
trx.payment.create({type: paymentName}).exec(function(err, item2) {
|
|
23
|
+
if (err) {
|
|
24
|
+
done(err);
|
|
25
|
+
}
|
|
26
|
+
assert(item2);
|
|
27
|
+
assert.equal(item2.type, paymentName);
|
|
28
|
+
trx.customer.find({name: customerName}).exec(function(err, result1) {
|
|
29
|
+
if (err) {
|
|
30
|
+
return done(err);
|
|
31
|
+
}
|
|
32
|
+
assert.equal(result1.length, 1, 'Should find customer in transaction');
|
|
33
|
+
assert.equal(result1[0].name, customerName);
|
|
34
|
+
|
|
35
|
+
trx.payment.find({type: paymentName}).exec(function(err, result2) {
|
|
36
|
+
if (err) {
|
|
37
|
+
return done(err);
|
|
38
|
+
}
|
|
39
|
+
assert.equal(result2.length, 1, 'Should find payment in transaction');
|
|
40
|
+
assert.equal(result2[0].type, paymentName);
|
|
41
|
+
cb(null, 'ok');
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
}).exec(function(err, result) {
|
|
47
|
+
if (err) {
|
|
48
|
+
done(err);
|
|
49
|
+
}
|
|
50
|
+
Transactable.Customer.find({name: customerName}, function(err, customers) {
|
|
51
|
+
if (err) {
|
|
52
|
+
return done(err);
|
|
53
|
+
}
|
|
54
|
+
assert.equal(customers.length, 1, 'Should find commited customer');
|
|
55
|
+
assert.equal(customers[0].name, customerName);
|
|
56
|
+
Transactable.Payment.find({type: paymentName}, function(err, payments) {
|
|
57
|
+
if (err) {
|
|
58
|
+
return done(err);
|
|
59
|
+
}
|
|
60
|
+
assert.equal(payments.length, 1, 'Should find commited payment');
|
|
61
|
+
assert.equal(payments[0].type, paymentName);
|
|
62
|
+
done();
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('should make a cross-adapter nested create', function(done) {
|
|
69
|
+
var customerName = 'Cross-adapter trx parent';
|
|
70
|
+
var child1 = 'Cross-adapter trx child 1';
|
|
71
|
+
var child2 = 'Cross-adapter trx child 2';
|
|
72
|
+
|
|
73
|
+
Offshore.Transaction([Transactable.Customer, Transactable.Payment], function(trx, cb) {
|
|
74
|
+
trx.customer.create({name: customerName, payments: [{type: child1},
|
|
75
|
+
{type: child2}]}).exec(function(err, parent) {
|
|
76
|
+
if (err) {
|
|
77
|
+
done(err);
|
|
78
|
+
}
|
|
79
|
+
assert(parent);
|
|
80
|
+
assert.equal(parent.name, customerName);
|
|
81
|
+
trx.customer.find({name: customerName}).populate('payments').exec(function(err, populatedEntry) {
|
|
82
|
+
if (err) {
|
|
83
|
+
return done(err);
|
|
84
|
+
}
|
|
85
|
+
assert.equal(populatedEntry.length, 1, 'Should find customer');
|
|
86
|
+
assert.equal(populatedEntry[0].id, parent.id);
|
|
87
|
+
assert.equal(populatedEntry[0].name, customerName);
|
|
88
|
+
assert.equal(populatedEntry[0].payments.length, 2, 'Should populate 2 payments');
|
|
89
|
+
assert.equal(populatedEntry[0].payments[0].a_customer, parent.id);
|
|
90
|
+
assert.equal(populatedEntry[0].payments[1].a_customer, parent.id);
|
|
91
|
+
cb(null, 'ok');
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
}).exec(function(err, result) {
|
|
95
|
+
if (err) {
|
|
96
|
+
done(err);
|
|
97
|
+
}
|
|
98
|
+
Transactable.Customer.find({name: customerName}).populate('payments').exec(function(err, customers) {
|
|
99
|
+
if (err) {
|
|
100
|
+
return done(err);
|
|
101
|
+
}
|
|
102
|
+
assert.equal(customers.length, 1, 'Should find customer');
|
|
103
|
+
assert.equal(customers[0].name, customerName);
|
|
104
|
+
assert.equal(customers[0].payments.length, 2, 'Should populate 2 payments');
|
|
105
|
+
Transactable.Payment.find({type: [child1, child2]}, function(err, payments) {
|
|
106
|
+
if (err) {
|
|
107
|
+
return done(err);
|
|
108
|
+
}
|
|
109
|
+
assert.equal(payments.length, 2, 'Should find commited payments');
|
|
110
|
+
done();
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should make a cross-adapter nested update', function(done) {
|
|
117
|
+
var customerName = 'Cross-adapter update trx parent';
|
|
118
|
+
var child1 = 'Cross-adapter update trx child 1';
|
|
119
|
+
var child2 = 'Cross-adapter update trx child 2';
|
|
120
|
+
var child3 = 'Cross-adapter update trx child 3';
|
|
121
|
+
|
|
122
|
+
Transactable.Customer.create({name: customerName, payments: [{type: child1},
|
|
123
|
+
{type: child2}]}, function(err, parent) {
|
|
124
|
+
if (err) {
|
|
125
|
+
done(err);
|
|
126
|
+
}
|
|
127
|
+
assert(parent);
|
|
128
|
+
assert.equal(parent.name, customerName);
|
|
129
|
+
Offshore.Transaction([Transactable.Customer, Transactable.Payment], function(trx, cb) {
|
|
130
|
+
trx.customer.find({name: customerName}).populate('payments').exec(function(err, customers) {
|
|
131
|
+
if (err) {
|
|
132
|
+
return done(err);
|
|
133
|
+
}
|
|
134
|
+
assert.equal(customers.length, 1, 'Should find customer');
|
|
135
|
+
assert.equal(customers[0].id, parent.id);
|
|
136
|
+
assert.equal(customers[0].name, customerName);
|
|
137
|
+
assert.equal(customers[0].payments.length, 2, 'Should populate 2 payments');
|
|
138
|
+
assert.equal(customers[0].payments[0].a_customer, parent.id);
|
|
139
|
+
assert.equal(customers[0].payments[1].a_customer, parent.id);
|
|
140
|
+
|
|
141
|
+
customers[0].payments.add({type: child3});
|
|
142
|
+
customers[0].save(function(err) {
|
|
143
|
+
if (err) {
|
|
144
|
+
return done(err);
|
|
145
|
+
}
|
|
146
|
+
trx.customer.find({name: customerName}).populate('payments').exec(function(err, customers) {
|
|
147
|
+
if (err) {
|
|
148
|
+
return done(err);
|
|
149
|
+
}
|
|
150
|
+
assert.equal(customers.length, 1, 'Should find updated customer');
|
|
151
|
+
assert.equal(customers[0].id, parent.id);
|
|
152
|
+
assert.equal(customers[0].name, customerName);
|
|
153
|
+
assert.equal(customers[0].payments.length, 3, 'Should populate 3 payments');
|
|
154
|
+
cb(null, 'ok');
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
}).exec(function(err, result) {
|
|
159
|
+
if (err) {
|
|
160
|
+
done(err);
|
|
161
|
+
}
|
|
162
|
+
Transactable.Customer.find({name: customerName}).populate('payments').exec(function(err, customers) {
|
|
163
|
+
if (err) {
|
|
164
|
+
return done(err);
|
|
165
|
+
}
|
|
166
|
+
assert.equal(customers.length, 1, 'Should find updated customer');
|
|
167
|
+
assert.equal(customers[0].id, parent.id);
|
|
168
|
+
assert.equal(customers[0].name, customerName);
|
|
169
|
+
assert.equal(customers[0].payments.length, 3, 'Should populate 3 payments');
|
|
170
|
+
Transactable.Payment.find({type: [child1, child2, child3]}, function(err, payments) {
|
|
171
|
+
if (err) {
|
|
172
|
+
return done(err);
|
|
173
|
+
}
|
|
174
|
+
assert.equal(payments.length, 3, 'Should find commited payments');
|
|
175
|
+
done();
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
describe('Rollback', function() {
|
|
184
|
+
it('should make a cross-adapter transaction rollback', function(done) {
|
|
185
|
+
var customerName = 'Cross-adapter trx rollback test';
|
|
186
|
+
var paymentName = 'Cross-adapter trx rollback test 2';
|
|
187
|
+
Offshore.Transaction([Transactable.Customer, Transactable.Payment], function(trx, cb) {
|
|
188
|
+
trx.customer.create({name: customerName}).exec(function(err, item1) {
|
|
189
|
+
if (err) {
|
|
190
|
+
return done(err);
|
|
191
|
+
}
|
|
192
|
+
assert(item1);
|
|
193
|
+
assert.equal(item1.name, 'Cross-adapter trx rollback test');
|
|
194
|
+
trx.payment.create({type: paymentName}).exec(function(err, item2) {
|
|
195
|
+
if (err) {
|
|
196
|
+
return done(err);
|
|
197
|
+
}
|
|
198
|
+
assert(item2);
|
|
199
|
+
assert.equal(item2.type, paymentName);
|
|
200
|
+
trx.customer.find({name: customerName}).exec(function(err, customers) {
|
|
201
|
+
if (err) {
|
|
202
|
+
return done(err);
|
|
203
|
+
}
|
|
204
|
+
assert.equal(customers.length, 1, 'Should find customer');
|
|
205
|
+
assert.equal(customers[0].name, 'Cross-adapter trx rollback test', 'Should have found the created customer');
|
|
206
|
+
|
|
207
|
+
trx.payment.find({type: paymentName}).exec(function(err, payments) {
|
|
208
|
+
if (err) {
|
|
209
|
+
return done(err);
|
|
210
|
+
}
|
|
211
|
+
assert.equal(payments.length, 1, 'Should find payment');
|
|
212
|
+
assert.equal(payments[0].type, paymentName, 'Should have found the created payment');
|
|
213
|
+
cb(rollbackError);
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
}).exec(function(err, result) {
|
|
219
|
+
assert(err, rollbackError, 'Exec callback should return error passed in transaction callback');
|
|
220
|
+
assert(!result, 'Rollback should not return results');
|
|
221
|
+
Transactable.Customer.find({name: customerName}, function(err, firstResult) {
|
|
222
|
+
if (err) {
|
|
223
|
+
return done(err);
|
|
224
|
+
}
|
|
225
|
+
assert.equal(firstResult.length, 0, 'Customer creation should have been rollbacked');
|
|
226
|
+
Transactable.Payment.find({type: paymentName}, function(err, secondResult) {
|
|
227
|
+
if (err) {
|
|
228
|
+
return done(err);
|
|
229
|
+
}
|
|
230
|
+
assert.equal(secondResult.length, 0, 'Payment creation should have been rollbacked');
|
|
231
|
+
done();
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('should make a cross-adapter nested create rollback', function(done) {
|
|
238
|
+
var parentName = 'Cross-adapter trx rollback parent';
|
|
239
|
+
var child1 = 'Cross-adapter trx rollback child 1';
|
|
240
|
+
var child2 = 'Cross-adapter trx rollback child 2';
|
|
241
|
+
|
|
242
|
+
Offshore.Transaction([Transactable.Customer, Transactable.Payment], function(trx, cb) {
|
|
243
|
+
trx.customer.create({name: parentName, payments: [{type: child1},
|
|
244
|
+
{type: child2}]}).exec(function(err, parent) {
|
|
245
|
+
if (err) {
|
|
246
|
+
return done(err);
|
|
247
|
+
}
|
|
248
|
+
assert(parent);
|
|
249
|
+
assert.equal(parentName, parent.name);
|
|
250
|
+
trx.customer.find({name: parentName}).populate('payments').exec(function(err, populatedEntry) {
|
|
251
|
+
if (err) {
|
|
252
|
+
return done(err);
|
|
253
|
+
}
|
|
254
|
+
assert.equal(populatedEntry.length, 1, 'Should find created data before rollback');
|
|
255
|
+
populatedEntry = populatedEntry[0];
|
|
256
|
+
assert.equal(populatedEntry.id, parent.id);
|
|
257
|
+
assert.equal(populatedEntry.name, parentName);
|
|
258
|
+
assert.equal(populatedEntry.payments.length, 2, 'Should populate 2 payments in transaction');
|
|
259
|
+
assert.equal(populatedEntry.payments[0].a_customer, parent.id);
|
|
260
|
+
assert.equal(populatedEntry.payments[1].a_customer, parent.id);
|
|
261
|
+
cb(rollbackError);
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
}).exec(function(err, result) {
|
|
265
|
+
assert(err, rollbackError, 'Exec callback should return error passed in transaction callback');
|
|
266
|
+
assert(!result, 'Rollback should not return results');
|
|
267
|
+
Transactable.Customer.find({name: parentName}, function(err, firstResult) {
|
|
268
|
+
if (err) {
|
|
269
|
+
return done(err);
|
|
270
|
+
}
|
|
271
|
+
assert.equal(firstResult.length, 0, 'Parent creation should have been rollbacked');
|
|
272
|
+
Transactable.Payment.find({type: [child1, child2]}, function(err, secondResult) {
|
|
273
|
+
if (err) {
|
|
274
|
+
return done(err);
|
|
275
|
+
}
|
|
276
|
+
assert.equal(secondResult.length, 0, 'Children creation should have been rollbacked');
|
|
277
|
+
done();
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it('should make a cross-adapter nested update rollback', function(done) {
|
|
284
|
+
var parentName = 'Cross-adapter update trx rollback parent';
|
|
285
|
+
var child1 = 'Cross-adapter update trx rollback child 1';
|
|
286
|
+
var child2 = 'Cross-adapter update trx rollback child 2';
|
|
287
|
+
var child3 = 'Cross-adapter update trx rollback child 3';
|
|
288
|
+
Transactable.Customer.create({name: parentName, payments: [{type: child1},
|
|
289
|
+
{type: child2}]}, function(err, parent) {
|
|
290
|
+
if (err) {
|
|
291
|
+
return done(err);
|
|
292
|
+
}
|
|
293
|
+
assert(parent);
|
|
294
|
+
assert.equal(parent.name, parentName);
|
|
295
|
+
|
|
296
|
+
Offshore.Transaction([Transactable.Customer, Transactable.Payment], function(trx, cb) {
|
|
297
|
+
trx.customer.find({name: parentName}).populate('payments').exec(function(err, customers) {
|
|
298
|
+
if (err) {
|
|
299
|
+
return done(err);
|
|
300
|
+
}
|
|
301
|
+
assert.equal(customers.length, 1, 'Should find customer');
|
|
302
|
+
assert.equal(customers[0].id, parent.id);
|
|
303
|
+
assert.equal(customers[0].name, parentName);
|
|
304
|
+
assert.equal(customers[0].payments.length, 2, 'Should populate 2 payments');
|
|
305
|
+
assert.equal(customers[0].payments[0].a_customer, parent.id);
|
|
306
|
+
assert.equal(customers[0].payments[1].a_customer, parent.id);
|
|
307
|
+
|
|
308
|
+
customers[0].payments.add({type: child3});
|
|
309
|
+
customers[0].save(function(err) {
|
|
310
|
+
if (err) {
|
|
311
|
+
return done(err);
|
|
312
|
+
}
|
|
313
|
+
trx.customer.find({name: parentName}).populate('payments').exec(function(err, updatedCustomers) {
|
|
314
|
+
if (err) {
|
|
315
|
+
return done(err);
|
|
316
|
+
}
|
|
317
|
+
assert.equal(updatedCustomers.length, 1, 'Should find updated customer in transaction');
|
|
318
|
+
assert.equal(updatedCustomers[0].id, parent.id);
|
|
319
|
+
assert.equal(updatedCustomers[0].name, parentName);
|
|
320
|
+
assert.equal(updatedCustomers[0].payments.length, 3, 'Should populate 3 payments in transaction');
|
|
321
|
+
cb(rollbackError);
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
}).exec(function(err, result) {
|
|
326
|
+
assert(err, rollbackError, 'Exec callback should return error passed in transaction callback');
|
|
327
|
+
assert(!result, 'Rollback should not return results');
|
|
328
|
+
Transactable.Customer.find({name: parentName}, function(err, customers) {
|
|
329
|
+
if (err) {
|
|
330
|
+
return done(err);
|
|
331
|
+
}
|
|
332
|
+
assert.equal(customers.length, 1, 'Should not alter customers original data');
|
|
333
|
+
Transactable.Payment.find({type: [child1, child2, child3]}, function(err, payments) {
|
|
334
|
+
if (err) {
|
|
335
|
+
return done(err);
|
|
336
|
+
}
|
|
337
|
+
assert.equal(payments.length, 2, 'Should not alter payments original data');
|
|
338
|
+
done();
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
var Offshore = require(process.env.offshorePath || 'offshore');
|
|
2
|
+
var assert = require('assert');
|
|
3
|
+
var _ = require('lodash');
|
|
4
|
+
|
|
5
|
+
describe('Transaction cross adapter', function() {
|
|
6
|
+
|
|
7
|
+
it('should deep populate', function(done) {
|
|
8
|
+
var customerName = 'Parent customer deep';
|
|
9
|
+
var paymentName = 'Child payment deep';
|
|
10
|
+
var receiptName = 'Grandchild label deep';
|
|
11
|
+
|
|
12
|
+
Transactable.Customer.create({name: customerName}, function(err, customer) {
|
|
13
|
+
if (err) {
|
|
14
|
+
done(err);
|
|
15
|
+
}
|
|
16
|
+
assert.equal(customer.name, customerName);
|
|
17
|
+
Transactable.Payment.createEach([{type: paymentName, a_customer: customer.id},
|
|
18
|
+
{type: paymentName, a_customer: customer.id}], function(err, payments) {
|
|
19
|
+
if (err) {
|
|
20
|
+
done(err);
|
|
21
|
+
}
|
|
22
|
+
assert.equal(payments.length, 2);
|
|
23
|
+
Transactable.Receipt.createEach([{label: receiptName, a_payment: payments[0].id},
|
|
24
|
+
{label: receiptName, a_payment: payments[0].id}, {label: receiptName, a_payment: payments[1].id},
|
|
25
|
+
{label: receiptName, a_payment: payments[1].id}], function(err, receipts) {
|
|
26
|
+
if (err) {
|
|
27
|
+
done(err);
|
|
28
|
+
}
|
|
29
|
+
assert.equal(receipts.length, 4);
|
|
30
|
+
|
|
31
|
+
Offshore.Transaction([Transactable.Customer, Transactable.Payment, Transactable.Receipt], function(trx, cb) {
|
|
32
|
+
trx.customer.findOne({name: customerName}).populate('payments.receipts').exec(function(err, populatedCustomer) {
|
|
33
|
+
if (err) {
|
|
34
|
+
return done(err);
|
|
35
|
+
}
|
|
36
|
+
assert.equal(populatedCustomer.name, customerName);
|
|
37
|
+
assert.equal(populatedCustomer.payments.length, 2, 'Should populate 2 payments');
|
|
38
|
+
assert.equal(populatedCustomer.payments[0].receipts.length, 2, 'Should populate 2 receipts for each payment');
|
|
39
|
+
assert.equal(populatedCustomer.payments[1].receipts.length, 2, 'Should populate 2 receipts for each payment');
|
|
40
|
+
return cb(null, populatedCustomer);
|
|
41
|
+
});
|
|
42
|
+
}).exec(function(err, result) {
|
|
43
|
+
if (err) {
|
|
44
|
+
done(err);
|
|
45
|
+
}
|
|
46
|
+
done();
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('find deep criteria', function(done) {
|
|
54
|
+
var customerName1 = 'Parent customer find deep';
|
|
55
|
+
var paymentName = 'Child payment find deep';
|
|
56
|
+
var receiptName1 = 'Grandchild label find deep';
|
|
57
|
+
var customerName2 = 'Parent customer find deep 2';
|
|
58
|
+
var receiptName2 = 'Grandchild label find deep 2';
|
|
59
|
+
|
|
60
|
+
Transactable.Customer.createEach([{name: customerName1}, {name: customerName2}], function(err, customers) {
|
|
61
|
+
if (err) {
|
|
62
|
+
done(err);
|
|
63
|
+
}
|
|
64
|
+
assert.equal(customers.length, 2);
|
|
65
|
+
Transactable.Payment.createEach([{type: paymentName, a_customer: customers[0].id},
|
|
66
|
+
{type: paymentName, a_customer: customers[1].id}], function(err, payments) {
|
|
67
|
+
if (err) {
|
|
68
|
+
done(err);
|
|
69
|
+
}
|
|
70
|
+
assert.equal(payments.length, 2);
|
|
71
|
+
Transactable.Receipt.createEach([{label: receiptName1, a_payment: payments[0].id},
|
|
72
|
+
{label: receiptName1, a_payment: payments[0].id}, {label: receiptName2, a_payment: payments[1].id},
|
|
73
|
+
{label: receiptName2, a_payment: payments[1].id}], function(err, receipts) {
|
|
74
|
+
if (err) {
|
|
75
|
+
done(err);
|
|
76
|
+
}
|
|
77
|
+
assert.equal(receipts.length, 4);
|
|
78
|
+
|
|
79
|
+
Offshore.Transaction([Transactable.Customer, Transactable.Payment, Transactable.Receipt], function(trx, cb) {
|
|
80
|
+
trx.customer.find({payments: {receipts: {label: receiptName1}}}).exec(function(err, populatedCustomers) {
|
|
81
|
+
if (err) {
|
|
82
|
+
return done(err);
|
|
83
|
+
}
|
|
84
|
+
assert.equal(populatedCustomers.length, 1);
|
|
85
|
+
assert.equal(populatedCustomers[0].id, customers[0].id);
|
|
86
|
+
assert.equal(populatedCustomers[0].name, customerName1);
|
|
87
|
+
return cb(null, populatedCustomers);
|
|
88
|
+
});
|
|
89
|
+
}).exec(function(err, result) {
|
|
90
|
+
if (err) {
|
|
91
|
+
done(err);
|
|
92
|
+
}
|
|
93
|
+
done();
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
});
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
var Offshore = require(process.env.offshorePath || 'offshore');
|
|
2
|
+
var assert = require('assert');
|
|
3
|
+
var _ = require('lodash');
|
|
4
|
+
|
|
5
|
+
describe('Transaction rollback', function() {
|
|
6
|
+
|
|
7
|
+
var rollbackError = new Error('rollback');
|
|
8
|
+
|
|
9
|
+
it('should rollback the created entry', function(done) {
|
|
10
|
+
var customerName = 'Test create rollback';
|
|
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
|
+
var id = createResult.id;
|
|
17
|
+
trx.customer.find({id: id}).exec(function(err, findInTrx) {
|
|
18
|
+
if (err) {
|
|
19
|
+
return done(err);
|
|
20
|
+
}
|
|
21
|
+
assert.equal(findInTrx.length, 1, 'Customer should be visible inside the transaction');
|
|
22
|
+
Transactable.Customer.find({id: id}, function(err, findOutTrx) {
|
|
23
|
+
if (err) {
|
|
24
|
+
return done(err);
|
|
25
|
+
}
|
|
26
|
+
assert.equal(findOutTrx.length, 0, 'Customer should not be visible outside the transaction');
|
|
27
|
+
cb(rollbackError);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
}).exec(function(err, result) {
|
|
32
|
+
assert(err, rollbackError, 'Exec callback should return error passed in transaction callback');
|
|
33
|
+
assert(!result, 'Rollback should not return results');
|
|
34
|
+
Transactable.Customer.find({name: customerName}, function(err, test) {
|
|
35
|
+
if (err) {
|
|
36
|
+
return done(err);
|
|
37
|
+
}
|
|
38
|
+
assert.equal(test.length, 0, 'Created entry should be rollbacked');
|
|
39
|
+
done();
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should rollback without doing anything', function(done) {
|
|
45
|
+
Offshore.Transaction(Transactable.Customer, function(trx, cb) {
|
|
46
|
+
cb(rollbackError);
|
|
47
|
+
}).exec(function(err, result) {
|
|
48
|
+
assert(err, rollbackError, 'Exec callback should return error passed in transaction callback');
|
|
49
|
+
assert(!result, 'Rollback should not return results');
|
|
50
|
+
done();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('should rollback the delete', function(done) {
|
|
55
|
+
var customerName = 'Test delete rollback';
|
|
56
|
+
Transactable.Customer.create({name: customerName}, function(err, testEntry) {
|
|
57
|
+
if (err) {
|
|
58
|
+
done(err);
|
|
59
|
+
}
|
|
60
|
+
Transactable.Customer.find({name: customerName}, function(err, entry) {
|
|
61
|
+
if (err) {
|
|
62
|
+
done(err);
|
|
63
|
+
}
|
|
64
|
+
assert.equal(entry.length, 1);
|
|
65
|
+
Offshore.Transaction(Transactable.Customer, function(trx, cb) {
|
|
66
|
+
trx.customer.destroy({name: customerName}).exec(function(err, deleteResult) {
|
|
67
|
+
if (err) {
|
|
68
|
+
return done(err);
|
|
69
|
+
}
|
|
70
|
+
cb(rollbackError);
|
|
71
|
+
});
|
|
72
|
+
}).exec(function(err, result) {
|
|
73
|
+
assert(err, rollbackError, 'Exec callback should return error passed in transaction callback');
|
|
74
|
+
assert(!result, 'Rollback should not return results');
|
|
75
|
+
Transactable.Customer.find({name: customerName}, function(err, result) {
|
|
76
|
+
if (err) {
|
|
77
|
+
return done(err);
|
|
78
|
+
}
|
|
79
|
+
assert.equal(result.length, 1, 'Customer destroy should be rollbacked');
|
|
80
|
+
done();
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should rollback the update', function(done) {
|
|
88
|
+
var customerName = 'Test update rollback';
|
|
89
|
+
var updatedName = 'Updated test rollback';
|
|
90
|
+
Transactable.Customer.create({name: customerName}, function(err, testEntry) {
|
|
91
|
+
if (err) {
|
|
92
|
+
done(err);
|
|
93
|
+
}
|
|
94
|
+
Transactable.Customer.find({name: customerName}, function(err, entry) {
|
|
95
|
+
if (err) {
|
|
96
|
+
done(err);
|
|
97
|
+
}
|
|
98
|
+
assert.equal(entry.length, 1);
|
|
99
|
+
Offshore.Transaction(Transactable.Customer, function(trx, cb) {
|
|
100
|
+
trx.customer.update({name: customerName}, {name: updatedName}).exec(function(err, updatedResult) {
|
|
101
|
+
if (err) {
|
|
102
|
+
return done(err);
|
|
103
|
+
}
|
|
104
|
+
assert.equal(updatedResult.length, 1, 'Should update customer in transaction');
|
|
105
|
+
cb(rollbackError);
|
|
106
|
+
});
|
|
107
|
+
}).exec(function(err, result) {
|
|
108
|
+
assert(err, rollbackError, 'Exec callback should return error passed in transaction callback');
|
|
109
|
+
assert(!result, 'should return no results');
|
|
110
|
+
Transactable.Customer.find({name: updatedName}, function(err, result) {
|
|
111
|
+
if (err) {
|
|
112
|
+
return done(err);
|
|
113
|
+
}
|
|
114
|
+
assert.equal(result.length, 0, 'Customer should not be updated');
|
|
115
|
+
Transactable.Customer.find({name: customerName}, function(err, result) {
|
|
116
|
+
if (err) {
|
|
117
|
+
return done(err);
|
|
118
|
+
}
|
|
119
|
+
assert.equal(result.length, 1, 'Original customer should still exist');
|
|
120
|
+
done();
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('should do a nested transaction and rollback only the first level', function(done) {
|
|
129
|
+
var customerName = 'Test rollback transaception2';
|
|
130
|
+
|
|
131
|
+
Offshore.Transaction(Transactable.Customer, function(trx1, cb1) {
|
|
132
|
+
trx1.customer.create({name: customerName}).exec(function(err, createResult1) {
|
|
133
|
+
if (err) {
|
|
134
|
+
return done(err);
|
|
135
|
+
}
|
|
136
|
+
Offshore.Transaction(Transactable.Customer, function(trx2, cb2) {
|
|
137
|
+
trx2.customer.create({name: customerName}).exec(function(err, createResult2) {
|
|
138
|
+
if (err) {
|
|
139
|
+
return done(err);
|
|
140
|
+
}
|
|
141
|
+
cb2(null, createResult2);
|
|
142
|
+
});
|
|
143
|
+
}).exec(function(err, result) {
|
|
144
|
+
if (err) {
|
|
145
|
+
done(err);
|
|
146
|
+
}
|
|
147
|
+
cb1(rollbackError);
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
}).exec(function(err, result) {
|
|
151
|
+
assert(err, rollbackError, 'Exec callback should return error passed in transaction callback');
|
|
152
|
+
assert(!result, 'Rollback should not return results');
|
|
153
|
+
Transactable.Customer.find({name: customerName}, function(err, test) {
|
|
154
|
+
if (err) {
|
|
155
|
+
return done(err);
|
|
156
|
+
}
|
|
157
|
+
assert.equal(test.length, 1, 'Only 1 of the transaction customer should have been commited');
|
|
158
|
+
done();
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('should rollback through table', function(done) {
|
|
164
|
+
var customerName = 'Through rollback customer';
|
|
165
|
+
var storeName1 = 'Through rollback store 1';
|
|
166
|
+
var storeName2 = 'Through rollback store 2';
|
|
167
|
+
var customerId;
|
|
168
|
+
var store1Id;
|
|
169
|
+
var store2Id;
|
|
170
|
+
|
|
171
|
+
Offshore.Transaction([Transactable.Customer, Transactable.Store], function(trx, cb) {
|
|
172
|
+
trx.customer.create({name: customerName, stores: [{name: storeName1}, {name: storeName2}]}).exec(function(err, customer) {
|
|
173
|
+
if (err) {
|
|
174
|
+
done(err);
|
|
175
|
+
}
|
|
176
|
+
assert.equal(customer.name, customerName);
|
|
177
|
+
customerId = customer.id;
|
|
178
|
+
trx.customer.findOne({name: customerName}).populate('stores').exec(function(err, populatedCustomer) {
|
|
179
|
+
if (err) {
|
|
180
|
+
return done(err);
|
|
181
|
+
}
|
|
182
|
+
assert.equal(populatedCustomer.name, customerName);
|
|
183
|
+
assert.equal(populatedCustomer.stores.length, 2, 'Should populate 2 stores');
|
|
184
|
+
store1Id = populatedCustomer.stores[0].id;
|
|
185
|
+
store2Id = populatedCustomer.stores[1].id;
|
|
186
|
+
return cb(rollbackError);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
}).exec(function(err, result) {
|
|
190
|
+
assert(err, rollbackError, 'Exec callback should return error passed in transaction callback');
|
|
191
|
+
assert(!result, 'Rollback should not return results');
|
|
192
|
+
Transactable.Customer.find({name: customerName}).populate('stores').exec(function(err, customer) {
|
|
193
|
+
if (err) {
|
|
194
|
+
done(err);
|
|
195
|
+
}
|
|
196
|
+
assert.equal(customer.length, 0, 'Created customer should be rollbacked');
|
|
197
|
+
Transactable.Store.find({name: storeName1}).populate('customers').exec(function(err, store1) {
|
|
198
|
+
if (err) {
|
|
199
|
+
done(err);
|
|
200
|
+
}
|
|
201
|
+
assert.equal(store1.length, 0, 'Created first store should be rollbacked');
|
|
202
|
+
Transactable.Store.find({name: storeName2}).populate('customers').exec(function(err, store2) {
|
|
203
|
+
if (err) {
|
|
204
|
+
done(err);
|
|
205
|
+
}
|
|
206
|
+
assert.equal(store2.length, 0, 'Created second store be rollbacked');
|
|
207
|
+
|
|
208
|
+
Transactable.Storecustomer.find([{customer: customerId}, {store: store1Id}, {store: store2Id}]).exec(function(err, junction) {
|
|
209
|
+
if (err) {
|
|
210
|
+
done(err);
|
|
211
|
+
}
|
|
212
|
+
assert.equal(junction.length, 0, 'Junction entries should be rollbacked');
|
|
213
|
+
done();
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
});
|