Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.

Commit cdf16bc

Browse files
phaylonxdg
authored andcommitted
PERL-809 Added t/examples/changestream.t
1 parent cf19d12 commit cdf16bc

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

t/examples/changestream.t

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#
2+
# Copyright 2018 MongoDB, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
# MongoDB documentation examples in Perl.
18+
19+
# NOTE: Developers: Do not change these examples without approval of the
20+
# MongoDB documentation team as they are extracted to populate examples
21+
# on the MongoDB docs website.
22+
#
23+
# Examples use `$db->coll("inventory")` to parallel the shell examples, which
24+
# use `db.inventory`. Testing commands use a `$coll` variable for more
25+
# idiomatic brevity.
26+
27+
use strict;
28+
use warnings;
29+
use Test::More 0.96;
30+
31+
use MongoDB;
32+
use Tie::IxHash;
33+
use boolean;
34+
35+
use lib "t/lib";
36+
use MongoDBTest qw/
37+
skip_unless_mongod build_client get_test_db
38+
server_version server_type
39+
/;
40+
41+
skip_unless_mongod();
42+
43+
my $conn = build_client();
44+
my $db = get_test_db($conn);
45+
my $coll = $db->coll("inventory");
46+
my $server_version = server_version($conn);
47+
my $server_type = server_type($conn);
48+
my $cursor;
49+
50+
# We want to show the examples without showing the inserts/updates
51+
# that would make them work in reality and we don't have threads to
52+
# do the work concurrently. Therefore, we show the example and then
53+
# repeat the work with database operations and tests intermingled.
54+
55+
#<<< No perltidy
56+
57+
subtest "change streams" => sub {
58+
plan skip_all => '$currentDate operator requires MongoDB 3.6+'
59+
unless $server_version >= v3.6.0;
60+
plan skip_all => 'Change Streams require replica set'
61+
unless $server_type eq 'RSPrimary';
62+
63+
my $document;
64+
my $resume_token;
65+
my @pipeline;
66+
67+
# Initialize the database
68+
$db->coll('warmup')->insert_one({});
69+
70+
# Start Changestream Example 1
71+
$cursor = $db->coll('inventory')->watch();
72+
$document = $cursor->next;
73+
# End Changestream Example 1
74+
75+
is $document, undef, 'no changes after example 1';
76+
$coll->insert_one({ username => 'alice' });
77+
$document = $cursor->next;
78+
is $document->{fullDocument}{username}, 'alice',
79+
'found change inserted after example 1';
80+
81+
# Start Changestream Example 2
82+
$cursor = $db->coll('inventory')->watch(
83+
[],
84+
{ fullDocument => 'updateLookup' },
85+
);
86+
$document = $cursor->next;
87+
# End Changestream Example 2
88+
89+
is $document, undef, 'no changes after example 2';
90+
$coll->update_one(
91+
{ username => 'alice' },
92+
{ '$set' => { updated => 1 } },
93+
);
94+
$document = $cursor->next;
95+
is $document->{fullDocument}{username}, 'alice',
96+
'found change made after example 2';
97+
98+
# Start Changestream Example 3
99+
$resume_token = $document->{_id};
100+
$cursor = $db->coll('inventory')->watch(
101+
[],
102+
{ resumeAfter => $resume_token },
103+
);
104+
$document = $cursor->next;
105+
# End Changestream Example 3
106+
107+
is $document, undef, 'no changes after example 3';
108+
$coll->update_one(
109+
{ username => 'alice' },
110+
{ '$set' => { updated => 2 } },
111+
);
112+
$document = $cursor->next;
113+
ok $document, 'found change made after example 3';
114+
115+
# Start Changestream Example 4
116+
@pipeline = (
117+
{ '$match' => {
118+
'$or' => [
119+
{ 'fullDocument.username' => 'alice' },
120+
{ 'operationType' => { '$in' => ['delete'] } },
121+
],
122+
} },
123+
);
124+
$cursor = $db->coll('inventory')->watch(\@pipeline);
125+
$document = $cursor->next;
126+
# End Changestream Example 4
127+
128+
is $document, undef, 'no changes after example 4';
129+
$coll->delete_one({ username => 'alice' });
130+
$document = $cursor->next;
131+
ok $document, 'found change made after example 4';
132+
133+
$coll->drop;
134+
};
135+
136+
#>>> no perltidy
137+
138+
done_testing;

t/testrules.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ seq:
44
- seq: t/max_time_ms.t
55
- seq: t/using_profiler.t
66
- seq: t/changestreams.t
7+
- seq: t/examples/changestream.t
78
- par: **

0 commit comments

Comments
 (0)