Skip to content

Commit 14e0cdf

Browse files
committed
add tests to check bson option inheritance
1 parent 39d9d2d commit 14e0cdf

File tree

4 files changed

+134
-3
lines changed

4 files changed

+134
-3
lines changed

src/bson.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function inheritOrDefaultBSONSerializableOptions(
7878
): BSONSerializeOptions {
7979
// Merge the BSONSerializeOptions, preferring options over parentOptions, and substituting a
8080
// default for values not set.
81-
// Note that we exclude fieldsAsRaw and serializeFunctions because I was not sure about their usage
81+
// Note that we exclude fieldsAsRaw and serializeFunctions because I was not sure about their usage //TODO: serialize functions?
8282
return {
8383
raw: options?.raw ?? parentOptions?.raw ?? false,
8484
promoteLongs: options?.promoteLongs ?? parentOptions?.promoteLongs ?? true,

src/operations/command.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ export abstract class CommandOperation<
101101
}
102102

103103
// Assign all bsonOptions to OperationBase obj, preferring command options over parent options
104-
// TODO, for collection it makes sense to take it from parent.s -- is this true for others?
105-
// TODO: downside of this is after the command is created these values are *fixed* because they are already set to default/inherited values
104+
// base accounts for the fact that Collection stores bson options in s, while Db stores bson options in s.options
106105
const base = Object.assign({}, parent?.s.options, parent?.s);
107106
Object.assign(this, inheritOrDefaultBSONSerializableOptions(options, base));
108107
}

test/functional/ignore_undefined.test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,75 @@ 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+
41110
it(
42111
'Should correctly connect using MongoClient and perform insert document ignoring undefined field',
43112
{

test/functional/insert.test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,69 @@ describe('Insert', function () {
19021902
}
19031903
});
19041904

1905+
it('shouldCorrectlyInheritPromoteLongFalseNativeBSONWithGetMore', {
1906+
// Add a tag that our runner can trigger on
1907+
// in this case we are setting that node needs to be higher than 0.10.X to run
1908+
metadata: {
1909+
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
1910+
},
1911+
1912+
test: function (done) {
1913+
var configuration = this.configuration;
1914+
1915+
var client = configuration.newClient(configuration.writeConcernMax(), {
1916+
poolSize: 1,
1917+
promoteLongs: true
1918+
});
1919+
client.connect(function (err, client) {
1920+
var db = client.db(configuration.db);
1921+
db.collection('shouldCorrectlyInheritPromoteLongFalseNativeBSONWithGetMore').insertMany(
1922+
[
1923+
{ a: Long.fromNumber(10) },
1924+
{ a: Long.fromNumber(10) },
1925+
{ a: Long.fromNumber(10) },
1926+
{ a: Long.fromNumber(10) },
1927+
{ a: Long.fromNumber(10) },
1928+
{ a: Long.fromNumber(10) },
1929+
{ a: Long.fromNumber(10) },
1930+
{ a: Long.fromNumber(10) },
1931+
{ a: Long.fromNumber(10) },
1932+
{ a: Long.fromNumber(10) },
1933+
{ a: Long.fromNumber(10) },
1934+
{ a: Long.fromNumber(10) },
1935+
{ a: Long.fromNumber(10) },
1936+
{ a: Long.fromNumber(10) },
1937+
{ a: Long.fromNumber(10) },
1938+
{ a: Long.fromNumber(10) },
1939+
{ a: Long.fromNumber(10) },
1940+
{ a: Long.fromNumber(10) },
1941+
{ a: Long.fromNumber(10) },
1942+
{ a: Long.fromNumber(10) },
1943+
{ a: Long.fromNumber(10) },
1944+
{ a: Long.fromNumber(10) },
1945+
{ a: Long.fromNumber(10) },
1946+
{ a: Long.fromNumber(10) }
1947+
],
1948+
function (err, doc) {
1949+
expect(err).to.not.exist;
1950+
test.ok(doc);
1951+
1952+
db.collection('shouldCorrectlyInheritPromoteLongFalseNativeBSONWithGetMore')
1953+
.find({}, { promoteLongs: false })
1954+
.batchSize(2)
1955+
.toArray(function (err, docs) {
1956+
expect(err).to.not.exist;
1957+
var doc = docs.pop();
1958+
1959+
test.ok(doc.a._bsontype === 'Long');
1960+
client.close(done);
1961+
});
1962+
}
1963+
);
1964+
});
1965+
}
1966+
});
1967+
19051968
it('shouldCorrectlyHonorPromoteLongTrueNativeBSON', {
19061969
// Add a tag that our runner can trigger on
19071970
// in this case we are setting that node needs to be higher than 0.10.X to run

0 commit comments

Comments
 (0)