@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,219 @@
|
|
|
1
|
+
var util = require('util');
|
|
2
|
+
var assert = require('assert');
|
|
3
|
+
var _ = require('lodash');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
describe('Association Interface', function() {
|
|
9
|
+
|
|
10
|
+
describe('Has Many Association', function() {
|
|
11
|
+
|
|
12
|
+
/////////////////////////////////////////////////////
|
|
13
|
+
// TEST SETUP
|
|
14
|
+
////////////////////////////////////////////////////
|
|
15
|
+
|
|
16
|
+
var customerRecords, paymentRecords
|
|
17
|
+
|
|
18
|
+
before(function(done) {
|
|
19
|
+
|
|
20
|
+
// Check Customer hasMany Payments
|
|
21
|
+
assert.strictEqual(Associations.Customer.attributes.payments.collection, 'payment');
|
|
22
|
+
assert.strictEqual(Associations.Payment.attributes.a_customer.model, 'customer');
|
|
23
|
+
|
|
24
|
+
var customers = [
|
|
25
|
+
{ name: 'hasMany find pop' },
|
|
26
|
+
{ name: 'hasMany find pop' },
|
|
27
|
+
{ name: 'hasMany find pop no child' }
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
Associations.Customer.createEach(customers, function(err, customers) {
|
|
31
|
+
assert.ifError(err);
|
|
32
|
+
|
|
33
|
+
customerRecords = customers;
|
|
34
|
+
|
|
35
|
+
Associations.Customer.find({ name: 'hasMany find pop'})
|
|
36
|
+
.sort('id asc')
|
|
37
|
+
.exec(function(err, customers) {
|
|
38
|
+
assert.ifError(err);
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
// Create 8 payments, 4 from one customer, 4 from another
|
|
42
|
+
var payments = [];
|
|
43
|
+
for(var i=0; i<8; i++) {
|
|
44
|
+
if(i < 4) payments.push({ amount: i, a_customer: customers[0].id });
|
|
45
|
+
if(i >= 4) payments.push({ amount: i, a_customer: customers[1].id });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
Associations.Payment.createEach(payments, function(err, payments) {
|
|
49
|
+
assert.ifError(err);
|
|
50
|
+
|
|
51
|
+
paymentRecords = payments;
|
|
52
|
+
done();
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe('.find', function() {
|
|
59
|
+
|
|
60
|
+
/////////////////////////////////////////////////////
|
|
61
|
+
// TEST METHODS
|
|
62
|
+
////////////////////////////////////////////////////
|
|
63
|
+
|
|
64
|
+
it('should return payments when the populate criteria is added', function(done) {
|
|
65
|
+
Associations.Customer.find({ name: 'hasMany find pop' })
|
|
66
|
+
.populate('payments')
|
|
67
|
+
.exec(function(err, customers) {
|
|
68
|
+
assert(!err, err);
|
|
69
|
+
|
|
70
|
+
assert(Array.isArray(customers));
|
|
71
|
+
assert.strictEqual(customers.length, 2, 'expected 2 customers, got these customers:'+require('util').inspect(customers, false, null));
|
|
72
|
+
|
|
73
|
+
assert(Array.isArray(customers[0].payments));
|
|
74
|
+
assert(Array.isArray(customers[1].payments));
|
|
75
|
+
|
|
76
|
+
assert.strictEqual(customers[0].payments.length, 4);
|
|
77
|
+
assert.strictEqual(customers[1].payments.length, 4);
|
|
78
|
+
|
|
79
|
+
done();
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should return all the populated records when a limit clause is used', function(done) {
|
|
84
|
+
|
|
85
|
+
Associations.Customer.find({ name: 'hasMany find pop' })
|
|
86
|
+
.populate('payments', {sort: 'amount asc'})
|
|
87
|
+
.limit(1)
|
|
88
|
+
.sort('id asc')
|
|
89
|
+
.exec(function(err, customers) {
|
|
90
|
+
assert(!err, err);
|
|
91
|
+
|
|
92
|
+
assert(Array.isArray(customers));
|
|
93
|
+
assert.strictEqual(customers.length, 1);
|
|
94
|
+
|
|
95
|
+
assert(Array.isArray(customers[0].payments));
|
|
96
|
+
assert.strictEqual(customers[0].payments.length, 4, 'Expected customers[0] to have 4 payments, but got '+customers[0].payments.length+'. Customers: '+require('util').inspect(customers, false, null));
|
|
97
|
+
assert.strictEqual(customers[0].payments[0].amount, 0);
|
|
98
|
+
|
|
99
|
+
done();
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// TODO: pull this stuff into a separate test:
|
|
104
|
+
////////////////////////////////////////////////////////////////////////////////////
|
|
105
|
+
|
|
106
|
+
// e.g.
|
|
107
|
+
// Associations.Customer.find({ name: 'hasMany find' })
|
|
108
|
+
// .populate('payments', {
|
|
109
|
+
// limit: 2,
|
|
110
|
+
// sort: {amount: -1}
|
|
111
|
+
// })
|
|
112
|
+
// .limit(2)
|
|
113
|
+
// .sort('id DESC')
|
|
114
|
+
|
|
115
|
+
// it('should return expected child records for ALL parent records when populate..limit is used');
|
|
116
|
+
// it('should return expected child records for ALL parent records when populate..skip is used');
|
|
117
|
+
// it('should return expected child records for ALL parent records when populate..limit..skip is used');
|
|
118
|
+
|
|
119
|
+
// it('should return expected child records for ALL parent records when populate..sort is used');
|
|
120
|
+
// it('should return expected child records for ALL parent records when populate..sort..limit is used');
|
|
121
|
+
// it('should return expected child records for ALL parent records when populate..sort..skip is used');
|
|
122
|
+
// it('should return expected child records for ALL parent records when populate..sort..limit..skip is used');
|
|
123
|
+
|
|
124
|
+
// it('should return expected child records for ALL parent records when populate..where is used');
|
|
125
|
+
// it('should return expected child records for ALL parent records when populate..where..limit is used');
|
|
126
|
+
// it('should return expected child records for ALL parent records when populate..where..skip is used');
|
|
127
|
+
// it('should return expected child records for ALL parent records when populate..where..limit..skip is used');
|
|
128
|
+
|
|
129
|
+
// it('should return expected child records for ALL parent records when populate..where..limit..sort is used');
|
|
130
|
+
// it('should return expected child records for ALL parent records when populate..where..skip..sort is used');
|
|
131
|
+
// it('should return expected child records for ALL parent records when populate..where..limit..skip..sort is used');
|
|
132
|
+
////////////////////////////////////////////////////////////////////////////////////
|
|
133
|
+
|
|
134
|
+
it('should return all the populated records when a skip clause is used', function(done) {
|
|
135
|
+
Associations.Customer.find({ name: 'hasMany find pop' })
|
|
136
|
+
.populate('payments', { sort: { amount: 1 } })
|
|
137
|
+
.skip(1)
|
|
138
|
+
.sort('id asc')
|
|
139
|
+
.exec(function(err, customers) {
|
|
140
|
+
assert(!err, err);
|
|
141
|
+
|
|
142
|
+
assert(Array.isArray(customers));
|
|
143
|
+
assert.strictEqual(customers.length, 1);
|
|
144
|
+
|
|
145
|
+
assert(Array.isArray(customers[0].payments));
|
|
146
|
+
assert.strictEqual(customers[0].payments.length, 4);
|
|
147
|
+
assert(customers[0].payments[0].amount === 4,
|
|
148
|
+
'Expected customers[0].payments[0].amount === 4, but customers[0] ==>\n'+
|
|
149
|
+
util.inspect(customers[0]));
|
|
150
|
+
|
|
151
|
+
done();
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('should add a flag to not serialize association object when the populate is not added', function(done) {
|
|
156
|
+
Associations.Customer.find({ name: 'hasMany find pop' })
|
|
157
|
+
.exec(function(err, customers) {
|
|
158
|
+
assert.ifError(err);
|
|
159
|
+
|
|
160
|
+
var obj = customers[0].toJSON();
|
|
161
|
+
assert(!obj.payments);
|
|
162
|
+
|
|
163
|
+
done();
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('should call toJSON on all associated records if available', function(done) {
|
|
168
|
+
Associations.Customer.find({ name: 'hasMany find pop' })
|
|
169
|
+
.populate('payments')
|
|
170
|
+
.exec(function(err, customers) {
|
|
171
|
+
assert.ifError(err);
|
|
172
|
+
|
|
173
|
+
var obj = customers[0].toJSON();
|
|
174
|
+
|
|
175
|
+
assert(Array.isArray(obj.payments));
|
|
176
|
+
assert.strictEqual(obj.payments.length, 4);
|
|
177
|
+
|
|
178
|
+
assert(!obj.payments[0].hasOwnProperty('type'));
|
|
179
|
+
assert(!obj.payments[1].hasOwnProperty('type'));
|
|
180
|
+
assert(!obj.payments[2].hasOwnProperty('type'));
|
|
181
|
+
assert(!obj.payments[3].hasOwnProperty('type'));
|
|
182
|
+
|
|
183
|
+
done();
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('should find association key in parent model', function(done) {
|
|
188
|
+
Associations.Payment.find({a_customer: customerRecords[0].id})
|
|
189
|
+
.populate('a_customer')
|
|
190
|
+
.exec(function(err, payments) {
|
|
191
|
+
assert.ifError(err);
|
|
192
|
+
assert.strictEqual(payments.length, 4);
|
|
193
|
+
assert.strictEqual(payments[0].a_customer.id, customerRecords[0].id);
|
|
194
|
+
assert.strictEqual(payments[1].a_customer.id, customerRecords[0].id);
|
|
195
|
+
assert.strictEqual(payments[2].a_customer.id, customerRecords[0].id);
|
|
196
|
+
assert.strictEqual(payments[3].a_customer.id, customerRecords[0].id);
|
|
197
|
+
|
|
198
|
+
done();
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('populate should be an empty array if id refer to an undefined child', function(done) {
|
|
203
|
+
Associations.Customer.find({ name: 'hasMany find pop no child' })
|
|
204
|
+
.populate('payments')
|
|
205
|
+
.exec(function(err, customer) {
|
|
206
|
+
assert.ifError(err);
|
|
207
|
+
assert.strictEqual(customer.length, 1);
|
|
208
|
+
assert.strictEqual(customer[0].name, 'hasMany find pop no child');
|
|
209
|
+
assert(!_.isUndefined(customer[0].payments), 'Child should not be undefined');
|
|
210
|
+
assert(_.isArray(customer[0].payments), 'Child should be an array');
|
|
211
|
+
assert.strictEqual(customer[0].payments.length, 0);
|
|
212
|
+
|
|
213
|
+
done();
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
});
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
var assert = require('assert');
|
|
2
|
+
var _ = require('lodash');
|
|
3
|
+
var util = require('util');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
describe('Association Interface', function() {
|
|
10
|
+
|
|
11
|
+
describe('Has Many Association', function() {
|
|
12
|
+
|
|
13
|
+
var Customer;
|
|
14
|
+
|
|
15
|
+
/////////////////////////////////////////////////////
|
|
16
|
+
// TEST SETUP
|
|
17
|
+
////////////////////////////////////////////////////
|
|
18
|
+
|
|
19
|
+
before(function(done) {
|
|
20
|
+
|
|
21
|
+
// Check Customer hasMany Payments
|
|
22
|
+
assert.strictEqual(Associations.Customer.attributes.payments.collection, 'payment');
|
|
23
|
+
assert.strictEqual(Associations.Payment.attributes.a_customer.model, 'customer');
|
|
24
|
+
|
|
25
|
+
Associations.Customer.createEach([{
|
|
26
|
+
name: 'hasMany find where', capital : 1000
|
|
27
|
+
}, {
|
|
28
|
+
name: 'hasMany find where', capital : 2000
|
|
29
|
+
}],
|
|
30
|
+
function(err, customers) {
|
|
31
|
+
assert.ifError(err);
|
|
32
|
+
|
|
33
|
+
Associations.Customer.find({ name: 'hasMany find where'})
|
|
34
|
+
.sort('capital asc')
|
|
35
|
+
.exec(function(err, customers) {
|
|
36
|
+
assert.ifError(err);
|
|
37
|
+
|
|
38
|
+
Customer = customers[0];
|
|
39
|
+
|
|
40
|
+
var payments = [];
|
|
41
|
+
|
|
42
|
+
for(var i=0; i<8; i++) {
|
|
43
|
+
if(i < 4) payments.push({ amount: i, a_customer: customers[0].id });
|
|
44
|
+
if(i >= 4) payments.push({ amount: i, a_customer: customers[1].id });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
Associations.Payment.createEach(payments, function(err, payments) {
|
|
48
|
+
assert.ifError(err);
|
|
49
|
+
done();
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('.find', function() {
|
|
56
|
+
|
|
57
|
+
/////////////////////////////////////////////////////
|
|
58
|
+
// TEST METHODS
|
|
59
|
+
////////////////////////////////////////////////////
|
|
60
|
+
|
|
61
|
+
it('should return only payments less than or equal to 2', function(done) {
|
|
62
|
+
Associations.Customer.find({ name: 'hasMany find where' })
|
|
63
|
+
.populate('payments', { amount: { '<': 2 }, limit: 2, sort: { amount: 1 }})
|
|
64
|
+
.sort('capital asc')
|
|
65
|
+
.exec(function(err, customers) {
|
|
66
|
+
assert(!err,err);
|
|
67
|
+
|
|
68
|
+
assert(Array.isArray(customers));
|
|
69
|
+
assert.strictEqual(customers.length, 2);
|
|
70
|
+
|
|
71
|
+
assert(Array.isArray(customers[0].payments));
|
|
72
|
+
assert(Array.isArray(customers[1].payments));
|
|
73
|
+
|
|
74
|
+
assert(customers[0].payments.length === 2,
|
|
75
|
+
'expecting customers[0] to have 2 payments, but actually she looks like: \n'+util.inspect(customers[0],false, null));
|
|
76
|
+
|
|
77
|
+
assert.strictEqual(customers[0].payments[0].amount, 0);
|
|
78
|
+
assert.strictEqual(customers[0].payments[1].amount, 1);
|
|
79
|
+
|
|
80
|
+
assert.strictEqual(customers[1].payments.length, 0);
|
|
81
|
+
|
|
82
|
+
done();
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('should return payments using skip and limit', function(done) {
|
|
87
|
+
Associations.Customer.find({ name: 'hasMany find where' })
|
|
88
|
+
.populate('payments', { skip: 1, limit: 2, sort: { amount: 1 } })
|
|
89
|
+
.sort('capital asc')
|
|
90
|
+
.exec(function(err, customers) {
|
|
91
|
+
assert.ifError(err);
|
|
92
|
+
|
|
93
|
+
assert(Array.isArray(customers));
|
|
94
|
+
assert.strictEqual(customers.length, 2);
|
|
95
|
+
|
|
96
|
+
assert(Array.isArray(customers[0].payments));
|
|
97
|
+
assert(Array.isArray(customers[1].payments));
|
|
98
|
+
|
|
99
|
+
assert.strictEqual(customers[0].payments.length, 2);
|
|
100
|
+
assert.strictEqual(customers[0].payments[0].amount, 1);
|
|
101
|
+
assert.strictEqual(customers[0].payments[1].amount, 2);
|
|
102
|
+
|
|
103
|
+
assert(
|
|
104
|
+
customers[1].payments.length === 2,
|
|
105
|
+
'expected customers[1] to have 2 payments but instead it looks like:\n'+
|
|
106
|
+
util.inspect(customers[1].payments, false, null)
|
|
107
|
+
);
|
|
108
|
+
assert(
|
|
109
|
+
customers[1].payments[0].amount === 5,
|
|
110
|
+
'expected customers[1].payments[0].amount === 5, but customers[1] ==>\n'+
|
|
111
|
+
util.inspect(customers[1], false, null)
|
|
112
|
+
);
|
|
113
|
+
assert.strictEqual(customers[1].payments[1].amount, 6);
|
|
114
|
+
|
|
115
|
+
done();
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('should allow filtering by primary key', function(done) {
|
|
120
|
+
|
|
121
|
+
// Find the payments
|
|
122
|
+
Associations.Payment.findOne({ amount: 1, a_customer: Customer.id }).exec(function(err, payment) {
|
|
123
|
+
assert.ifError(err);
|
|
124
|
+
|
|
125
|
+
Associations.Customer.find({ name: 'hasMany find where' })
|
|
126
|
+
.populate('payments', {where : { id: payment.id }, sort : {amount : 1}})
|
|
127
|
+
.sort('capital asc')
|
|
128
|
+
.exec(function(err, customers) {
|
|
129
|
+
assert.ifError(err);
|
|
130
|
+
|
|
131
|
+
assert(Array.isArray(customers));
|
|
132
|
+
assert.strictEqual(customers.length, 2);
|
|
133
|
+
|
|
134
|
+
assert(Array.isArray(customers[0].payments));
|
|
135
|
+
assert(Array.isArray(customers[1].payments));
|
|
136
|
+
|
|
137
|
+
assert.strictEqual(customers[0].payments.length, 1);
|
|
138
|
+
assert.strictEqual(customers[0].payments[0].amount, 1);
|
|
139
|
+
assert.strictEqual(customers[1].payments.length, 0);
|
|
140
|
+
done();
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
});
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
var assert = require('assert'),
|
|
2
|
+
_ = require('lodash');
|
|
3
|
+
|
|
4
|
+
describe('Association Interface', function() {
|
|
5
|
+
|
|
6
|
+
describe('Has Many Association', function() {
|
|
7
|
+
|
|
8
|
+
/////////////////////////////////////////////////////
|
|
9
|
+
// TEST SETUP
|
|
10
|
+
////////////////////////////////////////////////////
|
|
11
|
+
|
|
12
|
+
var customerRecord;
|
|
13
|
+
|
|
14
|
+
before(function(done) {
|
|
15
|
+
// Check Customer hasMany Payments
|
|
16
|
+
assert.strictEqual(Associations.Customer.attributes.payments.collection, 'payment');
|
|
17
|
+
assert.strictEqual(Associations.Payment.attributes.a_customer.model, 'customer');
|
|
18
|
+
|
|
19
|
+
Associations.Customer.create({ name: 'hasMany findOne' }, function(err, customer) {
|
|
20
|
+
assert.ifError(err);
|
|
21
|
+
|
|
22
|
+
customerRecord = customer;
|
|
23
|
+
|
|
24
|
+
var payments = [];
|
|
25
|
+
|
|
26
|
+
for(var i=0; i<4; i++) {
|
|
27
|
+
payments.push({ amount: i, a_customer: customer.id });
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
Associations.Payment.createEach(payments, function(err) {
|
|
31
|
+
assert.ifError(err);
|
|
32
|
+
done();
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('.findOne', function() {
|
|
38
|
+
|
|
39
|
+
/////////////////////////////////////////////////////
|
|
40
|
+
// TEST METHODS
|
|
41
|
+
////////////////////////////////////////////////////
|
|
42
|
+
|
|
43
|
+
it('should return payments when the populate criteria is added', function(done) {
|
|
44
|
+
Associations. Customer.findOne({ id: customerRecord.id })
|
|
45
|
+
.populate('payments')
|
|
46
|
+
.exec(function(err, customer) {
|
|
47
|
+
assert.ifError(err);
|
|
48
|
+
|
|
49
|
+
assert(Array.isArray(customer.payments));
|
|
50
|
+
assert.strictEqual(customer.payments.length, 4);
|
|
51
|
+
done();
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should add a flag to not serialize association object when the populate is not added', function(done) {
|
|
56
|
+
Associations.Customer.findOne({ id: customerRecord.id })
|
|
57
|
+
.exec(function(err, customer) {
|
|
58
|
+
assert.ifError(err);
|
|
59
|
+
|
|
60
|
+
var obj = customer.toJSON();
|
|
61
|
+
assert(!obj.payments);
|
|
62
|
+
|
|
63
|
+
done();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should call toJSON on all associated records if available', function(done) {
|
|
68
|
+
Associations.Customer.findOne({ id: customerRecord.id })
|
|
69
|
+
.populate('payments')
|
|
70
|
+
.exec(function(err, customer) {
|
|
71
|
+
assert.ifError(err);
|
|
72
|
+
|
|
73
|
+
var obj = customer.toJSON();
|
|
74
|
+
|
|
75
|
+
assert(Array.isArray(obj.payments));
|
|
76
|
+
assert.strictEqual(obj.payments.length, 4);
|
|
77
|
+
assert(!obj.payments[0].hasOwnProperty('type'));
|
|
78
|
+
assert(!obj.payments[1].hasOwnProperty('type'));
|
|
79
|
+
assert(!obj.payments[2].hasOwnProperty('type'));
|
|
80
|
+
assert(!obj.payments[3].hasOwnProperty('type'));
|
|
81
|
+
|
|
82
|
+
done();
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|