|
| 1 | +# Copyright 2015 - present MongoDB, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +use strict; |
| 16 | +use warnings; |
| 17 | +use Test::More 0.96; |
| 18 | +use Test::Fatal; |
| 19 | +use Test::Deep qw/!blessed/; |
| 20 | + |
| 21 | +use utf8; |
| 22 | +use Tie::IxHash; |
| 23 | + |
| 24 | +use MongoDB; |
| 25 | +use MongoDB::Error; |
| 26 | +use BSON::Types ':all'; |
| 27 | + |
| 28 | +use lib "t/lib"; |
| 29 | +use MongoDBTest qw/skip_unless_mongod build_client get_test_db server_version server_type get_capped/; |
| 30 | + |
| 31 | +skip_unless_mongod(); |
| 32 | + |
| 33 | +my $conn = build_client(); |
| 34 | +my $testdb = get_test_db($conn); |
| 35 | +my $server_version = server_version($conn); |
| 36 | +my $server_type = server_type($conn); |
| 37 | +my $coll = $testdb->get_collection('test_collection'); |
| 38 | + |
| 39 | +$coll->insert_many( [ |
| 40 | + { _id => 1, size => 10 }, |
| 41 | + { _id => 2, size => 5 }, |
| 42 | + { _id => 3, size => 15 }, |
| 43 | +] ); |
| 44 | + |
| 45 | +subtest "sort standard hash" => sub { |
| 46 | + my @res = $coll->find( {}, { sort => { size => 1 } } )->result->all; |
| 47 | + |
| 48 | + cmp_deeply \@res, |
| 49 | + [ |
| 50 | + { _id => 2, size => 5 }, |
| 51 | + { _id => 1, size => 10 }, |
| 52 | + { _id => 3, size => 15 }, |
| 53 | + ], |
| 54 | + 'Got correct sort order'; |
| 55 | +}; |
| 56 | + |
| 57 | +subtest "sort BSON::Doc" => sub { |
| 58 | + my $b_doc = bson_doc( size => 1 ); |
| 59 | + my @res = $coll->find( {}, { sort => $b_doc } )->result->all; |
| 60 | + |
| 61 | + cmp_deeply \@res, |
| 62 | + [ |
| 63 | + { _id => 2, size => 5 }, |
| 64 | + { _id => 1, size => 10 }, |
| 65 | + { _id => 3, size => 15 }, |
| 66 | + ], |
| 67 | + 'Got correct sort order'; |
| 68 | +}; |
| 69 | + |
| 70 | +done_testing; |
0 commit comments