Skip to content

Commit 0f24449

Browse files
committedJul 29, 2018
test(query): repro #6754
1 parent bc21555 commit 0f24449

File tree

1 file changed

+76
-24
lines changed

1 file changed

+76
-24
lines changed
 

‎test/docs/transactions.test.js

+76-24
Original file line numberDiff line numberDiff line change
@@ -136,33 +136,85 @@ describe('transactions', function() {
136136
});
137137
});
138138

139-
it('populate (gh-6754)', function() {
140-
const Author = db.model('Author', new Schema({ name: String }), 'Author');
141-
const Article = db.model('Article', new Schema({
142-
author: {
143-
type: mongoose.Schema.Types.ObjectId,
144-
ref: 'Author'
145-
}
146-
}), 'Article');
139+
describe('populate (gh-6754)', function() {
140+
let Author;
141+
let Article;
142+
let session;
147143

148-
let session = null;
149-
return db.createCollection('Author').
150-
then(() => db.createCollection('Article')).
151-
then(() => db.startSession()).
152-
then(_session => {
144+
before(function() {
145+
Author = db.model('Author', new Schema({ name: String }), 'Author');
146+
Article = db.model('Article', new Schema({
147+
author: {
148+
type: mongoose.Schema.Types.ObjectId,
149+
ref: 'Author'
150+
}
151+
}), 'Article');
152+
153+
return db.createCollection('Author').
154+
then(() => db.createCollection('Article'));
155+
});
156+
157+
beforeEach(function() {
158+
return Author.deleteMany({}).then(() => Article.deleteMany({}));
159+
});
160+
161+
beforeEach(function() {
162+
return db.startSession().then(_session => {
153163
session = _session;
154164
session.startTransaction();
155-
return Author.create([{ name: 'Val' }], { session: session });
156-
}).
157-
then(authors => Article.create([{ author: authors[0]._id }], { session: session })).
158-
then(articles => Article.findById(articles[0]._id).session(session)).
159-
then(article => {
160-
assert.ok(article.$session());
161-
return article.populate('author').execPopulate();
162-
}).
163-
then(article => {
164-
assert.equal(article.author.name, 'Val');
165-
session.commitTransaction();
166165
});
166+
});
167+
168+
afterEach(function() {
169+
return session.commitTransaction();
170+
});
171+
172+
it('`populate()` uses the querys session', function() {
173+
return Author.create([{ name: 'Val' }], { session: session }).
174+
then(authors => Article.create([{ author: authors[0]._id }], { session: session })).
175+
then(articles => {
176+
return Article.
177+
findById(articles[0]._id).
178+
session(session).
179+
populate('author');
180+
}).
181+
then(article => assert.equal(article.author.name, 'Val'));
182+
});
183+
184+
it('can override `populate()` session', function() {
185+
return Author.create([{ name: 'Val' }], { session: session }).
186+
// Article created _outside_ the transaction
187+
then(authors => Article.create([{ author: authors[0]._id }])).
188+
then(articles => {
189+
return Article.
190+
findById(articles[0]._id).
191+
populate({ path: 'author', options: { session: session } });
192+
}).
193+
then(article => assert.equal(article.author.name, 'Val'));
194+
});
195+
196+
it('`execPopulate()` uses the documents `$session()` by default', function() {
197+
return Author.create([{ name: 'Val' }], { session: session }).
198+
then(authors => Article.create([{ author: authors[0]._id }], { session: session })).
199+
// By default, the populate query should use the associated `$session()`
200+
then(articles => Article.findById(articles[0]._id).session(session)).
201+
then(article => {
202+
assert.ok(article.$session());
203+
return article.populate('author').execPopulate();
204+
}).
205+
then(article => assert.equal(article.author.name, 'Val'));
206+
});
207+
208+
it('`execPopulate()` supports overwriting the session', function() {
209+
return Author.create([{ name: 'Val' }], { session: session }).
210+
then(authors => Article.create([{ author: authors[0]._id }], { session: session })).
211+
then(() => Article.findOne().session(session)).
212+
then(article => {
213+
return article.
214+
populate({ path: 'author', options: { session: null } }).
215+
execPopulate();
216+
}).
217+
then(article => assert.ok(!article.author));
218+
});
167219
});
168220
});

0 commit comments

Comments
 (0)
Please sign in to comment.