|
| 1 | +/* |
| 2 | + * Tests for the $isNumber aggregation expression. |
| 3 | + */ |
| 4 | +(function() { |
| 5 | +'use strict'; |
| 6 | +const coll = db.isNumber_expr; |
| 7 | +coll.drop(); |
| 8 | + |
| 9 | +function testIsNumber(inputExprPath, expectedOutput, inputId) { |
| 10 | + const result = coll.aggregate([ |
| 11 | + {"$match": {_id: inputId}}, |
| 12 | + {"$project": {_id: 0, "isNum": {"$isNumber": inputExprPath}}}, |
| 13 | + ]) |
| 14 | + .toArray(); |
| 15 | + assert.eq(result, expectedOutput); |
| 16 | +} |
| 17 | + |
| 18 | +// Test when $isNumber evaluates to an integer, when the input expression is a document field. |
| 19 | +assert.commandWorked(coll.insert({_id: 1, integerFieldPath: NumberInt(56072)})); |
| 20 | +testIsNumber("$integerFieldPath", [{"isNum": true}], 1); |
| 21 | +// Same as above, but when the input expression is a nested document field. |
| 22 | +assert.commandWorked(coll.insert({_id: 11, nestedPath: {nestedNumber: NumberInt(500)}})); |
| 23 | +testIsNumber("$nestedPath.nestedNumber", [{"isNum": true}], 11); |
| 24 | + |
| 25 | +// Test when $isNumber evaluates to a double, when the input expression is a document field. |
| 26 | +assert.commandWorked(coll.insert({_id: 2, doubleFieldPath: 56072.2})); |
| 27 | +testIsNumber("$doubleFieldPath", [{"isNum": true}], 2); |
| 28 | +// Same as above, but when the input expression is a nested document field. |
| 29 | +assert.commandWorked(coll.insert({_id: 12, nestedPath: {nestedNumber: 56072.2}})); |
| 30 | +testIsNumber("$nestedPath.nestedNumber", [{"isNum": true}], 12); |
| 31 | + |
| 32 | +// Test when $isNumber evaluates to a decimal, when the input expression is a document field. |
| 33 | +assert.commandWorked(coll.insert({_id: 3, decimalFieldPath: NumberDecimal("1.234")})); |
| 34 | +testIsNumber("$decimalFieldPath", [{"isNum": true}], 3); |
| 35 | +// Same as above, but when the input expression is a nested document field. |
| 36 | +assert.commandWorked(coll.insert({_id: 13, nestedPath: {nestedNumber: NumberDecimal("1.234")}})); |
| 37 | +testIsNumber("$nestedPath.nestedNumber", [{"isNum": true}], 13); |
| 38 | + |
| 39 | +// Test when $isNumber evaluates to a long when the input expression is a document field. |
| 40 | +assert.commandWorked(coll.insert({_id: 4, longFieldPath: NumberLong("123456789")})); |
| 41 | +testIsNumber("$longFieldPath", [{"isNum": true}], 4); |
| 42 | +// Same as above, but when the input expression is a nested document field. |
| 43 | +assert.commandWorked(coll.insert({_id: 14, nestedPath: {nestedNumber: NumberLong("123456789")}})); |
| 44 | +testIsNumber("$nestedPath.nestedNumber", [{"isNum": true}], 14); |
| 45 | + |
| 46 | +// Test when $isNumber evaluates to null, when the input expression is a document field. |
| 47 | +assert.commandWorked(coll.insert({_id: 5, nullFieldPath: null})); |
| 48 | +testIsNumber("$nullFieldPath", [{"isNum": false}], 5); |
| 49 | +// Same as above, but when the input expression is a nested document field. |
| 50 | +assert.commandWorked(coll.insert({_id: 15, nestedPath: {nestedNull: null}})); |
| 51 | +testIsNumber("$nestedPath.nestedNumber", [{"isNum": false}], 15); |
| 52 | + |
| 53 | +// Test when $isNumber evaluates to missing, when the input expression is a document field. |
| 54 | +assert.commandWorked(coll.insert({_id: 6})); |
| 55 | +testIsNumber("$missingFieldPath", [{"isNum": false}], 6); |
| 56 | +// Same as above, but when the input expression is a nested document field. |
| 57 | +assert.commandWorked(coll.insert({_id: 16, nestedPath: {}})); |
| 58 | +testIsNumber("$nestedPath.nestedNumber", [{"isNum": false}], 16); |
| 59 | + |
| 60 | +// Test when $isNumber evaluates to an array, when the input expression is a document field. |
| 61 | +assert.commandWorked(coll.insert({_id: 7, arrayFieldPath: [1]})); |
| 62 | +testIsNumber("$arrayFieldPath", [{"isNum": false}], 7); |
| 63 | +// Same as above, but when the input expression is a nested document field. |
| 64 | +assert.commandWorked(coll.insert({_id: 17, nestedPath: {nestedArray: [1]}})); |
| 65 | +testIsNumber("$nestedPath.nestedNumber", [{"isNum": false}], 17); |
| 66 | + |
| 67 | +// Test when $isNumber evaluates to a string, when the input expression is a document field. |
| 68 | +assert.commandWorked(coll.insert({_id: 8, stringFieldPath: "1234"})); |
| 69 | +testIsNumber("$stringFieldPath", [{"isNum": false}], 8); |
| 70 | +// Same as above, but when the input expression is a nested document field. |
| 71 | +assert.commandWorked(coll.insert({_id: 18, nestedPath: {nestedString: "12345"}})); |
| 72 | +testIsNumber("$nestedPath.nestedNumber", [{"isNum": false}], 18); |
| 73 | + |
| 74 | +// Test when $isNumber evaluates to a Date, when the input expression is a document field. |
| 75 | +assert.commandWorked(coll.insert({_id: 9, dateFieldPath: new Date()})); |
| 76 | +testIsNumber("$dateFieldPath", [{"isNum": false}], 9); |
| 77 | +// Same as above, but when the input expression is a nested document field. |
| 78 | +assert.commandWorked(coll.insert({_id: 19, nestedPath: {nestedDate: new Date()}})); |
| 79 | +testIsNumber("$nestedPath.nestedNumber", [{"isNum": false}], 19); |
| 80 | + |
| 81 | +// Test when $isNumber evaluates to a BinData, when the input expression is a document field. |
| 82 | +// (UUID's are encoded as binary data) |
| 83 | +assert.commandWorked(coll.insert({_id: 10, binDataFieldPath: UUID()})); |
| 84 | +testIsNumber("$binDataFieldPath", [{"isNum": false}], 10); |
| 85 | +// Same as above, but when the input expression is a nested document field. |
| 86 | +assert.commandWorked(coll.insert({_id: 20, nestedPath: {nestedBinData: UUID()}})); |
| 87 | +testIsNumber("$nestedPath.nestedNumber", [{"isNum": false}], 20); |
| 88 | + |
| 89 | +// Test a few literal expressions, rather than ones retrieved from a document. |
| 90 | +assert.commandWorked(coll.insert({_id: 21})); |
| 91 | + |
| 92 | +// Test when $isNumber's input expression is a literal long. |
| 93 | +testIsNumber(NumberLong("12345678"), [{"isNum": true}], 21); |
| 94 | + |
| 95 | +// Test when $isNumber's input expression is a literal decimal. |
| 96 | +testIsNumber(NumberDecimal("1.2345678"), [{"isNum": true}], 21); |
| 97 | + |
| 98 | +// Test when $isNumber's input expression is a literal int. |
| 99 | +testIsNumber(NumberInt(18), [{"isNum": true}], 21); |
| 100 | + |
| 101 | +// Test when $isNumber's input expression is a literal double. |
| 102 | +testIsNumber(5.34, [{"isNum": true}], 21); |
| 103 | + |
| 104 | +// Test when $isNumber's input expression is null literal. |
| 105 | +testIsNumber(null, [{"isNum": false}], 21); |
| 106 | + |
| 107 | +// Test when $isNumber's input expression is a literal array. |
| 108 | +testIsNumber([[2]], [{"isNum": false}], 21); |
| 109 | + |
| 110 | +// Test when $isNumber's input expression is a literal string. |
| 111 | +testIsNumber("a literal string", [{"isNum": false}], 21); |
| 112 | + |
| 113 | +// Test when $isNumber's input expression is a literal Date. |
| 114 | +testIsNumber(new Date(), [{"isNum": false}], 21); |
| 115 | + |
| 116 | +// Test when $isNumber's input expression is a literal BinData. |
| 117 | +testIsNumber(UUID(), [{"isNum": false}], 21); |
| 118 | +}()); |
0 commit comments