forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract_cursor.test.ts
114 lines (87 loc) · 3.23 KB
/
abstract_cursor.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { expect } from 'chai';
import { inspect } from 'util';
import { Collection, MongoAPIError, MongoClient } from '../../../src';
const falseyValues = [0, 0n, NaN, '', false, undefined];
describe('class AbstractCursor', function () {
let client: MongoClient;
let collection: Collection;
beforeEach(async function () {
client = this.configuration.newClient();
collection = client.db('abstract_cursor_integration').collection('test');
await collection.insertMany(Array.from({ length: 5 }, (_, index) => ({ index })));
});
afterEach(async function () {
await collection.deleteMany({});
await client.close();
});
context('toArray() with custom transforms', function () {
for (const value of falseyValues) {
it(`supports mapping to falsey value '${inspect(value)}'`, async function () {
const cursor = collection.find();
cursor.map(() => value);
const result = await cursor.toArray();
const expected = Array.from({ length: 5 }, () => value);
expect(result).to.deep.equal(expected);
});
}
it('throws when mapping to `null` and cleans up cursor', async function () {
const cursor = collection.find();
cursor.map(() => null);
const error = await cursor.toArray().catch(e => e);
expect(error).be.instanceOf(MongoAPIError);
expect(cursor.closed).to.be.true;
});
});
context('Symbol.asyncIterator() with custom transforms', function () {
for (const value of falseyValues) {
it(`supports mapping to falsey value '${inspect(value)}'`, async function () {
const cursor = collection.find();
cursor.map(() => value);
let count = 0;
for await (const document of cursor) {
expect(document).to.deep.equal(value);
count++;
}
expect(count).to.equal(5);
});
}
it('throws when mapping to `null` and cleans up cursor', async function () {
const cursor = collection.find();
cursor.map(() => null);
try {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for await (const document of cursor) {
expect.fail('Expected error to be thrown');
}
} catch (error) {
expect(error).to.be.instanceOf(MongoAPIError);
expect(cursor.closed).to.be.true;
}
});
});
context('forEach() with custom transforms', function () {
for (const value of falseyValues) {
it(`supports mapping to falsey value '${inspect(value)}'`, async function () {
const cursor = collection.find();
cursor.map(() => value);
let count = 0;
function transform(value) {
expect(value).to.deep.equal(value);
count++;
}
await cursor.forEach(transform);
expect(count).to.equal(5);
});
}
it('throws when mapping to `null` and cleans up cursor', async function () {
const cursor = collection.find();
cursor.map(() => null);
function iterator() {
expect.fail('Expected no documents from cursor, received at least one.');
}
const error = await cursor.forEach(iterator).catch(e => e);
expect(error).to.be.instanceOf(MongoAPIError);
expect(cursor.closed).to.be.true;
});
});
});