@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,560 @@
|
|
|
1
|
+
var assert = require('assert'),
|
|
2
|
+
_ = require('lodash');
|
|
3
|
+
|
|
4
|
+
describe('Association Interface', function() {
|
|
5
|
+
|
|
6
|
+
describe('Has Many Association', function() {
|
|
7
|
+
describe('Modifiers', function() {
|
|
8
|
+
|
|
9
|
+
/////////////////////////////////////////////////////
|
|
10
|
+
// TEST SETUP
|
|
11
|
+
////////////////////////////////////////////////////
|
|
12
|
+
|
|
13
|
+
var names = ['modifier1', 'modifier2', 'modifier3', 'modifier4'];
|
|
14
|
+
|
|
15
|
+
before(function(done) {
|
|
16
|
+
|
|
17
|
+
// Check Customer hasMany Payments
|
|
18
|
+
assert.strictEqual(Associations.Customer.attributes.payments.collection, 'payment');
|
|
19
|
+
assert.strictEqual(Associations.Payment.attributes.a_customer.model, 'customer');
|
|
20
|
+
|
|
21
|
+
var Customers = [];
|
|
22
|
+
|
|
23
|
+
Customers.push({name: 'modifier1', capital: 10000, payments: []});
|
|
24
|
+
Customers.push({name: 'modifier2', capital: 25000, payments: []});
|
|
25
|
+
Customers.push({name: 'modifier3', capital: 32000, payments: []});
|
|
26
|
+
Customers.push({name: 'modifier4', capital: 25000, payments: []});
|
|
27
|
+
|
|
28
|
+
Customers[1].payments.push({amount: 150, type: 'aquaman', bank: 'Money Inc', currency: 'euro'});
|
|
29
|
+
Customers[1].payments.push({amount: 350, type: 'batman', bank: 'Cash Tm', currency: 'dollar'});
|
|
30
|
+
Customers[1].payments.push({amount: 180, type: 'catwoman', bank: 'Money Inc', currency: 'euro'});
|
|
31
|
+
Customers[1].payments.push({amount: 180, type: 'daredevil', bank: 'Cash Tm', currency: 'euro'});
|
|
32
|
+
|
|
33
|
+
Associations.Customer.createEach(Customers, function(err) {
|
|
34
|
+
assert.ifError(err);
|
|
35
|
+
done();
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
/////////////////////////////////////////////////////
|
|
40
|
+
// TEST METHODS
|
|
41
|
+
////////////////////////////////////////////////////
|
|
42
|
+
|
|
43
|
+
it('should sort with parent asc and child asc', function(done) {
|
|
44
|
+
Associations.Customer.find({name: names, sort: 'name asc'})
|
|
45
|
+
.populate('payments', {sort: 'type asc'})
|
|
46
|
+
.exec(function(err, customers) {
|
|
47
|
+
assert.ifError(err);
|
|
48
|
+
|
|
49
|
+
assert(Array.isArray(customers));
|
|
50
|
+
assert.strictEqual(customers.length, 4);
|
|
51
|
+
assert.strictEqual(customers[0].name, 'modifier1');
|
|
52
|
+
assert.strictEqual(customers[1].name, 'modifier2');
|
|
53
|
+
assert.strictEqual(customers[2].name, 'modifier3');
|
|
54
|
+
assert.strictEqual(customers[3].name, 'modifier4');
|
|
55
|
+
|
|
56
|
+
assert(Array.isArray(customers[1].payments));
|
|
57
|
+
assert.strictEqual(customers[1].payments.length, 4);
|
|
58
|
+
assert.strictEqual(customers[1].payments[0].type, 'aquaman');
|
|
59
|
+
assert.strictEqual(customers[1].payments[1].type, 'batman');
|
|
60
|
+
assert.strictEqual(customers[1].payments[2].type, 'catwoman');
|
|
61
|
+
assert.strictEqual(customers[1].payments[3].type, 'daredevil');
|
|
62
|
+
|
|
63
|
+
done();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should sort with parent desc and child desc', function(done) {
|
|
68
|
+
Associations.Customer.find({name: names, sort: 'name desc'})
|
|
69
|
+
.populate('payments', {sort: 'type desc'})
|
|
70
|
+
.exec(function(err, customers) {
|
|
71
|
+
assert.ifError(err);
|
|
72
|
+
|
|
73
|
+
assert(Array.isArray(customers));
|
|
74
|
+
assert.strictEqual(customers.length, 4);
|
|
75
|
+
assert.strictEqual(customers[3].name, 'modifier1');
|
|
76
|
+
assert.strictEqual(customers[2].name, 'modifier2');
|
|
77
|
+
assert.strictEqual(customers[1].name, 'modifier3');
|
|
78
|
+
assert.strictEqual(customers[0].name, 'modifier4');
|
|
79
|
+
|
|
80
|
+
assert(Array.isArray(customers[2].payments));
|
|
81
|
+
assert.strictEqual(customers[2].payments.length, 4);
|
|
82
|
+
assert.strictEqual(customers[2].payments[3].type, 'aquaman');
|
|
83
|
+
assert.strictEqual(customers[2].payments[2].type, 'batman');
|
|
84
|
+
assert.strictEqual(customers[2].payments[1].type, 'catwoman');
|
|
85
|
+
assert.strictEqual(customers[2].payments[0].type, 'daredevil');
|
|
86
|
+
|
|
87
|
+
done();
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should sort with parent asc and child desc', function(done) {
|
|
92
|
+
Associations.Customer.find({name: names, sort: 'name asc'})
|
|
93
|
+
.populate('payments', {sort: 'type desc'})
|
|
94
|
+
.exec(function(err, customers) {
|
|
95
|
+
assert.ifError(err);
|
|
96
|
+
|
|
97
|
+
assert(Array.isArray(customers));
|
|
98
|
+
assert.strictEqual(customers.length, 4);
|
|
99
|
+
assert.strictEqual(customers[0].name, 'modifier1');
|
|
100
|
+
assert.strictEqual(customers[1].name, 'modifier2');
|
|
101
|
+
assert.strictEqual(customers[2].name, 'modifier3');
|
|
102
|
+
assert.strictEqual(customers[3].name, 'modifier4');
|
|
103
|
+
|
|
104
|
+
assert(Array.isArray(customers[1].payments));
|
|
105
|
+
assert.strictEqual(customers[1].payments.length, 4);
|
|
106
|
+
assert.strictEqual(customers[1].payments[3].type, 'aquaman');
|
|
107
|
+
assert.strictEqual(customers[1].payments[2].type, 'batman');
|
|
108
|
+
assert.strictEqual(customers[1].payments[1].type, 'catwoman');
|
|
109
|
+
assert.strictEqual(customers[1].payments[0].type, 'daredevil');
|
|
110
|
+
|
|
111
|
+
done();
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should sort with parent desc and child asc', function(done) {
|
|
116
|
+
Associations.Customer.find({name: names, sort: 'name desc'})
|
|
117
|
+
.populate('payments', {sort: 'type asc'})
|
|
118
|
+
.exec(function(err, customers) {
|
|
119
|
+
assert.ifError(err);
|
|
120
|
+
|
|
121
|
+
assert(Array.isArray(customers));
|
|
122
|
+
assert.strictEqual(customers.length, 4);
|
|
123
|
+
assert.strictEqual(customers[3].name, 'modifier1');
|
|
124
|
+
assert.strictEqual(customers[2].name, 'modifier2');
|
|
125
|
+
assert.strictEqual(customers[1].name, 'modifier3');
|
|
126
|
+
assert.strictEqual(customers[0].name, 'modifier4');
|
|
127
|
+
|
|
128
|
+
assert(Array.isArray(customers[2].payments));
|
|
129
|
+
assert.strictEqual(customers[2].payments.length, 4);
|
|
130
|
+
assert.strictEqual(customers[2].payments[0].type, 'aquaman');
|
|
131
|
+
assert.strictEqual(customers[2].payments[1].type, 'batman');
|
|
132
|
+
assert.strictEqual(customers[2].payments[2].type, 'catwoman');
|
|
133
|
+
assert.strictEqual(customers[2].payments[3].type, 'daredevil');
|
|
134
|
+
|
|
135
|
+
done();
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('should sort skip limit with parent asc and child asc', function(done) {
|
|
140
|
+
Associations.Customer.find({name: names, sort: 'name asc'})
|
|
141
|
+
.populate('payments', {sort: 'type asc', skip: 1, limit: 2})
|
|
142
|
+
.exec(function(err, customers) {
|
|
143
|
+
assert.ifError(err);
|
|
144
|
+
|
|
145
|
+
assert(Array.isArray(customers));
|
|
146
|
+
assert.strictEqual(customers.length, 4);
|
|
147
|
+
assert.strictEqual(customers[0].name, 'modifier1');
|
|
148
|
+
assert.strictEqual(customers[1].name, 'modifier2');
|
|
149
|
+
assert.strictEqual(customers[2].name, 'modifier3');
|
|
150
|
+
assert.strictEqual(customers[3].name, 'modifier4');
|
|
151
|
+
|
|
152
|
+
assert(Array.isArray(customers[1].payments));
|
|
153
|
+
assert.strictEqual(customers[1].payments.length, 2);
|
|
154
|
+
assert.strictEqual(customers[1].payments[0].type, 'batman');
|
|
155
|
+
assert.strictEqual(customers[1].payments[1].type, 'catwoman');
|
|
156
|
+
|
|
157
|
+
done();
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('should sort skip limit with parent desc and child desc', function(done) {
|
|
162
|
+
Associations.Customer.find({name: names, sort: 'name desc'})
|
|
163
|
+
.populate('payments', {sort: 'type desc', skip: 1, limit: 2})
|
|
164
|
+
.exec(function(err, customers) {
|
|
165
|
+
assert.ifError(err);
|
|
166
|
+
|
|
167
|
+
assert(Array.isArray(customers));
|
|
168
|
+
assert.strictEqual(customers.length, 4);
|
|
169
|
+
assert.strictEqual(customers[3].name, 'modifier1');
|
|
170
|
+
assert.strictEqual(customers[2].name, 'modifier2');
|
|
171
|
+
assert.strictEqual(customers[1].name, 'modifier3');
|
|
172
|
+
assert.strictEqual(customers[0].name, 'modifier4');
|
|
173
|
+
|
|
174
|
+
assert(Array.isArray(customers[2].payments));
|
|
175
|
+
assert.strictEqual(customers[2].payments.length, 2);
|
|
176
|
+
assert.strictEqual(customers[2].payments[1].type, 'batman');
|
|
177
|
+
assert.strictEqual(customers[2].payments[0].type, 'catwoman');
|
|
178
|
+
|
|
179
|
+
done();
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('should sort skip limit with parent asc and child desc', function(done) {
|
|
184
|
+
Associations.Customer.find({name: names, sort: 'name asc'})
|
|
185
|
+
.populate('payments', {sort: 'type desc', skip: 1, limit: 2})
|
|
186
|
+
.exec(function(err, customers) {
|
|
187
|
+
assert.ifError(err);
|
|
188
|
+
|
|
189
|
+
assert(Array.isArray(customers));
|
|
190
|
+
assert.strictEqual(customers.length, 4);
|
|
191
|
+
assert.strictEqual(customers[0].name, 'modifier1');
|
|
192
|
+
assert.strictEqual(customers[1].name, 'modifier2');
|
|
193
|
+
assert.strictEqual(customers[2].name, 'modifier3');
|
|
194
|
+
assert.strictEqual(customers[3].name, 'modifier4');
|
|
195
|
+
|
|
196
|
+
assert(Array.isArray(customers[1].payments));
|
|
197
|
+
assert.strictEqual(customers[1].payments.length, 2);
|
|
198
|
+
assert.strictEqual(customers[1].payments[1].type, 'batman');
|
|
199
|
+
assert.strictEqual(customers[1].payments[0].type, 'catwoman');
|
|
200
|
+
|
|
201
|
+
done();
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('should sort skip limit with parent desc and child asc', function(done) {
|
|
206
|
+
Associations.Customer.find({name: names, sort: 'name desc'})
|
|
207
|
+
.populate('payments', {sort: 'type asc', skip: 1, limit: 2})
|
|
208
|
+
.exec(function(err, customers) {
|
|
209
|
+
assert.ifError(err);
|
|
210
|
+
|
|
211
|
+
assert(Array.isArray(customers));
|
|
212
|
+
assert.strictEqual(customers.length, 4);
|
|
213
|
+
assert.strictEqual(customers[3].name, 'modifier1');
|
|
214
|
+
assert.strictEqual(customers[2].name, 'modifier2');
|
|
215
|
+
assert.strictEqual(customers[1].name, 'modifier3');
|
|
216
|
+
assert.strictEqual(customers[0].name, 'modifier4');
|
|
217
|
+
|
|
218
|
+
assert(Array.isArray(customers[2].payments));
|
|
219
|
+
assert.strictEqual(customers[2].payments.length, 2);
|
|
220
|
+
assert.strictEqual(customers[2].payments[0].type, 'batman');
|
|
221
|
+
assert.strictEqual(customers[2].payments[1].type, 'catwoman');
|
|
222
|
+
|
|
223
|
+
done();
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it('should return the correct average', function(done) {
|
|
228
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
229
|
+
.populate('payments', {average: ['amount'] })
|
|
230
|
+
.exec(function(err, customers) {
|
|
231
|
+
assert.ifError(err);
|
|
232
|
+
|
|
233
|
+
assert(Array.isArray(customers));
|
|
234
|
+
assert.strictEqual(customers.length, 1);
|
|
235
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
236
|
+
|
|
237
|
+
assert(Array.isArray(customers[0].payments));
|
|
238
|
+
assert.strictEqual(customers[0].payments.length, 1);
|
|
239
|
+
assert.strictEqual(customers[0].payments[0].amount, 215);
|
|
240
|
+
done();
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('should return the correct min', function(done) {
|
|
245
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
246
|
+
.populate('payments', {min: ['amount'] })
|
|
247
|
+
.exec(function(err, customers) {
|
|
248
|
+
assert.ifError(err);
|
|
249
|
+
assert(Array.isArray(customers));
|
|
250
|
+
assert.strictEqual(customers.length, 1);
|
|
251
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
252
|
+
assert(Array.isArray(customers[0].payments));
|
|
253
|
+
assert.strictEqual(customers[0].payments.length, 1);
|
|
254
|
+
assert.strictEqual(customers[0].payments[0].amount, 150);
|
|
255
|
+
done();
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('should return the correct max', function(done) {
|
|
260
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
261
|
+
.populate('payments', {max: ['amount'] })
|
|
262
|
+
.exec(function(err, customers) {
|
|
263
|
+
assert.ifError(err);
|
|
264
|
+
assert(Array.isArray(customers));
|
|
265
|
+
assert.strictEqual(customers.length, 1);
|
|
266
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
267
|
+
assert(Array.isArray(customers[0].payments));
|
|
268
|
+
assert.strictEqual(customers[0].payments.length, 1);
|
|
269
|
+
assert.strictEqual(customers[0].payments[0].amount, 350);
|
|
270
|
+
done();
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('should return the correct sum', function(done) {
|
|
275
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
276
|
+
.populate('payments', {sum: ['amount'] })
|
|
277
|
+
.exec(function(err, customers) {
|
|
278
|
+
assert.ifError(err);
|
|
279
|
+
assert(Array.isArray(customers));
|
|
280
|
+
assert.strictEqual(customers.length, 1);
|
|
281
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
282
|
+
assert(Array.isArray(customers[0].payments));
|
|
283
|
+
assert.strictEqual(customers[0].payments.length, 1);
|
|
284
|
+
assert.strictEqual(customers[0].payments[0].amount, 860);
|
|
285
|
+
done();
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it('should return records with contains modifier', function(done) {
|
|
290
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
291
|
+
.populate('payments', {type: {contains: 'at'}, sort: 'type asc' })
|
|
292
|
+
.exec(function(err, customers) {
|
|
293
|
+
assert.ifError(err);
|
|
294
|
+
assert(Array.isArray(customers));
|
|
295
|
+
assert.strictEqual(customers.length, 1);
|
|
296
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
297
|
+
assert(Array.isArray(customers[0].payments));
|
|
298
|
+
assert.strictEqual(customers[0].payments.length, 2);
|
|
299
|
+
assert.strictEqual(customers[0].payments[0].type, 'batman');
|
|
300
|
+
assert.strictEqual(customers[0].payments[1].type, 'catwoman');
|
|
301
|
+
done();
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it('should return records with endsWith modifier', function(done) {
|
|
306
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
307
|
+
.populate('payments', {type: {endsWith: 'man'}, sort: 'type asc' })
|
|
308
|
+
.exec(function(err, customers) {
|
|
309
|
+
assert.ifError(err);
|
|
310
|
+
assert(Array.isArray(customers));
|
|
311
|
+
assert.strictEqual(customers.length, 1);
|
|
312
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
313
|
+
assert(Array.isArray(customers[0].payments));
|
|
314
|
+
assert.strictEqual(customers[0].payments.length, 3);
|
|
315
|
+
assert.strictEqual(customers[0].payments[0].type, 'aquaman');
|
|
316
|
+
assert.strictEqual(customers[0].payments[1].type, 'batman');
|
|
317
|
+
assert.strictEqual(customers[0].payments[2].type, 'catwoman');
|
|
318
|
+
done();
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it('should return records with greaterThan modifier', function(done) {
|
|
323
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
324
|
+
.populate('payments', {amount: {greaterThan: 160}, sort: 'amount asc' })
|
|
325
|
+
.exec(function(err, customers) {
|
|
326
|
+
assert.ifError(err);
|
|
327
|
+
assert(Array.isArray(customers));
|
|
328
|
+
assert.strictEqual(customers.length, 1);
|
|
329
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
330
|
+
assert(Array.isArray(customers[0].payments));
|
|
331
|
+
assert.strictEqual(customers[0].payments.length, 3);
|
|
332
|
+
assert.strictEqual(customers[0].payments[0].amount, 180);
|
|
333
|
+
assert.strictEqual(customers[0].payments[1].amount, 180);
|
|
334
|
+
assert.strictEqual(customers[0].payments[2].amount, 350);
|
|
335
|
+
done();
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it('should return records with multiple sort array notation', function(done) {
|
|
340
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
341
|
+
.populate('payments', {sort: ['amount asc', 'type desc'] })
|
|
342
|
+
.exec(function(err, customers) {
|
|
343
|
+
assert.ifError(err);
|
|
344
|
+
assert(Array.isArray(customers));
|
|
345
|
+
assert.strictEqual(customers.length, 1);
|
|
346
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
347
|
+
assert(Array.isArray(customers[0].payments));
|
|
348
|
+
assert.strictEqual(customers[0].payments.length, 4);
|
|
349
|
+
assert.strictEqual(customers[0].payments[0].type, 'aquaman');
|
|
350
|
+
assert.strictEqual(customers[0].payments[1].type, 'daredevil');
|
|
351
|
+
assert.strictEqual(customers[0].payments[2].type, 'catwoman');
|
|
352
|
+
assert.strictEqual(customers[0].payments[3].type, 'batman');
|
|
353
|
+
done();
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it('should return records with in modifier', function(done) {
|
|
358
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
359
|
+
.populate('payments', {type: ['aquaman', 'daredevil'], sort: 'type desc'})
|
|
360
|
+
.exec(function(err, customers) {
|
|
361
|
+
assert.ifError(err);
|
|
362
|
+
assert(Array.isArray(customers));
|
|
363
|
+
assert.strictEqual(customers.length, 1);
|
|
364
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
365
|
+
assert(Array.isArray(customers[0].payments));
|
|
366
|
+
assert.strictEqual(customers[0].payments.length, 2);
|
|
367
|
+
assert.strictEqual(customers[0].payments[0].type, 'daredevil');
|
|
368
|
+
assert.strictEqual(customers[0].payments[1].type, 'aquaman');
|
|
369
|
+
done();
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
it('should return records with lessThan modifier', function(done) {
|
|
374
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
375
|
+
.populate('payments', {amount: {lessThan: 300}, sort: 'type desc' })
|
|
376
|
+
.exec(function(err, customers) {
|
|
377
|
+
assert.ifError(err);
|
|
378
|
+
assert(Array.isArray(customers));
|
|
379
|
+
assert.strictEqual(customers.length, 1);
|
|
380
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
381
|
+
assert(Array.isArray(customers[0].payments));
|
|
382
|
+
assert.strictEqual(customers[0].payments.length, 3);
|
|
383
|
+
assert.strictEqual(customers[0].payments[0].type, 'daredevil');
|
|
384
|
+
assert.strictEqual(customers[0].payments[1].type, 'catwoman');
|
|
385
|
+
assert.strictEqual(customers[0].payments[2].type, 'aquaman');
|
|
386
|
+
done();
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
it('should return records with like modifier', function(done) {
|
|
391
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
392
|
+
.populate('payments', {type: {like: '%at%man'}, sort: 'type desc' })
|
|
393
|
+
.exec(function(err, customers) {
|
|
394
|
+
assert.ifError(err);
|
|
395
|
+
assert(Array.isArray(customers));
|
|
396
|
+
assert.strictEqual(customers.length, 1);
|
|
397
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
398
|
+
assert(Array.isArray(customers[0].payments));
|
|
399
|
+
assert.strictEqual(customers[0].payments.length, 2);
|
|
400
|
+
assert.strictEqual(customers[0].payments[0].type, 'catwoman');
|
|
401
|
+
assert.strictEqual(customers[0].payments[1].type, 'batman');
|
|
402
|
+
done();
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it('should return records with limit modifier', function(done) {
|
|
407
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
408
|
+
.populate('payments', {amount: 180, limit: 1, sort: 'type desc'})
|
|
409
|
+
.exec(function(err, customers) {
|
|
410
|
+
assert.ifError(err);
|
|
411
|
+
assert(Array.isArray(customers));
|
|
412
|
+
assert.strictEqual(customers.length, 1);
|
|
413
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
414
|
+
assert(Array.isArray(customers[0].payments));
|
|
415
|
+
assert.strictEqual(customers[0].payments.length, 1);
|
|
416
|
+
assert.strictEqual(customers[0].payments[0].type, 'daredevil');
|
|
417
|
+
done();
|
|
418
|
+
});
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
it('should return records with not modifier', function(done) {
|
|
422
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
423
|
+
.populate('payments', {amount: {'!': 180}, sort: 'type desc'})
|
|
424
|
+
.exec(function(err, customers) {
|
|
425
|
+
assert.ifError(err);
|
|
426
|
+
assert(Array.isArray(customers));
|
|
427
|
+
assert.strictEqual(customers.length, 1);
|
|
428
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
429
|
+
assert(Array.isArray(customers[0].payments));
|
|
430
|
+
assert.strictEqual(customers[0].payments.length, 2);
|
|
431
|
+
assert.strictEqual(customers[0].payments[0].type, 'batman');
|
|
432
|
+
assert.strictEqual(customers[0].payments[1].type, 'aquaman');
|
|
433
|
+
done();
|
|
434
|
+
});
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
it('should return records with notIn modifier', function(done) {
|
|
438
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
439
|
+
.populate('payments', {type: { '!': ['aquaman', 'daredevil']}, sort: 'type desc'})
|
|
440
|
+
.exec(function(err, customers) {
|
|
441
|
+
assert.ifError(err);
|
|
442
|
+
assert(Array.isArray(customers));
|
|
443
|
+
assert.strictEqual(customers.length, 1);
|
|
444
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
445
|
+
assert(Array.isArray(customers[0].payments));
|
|
446
|
+
assert.strictEqual(customers[0].payments.length, 2);
|
|
447
|
+
assert.strictEqual(customers[0].payments[0].type, 'catwoman');
|
|
448
|
+
assert.strictEqual(customers[0].payments[1].type, 'batman');
|
|
449
|
+
done();
|
|
450
|
+
});
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
it('should return records with or modifier', function(done) {
|
|
454
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
455
|
+
.populate('payments', {where: { or: [{type:'batman'}, {amount: 150}]}, sort: 'type desc'})
|
|
456
|
+
.exec(function(err, customers) {
|
|
457
|
+
assert.ifError(err);
|
|
458
|
+
assert(Array.isArray(customers));
|
|
459
|
+
assert.strictEqual(customers.length, 1);
|
|
460
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
461
|
+
assert(Array.isArray(customers[0].payments));
|
|
462
|
+
assert.strictEqual(customers[0].payments.length, 2);
|
|
463
|
+
assert.strictEqual(customers[0].payments[0].type, 'batman');
|
|
464
|
+
assert.strictEqual(customers[0].payments[1].type, 'aquaman');
|
|
465
|
+
done();
|
|
466
|
+
});
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
it('should return records with select modifier', function(done) {
|
|
470
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
471
|
+
.populate('payments', {amount: 180, select: ['type'], sort: 'type desc'})
|
|
472
|
+
.exec(function(err, customers) {
|
|
473
|
+
assert.ifError(err);
|
|
474
|
+
assert(Array.isArray(customers));
|
|
475
|
+
assert.strictEqual(customers.length, 1);
|
|
476
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
477
|
+
assert(Array.isArray(customers[0].payments));
|
|
478
|
+
assert.strictEqual(customers[0].payments.length, 2);
|
|
479
|
+
assert.strictEqual(customers[0].payments[0].type, 'daredevil');
|
|
480
|
+
assert.strictEqual(customers[0].payments[0].amount, undefined);
|
|
481
|
+
assert.strictEqual(customers[0].payments[1].type, 'catwoman');
|
|
482
|
+
assert.strictEqual(customers[0].payments[1].amount, undefined);
|
|
483
|
+
|
|
484
|
+
done();
|
|
485
|
+
});
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
it('should return records with skip modifier', function(done) {
|
|
489
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
490
|
+
.populate('payments', {amount: 180, skip: 1, sort: 'type desc'})
|
|
491
|
+
.exec(function(err, customers) {
|
|
492
|
+
assert.ifError(err);
|
|
493
|
+
assert(Array.isArray(customers));
|
|
494
|
+
assert.strictEqual(customers.length, 1);
|
|
495
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
496
|
+
assert(Array.isArray(customers[0].payments));
|
|
497
|
+
assert.strictEqual(customers[0].payments.length, 1);
|
|
498
|
+
assert.strictEqual(customers[0].payments[0].type, 'catwoman');
|
|
499
|
+
done();
|
|
500
|
+
});
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
it('should return records with startsWith modifier', function(done) {
|
|
504
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
505
|
+
.populate('payments', {type: {startsWith: 'bat'}})
|
|
506
|
+
.exec(function(err, customers) {
|
|
507
|
+
assert.ifError(err);
|
|
508
|
+
assert(Array.isArray(customers));
|
|
509
|
+
assert.strictEqual(customers.length, 1);
|
|
510
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
511
|
+
assert(Array.isArray(customers[0].payments));
|
|
512
|
+
assert.strictEqual(customers[0].payments.length, 1);
|
|
513
|
+
assert.strictEqual(customers[0].payments[0].type, 'batman');
|
|
514
|
+
done();
|
|
515
|
+
});
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
it('should return the correct records with skip, limit and sort', function(done) {
|
|
519
|
+
Associations.Customer.find({ where: { name: {startsWith: 'modifier'}}, limit: 2, skip: 1, sort: {capital: 1, name: 0} })
|
|
520
|
+
.populate('payments', { limit: 2, skip: 1, sort: {amount: 1, type: 0} })
|
|
521
|
+
.exec(function(err, customers) {
|
|
522
|
+
assert.ifError(err);
|
|
523
|
+
assert(Array.isArray(customers));
|
|
524
|
+
assert.strictEqual(customers.length, 2);
|
|
525
|
+
assert.strictEqual(customers[0].name, 'modifier4');
|
|
526
|
+
assert.strictEqual(customers[1].name, 'modifier2');
|
|
527
|
+
assert(Array.isArray(customers[1].payments));
|
|
528
|
+
assert.strictEqual(customers[1].payments.length, 2);
|
|
529
|
+
assert.strictEqual(customers[1].payments[0].type, 'daredevil');
|
|
530
|
+
assert.strictEqual(customers[1].payments[1].type, 'catwoman');
|
|
531
|
+
done();
|
|
532
|
+
});
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
it('should return the correct sum with groupBy', function(done) {
|
|
536
|
+
Associations.Customer.find({ where: { name: 'modifier2' }})
|
|
537
|
+
.populate('payments', {sum: ['amount'], groupBy: ['bank', 'currency'], sort: ['bank asc', 'currency desc'] })
|
|
538
|
+
.exec(function(err, customers) {
|
|
539
|
+
assert.ifError(err);
|
|
540
|
+
assert(Array.isArray(customers));
|
|
541
|
+
assert.strictEqual(customers.length, 1);
|
|
542
|
+
assert.strictEqual(customers[0].name, 'modifier2');
|
|
543
|
+
assert(Array.isArray(customers[0].payments));
|
|
544
|
+
assert.strictEqual(customers[0].payments.length, 3);
|
|
545
|
+
assert.strictEqual(customers[0].payments[0].amount, 180);
|
|
546
|
+
assert.strictEqual(customers[0].payments[0].bank, 'Cash Tm');
|
|
547
|
+
assert.strictEqual(customers[0].payments[0].currency, 'euro');
|
|
548
|
+
assert.strictEqual(customers[0].payments[1].amount, 350);
|
|
549
|
+
assert.strictEqual(customers[0].payments[1].bank, 'Cash Tm');
|
|
550
|
+
assert.strictEqual(customers[0].payments[1].currency, 'dollar');
|
|
551
|
+
assert.strictEqual(customers[0].payments[2].amount, 330);
|
|
552
|
+
assert.strictEqual(customers[0].payments[2].bank, 'Money Inc');
|
|
553
|
+
assert.strictEqual(customers[0].payments[2].currency, 'euro');
|
|
554
|
+
done();
|
|
555
|
+
});
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
});
|
|
559
|
+
});
|
|
560
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
var assert = require('assert'),
|
|
2
|
+
_ = require('lodash');
|
|
3
|
+
|
|
4
|
+
describe('Association Interface', function() {
|
|
5
|
+
|
|
6
|
+
describe('Multiple Has Many Associations', function() {
|
|
7
|
+
describe('association .add()', function() {
|
|
8
|
+
|
|
9
|
+
describe('with an object', function() {
|
|
10
|
+
|
|
11
|
+
/////////////////////////////////////////////////////
|
|
12
|
+
// TEST SETUP
|
|
13
|
+
////////////////////////////////////////////////////
|
|
14
|
+
|
|
15
|
+
var customer;
|
|
16
|
+
|
|
17
|
+
before(function(done) {
|
|
18
|
+
// Check Customer hasMany Payments
|
|
19
|
+
assert.strictEqual(Associations.Customer_many.attributes.payments.collection, 'payment_many');
|
|
20
|
+
assert.strictEqual(Associations.Customer_many.attributes.transactions.collection, 'payment_many');
|
|
21
|
+
assert.strictEqual(Associations.Payment_many.attributes.customer.model, 'customer_many');
|
|
22
|
+
assert.strictEqual(Associations.Payment_many.attributes.patron.model, 'customer_many');
|
|
23
|
+
|
|
24
|
+
Associations.Customer_many.create({ name: 'manyAssociations uno hasMany add' }, function(err, model) {
|
|
25
|
+
assert.ifError(err);
|
|
26
|
+
|
|
27
|
+
customer = model;
|
|
28
|
+
Associations.Payment_many.create({ amount: 1, customer: customer.id }, done);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
/////////////////////////////////////////////////////
|
|
33
|
+
// TEST METHODS
|
|
34
|
+
////////////////////////////////////////////////////
|
|
35
|
+
|
|
36
|
+
it('should create a new payment and transaction association', function(done) {
|
|
37
|
+
customer.payments.add({ amount: 1337 });
|
|
38
|
+
customer.transactions.add({ amount: 100 });
|
|
39
|
+
customer.save(function(err) {
|
|
40
|
+
assert.ifError(err);
|
|
41
|
+
|
|
42
|
+
// Look up the customer again to be sure the payment was added
|
|
43
|
+
Associations.Customer_many.findOne(customer.id)
|
|
44
|
+
.populate('payments', {sort: 'amount asc'})
|
|
45
|
+
.populate('transactions')
|
|
46
|
+
.exec(function(err, customer) {
|
|
47
|
+
assert.ifError(err);
|
|
48
|
+
|
|
49
|
+
assert.strictEqual(customer.payments.length, 2);
|
|
50
|
+
assert.strictEqual(customer.payments[1].amount, 1337);
|
|
51
|
+
|
|
52
|
+
assert.strictEqual(customer.transactions.length, 1, 'Expected customer to have 1 transaction, but actually it has '+customer.transactions.length+', see? \n'+require('util').inspect(customer,false,null));
|
|
53
|
+
assert.strictEqual(customer.transactions[0].amount, 100);
|
|
54
|
+
done();
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe('with an id', function() {
|
|
61
|
+
|
|
62
|
+
/////////////////////////////////////////////////////
|
|
63
|
+
// TEST SETUP
|
|
64
|
+
////////////////////////////////////////////////////
|
|
65
|
+
|
|
66
|
+
var customer, payment;
|
|
67
|
+
|
|
68
|
+
before(function(done) {
|
|
69
|
+
|
|
70
|
+
var records = [
|
|
71
|
+
{ name: 'manyAssociations hasMany add uno' },
|
|
72
|
+
{ name: 'manyAssociations uno hasMany dos' }
|
|
73
|
+
];
|
|
74
|
+
|
|
75
|
+
Associations.Customer_many.createEach(records, function(err, models) {
|
|
76
|
+
assert.ifError(err);
|
|
77
|
+
|
|
78
|
+
customer = models[0];
|
|
79
|
+
Associations.Payment_many.create({ amount: 1, customer: models[1].id }, function(err, paymentModel) {
|
|
80
|
+
assert.ifError(err);
|
|
81
|
+
|
|
82
|
+
payment = paymentModel;
|
|
83
|
+
done();
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
/////////////////////////////////////////////////////
|
|
89
|
+
// TEST METHODS
|
|
90
|
+
////////////////////////////////////////////////////
|
|
91
|
+
|
|
92
|
+
it('should link the payment to another association', function(done) {
|
|
93
|
+
customer.payments.add(payment.id);
|
|
94
|
+
customer.save(function(err) {
|
|
95
|
+
assert.ifError(err);
|
|
96
|
+
|
|
97
|
+
// Look up the customer again to be sure the payment was added
|
|
98
|
+
Associations.Customer_many.findOne(customer.id)
|
|
99
|
+
.populate('payments')
|
|
100
|
+
.exec(function(err, data) {
|
|
101
|
+
assert.ifError(err);
|
|
102
|
+
|
|
103
|
+
assert.strictEqual(data.payments.length, 1);
|
|
104
|
+
assert.strictEqual(data.payments[0].amount, 1);
|
|
105
|
+
done();
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
});
|