Skip to content

Commit a3d7338

Browse files
committed
small clean up and more tests
1 parent f30ca60 commit a3d7338

File tree

3 files changed

+165
-75
lines changed

3 files changed

+165
-75
lines changed

src/bson.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,12 @@ export function pluckBSONSerializeOptions(options: BSONSerializeOptions): BSONSe
7070
};
7171
}
7272

73-
// TODO: naming
74-
// TODO: testing
73+
// Merge the given BSONSerializeOptions, preferring options over parentOptions, and substituting a
74+
// default for values not set.
7575
export function inheritOrDefaultBSONSerializableOptions(
7676
options?: BSONSerializeOptions,
7777
parentOptions?: BSONSerializeOptions
7878
): BSONSerializeOptions {
79-
// Merge the BSONSerializeOptions, preferring options over parentOptions, and substituting a
80-
// default for values not set.
8179
return {
8280
raw: options?.raw ?? parentOptions?.raw ?? false,
8381
promoteLongs: options?.promoteLongs ?? parentOptions?.promoteLongs ?? true,

src/operations/command.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ export abstract class CommandOperation<
100100
this.logger = parent.logger;
101101
}
102102

103-
// Assign all bsonOptions to OperationBase obj, preferring command options over parent options
104-
// base accounts for the fact that Collection stores bson options in s, while Db stores bson options in s.options
103+
// Assign all bsonOptions to OperationBase obj, preferring command options over parent options.
104+
// base accounts for the fact that Collection stores bson options in s, while other parents,
105+
// like Db, stores bson options in s.options
105106
const base = Object.assign({}, parent?.s.options, parent?.s);
106107
Object.assign(this, inheritOrDefaultBSONSerializableOptions(options, base));
107108
}

test/functional/ignore_undefined.test.js

Lines changed: 160 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -38,75 +38,6 @@ describe('Ignore Undefined', function () {
3838
}
3939
});
4040

41-
it('Should correctly inherit ignore undefined field from collection during insert', {
42-
metadata: { requires: { topology: ['single'] } },
43-
44-
test: function (done) {
45-
var configuration = this.configuration;
46-
var client = configuration.newClient(configuration.writeConcernMax(), {
47-
poolSize: 1,
48-
ignoreUndefined: false
49-
});
50-
51-
client.connect(function (err, client) {
52-
var db = client.db(configuration.db);
53-
var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue', {
54-
ignoreUndefined: true
55-
});
56-
57-
// Ignore the undefined field
58-
collection.insert({ a: 1, b: undefined }, configuration.writeConcernMax(), function (err) {
59-
expect(err).to.not.exist;
60-
61-
// Locate the doument
62-
collection.findOne(function (err, item) {
63-
test.equal(1, item.a);
64-
test.ok(item.b === undefined);
65-
client.close(done);
66-
});
67-
});
68-
});
69-
}
70-
});
71-
72-
it('Should correctly inherit ignore undefined field from operation during findOneAndReplace', {
73-
metadata: { requires: { topology: ['single'] } },
74-
75-
test: function (done) {
76-
var configuration = this.configuration;
77-
var client = configuration.newClient(configuration.writeConcernMax(), {
78-
poolSize: 1,
79-
ignoreUndefined: false
80-
});
81-
82-
client.connect(function (err, client) {
83-
var db = client.db(configuration.db);
84-
var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue');
85-
86-
collection.insert({ a: 1, b: 2 }, configuration.writeConcernMax(), function (err) {
87-
expect(err).to.not.exist;
88-
89-
// Replace the doument, ignoring undefined fields
90-
collection.findOneAndReplace(
91-
{},
92-
{ a: 1, b: undefined },
93-
{ ignoreUndefined: true },
94-
function (err) {
95-
expect(err).to.not.exist;
96-
97-
// Locate the doument
98-
collection.findOne(function (err, item) {
99-
test.equal(1, item.a);
100-
test.ok(item.b === undefined);
101-
client.close(done);
102-
});
103-
}
104-
);
105-
});
106-
});
107-
}
108-
});
109-
11041
it(
11142
'Should correctly connect using MongoClient and perform insert document ignoring undefined field',
11243
{
@@ -217,4 +148,164 @@ describe('Ignore Undefined', function () {
217148
});
218149
}
219150
});
151+
152+
it('Should correctly inherit ignore undefined field from db during insert', {
153+
metadata: { requires: { topology: ['single'] } },
154+
155+
test: function (done) {
156+
var configuration = this.configuration;
157+
var client = configuration.newClient(configuration.writeConcernMax(), {
158+
poolSize: 1,
159+
ignoreUndefined: false
160+
});
161+
162+
client.connect(function (err, client) {
163+
var db = client.db(configuration.db, { ignoreUndefined: true });
164+
var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue3');
165+
166+
// Ignore the undefined field
167+
collection.insert({ a: 1, b: undefined }, configuration.writeConcernMax(), function (err) {
168+
expect(err).to.not.exist;
169+
170+
// Locate the doument
171+
collection.findOne(function (err, item) {
172+
test.equal(1, item.a);
173+
test.ok(item.b === undefined);
174+
client.close(done);
175+
});
176+
});
177+
});
178+
}
179+
});
180+
181+
it('Should correctly inherit ignore undefined field from collection during insert', {
182+
metadata: { requires: { topology: ['single'] } },
183+
184+
test: function (done) {
185+
var configuration = this.configuration;
186+
var client = configuration.newClient(configuration.writeConcernMax(), {
187+
poolSize: 1
188+
});
189+
190+
client.connect(function (err, client) {
191+
var db = client.db(configuration.db, { ignoreUndefined: false });
192+
var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue4', {
193+
ignoreUndefined: true
194+
});
195+
196+
// Ignore the undefined field
197+
collection.insert({ a: 1, b: undefined }, configuration.writeConcernMax(), function (err) {
198+
expect(err).to.not.exist;
199+
200+
// Locate the doument
201+
collection.findOne(function (err, item) {
202+
test.equal(1, item.a);
203+
test.ok(item.b === undefined);
204+
client.close(done);
205+
});
206+
});
207+
});
208+
}
209+
});
210+
211+
it('Should correctly inherit ignore undefined field from operation during insert', {
212+
metadata: { requires: { topology: ['single'] } },
213+
214+
test: function (done) {
215+
var configuration = this.configuration;
216+
var client = configuration.newClient(configuration.writeConcernMax(), {
217+
poolSize: 1
218+
});
219+
220+
client.connect(function (err, client) {
221+
var db = client.db(configuration.db);
222+
var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue5', {
223+
ignoreUndefined: false
224+
});
225+
226+
// Ignore the undefined field
227+
collection.insert({ a: 1, b: undefined }, { ignoreUndefined: true }, function (err) {
228+
expect(err).to.not.exist;
229+
230+
// Locate the doument
231+
collection.findOne({}, function (err, item) {
232+
expect(err).to.not.exist;
233+
test.equal(1, item.a);
234+
test.ok(item.b === undefined);
235+
client.close(done);
236+
});
237+
});
238+
});
239+
}
240+
});
241+
242+
it('Should correctly inherit ignore undefined field from operation during findOneAndReplace', {
243+
metadata: { requires: { topology: ['single'] } },
244+
245+
test: function (done) {
246+
var configuration = this.configuration;
247+
var client = configuration.newClient(configuration.writeConcernMax(), {
248+
poolSize: 1,
249+
ignoreUndefined: false
250+
});
251+
252+
client.connect(function (err, client) {
253+
var db = client.db(configuration.db);
254+
var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue6');
255+
256+
collection.insert({ a: 1, b: 2 }, configuration.writeConcernMax(), function (err) {
257+
expect(err).to.not.exist;
258+
259+
// Replace the doument, ignoring undefined fields
260+
collection.findOneAndReplace(
261+
{},
262+
{ a: 1, b: undefined },
263+
{ ignoreUndefined: true },
264+
function (err) {
265+
expect(err).to.not.exist;
266+
267+
// Locate the doument
268+
collection.findOne(function (err, item) {
269+
test.equal(1, item.a);
270+
test.ok(item.b === undefined);
271+
client.close(done);
272+
});
273+
}
274+
);
275+
});
276+
});
277+
}
278+
});
279+
280+
it('Should correctly ignore undefined field during bulk write', {
281+
metadata: { requires: { topology: ['single'] } },
282+
283+
test: function (done) {
284+
var configuration = this.configuration;
285+
var client = configuration.newClient(configuration.writeConcernMax(), {
286+
poolSize: 1
287+
});
288+
289+
client.connect(function (err, client) {
290+
var db = client.db(configuration.db);
291+
var collection = db.collection('shouldCorrectlyIgnoreUndefinedValue7');
292+
293+
// Ignore the undefined field
294+
collection.bulkWrite(
295+
[{ insertOne: { a: 0, b: undefined } }],
296+
{ ignoreUndefined: true },
297+
function (err) {
298+
expect(err).to.not.exist;
299+
300+
// Locate the doument
301+
collection.findOne(function (err, item) {
302+
test.equal(0, item.a);
303+
test.ok(item.b === undefined);
304+
client.close(done);
305+
});
306+
}
307+
);
308+
});
309+
}
310+
});
220311
});

0 commit comments

Comments
 (0)