diff options
author | Aaron <aaron@10gen.com> | 2012-07-30 11:41:49 -0700 |
---|---|---|
committer | Aaron <aaron@10gen.com> | 2012-07-31 20:09:16 -0700 |
commit | 242ec02fce2ace1f40b06a8e486e14fd597ebb44 (patch) | |
tree | 6e9746921d0390783b54ba5fba93514519f194f7 /jstests/aggregation | |
parent | fb51c008c78c0e5c31a81cbb328918bc8c2d003f (diff) | |
download | mongo-242ec02fce2ace1f40b06a8e486e14fd597ebb44.tar.gz |
Functional tests for $first and $last.
Diffstat (limited to 'jstests/aggregation')
-rw-r--r-- | jstests/aggregation/bugs/firstlast.js | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/jstests/aggregation/bugs/firstlast.js b/jstests/aggregation/bugs/firstlast.js new file mode 100644 index 00000000000..f0398aaa74f --- /dev/null +++ b/jstests/aggregation/bugs/firstlast.js @@ -0,0 +1,110 @@ +// Check $first/$last group accumulators. SERVER-3862 +// $first/$last select first/last value for a group key from the previous pipeline. + +t = db.jstests_aggregation_firstlast; +t.drop(); + +/** Check expected $first and $last result values. */ +function assertFirstLast( expectedFirst, expectedLast, pipeline, expression ) { + pipeline = pipeline || []; + expression = expression || '$b' + pipeline.push( { $group:{ _id:'$a', + first:{ $first:expression }, + last:{ $last:expression } } } ); + result = t.aggregate( pipeline ).result; + for( var i = 0; i < result.length; ++i ) { + if ( result[ i ]._id == 1 ) { + // Check results for group _id 1. + assert.eq( expectedFirst, result[ i ].first ); + assert.eq( expectedLast, result[ i ].last ); + return; + } + } + assert( false, "Expected group _id '1' missing." ); +} + +// One document. +t.save( { a:1, b:1 } ); +assertFirstLast( 1, 1 ); + +// Two documents. +t.save( { a:1, b:2 } ); +assertFirstLast( 1, 2 ); + +// Three documents. +t.save( { a:1, b:3 } ); +assertFirstLast( 1, 3 ); + +// Another 'a' key value does not affect outcome. +t.drop(); +t.save( { a:3, b:0 } ); +t.save( { a:1, b:1 } ); +t.save( { a:1, b:2 } ); +t.save( { a:1, b:3 } ); +t.save( { a:2, b:0 } ); +assertFirstLast( 1, 3 ); + +// Additional pipeline stages do not affect outcome if order is maintained. +assertFirstLast( 1, 3, [ { $project:{ x:'$a', y:'$b' } }, { $project:{ a:'$x', b:'$y' } } ] ); + +// Additional pipeline stages affect outcome if order is modified. +assertFirstLast( 3, 1, [ { $sort:{ b:-1 } } ] ); + +// Skip and limit affect the results seen. +t.drop(); +t.save( { a:1, b:1 } ); +t.save( { a:1, b:2 } ); +t.save( { a:1, b:3 } ); +assertFirstLast( 1, 2, [ { $limit:2 } ] ); +assertFirstLast( 2, 3, [ { $skip:1 }, { $limit:2 } ] ); +assertFirstLast( 2, 2, [ { $skip:1 }, { $limit:1 } ] ); + +// Mixed type values. +t.save( { a:1, b:'foo' } ); +assertFirstLast( 1, 'foo' ); + +t.drop(); +t.save( { a:1, b:'bar' } ); +t.save( { a:1, b:true } ); +assertFirstLast( 'bar', true ); + +// Value null. +t.drop(); +t.save( { a:1, b:null } ); +t.save( { a:1, b:2 } ); +assertFirstLast( null, 2 ); +t.drop(); +t.save( { a:1, b:2 } ); +t.save( { a:1, b:null } ); +assertFirstLast( 2, null ); +t.drop(); +t.save( { a:1, b:null } ); +t.save( { a:1, b:null } ); +assertFirstLast( null, null ); + +// Value missing. +t.drop(); +t.save( { a:1 } ); +t.save( { a:1, b:2 } ); +assertFirstLast( undefined, 2 ); +t.drop(); +t.save( { a:1, b:2 } ); +t.save( { a:1 } ); +assertFirstLast( 2, undefined ); +t.drop(); +t.save( { a:1 } ); +t.save( { a:1 } ); +assertFirstLast( undefined, undefined ); + +// Dotted field. +t.drop(); +t.save( { a:1, b:[ { c:1 }, { c:2 } ] } ); +t.save( { a:1, b:[ { c:6 }, {} ] } ); +assertFirstLast( [ 1, 2 ], [ 6, null ], [], '$b.c' ); + +// Computed expressions. +t.drop(); +t.save( { a:1, b:1 } ); +t.save( { a:1, b:2 } ); +assertFirstLast( 1, 0, [], { $mod:[ '$b', 2 ] } ); +assertFirstLast( 0, 1, [], { $mod:[ { $add:[ '$b', 1 ] }, 2 ] } ); |