File Coverage

File:t/11-limits.t
Coverage:100.0%

linestmtbrancondsubpodtimecode
1
1
1
1
31
4
17
use Test::More 'no_plan';
2#use Smart::Comments;
3
1
1
1
15
4
11
use lib 'lib';
4
1
1
1
15
4
15
use JSON::Syck 'Dump';
5
1
1
1
15
4
11
use OpenAPI::Limits;
6
1
1
1
17
5
15
use t::OpenAPI;
7
1
1
1
1
1
1
1
1
1
9
3
7
9
2
10
12
6
20
use WWW::OpenAPI;
8
1
1
1
12
4
16
use OpenAPI::Config;
9
10
1
11
OpenAPI::Config->init;
11
1
46
my $host = $t::OpenAPI::host;
12
1
23
my $client = WWW::OpenAPI->new( { server => $host } );
13
1
8
my $res = $client->delete('/=/model?user=peee&password=4423037&use_cookie=1');
14
1
9
ok $res->is_success, 'response OK';
15
16
1
1482
for (1..$MODEL_LIMIT + 1) {
17
41
916
    my $model_name = 'Foo'.$_;
18
41
292
    my $url = '/=/model/'.$model_name;
19    ### $url
20
41
215
    my $body = '{description:"blah",columns:[{name:"title",label:"title"}]}';
21
41
459
    my $res = $client->post($body, $url);
22
41
323
    ok $res->is_success, '1..' . $MODEL_LIMIT . ' OK';
23
41
33648
    my $res_body = $res->content;
24    ### $res_body
25
41
2640
    if ($_ <= $MODEL_LIMIT) {
26
40
454
        is $res_body, '{"success":1}'."\n", "Model limit test ".$_;
27    } else {
28
1
21
        is $res_body, '{"success":0,"error":"Exceeded model count limit '
29            .$MODEL_LIMIT.'."}'."\n", "Model limit test ".$_;
30    }
31}
32
33
1
30
$res = $client->delete('/=/model');
34
1
3
ok $res->is_success, 'response OK';
35
36# column limit test
37
1
922
my @cols;
38
1
16
my $data = { description => 'blah', columns => \@cols };
39
1
19
for (1..$COLUMN_LIMIT - 1) {
40
39
565
    push @cols, { name => "Foo$_", label => 'abc' };
41}
42
43## 1. create a mode first (column number: $COLUMN_LIMIT - 1)
44
1
5
my $url = '/=/model/foos';
45
1
543
my $body = Dump($data);
46
1
14
$res = $client->post($body, $url);
47
1
3
ok $res->is_success, 'Create model okay';
48
1
946
is $res->content(), '{"success":1}'."\n";
49
50
1
766
my @cols_bak = @cols;
51
52## 2. step to exceed the column limit using add method
53
1
18
for ($COLUMN_LIMIT..$COLUMN_LIMIT + 1) {
54
2
788
    my $col_name = 'Foo'.$_;
55
56
2
23
    my $url_local = $url.'/'.$col_name;
57
2
24
    $body = '{label:"'.$col_name.'"}';
58
59
2
29
    $res = $client->post($body, $url_local);
60
2
4
    ok $res->is_success, $COLUMN_LIMIT . ' OK';
61
2
1865
    my $res_body = $res->content;
62    ### $res_body
63
2
156
    if ($_ <= $COLUMN_LIMIT) {
64
1
22
        is $res_body, '{"success":1,"src":"/=/model/foos/Foo'.$_.'"}'."\n", "Model column limit test - add column FOO".$_;
65    } else {
66
1
21
        is $res_body, '{"success":0,"error":"Exceeded model column count limit: '.$COLUMN_LIMIT.'."}'."\n", "Model column limit test ".$_;
67    }
68}
69
70## 3. post a mode which number of columns is $COLUMN_LIMIT
71
1
792
@cols = @cols_bak;
72
1
15
for ($COLUMN_LIMIT..$COLUMN_LIMIT + 2) {
73
3
1416
    my $col_name = 'Foo'.$_;
74
3
48
    push @cols, { name => $col_name, label => $col_name };
75
76
3
45
    my $data = { description => 'blah', columns => \@cols };
77
3
1372
    my $body = Dump($data);
78    ### $body
79
80    #my $url = $host.$url;
81    #die $url;
82
3
30
    $res = $client->delete($url);
83
3
10
    $res = $client->post($body, $url);
84
3
6
    ok $res->is_success, $COLUMN_LIMIT . ' OK';
85
3
2474
    my $res_body = $res->content;
86    ### $res_body
87
3
197
    if ($_ <= $COLUMN_LIMIT) {
88
1
13
        is $res_body, '{"success":1}'."\n", "Model column limit test ".$_;
89    } else {
90
2
36
        is $res_body, '{"success":0,"error":"Exceeded model column count limit: '.$COLUMN_LIMIT.'."}'."\n", "Model column limit test ". $_;
91    }
92}
93
94
95# We need a faster way to test $RECORD_LIMIT because it's normally huge...
96
97# record limit test
98## 1. delete the 'foo' model first
99
1
688
$res = $client->delete($url);
100
101## 2. create it
102
1
2
$body = '{description:"blah",columns:[{name:"title",label:"title"}]}';
103
1
20
$res = $client->post($body, $url);
104### $res
105
106## 3. insert $RECORD_LIMIT - 1 records
107
1
2
for (1..$RECORD_LIMIT - 1) {
108
199
13870
    $body = '{title:'.($RECORD_LIMIT-1).'}';
109    ### $body
110
199
2589
    $res = $client->post($body, $url.'/~/~');
111
199
508
    ok $res->is_success, $COLUMN_LIMIT . ' OK';
112
199
173517
    my $res_body = $res->content;
113    ### $res_body
114}
115
116## 4. add 2 more records
117
1
117
for ($RECORD_LIMIT..$RECORD_LIMIT + 1) {
118
2
922
    $body = '{title:'.($RECORD_LIMIT-1).'}';
119
120    ### $body
121
2
37
    $res = $client->post($body, $url.'/~/~');
122
2
6
    ok $res->is_success, $RECORD_LIMIT . ' OK';
123
2
2134
    my $res_body = $res->content;
124    ### $res_body
125
2
177
    if ($_ <= $RECORD_LIMIT) {
126
1
26
        is $res_body, '{"success":1,"rows_affected":1,"last_row":"/=/model/foos/id/'.$RECORD_LIMIT.'"}'."\n", "Model record limit test ".$_;
127    } else {
128
1
26
        is $res_body, '{"success":0,"error":"Exceeded model row count limit: '.$RECORD_LIMIT.'."}'."\n", "Model record limit test ".$_;
129    }
130}
131
132# insert limit test: the maximum records in a session
133
1
913
for ($INSERT_LIMIT..$INSERT_LIMIT + 1) {
134
135    ## 1. delete the 'foo' model first, then create it
136
2
950
    $res = $client->delete($url);
137
2
6
    $body = '{description:"blah",columns:[{name:"title",label:"title"}]}';
138
2
44
    $res = $client->post($body, $url);
139
2
5
    ok $res->is_success, 'Create model okay';
140
141    ## 2. insert some records in a session
142
2
1880
        my @recs;
143
2
24
        for (1..$_) {
144
41
609
        push @recs, { title => "Foo$_" };
145        }
146
2
390
    $body = Dump(\@recs);
147
2
25
    $res = $client->post($body, $url.'/~/~');
148
2
5
    ok $res->is_success, $INSERT_LIMIT . ' OK';
149
2
1895
    my $res_body = $res->content;
150    ### $res_body
151
2
156
    if ($_ <= $INSERT_LIMIT) {
152
1
32
        is $res_body, '{"success":1,"rows_affected":'.$_.',"last_row":"/=/model/foos/id/'.$_.'"}'."\n", "Model insert limit test - insert records number $_ once";
153    } else {
154
1
20
        is $res_body, '{"success":0,"error":"You can only insert '.$INSERT_LIMIT.' rows at a time."}'."\n", "Model insert limit test ".$INSERT_LIMIT;
155    }
156}
157
158# post length limit test
159## new a data exceed the post length limit
160
1
1006
$data = 'a' x ($POST_LEN_LIMIT + 1);
161## delete the 'foo' model first, then create it
162
1
13
$res = $client->delete($url);
163
1
3
$body = '{description:"blah",columns:[{name:"title",label:"title"}]}';
164
1
19
$res = $client->post($data, $url);
165
1
2
ok $res->is_success, 'Create model okay';
166
167
1
963
$body = $data;
168
1
17
$res = $client->post($body, $url.'/~/~');
169
1
2
ok $res->is_success, $POST_LEN_LIMIT . ' OK';
170
1
832
my $res_body = $res->content;
171### $res_body
172
1
77
is $res_body, '{"success":0,"error":"Exceeded POST content length limit: '.$POST_LEN_LIMIT.'"}'."\n", "Model post limit test ".$POST_LEN_LIMIT;
173
174# put length limit test
175## new a data exceed the post length limit
176
1
902
$data = 'a' x ($POST_LEN_LIMIT + 1);
177## delete the 'foo' model first, then create it
178
1
9
$res = $client->delete($url);
179
1
3
$body = '{description:"blah",columns:[{name:"title",label:"title"}]}';
180
1
25
$res = $client->put($body, $url);
181
1
2
ok $res->is_success, $POST_LEN_LIMIT .'init model okay';
182
183## insert a record
184
1
820
$body = '{ title: "abc" }';
185
1
16
$res = $client->post($body, $url.'/~/~');
186
1
2
ok $res->is_success, $POST_LEN_LIMIT . 'insert a short record OK';
187
1
827
$res_body = $res->content;
188### $res_body
189
190
1
341
$data = 'a' x ($POST_LEN_LIMIT + 1);
191#$body = '{title:'.($data).'}';
192
1
14
$res = $client->put($data, $url.'/id/1');
193
1
3
ok $res->is_success, $POST_LEN_LIMIT . 'update OK';
194
1
1067
$res_body = $res->content;
195### $res_body
196
1
101
is $res_body, '{"success":0,"error":"Exceeded PUT content length limit: '.$POST_LEN_LIMIT.'"}'."\n", "Model put limit test ".$POST_LEN_LIMIT;
197
198# max select records in a request
199## delete the 'foo' model first, then create it
200
1
903
$res = $client->delete($url);
201
1
3
$body = '{description:"blah",columns:[{name:"title",label:"title"}]}';
202
1
33
$res = $client->post($body, $url);
203
1
3
ok $res->is_success, 'Create model okay';
204
205## insert MAX_SELECT_LIMIT + 1 records
206
1
1056
my @recs;
207
1
17
for (1..$MAX_SELECT_LIMIT + 1) {
208
501
5200
    push @recs, { title => "Foo$_" };
209}
210
1
3312
$body = Dump(\@recs);
211
1
15
$res = $client->post($body, $url.'/~/~');
212
1
3
ok $res->is_success, $INSERT_LIMIT . ' OK';
213
1
1084
$res_body = $res->content;
214### $res_body
215
216## request MAX_SELECT_LIMIT + 1 records
217
1
102
$res = $client->get($url.'/~/~?count='.($MAX_SELECT_LIMIT + 1));
218
1
3
ok $res->is_success, 'select OK';
219
1
1057
$res_body = $res->content;
220### $res_body
221
1
119
is $res_body, '{"success":0,"error":"Value too large for the limit param: '.($MAX_SELECT_LIMIT + 1).'"}'."\n", "Model select limit test ".$MAX_SELECT_LIMIT;
222
223
1
903
$res = $client->delete('/=/model');
224
1
3
ok $res->is_success, 'response OK';
225