File: upperlower.js

package info (click to toggle)
mongodb 1%3A2.4.10-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 82,464 kB
  • sloc: cpp: 740,225; ansic: 152,098; sh: 13,820; python: 11,864; makefile: 1,012; perl: 922; pascal: 617; java: 452; lisp: 222; asm: 174
file content (65 lines) | stat: -rw-r--r-- 1,952 bytes parent folder | download | duplicates (3)
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
// $toUpper, $toLower tests.

t = db.jstests_aggregation_upperlower;
t.drop();

t.save( {} );

function assertResult( expectedUpper, expectedLower, string ) {
    result = t.aggregate( { $project:{ upper:{ $toUpper:string },
                                       lower:{ $toLower:string } } } ).result[ 0 ];
    assert.eq( expectedUpper, result.upper );
    assert.eq( expectedLower, result.lower );
}

function assertException( string ) {
    assert.commandFailed(t.runCommand('aggregate',
                                      {pipeline: [{$project: {upper: {$toUpper: string}}}]}));
    assert.commandFailed(t.runCommand('aggregate',
                                      {pipeline: [{$project: {lower: {$toLower: string}}}]}));
}

// Wrong number of arguments.
assertException( [] );
assertException( [ 'a', 'b' ] );

// Upper and lower case conversion.
assertResult( '', '', '' );
assertResult( '', '', [ '' ] );
assertResult( 'AB', 'ab', 'aB' );
assertResult( 'AB', 'ab', [ 'Ab' ] );
assertResult( 'ABZ', 'abz', 'aBz' );

// With non alphabet characters.
assertResult( '1', '1', '1' );
assertResult( '1^A-A_$%.', '1^a-a_$%.', '1^a-A_$%.' );
assertResult( '1290B', '1290b', '1290b' );
assertResult( '0XFF0B', '0xff0b', '0XfF0b' );

// Type coercion.
assertResult( '555.5', '555.5', 555.5 );
assertResult( '1970-01-01T00:00:00', '1970-01-01t00:00:00', new Date( 0 ) );
assertResult( '', '', null );
assertException( /abc/ );

// Nested.
spec = 'aBcDeFg';
for( i = 0; i < 10; ++i ) {
    assertResult( 'ABCDEFG', 'abcdefg', spec );
    if ( i % 2 == 0 ) {
        spec = [ { $toUpper:spec } ];
    }
    else {
        spec = [ { $toLower:spec } ];
    }
}

// Utf8.
assertResult( '\u0080D\u20ac', '\u0080d\u20ac', '\u0080\u0044\u20ac' );
assertResult( 'ó', 'ó', 'ó' ); // Not handled.
assertResult( 'Ó', 'Ó', 'Ó' ); // Not handled.

// Value from field path.
t.drop();
t.save( { string:'-_aB' } );
assertResult( '-_AB', '-_ab', '$string' );