~ubuntu-branches/ubuntu/vivid/libmongodb-perl/vivid

« back to all changes in this revision

Viewing changes to t/bulk.t

  • Committer: Package Import Robot
  • Author(s): gregor herrmann
  • Date: 2014-07-12 20:41:25 UTC
  • mfrom: (2.2.5 sid)
  • Revision ID: package-import@ubuntu.com-20140712204125-m1cox1ikaj5d86q1
Tags: 0.704.2.0-1
* New upstream release.
* Drop build dependencies which are not needed anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
use MongoDB::Error;
29
29
 
30
30
use lib "t/lib";
31
 
use MongoDBTest '$conn', '$testdb', '$using_2_6';
 
31
use MongoDBTest '$conn', '$testdb', '$server_version';
32
32
 
33
33
my $coll = $testdb->get_collection("test_collection");
34
34
 
35
35
my $ismaster      = $testdb->run_command( { ismaster     => 1 } );
36
36
my $server_status = $testdb->run_command( { serverStatus => 1 } );
37
37
my $is_standalone = !( $conn->_is_mongos || exists $server_status->{repl} );
 
38
my $server_does_bulk = $server_version >= v2.5.5;
38
39
 
39
40
subtest "constructors" => sub {
40
41
    my @constructors = qw(
113
114
            $result,
114
115
            MongoDB::WriteResult->new(
115
116
                nInserted   => 1,
116
 
                nModified   => ( $using_2_6 ? 0 : undef ),
 
117
                nModified   => ( $server_does_bulk ? 0 : undef ),
117
118
                op_count    => 1,
118
119
                batch_count => 1,
119
120
            ),
125
126
        $coll->drop;
126
127
        my $bulk = $coll->$method;
127
128
        is( $coll->count, 0, "no docs in collection" );
128
 
        $bulk->insert( {} );
 
129
        my $doc = {};
 
130
        $bulk->insert( $doc );
 
131
        is( $doc->{_id}, undef, "adding _id doesn't modify original" );
129
132
        my ( $result, $err );
130
133
        $err = exception { $result = $bulk->execute };
131
134
        is( $err, undef, "no error on insert" ) or diag explain $err;
135
138
            $result,
136
139
            MongoDB::WriteResult->new(
137
140
                nInserted   => 1,
138
 
                nModified   => ( $using_2_6 ? 0 : undef ),
 
141
                nModified   => ( $server_does_bulk ? 0 : undef ),
139
142
                op_count    => 1,
140
143
                batch_count => 1,
141
144
            ),
216
219
            $result,
217
220
            MongoDB::WriteResult->new(
218
221
                nMatched    => 2,
219
 
                nModified   => ( $using_2_6 ? 2 : undef ),
 
222
                nModified   => ( $server_does_bulk ? 2 : undef ),
220
223
                op_count    => 1,
221
224
                batch_count => 1,
222
225
            ),
244
247
            $result,
245
248
            MongoDB::WriteResult->new(
246
249
                nMatched    => 2,
247
 
                nModified   => ( $using_2_6 ? 2 : undef ),
 
250
                nModified   => ( $server_does_bulk ? 2 : undef ),
248
251
                op_count    => 2,
249
 
                batch_count => $using_2_6 ? 1 : 2,
 
252
                batch_count => $server_does_bulk ? 1 : 2,
250
253
            ),
251
254
            "result object correct"
252
255
        );
270
273
            $result,
271
274
            MongoDB::WriteResult->new(
272
275
                nMatched    => 1,
273
 
                nModified   => ( $using_2_6 ? 1 : undef ),
 
276
                nModified   => ( $server_does_bulk ? 1 : undef ),
274
277
                op_count    => 1,
275
278
                batch_count => 1,
276
279
            ),
335
338
            $result,
336
339
            MongoDB::WriteResult->new(
337
340
                nMatched    => 1,
338
 
                nModified   => ( $using_2_6 ? 1 : undef ),
 
341
                nModified   => ( $server_does_bulk ? 1 : undef ),
339
342
                op_count    => 1,
340
343
                batch_count => 1,
341
344
            ),
384
387
            $result,
385
388
            MongoDB::WriteResult->new(
386
389
                nUpserted => 1,
387
 
                nModified => ( $using_2_6 ? 0 : undef ),
 
390
                nModified => ( $server_does_bulk ? 0 : undef ),
388
391
                upserted  => [ { index => 1, _id => ignore() } ],
389
392
                op_count  => 2,
390
 
                batch_count => $using_2_6 ? 1 : 2,
 
393
                batch_count => $server_does_bulk ? 1 : 2,
391
394
            ),
392
395
            "result object correct"
393
396
        ) or diag explain $result;
407
410
            $result,
408
411
            MongoDB::WriteResult->new(
409
412
                nMatched    => 1,
410
 
                nModified   => ( $using_2_6 ? 0 : undef ),
 
413
                nModified   => ( $server_does_bulk ? 0 : undef ),
411
414
                op_count    => 2,
412
 
                batch_count => $using_2_6 ? 1 : 2,
 
415
                batch_count => $server_does_bulk ? 1 : 2,
413
416
            ),
414
417
            "result object correct"
415
418
        ) or diag explain $result;
431
434
            $result,
432
435
            MongoDB::WriteResult->new(
433
436
                nMatched    => 2,
434
 
                nModified   => ( $using_2_6 ? 2 : undef ),
 
437
                nModified   => ( $server_does_bulk ? 2 : undef ),
435
438
                op_count    => 1,
436
439
                batch_count => 1,
437
440
            ),
452
455
        #
453
456
        # Using legacy API, the bigstring must be 16MiB - 97 for some reason.
454
457
 
455
 
        my $big_string = "a" x ( 16 * 1024 * 1024 - $using_2_6 ? 41 : 97 );
 
458
        my $big_string = "a" x ( 16 * 1024 * 1024 - $server_does_bulk ? 41 : 97 );
456
459
 
457
460
        my $bulk = $coll->$method;
458
461
        $bulk->find( { key => "1" } )->upsert->update( { '$set' => { x => $big_string } } );
465
468
            $result,
466
469
            MongoDB::WriteResult->new(
467
470
                nUpserted   => 1,
468
 
                nModified   => ( $using_2_6 ? 0 : undef ),
 
471
                nModified   => ( $server_does_bulk ? 0 : undef ),
469
472
                upserted    => [ { index => 0, _id => ignore() } ],
470
473
                op_count    => 1,
471
474
                batch_count => 1,
493
496
            $result,
494
497
            MongoDB::WriteResult->new(
495
498
                nUpserted => 1,
496
 
                nModified => ( $using_2_6 ? 0 : undef ),
 
499
                nModified => ( $server_does_bulk ? 0 : undef ),
497
500
                upserted  => [ { index => 1, _id => ignore() } ],
498
501
                op_count  => 2,
499
 
                batch_count => $using_2_6 ? 1 : 2,
 
502
                batch_count => $server_does_bulk ? 1 : 2,
500
503
            ),
501
504
            "result object correct"
502
505
        ) or diag explain $result;
525
528
            $result,
526
529
            MongoDB::WriteResult->new(
527
530
                nMatched    => 1,
528
 
                nModified   => ( $using_2_6 ? 1 : undef ),
 
531
                nModified   => ( $server_does_bulk ? 1 : undef ),
529
532
                op_count    => 1,
530
533
                batch_count => 1,
531
534
            ),
559
562
            $result,
560
563
            MongoDB::WriteResult->new(
561
564
                nUpserted => 1,
562
 
                nModified => ( $using_2_6 ? 0 : undef ),
 
565
                nModified => ( $server_does_bulk ? 0 : undef ),
563
566
                upserted  => [ { index => 1, _id => ignore() } ],
564
567
                op_count  => 2,
565
 
                batch_count => $using_2_6 ? 1 : 2,
 
568
                batch_count => $server_does_bulk ? 1 : 2,
566
569
            ),
567
570
            "result object correct"
568
571
        ) or diag explain $result;
591
594
            $result,
592
595
            MongoDB::WriteResult->new(
593
596
                nMatched    => 1,
594
 
                nModified   => ( $using_2_6 ? 1 : undef ),
 
597
                nModified   => ( $server_does_bulk ? 1 : undef ),
595
598
                op_count    => 1,
596
599
                batch_count => 1,
597
600
            ),
637
640
            $result,
638
641
            MongoDB::WriteResult->new(
639
642
                nRemoved    => 2,
640
 
                nModified   => ( $using_2_6 ? 0 : undef ),
 
643
                nModified   => ( $server_does_bulk ? 0 : undef ),
641
644
                op_count    => 1,
642
645
                batch_count => 1,
643
646
            ),
662
665
            $result,
663
666
            MongoDB::WriteResult->new(
664
667
                nRemoved    => 1,
665
 
                nModified   => ( $using_2_6 ? 0 : undef ),
 
668
                nModified   => ( $server_does_bulk ? 0 : undef ),
666
669
                op_count    => 1,
667
670
                batch_count => 1,
668
671
            ),
704
707
            $result,
705
708
            MongoDB::WriteResult->new(
706
709
                nRemoved    => 1,
707
 
                nModified   => ( $using_2_6 ? 0 : undef ),
 
710
                nModified   => ( $server_does_bulk ? 0 : undef ),
708
711
                op_count    => 1,
709
712
                batch_count => 1,
710
713
            ),
734
737
        MongoDB::WriteResult->new(
735
738
            nInserted   => 1,
736
739
            nMatched    => 1,
737
 
            nModified   => ( $using_2_6 ? 1 : undef ),
 
740
            nModified   => ( $server_does_bulk ? 1 : undef ),
738
741
            nUpserted   => 1,
739
742
            nRemoved    => 1,
740
743
            op_count    => 4,
741
 
            batch_count => $using_2_6 ? 3 : 4,
 
744
            batch_count => $server_does_bulk ? 3 : 4,
742
745
            # XXX QA Test says index should be 3, but with unordered, that's
743
746
            # not guaranteed, so we ignore the value
744
747
            upserted => [ { index => ignore(), _id => obj_isa("MongoDB::OID") } ],
768
771
            nInserted   => 2,
769
772
            nUpserted   => 1,
770
773
            nMatched    => 1,
771
 
            nModified   => ( $using_2_6 ? 1 : undef ),
 
774
            nModified   => ( $server_does_bulk ? 1 : undef ),
772
775
            nRemoved    => 1,
773
776
            op_count    => 5,
774
 
            batch_count => $using_2_6 ? 4 : 5,
 
777
            batch_count => $server_does_bulk ? 4 : 5,
775
778
            upserted    => [ { index => 2, _id => obj_isa("MongoDB::OID") } ],
776
779
        ),
777
780
        "result object correct"
799
802
 
800
803
    # Check if all ops ran in two batches (unless we're on a legacy server)
801
804
    is( $details->op_count, 6, "op_count" );
802
 
    is( $details->batch_count, $using_2_6 ? 2 : 6, "op_count" );
 
805
    is( $details->batch_count, $server_does_bulk ? 2 : 6, "op_count" );
803
806
 
804
807
    # XXX QA 477 doesn't cover *both* possible orders.  Either the inserts go
805
808
    # first or the upsert/update_ones goes first and different result states
811
814
        is( $details->nUpserted, 1, "nUpserted" );
812
815
        is( $details->nRemoved,  0, "nRemoved" );
813
816
        is( $details->nMatched,  0, "nMatched" );
814
 
        is( $details->nModified, ( $using_2_6 ? 0 : undef ), "nModified" );
 
817
        is( $details->nModified, ( $server_does_bulk ? 0 : undef ), "nModified" );
815
818
        is( $details->count_writeErrors, 3, "writeError count" )
816
819
          or diag explain $details;
817
820
        cmp_deeply( $details->upserted, [ { index => 4, _id => obj_isa("MongoDB::OID") }, ],
823
826
        is( $details->nUpserted, 2, "nUpserted" );
824
827
        is( $details->nRemoved,  0, "nRemoved" );
825
828
        is( $details->nMatched,  1, "nMatched" );
826
 
        is( $details->nModified, ( $using_2_6 ? 0 : undef ), "nModified" );
 
829
        is( $details->nModified, ( $server_does_bulk ? 0 : undef ), "nModified" );
827
830
        is( $details->count_writeErrors, 2, "writeError count" )
828
831
          or diag explain $details;
829
832
        cmp_deeply(
862
865
    is( $details->nUpserted, 0, "nUpserted" );
863
866
    is( $details->nMatched,  0, "nMatched" );
864
867
    is( $details->nRemoved,  0, "nRemoved" );
865
 
    is( $details->nModified, ( $using_2_6 ? 0 : undef ), "nModified" );
 
868
    is( $details->nModified, ( $server_does_bulk ? 0 : undef ), "nModified" );
866
869
    is( $details->nInserted, 1, "nInserted" );
867
870
 
868
871
    # on 2.6+, 4 ops run in two batches; but on legacy, we get an error on
869
872
    # the first update_one, so we only have two ops, still in two batches
870
 
    is( $details->op_count, $using_2_6 ? 4 : 2, "op_count" );
 
873
    is( $details->op_count, $server_does_bulk ? 4 : 2, "op_count" );
871
874
    is( $details->batch_count, 2, "op_count" );
872
875
 
873
876
    is( $details->count_writeErrors,       1,     "writeError count" );
1186
1189
    my $bulk = $coll->initialize_ordered_bulk_op;
1187
1190
    is( $coll->count, 0, "no docs in collection" );
1188
1191
    $bulk->insert( Tie::IxHash->new( _id => 1 ) );
1189
 
    $bulk->insert( Tie::IxHash->new() );
 
1192
    my $doc = Tie::IxHash->new();
 
1193
    $bulk->insert( $doc  );
 
1194
    is( $doc->FETCH('_id'), undef, "inserting _id doesn't modify original" );
1190
1195
    my ( $result, $err );
1191
1196
    $err = exception { $result = $bulk->execute };
1192
1197
    is( $err, undef, "no error on insert" ) or diag explain $err;
1261
1266
 
1262
1267
        # 2.6 doesn't allow changing _id, but previously that's OK, so we try it both ways
1263
1268
        # to ensure we use the right _id from the replace doc on older servers
1264
 
        $bulk->find( { _id => $using_2_6 ? 2 : 3 } )->upsert->replace_one( { _id => 2 } );
 
1269
        $bulk->find( { _id => $server_does_bulk ? 2 : 3 } )->upsert->replace_one( { _id => 2 } );
1265
1270
 
1266
1271
        my ( $result, $err );
1267
1272
        $err = exception { $result = $bulk->execute };
1272
1277
            $result,
1273
1278
            MongoDB::WriteResult->new(
1274
1279
                nUpserted => 3,
1275
 
                nModified => ( $using_2_6 ? 0 : undef ),
 
1280
                nModified => ( $server_does_bulk ? 0 : undef ),
1276
1281
                upserted =>
1277
1282
                  [ { index => 0, _id => 0 }, { index => 1, _id => 1 }, { index => 2, _id => 2 }, ],
1278
1283
                op_count    => 3,
1279
 
                batch_count => $using_2_6 ? 1 : 3,
 
1284
                batch_count => $server_does_bulk ? 1 : 3,
1280
1285
            ),
1281
1286
            "result object correct"
1282
1287
        ) or diag explain $result;