Skip to content

Commit 19d694a

Browse files
authoredJul 19, 2024··
Update cast.test.js
1 parent e5eb234 commit 19d694a

File tree

1 file changed

+49
-51
lines changed

1 file changed

+49
-51
lines changed
 

‎test/docs/cast.test.js

+49-51
Original file line numberDiff line numberDiff line change
@@ -101,62 +101,60 @@ describe('Cast Tutorial', function() {
101101
await query.exec();
102102
});
103103

104-
describe('strictQuery', function() {
105-
it('strictQuery true - simple object', async function() {
106-
mongoose.deleteModel('Character');
107-
const schema = new mongoose.Schema({ name: String, age: Number }, {
108-
strictQuery: true
109-
});
110-
Character = mongoose.model('Character', schema);
111-
112-
const query = Character.findOne({ notInSchema: { $lt: 'not a number' } });
113-
114-
await query.exec();
115-
query.getFilter(); // Empty object `{}`, Mongoose removes `notInSchema`
116-
// acquit:ignore:start
117-
assert.deepEqual(query.getFilter(), {});
118-
// acquit:ignore:end
104+
it('strictQuery true', async function() {
105+
mongoose.deleteModel('Character');
106+
const schema = new mongoose.Schema({ name: String, age: Number }, {
107+
strictQuery: true
119108
});
109+
Character = mongoose.model('Character', schema);
110+
111+
const query = Character.findOne({ notInSchema: { $lt: 'not a number' } });
112+
113+
await query.exec();
114+
query.getFilter(); // Empty object `{}`, Mongoose removes `notInSchema`
115+
// acquit:ignore:start
116+
assert.deepEqual(query.getFilter(), {});
117+
// acquit:ignore:end
118+
});
119+
120+
it('strictQuery throw', async function() {
121+
mongoose.deleteModel('Character');
122+
const schema = new mongoose.Schema({ name: String, age: Number }, {
123+
strictQuery: 'throw'
124+
});
125+
Character = mongoose.model('Character', schema);
126+
127+
const query = Character.findOne({ notInSchema: { $lt: 'not a number' } });
120128

121-
it('strictQuery true - conditions', async function() {
122-
mongoose.deleteModel('Character');
123-
const schema = new mongoose.Schema({ name: String, age: Number }, {
124-
strictQuery: true
125-
});
126-
Character = mongoose.model('Character', schema);
127-
128-
const query = Character.findOne({
129-
$or: [{ notInSchema: { $lt: 'not a number' } }],
130-
$and: [{ name: 'abc' }, { age: { $gt: 18 } }, { notInSchema: { $lt: 'not a number' } }],
131-
$nor: [{}] // should be kept
132-
});
133-
134-
await query.exec();
135-
query.getFilter(); // Empty object `{}`, Mongoose removes `notInSchema`
136-
// acquit:ignore:start
137-
assert.deepEqual(query.getFilter(), { $and: [{ name: 'abc' }, { age: { $gt: 18 } }], $nor: [{}] });
138-
// acquit:ignore:end
129+
const err = await query.exec().then(() => null, err => err);
130+
err.name; // 'StrictModeError'
131+
// Path "notInSchema" is not in schema and strictQuery is 'throw'.
132+
err.message;
133+
// acquit:ignore:start
134+
assert.equal(err.name, 'StrictModeError');
135+
assert.equal(err.message, 'Path "notInSchema" is not in schema and ' +
136+
'strictQuery is \'throw\'.');
137+
// acquit:ignore:end
138+
});
139+
140+
it('strictQuery removes casted empty objects', async function() {
141+
mongoose.deleteModel('Character');
142+
const schema = new mongoose.Schema({ name: String, age: Number }, {
143+
strictQuery: true
139144
});
145+
Character = mongoose.model('Character', schema);
140146

141-
it('strictQuery throw', async function() {
142-
mongoose.deleteModel('Character');
143-
const schema = new mongoose.Schema({ name: String, age: Number }, {
144-
strictQuery: 'throw'
145-
});
146-
Character = mongoose.model('Character', schema);
147-
148-
const query = Character.findOne({ notInSchema: { $lt: 'not a number' } });
149-
150-
const err = await query.exec().then(() => null, err => err);
151-
err.name; // 'StrictModeError'
152-
// Path "notInSchema" is not in schema and strictQuery is 'throw'.
153-
err.message;
154-
// acquit:ignore:start
155-
assert.equal(err.name, 'StrictModeError');
156-
assert.equal(err.message, 'Path "notInSchema" is not in schema and ' +
157-
'strictQuery is \'throw\'.');
158-
// acquit:ignore:end
147+
const query = Character.findOne({
148+
$or: [{ notInSchema: { $lt: 'not a number' } }],
149+
$and: [{ name: 'abc' }, { age: { $gt: 18 } }, { notInSchema: { $lt: 'not a number' } }],
150+
$nor: [{}] // should be kept
159151
});
152+
153+
await query.exec();
154+
query.getFilter(); // Empty object `{}`, Mongoose removes `notInSchema`
155+
// acquit:ignore:start
156+
assert.deepEqual(query.getFilter(), { $and: [{ name: 'abc' }, { age: { $gt: 18 } }], $nor: [{}] });
157+
// acquit:ignore:end
160158
});
161159

162160
it('implicit in', async function() {

0 commit comments

Comments
 (0)
Please sign in to comment.