Skip to content

Commit 1535739

Browse files
authored
test(NODE-7202): migrate integration/crud/server_errors tests (#4732)
1 parent ae08fb5 commit 1535739

File tree

2 files changed

+93
-108
lines changed

2 files changed

+93
-108
lines changed

test/integration/crud/server_errors.test.js

Lines changed: 0 additions & 108 deletions
This file was deleted.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { expect } from 'chai';
2+
3+
import { type MongoClient, MongoServerError } from '../../../src';
4+
import { setupDatabase } from '../shared';
5+
6+
describe('Errors', function () {
7+
before(function () {
8+
return setupDatabase(this.configuration);
9+
});
10+
11+
let client: MongoClient;
12+
13+
beforeEach(function () {
14+
client = this.configuration.newClient(this.configuration.writeConcernMax(), { maxPoolSize: 1 });
15+
return client.connect();
16+
});
17+
18+
afterEach(function () {
19+
return client.close();
20+
});
21+
22+
it('should fail insert due to unique index', async function () {
23+
const db = client.db(this.configuration.db);
24+
const collection = await db.createCollection('test_failing_insert_due_to_unique_index');
25+
await collection.createIndexes([
26+
{
27+
name: 'test_failing_insert_due_to_unique_index',
28+
key: { a: 1 },
29+
unique: true
30+
}
31+
]);
32+
33+
await collection.insertOne({ a: 2 }, { writeConcern: { w: 1 } });
34+
35+
const err = await collection.insertOne({ a: 2 }, { writeConcern: { w: 1 } }).catch(err => err);
36+
expect(err).to.be.instanceOf(MongoServerError);
37+
expect(err.code).to.equal(11000);
38+
});
39+
40+
// TODO(NODE-7219): remove as it duplicates "should fail insert due to unique index"
41+
// it('should fail insert due to unique index strict', function (done) {
42+
// const db = client.db(this.configuration.db);
43+
// db.dropCollection('test_failing_insert_due_to_unique_index_strict', () => {
44+
// db.createCollection('test_failing_insert_due_to_unique_index_strict', err => {
45+
// expect(err).to.not.exist;
46+
// const collection = db.collection('test_failing_insert_due_to_unique_index_strict');
47+
// collection.createIndexes(
48+
// [
49+
// {
50+
// name: 'test_failing_insert_due_to_unique_index_strict',
51+
// key: { a: 1 },
52+
// unique: true
53+
// }
54+
// ],
55+
// { writeConcern: { w: 1 } },
56+
// err => {
57+
// expect(err).to.not.exist;
58+
// collection.insertOne({ a: 2 }, { writeConcern: { w: 1 } }, err => {
59+
// expect(err).to.not.exist;
60+
//
61+
// collection.insertOne({ a: 2 }, { writeConcern: { w: 1 } }, err => {
62+
// expect(err.code).to.equal(11000);
63+
// done();
64+
// });
65+
// });
66+
// }
67+
// );
68+
// });
69+
// });
70+
// });
71+
72+
const PROJECTION_ERRORS = new Set([
73+
'Projection cannot have a mix of inclusion and exclusion.',
74+
'Cannot do exclusion on field b in inclusion projection'
75+
]);
76+
77+
it('should return an error object with message when mixing included and excluded fields', async () => {
78+
const db = client.db();
79+
const c = db.collection('test_error_object_should_include_message');
80+
await c.insertOne({ a: 2, b: 5 }, { writeConcern: { w: 1 } });
81+
const error = await c.findOne({ a: 2 }, { projection: { a: 1, b: 0 } }).catch(error => error);
82+
expect(error).to.be.instanceOf(MongoServerError);
83+
expect(PROJECTION_ERRORS).to.include(error.errmsg);
84+
});
85+
86+
it('should reject promise with projection errors', async () => {
87+
const db = client.db();
88+
const c = db.collection('test_error_object_should_include_message');
89+
const error = await c.findOne({}, { projection: { a: 1, b: 0 } }).catch(error => error);
90+
expect(error).to.be.instanceOf(MongoServerError);
91+
expect(PROJECTION_ERRORS).to.include(error.errmsg);
92+
});
93+
});

0 commit comments

Comments
 (0)