File: containers.t

package info (click to toggle)
libmongodb-perl 1.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,004 kB
  • ctags: 2,932
  • sloc: perl: 12,511; ansic: 9,566; makefile: 22
file content (243 lines) | stat: -rw-r--r-- 6,954 bytes parent folder | download
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#
#  Copyright 2015 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 Test::More 0.96;
use Test::Fatal;
use Test::Deep;
use Tie::IxHash;

use MongoDB;
use MongoDB::OID;

use lib 't/lib';
use TestBSON;

my $oid = MongoDB::OID->new("554ce5e4096df3be01323321");
my $bin_oid = pack( "C*", map hex($_), unpack( "(a2)12", "$oid" ) );

my $class = "MongoDB::BSON";

require_ok($class);

my $codec = new_ok( $class, [], "new with no args" );

my @cases = (
    {
        label => "empty doc",
        opts  => {},
        input => [],
        bson  => _doc(""),
    },
    {
        label => "BSON double",
        opts  => {},
        input => [ a => 1.23 ],
        bson  => _doc( BSON_DOUBLE . _ename("a") . _double(1.23) ),
    },
    {
        label => "BSON string",
        opts  => {},
        input => [ a => 'b' ],
        bson  => _doc( BSON_STRING . _ename("a") . _string("b") ),
    },
    {
        label => "BSON OID",
        opts  => {},
        input => [ _id => $oid ],
        bson  => _doc( BSON_OID . _ename("_id") . $bin_oid ),
    },
    {
        label => "add _id",
        opts  => {
            first_key   => '_id',
            first_value => $oid,
        },
        input => [],
        bson  => _doc( BSON_OID . _ename("_id") . $bin_oid ),
    },
    {
        label => "add _id, ignore existing",
        opts  => {
            first_key   => '_id',
            first_value => $oid,
        },
        input => [ _id => "12345" ],
        bson  => _doc( BSON_OID . _ename("_id") . $bin_oid ),
    },
    {
        label => "add _id with null",
        opts  => { first_key => '_id', },
        input => [ _id => "12345" ],
        bson  => _doc( BSON_NULL . _ename("_id") ),
    },
    {
        label => "empty key is error",
        opts  => {},
        input => [ "" => "12345" ],
        error => qr/empty key name/,
    },
    {
        label => "dot in key is normally valid",
        opts  => {},
        input => [ "a.b" => "c" ],
        bson  => _doc( BSON_STRING . _ename("a.b") . _string("c") ),
    },
    {
        label => "dot in key fails invalid check",
        opts  => { invalid_chars => '.' },
        input => [ "a.b" => "c" ],
        error => qr/cannot contain the '\.' character/,
    },
    {
        label => "dot in key fails multi invalid chars",
        opts  => { invalid_chars => '_$' },
        input => [ '$ab' => "c" ],
        error => qr/cannot contain the '\$' character/,
    },
    {
        label => "op_char replacement",
        opts  => { op_char => '-' },
        input => [ '-a' => "c" ],
        bson  => _doc( BSON_STRING . _ename('$a') . _string("c") ),
    },
    {
        label => "op_char change before invalid check",
        opts  => { op_char => '.', invalid_chars => '.' },
        input => [ '.a' => "c" ],
        bson  => _doc( BSON_STRING . _ename('$a') . _string("c") ),
    },
    {
        label => "op_char and invalid check ignore empty string",
        opts  => { op_char => '', invalid_chars => '' },
        input => [ '.a' => "c" ],
        bson  => _doc( BSON_STRING . _ename('.a') . _string("c") ),
    },
    {
        label => "prefer_numeric false",
        opts  => {},
        input => [ a => "1.23" ],
        bson  => _doc( BSON_STRING . _ename("a") . _string("1.23") ),
    },
    {
        label => "prefer_numeric true",
        opts  => { prefer_numeric => 1 },
        input => [ a => "1.23" ],
        bson  => _doc( BSON_DOUBLE . _ename("a") . _double(1.23) ),
    },
    {
        label => "BSON too long",
        opts  => { max_length => 2 },
        input => [ 'a' => 'b' ],
        error => qr/exceeds maximum size 2/,
    },
    {
        label => "BSON too long",
        opts  => {
            invalid_chars  => '.',
            error_callback => sub { die "Bad $_[1]: $_[0]" },
        },
        input => [ 'a.b' => 'b' ],
        error => qr/Bad (?:[A-Za-z:]+=)?\w+\(0x[a-f0-9]+\):.*the '\.' character/,
    },
);

for my $c (@cases) {
    if ( $c->{bson} ) {
        valid_case($c);
    }
    elsif ( $c->{error} ) {
        error_case($c);
    }
    else {
        die "Unknown case type for '$c->{label}'";
    }
}

# have to check one-off as we won't get this via a round-trip
{
    my $bson  = _doc( BSON_STRING . _ename("a") . _string("a"x20) );
    like(
        exception { $codec->decode_one( $bson, { max_length => 5 } ) },
        qr/exceeds maximum size 5/,
        "decode exceeding max_length throws error"
    );
}

# array documents can't have duplicate keys
{
    like(
        exception { $codec->encode_one( [ x => 1, y => 2, z => 3, y => 4 ] ) },
        qr/duplicate key 'y'/,
        "duplicate key in array document is fatal"
    );
}

#--------------------------------------------------------------------------#
# support functions
#--------------------------------------------------------------------------#

sub valid_case {
    my $c = shift;
    my ( $label, $input, $bson, $opts ) = @{$c}{qw/label input bson opts/};
    my ( $doc, $got );
    subtest $label => sub {
        # hash style
        $doc = {@$input};
        $got = $codec->encode_one( $doc, $opts );
        is_bin( $got, $bson, "encode_one( HASH )" );
        cmp_deeply( $doc, {@$input}, "doc unmodified" );

        # array style
        $doc = [@$input];
        $got = $codec->encode_one( $doc, $opts );
        is_bin( $got, $bson, "encode_one( ARRAY )" );
        cmp_deeply( $doc, [@$input], "doc unmodified" );

        # IxHash
        $doc = Tie::IxHash->new(@$input);
        $got = $codec->encode_one( $doc, $opts );
        is_bin( $got, $bson, "encode_one( IxHash )" );
        cmp_deeply( $doc, Tie::IxHash->new(@$input), "doc unmodified" );
    };

}

sub error_case {
    my $c = shift;
    my ( $label, $input, $error, $opts ) = @{$c}{qw/label input error opts/};
    my ( $doc, $got );
    subtest $label => sub {
        # hash style
        $doc = {@$input};
        like( exception { $got = $codec->encode_one( $doc, $opts ) },
            $error, "exception for HASH" );

        # array style
        $doc = [@$input];
        like( exception { $got = $codec->encode_one( $doc, $opts ) },
            $error, "exception for ARRAY" );

        # IxHash
        $doc = Tie::IxHash->new(@$input);
        like( exception { $got = $codec->encode_one( $doc, $opts ) },
            $error, "exception for Tie::IxHash" );
    };
}

done_testing;

# vim: ts=4 sts=4 sw=4 et: