| 1 | package XAO::testcases::FS::collection; | ||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 1 | ▶ | use Error qw(:try); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use XAO::Utils; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use XAO::Objects; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | ▶ | use base qw(XAO::testcases::FS::base); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | # It used to return a damaged object | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | sub test_am_20031029_wrong_id { | ||||||||||||||||||||
| 12 | 0 | ▶ | my $self=shift; | ||||||||||||||||||
|
Subroutine
test_am_20031029_wrong_id: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 13 | 0 | my $odb=$self->get_odb; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 0 | my $coll=$odb->collection(class => 'Data::Customer'); | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 0 | my $c; | |||||||||||||||||||
| 18 | try { | ||||||||||||||||||||
| 19 | 0 | ▶ | $c=$coll->get(9876543); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 20 | } | ||||||||||||||||||||
| 21 | otherwise { | ||||||||||||||||||||
| 22 | 0 | ▶ | my $e=shift; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 23 | 0 | dprint "Expected error: $e"; | |||||||||||||||||||
| 24 | 0 | }; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 0 | $self->assert(!$c, | |||||||||||||||||||
| 27 | "Returned non-existing object"); | ||||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | ## | ||||||||||||||||||||
| 31 | # my $ingredients = $odb->collection('class' => 'Data::Ingredient') ; | ||||||||||||||||||||
| 32 | # my $result = $ingredients->search( | ||||||||||||||||||||
| 33 | # { orderby => ['ascend' => 'name'], | ||||||||||||||||||||
| 34 | # distinct => 'name' } | ||||||||||||||||||||
| 35 | # ) ; | ||||||||||||||||||||
| 36 | # | ||||||||||||||||||||
| 37 | # ... doesn't seem to work correctly. It returns a list containing a | ||||||||||||||||||||
| 38 | # large list of identical keys, like: | ||||||||||||||||||||
| 39 | # | ||||||||||||||||||||
| 40 | # [ 5589, 5589, 5589, 5589 ... ] | ||||||||||||||||||||
| 41 | # | ||||||||||||||||||||
| 42 | sub test_bild_20030611_distinct { | ||||||||||||||||||||
| 43 | 0 | ▶ | my $self=shift; | ||||||||||||||||||
|
Subroutine
test_bild_20030611_distinct: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||
| 44 | 0 | my $odb=$self->get_odb; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 0 | my $c1=$odb->fetch('/Customers/c1'); | |||||||||||||||||||
| 47 | 0 | $c1->build_structure( | |||||||||||||||||||
| 48 | Orders => { | ||||||||||||||||||||
| 49 | type => 'list', | ||||||||||||||||||||
| 50 | class => 'Data::Order', | ||||||||||||||||||||
| 51 | key => 'order_id', | ||||||||||||||||||||
| 52 | structure => { | ||||||||||||||||||||
| 53 | name => { | ||||||||||||||||||||
| 54 | type => 'text', | ||||||||||||||||||||
| 55 | maxlength => 100, | ||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | }, | ||||||||||||||||||||
| 58 | }, | ||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 0 | my $orders1=$c1->get('Orders'); | |||||||||||||||||||
| 62 | 0 | my $orders2=$odb->fetch('/Customers/c2/Orders'); | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 0 | my $nc=$orders1->get_new; | |||||||||||||||||||
| 65 | 0 | $nc->put(name => 'test1'); | |||||||||||||||||||
| 66 | 0 | $orders1->put(test1_1_1 => $nc); | |||||||||||||||||||
| 67 | 0 | $orders1->put(test1_2_1 => $nc); | |||||||||||||||||||
| 68 | 0 | $orders1->put(test1_3_1 => $nc); | |||||||||||||||||||
| 69 | 0 | $orders2->put(test1_1_2 => $nc); | |||||||||||||||||||
| 70 | 0 | $orders2->put(test1_2_2 => $nc); | |||||||||||||||||||
| 71 | 0 | $nc->put(name => 'test2'); | |||||||||||||||||||
| 72 | 0 | $orders1->put(test2_1_1 => $nc); | |||||||||||||||||||
| 73 | 0 | $orders1->put(test2_2_1 => $nc); | |||||||||||||||||||
| 74 | 0 | $orders1->put(test2_3_1 => $nc); | |||||||||||||||||||
| 75 | 0 | $orders2->put(test2_1_2 => $nc); | |||||||||||||||||||
| 76 | 0 | $orders2->put(test2_2_2 => $nc); | |||||||||||||||||||
| 77 | 0 | $nc->put(name => 'test3'); | |||||||||||||||||||
| 78 | 0 | $orders1->put(test3_1_1 => $nc); | |||||||||||||||||||
| 79 | 0 | $nc->put(name => 'test4'); | |||||||||||||||||||
| 80 | 0 | $orders2->put(test4_1_2 => $nc); | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | my $coll=$odb->collection(class => 'Data::Order'); | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | my $sr=$coll->search({ orderby => [ ascend => 'name' ], | |||||||||||||||||||
| 85 | distinct => 'name' | ||||||||||||||||||||
| 86 | }); | ||||||||||||||||||||
| 87 | 0 | my $t1c=0; | |||||||||||||||||||
| 88 | 0 | my $t2c=0; | |||||||||||||||||||
| 89 | 0 | my $t3c=0; | |||||||||||||||||||
| 90 | 0 | my $t4c=0; | |||||||||||||||||||
| 91 | 0 | foreach my $id (@$sr) { | |||||||||||||||||||
| 92 | 0 | my $cust_id=$coll->get($id)->container_key; | |||||||||||||||||||
| 93 | 0 | ▶ | $t1c++ if $cust_id =~ /test1_/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 0 | ▶ | $t2c++ if $cust_id =~ /test2_/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 0 | ▶ | $t3c++ if $cust_id =~ /test3_/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 0 | ▶ | $t4c++ if $cust_id =~ /test4_/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | dprint "$id $cust_id"; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | 0 | $self->assert($t1c == 1, | |||||||||||||||||||
| 100 | "Expected one occurance of test1, got $t1c"); | ||||||||||||||||||||
| 101 | 0 | $self->assert($t2c == 1, | |||||||||||||||||||
| 102 | "Expected one occurance of test2, got $t2c"); | ||||||||||||||||||||
| 103 | 0 | $self->assert($t3c == 1, | |||||||||||||||||||
| 104 | "Expected one occurance of test3, got $t3c"); | ||||||||||||||||||||
| 105 | 0 | $self->assert($t4c == 1, | |||||||||||||||||||
| 106 | "Expected one occurance of test4, got $t4c"); | ||||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | ## | ||||||||||||||||||||
| 110 | # This is a testcase for a bug reported by Bil on 12/17/2002. It allows | ||||||||||||||||||||
| 111 | # collection to get some sort of read-only clone object by passing array | ||||||||||||||||||||
| 112 | # reference to collection get() method. Should throw an error instead! | ||||||||||||||||||||
| 113 | # | ||||||||||||||||||||
| 114 | sub test_bild_20021217 { | ||||||||||||||||||||
| 115 | 0 | ▶ | my $self=shift; | ||||||||||||||||||
|
Subroutine
test_bild_20021217: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 116 | 0 | my $odb=$self->get_odb(); | |||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 0 | my $clist=$odb->collection(class => 'Data::Customer'); | |||||||||||||||||||
| 119 | 0 | $self->assert(ref($clist), | |||||||||||||||||||
| 120 | "Can't create a collection"); | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 0 | my $sr=$clist->search('customer_id','eq','c1'); | |||||||||||||||||||
| 123 | 0 | $self->assert(@$sr==1, | |||||||||||||||||||
| 124 | "Should have got a single value"); | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 0 | my $pass; | |||||||||||||||||||
| 127 | try { | ||||||||||||||||||||
| 128 | 0 | ▶ | my $c=$clist->get($sr); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 129 | 0 | $pass=0; | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | otherwise { | ||||||||||||||||||||
| 132 | 0 | ▶ | my $e=shift; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 133 | ## dprint "Expected error: $e"; | ||||||||||||||||||||
| 134 | 0 | $pass=1; | |||||||||||||||||||
| 135 | 0 | }; | |||||||||||||||||||
| 136 | 0 | $self->assert($pass, | |||||||||||||||||||
| 137 | "Managed to get an object by passing array reference to get()"); | ||||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | ############################################################################### | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | sub test_describe { | ||||||||||||||||||||
| 143 | 0 | ▶ | my $self=shift; | ||||||||||||||||||
|
Subroutine
test_describe: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 0 | my $odb = $self->{odb}; | |||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 0 | my $list=$odb->collection(class => 'Data::Customer'); | |||||||||||||||||||
| 148 | 0 | $self->assert($list, "Can't create Data::Customer collection"); | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 0 | $self->assert(defined($list->can('describe')), | |||||||||||||||||||
| 151 | "Can't call function 'describe()' on the Collection object"); | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 0 | my $desc=$list->describe; | |||||||||||||||||||
| 154 | 0 | $self->assert(ref($desc) eq 'HASH', | |||||||||||||||||||
| 155 | "Collection description is not a hash reference"); | ||||||||||||||||||||
| 156 | 0 | $self->assert($desc->{type} eq 'collection', | |||||||||||||||||||
| 157 | "Type is not 'collection'"); | ||||||||||||||||||||
| 158 | 0 | $self->assert($desc->{class} eq 'Data::Customer', | |||||||||||||||||||
| 159 | "Class is not 'Data::Customer'"); | ||||||||||||||||||||
| 160 | 0 | $self->assert($desc->{key} => 'customer_id', | |||||||||||||||||||
| 161 | "Key is not 'customer_id'"); | ||||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | ############################################################################### | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | sub test_everything { | ||||||||||||||||||||
| 167 | 0 | ▶ | my $self=shift; | ||||||||||||||||||
|
Subroutine
test_everything: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 168 | 0 | my $odb=$self->get_odb(); | |||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 0 | my $clist=$odb->collection(class => 'Data::Customer'); | |||||||||||||||||||
| 171 | 0 | $self->assert(ref($clist), | |||||||||||||||||||
| 172 | "Can't create a collection"); | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 0 | $self->assert($clist->objtype eq 'Collection', | |||||||||||||||||||
| 175 | "Objtype() is not 'Collection'"); | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 0 | $self->assert($clist->objname eq 'FS::Collection', | |||||||||||||||||||
| 178 | "Objname() is not 'FS::Collection'"); | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 0 | my @kk=$clist->keys; | |||||||||||||||||||
| 181 | 0 | $self->assert(@kk == 2, | |||||||||||||||||||
| 182 | "Wrong number of items in the collection"); | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 0 | my $c=$clist->get($kk[0]); | |||||||||||||||||||
| 185 | 0 | $self->assert($c->objtype eq 'Hash', | |||||||||||||||||||
| 186 | "Got something wrong from collection"); | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 0 | $self->assert($c->collection_key eq $kk[0], | |||||||||||||||||||
| 189 | "Wrong value returned by collection_key()"); | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | 0 | $self->assert(!!$clist->exists($c->collection_key), | |||||||||||||||||||
| 192 | "Expected exists to return true"); | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 0 | $self->assert(!$clist->exists(time), | |||||||||||||||||||
| 195 | "Expected exists to return false in non-existent int key"); | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 0 | $self->assert(!$clist->exists('BAD'), | |||||||||||||||||||
| 198 | "Expected exists to return false in non-existent text key"); | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | 0 | my $kn=$c->container_key(); | |||||||||||||||||||
| 201 | 0 | ▶ | $self->assert($kn eq 'c1' || $kn eq 'c2', | ||||||||||||||||||
|
Condition0%0/3
$kn eq 'c1' || $kn eq 'c2'
MC/DC0%0/2
$kn eq 'c1' || $kn eq 'c2'
$kn eq 'c1'
$kn eq 'c2'
Truth table0%0/3
A || B
A= $kn eq 'c1'B= $kn eq 'c2'
| |||||||||||||||||||||
| 202 | "Container_key returned wrong value ($kn)"); | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | 0 | my $name='New Name'; | |||||||||||||||||||
| 205 | 0 | $c->put(name => $name); | |||||||||||||||||||
| 206 | 0 | my $got=$c->get('name'); | |||||||||||||||||||
| 207 | 0 | $self->assert($got eq $name, | |||||||||||||||||||
| 208 | "Something wrong with the hash object we got ($got!=$name)"); | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | 0 | my $uri=$c->uri; | |||||||||||||||||||
| 211 | 0 | ▶ | $self->assert($uri && $uri =~ '/Customers/c?', | ||||||||||||||||||
|
Condition0%0/3
$uri && $uri =~ m[/Customers/c?]
MC/DC0%0/2
$uri && $uri =~ m[/Customers/c?]
$uri
$uri =~ m[/Customers/c?]
Truth table0%0/3
A && B
A= $uriB= $uri =~ m[/Customers/c?]
| |||||||||||||||||||||
| 212 | "Wrong URI ($uri)"); | ||||||||||||||||||||
| 213 | } | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | sub test_deeper { | ||||||||||||||||||||
| 216 | 0 | ▶ | my $self=shift; | ||||||||||||||||||
|
Subroutine
test_deeper: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 217 | 0 | my $odb=$self->get_odb(); | |||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | 0 | my $c=$odb->fetch('/Customers/c2'); | |||||||||||||||||||
| 220 | 0 | $c->add_placeholder(name => 'Orders', | |||||||||||||||||||
| 221 | type => 'list', | ||||||||||||||||||||
| 222 | class => 'Data::Order', | ||||||||||||||||||||
| 223 | key => 'order_id', | ||||||||||||||||||||
| 224 | ); | ||||||||||||||||||||
| 225 | 0 | my $orders=$c->get('Orders'); | |||||||||||||||||||
| 226 | 0 | my $o=$orders->get_new(); | |||||||||||||||||||
| 227 | 0 | $o->add_placeholder(name => 'foo', | |||||||||||||||||||
| 228 | type => 'text', | ||||||||||||||||||||
| 229 | maxlength => 20); | ||||||||||||||||||||
| 230 | 0 | $o->put(foo => 'test'); | |||||||||||||||||||
| 231 | 0 | $orders->put($o); | |||||||||||||||||||
| 232 | 0 | $o->put(foo => 'fubar'); | |||||||||||||||||||
| 233 | 0 | $orders->put($o); | |||||||||||||||||||
| 234 | 0 | $o->put(foo => 'junk'); | |||||||||||||||||||
| 235 | 0 | $orders->put($o); | |||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | 0 | my $coll=$odb->collection(class => 'Data::Order'); | |||||||||||||||||||
| 238 | 0 | $self->assert(ref($coll), | |||||||||||||||||||
| 239 | "Can't create a collection"); | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | 0 | $self->assert($coll->objtype eq 'Collection', | |||||||||||||||||||
| 242 | "Objtype() is not 'Collection'"); | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | 0 | $self->assert($coll->objname eq 'FS::Collection', | |||||||||||||||||||
| 245 | "Objname() is not 'FS::Collection'"); | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | 0 | my @kk=$coll->keys; | |||||||||||||||||||
| 248 | 0 | $self->assert(@kk == 3, | |||||||||||||||||||
| 249 | "Wrong number of items in the collection"); | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | 0 | my $item=$coll->get($kk[1]); | |||||||||||||||||||
| 252 | 0 | $self->assert($item->objtype eq 'Hash', | |||||||||||||||||||
| 253 | "Got something wrong from collection"); | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | 0 | my $foo='New Name'; | |||||||||||||||||||
| 256 | 0 | $item->put(foo => $foo); | |||||||||||||||||||
| 257 | 0 | my $got=$item->get('foo'); | |||||||||||||||||||
| 258 | 0 | $self->assert($got eq $foo, | |||||||||||||||||||
| 259 | "Something wrong with the hash object we got ($got!=$foo)"); | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | 0 | my $uri=$item->uri; | |||||||||||||||||||
| 262 | 0 | ▶ | $self->assert($uri && $uri =~ '^/Customers/c2/Orders/', | ||||||||||||||||||
|
Condition0%0/3
$uri && $uri =~ m[^/Customers/c2/Orders/]
MC/DC0%0/2
$uri && $uri =~ m[^/Customers/c2/Orders/]
$uri
$uri =~ m[^/Customers/c2/Orders/]
Truth table0%0/3
A && B
A= $uriB= $uri =~ m[^/Customers/c2/Orders/]
| |||||||||||||||||||||
| 263 | "Wrong URI ($uri)"); | ||||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | 0 | $item=$coll->get($kk[0]); | |||||||||||||||||||
| 266 | 0 | $self->assert($item->objtype eq 'Hash', | |||||||||||||||||||
| 267 | "Got something wrong from collection"); | ||||||||||||||||||||
| 268 | |||||||||||||||||||||
| 269 | 0 | $item=$coll->get($kk[2]); | |||||||||||||||||||
| 270 | 0 | $self->assert($item->objtype eq 'Hash', | |||||||||||||||||||
| 271 | "Got something wrong from collection"); | ||||||||||||||||||||
| 272 | |||||||||||||||||||||
| 273 | 0 | my $list=$coll->search('foo', 'cs', 'New'); | |||||||||||||||||||
| 274 | 0 | ▶ | $self->assert($list && scalar(@$list), | ||||||||||||||||||
|
Condition0%0/2
$list && scalar @$list
MC/DC0%0/1
$list && scalar @$list
$list
Truth table0%0/2
A
A= $list
| |||||||||||||||||||||
| 275 | "Wrong search results on collection"); | ||||||||||||||||||||
| 276 | |||||||||||||||||||||
| 277 | 0 | my $id=$list->[0]; | |||||||||||||||||||
| 278 | 0 | $item=$coll->get($id); | |||||||||||||||||||
| 279 | 0 | $self->assert(ref($item), | |||||||||||||||||||
| 280 | "Can't get order reference using search results"); | ||||||||||||||||||||
| 281 | |||||||||||||||||||||
| 282 | 0 | $got=$item->get('foo'); | |||||||||||||||||||
| 283 | 0 | $self->assert($got =~ /New/, | |||||||||||||||||||
| 284 | "Wrong search results from collection ($got !~ /New/)"); | ||||||||||||||||||||
| 285 | |||||||||||||||||||||
| 286 | 0 | $foo='Super-Duper'; | |||||||||||||||||||
| 287 | 0 | $item->put(foo => $foo); | |||||||||||||||||||
| 288 | 0 | $got=$item->get('foo'); | |||||||||||||||||||
| 289 | 0 | $self->assert($got eq $foo, | |||||||||||||||||||
| 290 | "Something wrong with the hash object we got ($got!=$foo)"); | ||||||||||||||||||||
| 291 | } | ||||||||||||||||||||
| 292 | |||||||||||||||||||||
| 293 | 1 | 1; | |||||||||||||||||||