File: read_write_concern_spec.t

package info (click to toggle)
libmongodb-perl 2.2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,292 kB
  • sloc: perl: 14,421; python: 299; makefile: 20; sh: 11
file content (151 lines) | stat: -rw-r--r-- 5,035 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#  Copyright 2015 - present MongoDB, Inc.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

use strict;
use warnings;
use JSON::MaybeXS;
use Path::Tiny 0.058; # sibling
use Test::More 0.88;
use Test::Deep;
use Test::Fatal;
use Safe::Isa;

use lib "t/lib";

use MongoDBTest qw/
    build_client
    get_test_db
    clear_testdbs
    server_version
    skip_unless_mongod
/;
use MongoDB::ReadConcern;
use MongoDB::WriteConcern;

my $read_conn_spec = path(
"t/data/read-write-concern/connection-string/"
)->child("read-concern.json");
my $write_conn_spec = $read_conn_spec->sibling("write-concern.json");
my $read_doc_spec = $read_conn_spec->parent(2)->child("/document/read-concern.json");
my $write_doc_spec = $read_doc_spec->sibling("write-concern.json");

my $plan_rc_conn = _json_parse($read_conn_spec);
my $plan_wc_conn = _json_parse($write_conn_spec);
my $plan_rc_doc = _json_parse($read_doc_spec);
my $plan_wc_doc = _json_parse($write_doc_spec);

subtest "$read_conn_spec connection-string" => sub {
    for my $test ( @{ $plan_rc_conn->{tests} } ) {
        my $rc_level = $test->{readConcern}->{level};
        my $uri = $test->{uri};
        my $description = $test->{description};
        my $conn = build_client( host => $uri );
        is_deeply(
            $conn->read_concern->{level},
            $rc_level,
            "read_concern $description ok"
        );
    }
};

subtest "$read_doc_spec document" => sub {
    for my $test ( @{ $plan_rc_doc->{tests} } ) {
        my $rc_level = $test->{readConcern}->{level};
        my $rc_level_doc = $test->{readConcernDocument}->{level};
        my $description = $test->{description};
        my $rc_obj = MongoDB::ReadConcern->new( $test->{readConcern} )->as_args->[1];
        is_deeply(
            $rc_obj,
            defined $rc_obj ? $test->{readConcernDocument} : undef,
            "read_concern $description ok"
        );
    }
};

subtest "$write_conn_spec connection-string" => sub {
    for my $test ( @{ $plan_wc_conn->{tests} } ) {
        my $uri = $test->{uri};
        my $wc_valid = $test->{valid};
        if ( defined $test->{writeConcern}->{wtimeoutMS} ) {
            $test->{writeConcern}->{wtimeout} = delete $test->{writeConcern}->{wtimeoutMS};
        }
        if ( defined $test->{writeConcern}->{journal} ) {
            $test->{writeConcern}->{j} = bool(delete $test->{writeConcern}->{journal})|obj_isa('boolean');
        }
        my $description = $test->{description};

        subtest $description => sub {
            my (@warnings, $conn);
            local $SIG{__WARN__} = sub { push @warnings, $_[0] };
            # wtimeout will be 1000 if we do not send this
            eval { $conn = MongoDB->connect($uri, {wtimeout => undef}) };
            my $error = $@;
            if ( $wc_valid ) {
                cmp_deeply(
                    $conn->write_concern->as_args->[1] || {},
                    $test->{writeConcern},
                    "write_concern ok"
                );
            } else {
                ok( scalar(@warnings) || $error, "should throw or warn" );
            }
        }
    }
};

subtest "$write_doc_spec document" => sub {
    for my $test ( @{ $plan_wc_doc->{tests} } ) {
        my $wc_valid = $test->{valid};
        $test->{writeConcern}->{wtimeout} = delete $test->{writeConcern}->{wtimeoutMS};
        if ( defined $test->{writeConcern}->{journal} ) {
            $test->{writeConcern}->{j} = delete( $test->{writeConcern}->{journal}) ? 1 : 0;
        }
        if ( defined $test->{writeConcernDocument}->{j} ) {
            $test->{writeConcernDocument}->{j} = bool($test->{writeConcernDocument}->{j})|obj_isa('boolean');
        }
        my $description = $test->{description};

        subtest $description => sub {
            my $wc_obj;
            eval { $wc_obj = MongoDB::WriteConcern->new( $test->{writeConcern} ) };
            my $error = $@;
            if ( $wc_valid ) {
              cmp_deeply(
                  $wc_obj->as_args->[1],
                  defined $wc_obj->as_args->[1] ?
                  $test->{writeConcernDocument} : undef,
                  "write_concern ok"
              );
            } else {
              isa_ok(
                  $error,
                  "MongoDB::UsageError"
              );
            }
        }
    }
};

sub _json_parse {
    my $path = shift;
    my $plan = eval { decode_json( $path->slurp_utf8 ) };
    if ($@) {
        die "Error decoding $path: $@";
    }
    return $plan;
}

clear_testdbs;

done_testing;