/
300-Collection.t
175 lines (137 loc) · 4.89 KB
/
300-Collection.t
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
use v6;
use lib 't';
use Test;
use Test-support;
use MongoDB;
use MongoDB::Database;
use MongoDB::Client;
use BSON::Document;
#------------------------------------------------------------------------------
drop-send-to('mongodb');
drop-send-to('screen');
#modify-send-to( 'screen', :level(MongoDB::MdbLoglevels::Trace));
info-message("Test $?FILE start");
my MongoDB::Test-support $ts .= new;
my @serverkeys = $ts.serverkeys.sort;
# single server tests => one server key
my Hash $clients = $ts.create-clients;
my MongoDB::Client $client = $clients{$clients.keys[0]};
my MongoDB::Database $database = $client.database('test');
my $p1 = $ts.server-control.get-port-number(@serverkeys[0]);
my Str $server-version = $client.server-version("localhost:$p1");
diag "Running tests for server version $server-version";
# Create collection and insert data in it!
my MongoDB::Collection $collection = $database.collection('cl1');
isa-ok( $collection, 'MongoDB::Collection');
my BSON::Document $req;
my BSON::Document $doc;
my MongoDB::Cursor $cursor;
$database.run-command: (drop => $collection.name,);
#------------------------------------------------------------------------------
subtest "Several inserts", {
# Add records
$req .= new: (
insert => $collection.name,
documents => [
( name => 'Jan Klaassen'), ( name => 'Piet B'),
( name => 'Me T'), ( :name('Di D'))
]
);
$doc = $database.run-command($req);
is $doc<ok>, 1, "Result is ok";
is $doc<n>, 4, "Inserted 4 documents";
$req .= new: ( count => $collection.name, query => (name => 'Me T',));
$doc = $database.run-command($req);
is $doc<ok>, 1, "count request ok";
is $doc<n>, 1, 'count 1 record of "Me T"';
$req<query> = (name => 'Di D',);
$doc = $database.run-command($req);
is $doc<n>, 1, 'count 1 record of "Di D"';
$req<query> = (name => 'Di D',);
$doc = $database.run-command($req);
is $doc<n>, 1, '1 record of "Jan Klaassen"';
# Add next few records
$req .= new: (
insert => $collection.name,
documents => [
(:name('n1'), :test(0)), (:name('n2'), :test(0)),
(:name('n3'), :test(0)), (:name('n4'), :test(0)),
(:name('n5'), :test(0)), (:name('n6'), :test(0))
]
);
$doc = $database.run-command($req);
is $doc<ok>, 1, "Result is ok";
is $doc<n>, 6, "Inserted 6 documents";
$req .= new: ( :count($collection.name), :query((:test(0),)));
$doc = $database.run-command($req);
is $doc<n>, 6, '6 records of Test(0)';
$req .= new: ( :count($collection.name), :query((:name('Di D'),)));
$doc = $database.run-command($req);
is $doc<n>, 1, "1 record of name('Di D')";
};
#------------------------------------------------------------------------------
# Drop current collection twice
subtest 'drop collection two times', {
$req .= new: (drop => $collection.name);
$doc = $database.run-command($req);
ok $doc<ok>.Bool == True, 'Dropping cl1 ok';
#say $doc.perl;
is $doc<ns>, 'test.cl1', 'Dropped collection';
is $doc<nIndexesWas>, 1, 'Number of dropped indexes';
# Do it a second time
try {
$doc = $database.run-command($req);
CATCH {
when X::MongoDB {
ok $_.message ~~ m/ns \s+ not \s* found/, 'Collection cl1 not found';
}
}
}
};
#------------------------------------------------------------------------------
$client.cleanup;
done-testing();
=finish
#------------------------------------------------------------------------------
# Create using illegal collection name
try {
$database.create-collection('abc-def and a space');
CATCH {
when X::MongoDB {
ok $_.message ~~ m/Illegal \s* collection \s* name/, 'Illegal collection name';
}
}
}
#------------------------------------------------------------------------------
# Drop collection and create one explicitly with some parameters
#$collection.drop;
$database.create-collection( 'cl1', :capped, :size(1000));
# Fill collection with 100 records. Should be too much.
for ^200 -> $i, $j {
my %d = %( code1 => 'd' ~ $i, code2 => 'n' ~ (100 - $j));
$collection.insert(%d);
}
# Find all documents
my MongoDB::Cursor $cursor = $collection.find();
isnt $cursor.count, 100, 'Less than 100 records in collection';
#------------------------------------------------------------------------------
# Drop collection and create one explicitly with other parameters
$collection.drop;
$database.create-collection( 'cl1', :capped, :size(1000), :max(10));
# Fill collection with 100 records. Should be too much.
for ^200 -> $i, $j {
my %d = %( code1 => 'd' ~ $i, code2 => 'n' ~ (100 - $j));
$collection.insert(%d);
}
# Find all documents
$cursor = $collection.find();
is $cursor.count, 10, 'Only 10 records in collection';
#------------------------------------------------------------------------------
# Cleanup
$req .= new: ( dropDatabase => 1 );
$doc = $database.run-command($req);
say $doc.perl;
info-message("Test $?FILE stop");
sleep .2;
drop-all-send-to();
done-testing();