/
467-HLCollection.t
140 lines (116 loc) · 3.18 KB
/
467-HLCollection.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
use v6.c;
use Test;
use MongoDB;
use MongoDB::HL::Collection;
use BSON::Document;
#-------------------------------------------------------------------------------
drop-send-to('mongodb');
drop-send-to('screen');
#modify-send-to( 'screen', :level(* >= MongoDB::MdbLoglevels::Debug));
info-message("Test $?FILE start");
my MongoDB::HL::Collection $table = collection-object(
:uri<mongodb://:65010>,
:db-name<contacts>,
:cl-name<address>,
:schema( BSON::Document.new: (
street => [ True, Str],
number => [ True, Int],
number-mod => [ False, Str],
city => [ True, Str],
zip => [ False, Str],
state => [ False, Str],
country => [ True, Str],
)
)
);
#-------------------------------------------------------------------------------
subtest {
my Int $count = 4304;
# Insert enaugh records
my Array $r = [];
$r.push: %(
street => 'Jan Gestelsteeg',
number => $count++,
number-mod => 'zwart',
country => 'Nederland',
zip => '1043 XY',
city => 'Lutjebroek',
state => 'Gelderland',
);
for ^10 {
$r.push: %(
street => 'Jan Gestelsteeg',
number => $count++,
number-mod => 'zwart',
country => 'Nederland',
zip => '1043 XY',
city => 'Lutjebroek',
state => 'Gelderland',
);
}
my BSON::Document $doc = $table.insert(:inserts($r));
ok $doc<ok>, 'Write ok';
is $doc<n>, 11, '11 docs written';
$doc = $table.count( :criteria(%( number => ( '$gt' => 4307),)));
ok $doc<ok>, 'Count ok';
is $doc<n>, 7, '7 records counted';
my $n = 1;
$doc = $table.read( :criteria(%( number => ( '$gt' => 4307),)));
while $table.read-next { $n++; }
is $n, 7, '7 records read';
}, 'read test';
#-------------------------------------------------------------------------------
subtest {
my BSON::Document $doc = $table.update(
:updates( [ (
q => ( number => 4307, ),
u => ( '$inc' => ( number => 1, )),
), (
q => ( number => 4304, ),
u => ( '$inc' => ( number => -1, )),
)
]
)
);
is $doc<nModified>, 2, '2 records modified';
say $doc.perl;
$doc = $table.count( :criteria(%( number => 4307,)));
is $doc<n>, 0, '0 records with original number';
$doc = $table.count( :criteria(%( number => 4308,)));
is $doc<n>, 2, '2 records with same number';
$doc = $table.replace(
:replaces( [ (
q => ( number => 4306, ),
r => %(
street => 'Jan Gestelsteeg',
number => 8000,
number-mod => 'zwart',
country => 'Nederland',
zip => '1043 XY',
city => 'Lutjebroek',
state => 'Gelderland',
),
),
]
)
);
is $doc<nModified>, 1, '1 record modified';
say $doc;
$doc = $table.count( :criteria(%( number => 8000,)));
is $doc<n>, 1, '1 record with new number';
say $doc.perl;
}, 'update test';
#-------------------------------------------------------------------------------
# Cleanup
#
# delete data
my BSON::Document $doc = $table.delete(
:deletes( [
( :q(number => ('$gt' => 4300)), :!limit),
]
),
:!ordered
);
#say $doc.perl;
info-message("Test $?FILE end");
done-testing;