summaryrefslogtreecommitdiff
path: root/jstests/aggregation/expressions/split.js
blob: 3425e81ecc21555164660ab68e20f11d4cc74c01 (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
// In SERVER-6773, the $split expression was introduced. In this file, we test the functionality and
// error cases of the expression.

(function() {
"use strict";

load("jstests/aggregation/extras/utils.js");  // For assertErrorCode and testExpression.
load("jstests/libs/sbe_assert_error_override.js");

const coll = db.split;

coll.drop();
assert.commandWorked(coll.insert({}));

//
// Tests with constant-folding optimization.
//

// Basic tests.
testExpression(coll, {$split: ["abc", "b"]}, ["a", "c"]);
testExpression(coll, {$split: ["aaa", "b"]}, ["aaa"]);
testExpression(coll, {$split: ["a b a", "b"]}, ["a ", " a"]);
testExpression(coll, {$split: ["a", "a"]}, ["", ""]);
testExpression(coll, {$split: ["aa", "a"]}, ["", "", ""]);
testExpression(coll, {$split: ["aaa", "a"]}, ["", "", "", ""]);
testExpression(coll, {$split: ["", "a"]}, [""]);
testExpression(coll, {$split: ["abc abc cba abc", "abc"]}, ["", " ", " cba ", ""]);

// Ensure that $split operates correctly when the string has embedded null bytes.
testExpression(coll, {$split: ["a\0b\0c", "\0"]}, ["a", "b", "c"]);
testExpression(coll, {$split: ["\0a\0", "a"]}, ["\0", "\0"]);
testExpression(coll, {$split: ["\0\0\0", "a"]}, ["\0\0\0"]);

// Ensure that $split operates correctly when the string has multi-byte tokens or input strings.
testExpression(coll, {$split: ["∫a∫", "a"]}, ["∫", "∫"]);
testExpression(coll, {$split: ["a∫∫a", "∫"]}, ["a", "", "a"]);

// Ensure that $split produces null when given null as input.
testExpression(coll, {$split: ["abc", null]}, null);
testExpression(coll, {$split: [null, "abc"]}, null);

// Ensure that $split produces null when given missing fields as input.
testExpression(coll, {$split: ["$a", "a"]}, null);
testExpression(coll, {$split: ["a", "$a"]}, null);
testExpression(coll, {$split: ["$missing", {$toLower: "$missing"}]}, null);

//
// Error Code tests with constant-folding optimization.
//

// Ensure that $split errors when given more or less than two arguments.
let pipeline = {$project: {split: {$split: []}}};
assertErrorCode(coll, pipeline, 16020);

pipeline = {
    $project: {split: {$split: ["a"]}}
};
assertErrorCode(coll, pipeline, 16020);

pipeline = {
    $project: {split: {$split: ["a", "b", "c"]}}
};
assertErrorCode(coll, pipeline, 16020);

// Ensure that $split errors when given non-string input.
pipeline = {
    $project: {split: {$split: [1, "abc"]}}
};
assertErrorCode(coll, pipeline, 40085);

pipeline = {
    $project: {split: {$split: ["abc", 1]}}
};
assertErrorCode(coll, pipeline, 40086);

// Ensure that $split errors when given an empty separator.
pipeline = {
    $project: {split: {$split: ["abc", ""]}}
};
assertErrorCode(coll, pipeline, 40087);
})();