summaryrefslogtreecommitdiff
path: root/jstests/change_streams/change_stream_collation.js
blob: 314e7b0895467ed201ebcf53b03b0c2f15faae41 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
 * Tests that a change stream can use a user-specified, or collection-default collation.
 */
(function() {
    "use strict";

    load("jstests/libs/collection_drop_recreate.js");  // For assert[Drop|Create]Collection.
    load("jstests/libs/change_stream_util.js");        // For 'ChangeStreamTest'.

    let cst = new ChangeStreamTest(db);

    const caseInsensitive = {locale: "en_US", strength: 2};

    let caseInsensitiveCollection = "change_stream_case_insensitive";
    assertDropCollection(db, caseInsensitiveCollection);

    // Test that you can open a change stream before the collection exists, and it will use the
    // simple collation.
    const simpleCollationStream = cst.startWatchingChanges(
        {pipeline: [{$changeStream: {}}], collection: caseInsensitiveCollection});

    // Create the collection with a non-default collation - this should invalidate the stream we
    // opened before it existed.
    caseInsensitiveCollection =
        assertCreateCollection(db, caseInsensitiveCollection, {collation: caseInsensitive});
    cst.assertNextChangesEqual({
        cursor: simpleCollationStream,
        expectedChanges: [{operationType: "invalidate"}],
        expectInvalidate: true
    });

    const implicitCaseInsensitiveStream = cst.startWatchingChanges({
        pipeline: [
            {$changeStream: {}},
            {$match: {"fullDocument.text": "abc"}},
            // Be careful not to use _id in this projection, as startWatchingChanges() will exclude
            // it by default, assuming it is the resume token.
            {$project: {docId: "$documentKey._id"}}
        ],
        collection: caseInsensitiveCollection
    });
    const explicitCaseInsensitiveStream = cst.startWatchingChanges({
        pipeline: [
            {$changeStream: {}},
            {$match: {"fullDocument.text": "abc"}},
            {$project: {docId: "$documentKey._id"}}
        ],
        collection: caseInsensitiveCollection,
        aggregateOptions: {collation: caseInsensitive}
    });

    assert.writeOK(caseInsensitiveCollection.insert({_id: 0, text: "aBc"}));
    assert.writeOK(caseInsensitiveCollection.insert({_id: 1, text: "abc"}));

    cst.assertNextChangesEqual(
        {cursor: implicitCaseInsensitiveStream, expectedChanges: [{docId: 0}, {docId: 1}]});
    cst.assertNextChangesEqual(
        {cursor: explicitCaseInsensitiveStream, expectedChanges: [{docId: 0}, {docId: 1}]});

    // Test that the collation does not apply to the scan over the oplog.
    const similarNameCollection = assertDropAndRecreateCollection(
        db, "cHaNgE_sTrEaM_cAsE_iNsEnSiTiVe", {collation: {locale: "en_US"}});

    assert.writeOK(similarNameCollection.insert({_id: 0, text: "aBc"}));

    assert.writeOK(caseInsensitiveCollection.insert({_id: 2, text: "ABC"}));

    // The existing stream should not see the first insert (to the other collection), but should see
    // the second.
    cst.assertNextChangesEqual(
        {cursor: implicitCaseInsensitiveStream, expectedChanges: [{docId: 2}]});
    cst.assertNextChangesEqual(
        {cursor: explicitCaseInsensitiveStream, expectedChanges: [{docId: 2}]});

    // Test that creating a collection without a collation does not invalidate any change streams
    // that were opened before the collection existed.
    (function() {
        let noCollationCollection = "change_stream_no_collation";
        assertDropCollection(db, noCollationCollection);

        const streamCreatedBeforeNoCollationCollection = cst.startWatchingChanges({
            pipeline: [{$changeStream: {}}, {$project: {docId: "$documentKey._id"}}],
            collection: noCollationCollection
        });

        noCollationCollection = assertCreateCollection(db, noCollationCollection);
        assert.writeOK(noCollationCollection.insert({_id: 0}));

        cst.assertNextChangesEqual(
            {cursor: streamCreatedBeforeNoCollationCollection, expectedChanges: [{docId: 0}]});
    }());

    // Test that creating a collection and explicitly specifying the simple collation does not
    // invalidate any change streams that were opened before the collection existed.
    (function() {
        let simpleCollationCollection = "change_stream_simple_collation";
        assertDropCollection(db, simpleCollationCollection);

        const streamCreatedBeforeSimpleCollationCollection = cst.startWatchingChanges({
            pipeline: [{$changeStream: {}}, {$project: {docId: "$documentKey._id"}}],
            collection: simpleCollationCollection
        });

        simpleCollationCollection =
            assertCreateCollection(db, simpleCollationCollection, {collation: {locale: "simple"}});
        assert.writeOK(simpleCollationCollection.insert({_id: 0}));

        cst.assertNextChangesEqual(
            {cursor: streamCreatedBeforeSimpleCollationCollection, expectedChanges: [{docId: 0}]});
    }());

    // Test that creating a change stream with a non-default collation, then creating a collection
    // with the same collation will not invalidate the change stream.
    (function() {
        let frenchCollection = "change_stream_french_collation";
        assertDropCollection(db, frenchCollection);

        const frenchChangeStream = cst.startWatchingChanges({
            pipeline: [{$changeStream: {}}, {$project: {docId: "$documentKey._id"}}],
            aggregateOptions: {collation: {locale: "fr"}},
            collection: frenchCollection
        });

        frenchCollection =
            assertCreateCollection(db, frenchCollection, {collation: {locale: "fr"}});
        assert.writeOK(frenchCollection.insert({_id: 0}));

        cst.assertNextChangesEqual({cursor: frenchChangeStream, expectedChanges: [{docId: 0}]});
    }());

    // Test that creating a change stream with a non-default collation, then creating a collection
    // with *a different* collation will not invalidate the change stream.
    (function() {
        let germanCollection = "change_stream_german_collation";
        assertDropCollection(db, germanCollection);

        const englishCaseInsensitiveStream = cst.startWatchingChanges({
            pipeline: [
                {$changeStream: {}},
                {$match: {"fullDocument.text": "abc"}},
                {$project: {docId: "$documentKey._id"}}
            ],
            aggregateOptions: {collation: caseInsensitive},
            collection: germanCollection
        });

        germanCollection =
            assertCreateCollection(db, germanCollection, {collation: {locale: "de"}});
        assert.writeOK(germanCollection.insert({_id: 0, text: "aBc"}));

        cst.assertNextChangesEqual(
            {cursor: englishCaseInsensitiveStream, expectedChanges: [{docId: 0}]});
    }());

    // Test that creating a change stream with a non-default collation against a collection that has
    // a non-simple default collation will use the collation specified on the operation.
    (function() {
        const caseInsensitiveCollection = assertDropAndRecreateCollection(
            db, "change_stream_case_insensitive", {collation: caseInsensitive});

        const englishCaseSensitiveStream = cst.startWatchingChanges({
            pipeline: [
                {$changeStream: {}},
                {$match: {"fullDocument.text": "abc"}},
                {$project: {docId: "$documentKey._id"}}
            ],
            aggregateOptions: {collation: {locale: "en_US"}},
            collection: caseInsensitiveCollection
        });

        assert.writeOK(caseInsensitiveCollection.insert({_id: 0, text: "aBc"}));
        assert.writeOK(caseInsensitiveCollection.insert({_id: 1, text: "abc"}));

        cst.assertNextChangesEqual(
            {cursor: englishCaseSensitiveStream, expectedChanges: [{docId: 1}]});
    }());

    // Test that collation is supported by the shell helper.
    // Test that creating a change stream with a non-default collation against a collection that has
    // a simple default collation will use the collation specified on the operation.
    (function() {
        const noCollationCollection =
            assertDropAndRecreateCollection(db, "change_stream_no_collation");

        const cursor = noCollationCollection.watch(
            [
              {$match: {"fullDocument.text": "abc"}},
              {$project: {docId: "$documentKey._id", _id: 0}}
            ],
            {collation: caseInsensitive});
        assert(!cursor.hasNext());
        assert.writeOK(noCollationCollection.insert({_id: 0, text: "aBc"}));
        assert.writeOK(noCollationCollection.insert({_id: 1, text: "abc"}));
        assert.soon(() => cursor.hasNext());
        assert.docEq(cursor.next(), {docId: 0});
        assert(cursor.hasNext());
        assert.docEq(cursor.next(), {docId: 1});
        assert(!cursor.hasNext());
    }());

})();