@dhyasama/totem-models 12.28.0 → 12.29.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/lib/Organization.js +2 -1
- package/package.json +1 -1
- package/test/Financials.js +33 -0
package/lib/Organization.js
CHANGED
package/package.json
CHANGED
package/test/Financials.js
CHANGED
|
@@ -130,6 +130,39 @@ describe('Financials', function() {
|
|
|
130
130
|
|
|
131
131
|
});
|
|
132
132
|
|
|
133
|
+
it('getOrCreateByOrg returns one document under concurrent calls', async function() {
|
|
134
|
+
|
|
135
|
+
var customer = new mongoose.Types.ObjectId();
|
|
136
|
+
var org = new mongoose.Types.ObjectId();
|
|
137
|
+
|
|
138
|
+
// mirror production's unique index so the losing writer of a race can never insert a duplicate
|
|
139
|
+
await Financials.collection.createIndex({ customer: 1, organization: 1 }, { unique: true });
|
|
140
|
+
|
|
141
|
+
var results = await Promise.all(
|
|
142
|
+
Array.from({ length: 10 }, function() { return Financials.getOrCreateByOrg(customer, org); })
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
var ids = new Set(results.map(function(r) { return r._id.toString(); }));
|
|
146
|
+
ids.size.should.equal(1);
|
|
147
|
+
|
|
148
|
+
var count = await Financials.countDocuments({ customer: customer, organization: org });
|
|
149
|
+
count.should.equal(1);
|
|
150
|
+
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('getOrCreateByOrg returns the existing document', function(done) {
|
|
154
|
+
|
|
155
|
+
Financials.getOrCreateByOrg(customerA, orgA, function(err, result) {
|
|
156
|
+
should.not.exist(err);
|
|
157
|
+
should.exist(result);
|
|
158
|
+
result.customer.toString().should.equal(customerA.toString());
|
|
159
|
+
result.organization.toString().should.equal(orgA.toString());
|
|
160
|
+
should.exist(result.asOfDate); // proves it found the doc created above, not a fresh one
|
|
161
|
+
done();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
});
|
|
165
|
+
|
|
133
166
|
describe.skip('old so skip for now', function() {
|
|
134
167
|
|
|
135
168
|
it('gets financials for an org', function(done) {
|