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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
|
# -*- cperl -*-
use ExtUtils::testlib;
use Test::More;
use Test::Exception;
use Test::Differences;
use Test::Memory::Cycle;
use Test::Log::Log4perl;
use Config::Model;
use Config::Model::AnyId;
use strict;
use warnings;
use Config::Model::Tester::Setup qw/init_test/;
Test::Log::Log4perl->ignore_priority("info");
my ($model, $trace) = init_test();
my @element = (
# Value constructor args are passed in their specific array ref
cargo => {
type => 'leaf',
value_type => 'string'
},
);
# minimal set up to get things working
$model->create_config_class(
name => "Master",
element => [
bounded_list => {
type => 'list',
class => 'Config::Model::ListId', # default
max => 123,
cargo => {
type => 'leaf',
value_type => 'string',
match => '^.{1,5}$',
},
},
plain_list => { type => 'list', @element },
list_with_auto_created_id => {
type => 'list',
auto_create_ids => 4,
@element
},
olist => {
type => 'list',
cargo => {
type => 'node',
config_class_name => 'Slave'
},
},
list_with_default_with_init_leaf => {
type => 'list',
default_with_init => {
0 => 'def_1 stuff',
1 => 'def_2 stuff'
},
@element,
},
list_with_default_with_init_node => {
type => 'list',
default_with_init => {
0 => 'X=Bv Y=Cv',
1 => 'X=Av'
},
cargo => {
type => 'node',
config_class_name => 'Slave'
},
},
map {
( "list_with_" . $_ . "_duplicates" => { type => 'list', duplicates => $_, @element, },
);
} qw/warn allow forbid suppress/,
] );
$model->create_config_class(
name => "Bogus",
element => [
list_with_wrong_auto_create => {
type => 'list',
auto_create_ids => ['foo'],
@element
},
list_with_wrong_duplicates => {
type => 'list',
duplicates => 'forbid',
cargo => {
type => 'node',
config_class_name => 'Slave'
},
},
list_with_yada_duplicates => {
type => 'list',
duplicates => 'yada',
@element,
},
] );
$model->create_config_class(
name => "Slave",
gist => '{X} and {Y}',
element => [
[qw/X Y Z/] => {
type => 'leaf',
value_type => 'enum',
choice => [qw/Av Bv Cv/]
},
] );
ok( 1, "config classes created" );
my ($inst,$root);
subtest "array initialisation" => sub {
$inst = $model->instance(
root_class_name => 'Master',
instance_name => 'test1'
);
ok( $inst, "created dummy instance" );
$inst->initial_load_stop;
$root = $inst->config_root;
is( $inst->needs_save, 0, "verify instance needs_save status after creation" );
eq_or_diff( [ $root->fetch_element('olist')->fetch_all_indexes ],
[], "check index list of empty list" );
is( $inst->needs_save, 0, "verify instance needs_save status after olist creation" );
};
subtest "bounded list" => sub {
my $b = $root->fetch_element('bounded_list');
ok( $b, "bounded list created" );
is( $inst->needs_save, 0, "verify instance needs_save status after element creation" );
# each line triggers 2 changes: element creation and value storage
is( $b->fetch_with_id(1)->store('foo'), 1, "stored in 1" );
is( $b->fetch_with_id(0)->store('baz'), 1, "stored in 0" );
is( $b->fetch_with_id(2)->store('bar'), 1, "stored in 2" );
is( $inst->needs_save, 3, "verify instance needs_save status after storing into element" );
print join( "\n", $inst->list_changes("\n") ), "\n" if $trace;
throws_ok { $b->fetch_with_id(124)->store('baz'); } qr/Index 124 > max_index limit 123/,
'max error caught';
eq_or_diff( [ $b->fetch_all_indexes ], [ 0, 1, 2 ], "check ids" );
$b->delete(1);
is( $b->fetch_with_id(1)->fetch, undef, "check deleted id" );
is( $b->index_type, 'integer', 'check list index_type' );
is( $b->max_index, 123, 'check list max boundary' );
$b->push( 'toto', 'titi' );
is( $b->fetch_with_id(2)->fetch, 'bar', "check last item of table" );
is( $b->fetch_with_id(3)->fetch, 'toto', "check pushed toto item" );
is( $b->fetch_with_id(4)->fetch, 'titi', "check pushed titi item" );
$b->push_x(
values => [ 'toto', 'titi' ],
check => 'no',
annotation => ['toto comment'] );
is( $b->fetch_with_id(5)->fetch, 'toto', "check pushed toto item with push_x" );
is( $b->fetch_with_id(5)->annotation, 'toto comment', "check pushed toto annotation with push_x" );
is( $b->fetch_with_id(6)->fetch, 'titi', "check pushed titi item with push_x" );
$b->push_x(
values => 'toto2',
check => 'no',
annotation => 'toto2 comment'
);
is( $b->fetch_with_id(7)->fetch, 'toto2', "check pushed toto2 item with push_x" );
is( $b->fetch_with_id(7)->annotation,
'toto2 comment', "check pushed toto2 annotation with push_x" );
my @all = $b->fetch_all_values;
eq_or_diff( \@all, [qw/baz bar toto titi toto titi toto2/], "check fetch_all_values" );
is( $b->fetch, 'baz,bar,toto,titi,toto,titi,toto2', "check fetch" );
$b->clear;
};
subtest "model with errors" => sub {
my $bogus_root = $model->instance( root_class_name => 'Bogus' )->config_root;
throws_ok { $bogus_root->fetch_element('list_with_wrong_auto_create'); }
qr/Wrong auto_create argument for list/, 'wrong auto_create caught';
throws_ok { $bogus_root->fetch_element('list_with_wrong_duplicates'); }
"Config::Model::Exception::Model",
"fails duplicates with node cargo";
throws_ok { $bogus_root->fetch_element('list_with_yada_duplicates'); }
"Config::Model::Exception::Model",
"fails yada duplicates";
};
subtest "list with auto_created_id" => sub {
my $lac = $root->fetch_element('list_with_auto_created_id');
eq_or_diff( [ $lac->fetch_all_indexes ], [ 0 .. 3 ], "check list_with_auto_created_id" );
};
subtest "move, swap in bounded_list" => sub {
my $b = $root->fetch_element('bounded_list');
$b->store_set( 'baz', undef, qw/bar toto titi toto titi toto2/);
for ( 0 .. 4 ) {
is( $b->fetch_with_id($_)->index_value, $_, "Check index value $_" );
}
$b->move( 3, 4 );
is( $b->fetch_with_id(3)->fetch, undef, "check after move idx 3 in 4" );
is( $b->fetch_with_id(4)->fetch, 'toto', "check after move idx 3 in 4" );
for ( 0 .. 4 ) {
is( $b->fetch_with_id($_)->index_value, $_, "Check moved index value $_" );
}
$b->fetch_with_id(3)->store('titi');
$b->swap( 3, 4 );
for ( 0 .. 4 ) {
is( $b->fetch_with_id($_)->index_value, $_, "Check swapped index value $_" );
}
is( $b->fetch_with_id(3)->fetch, 'toto', "check value after swap" );
is( $b->fetch_with_id(4)->fetch, 'titi', "check value after swap" );
$inst->clear_changes;
$b->remove(3);
is( $b->fetch_with_id(3)->fetch, 'titi', "check after remove" );
is( scalar $inst->list_changes(), q!bounded_list: removed idx 3 ("toto")!, "check removal message");
$b->clear;
};
subtest "test move swap with node list" => sub {
my $ol = $root->fetch_element('olist');
my @set = ( [qw/X Av/], [qw/X Bv/], [qw/Y Av/], [qw/Z Cv/], [qw/Z Av/], );
my $i = 0;
foreach my $item (@set) {
my ( $e, $v ) = @$item;
$ol->fetch_with_id( $i++ )->fetch_element($e)->store($v);
}
$inst->clear_changes;
$ol->move( 3, 4 );
is( $inst->needs_save, 1, "verify instance needs_save status after move" );
print scalar $inst->list_changes, "\n" if $trace;
$inst->clear_changes;
is( $ol->fetch_with_id(3)->fetch_element('Z')->fetch, undef, "check after move idx 3 in 4" );
is( $ol->fetch_with_id(4)->fetch_element('Z')->fetch, 'Cv', "check after move idx 3 in 4" );
for ( 0 .. 4 ) {
is( $ol->fetch_with_id($_)->index_value, $_, "Check moved index value $_" );
}
;
$inst->clear_changes;
$ol->swap( 0, 2 );
is( $inst->needs_save, 1, "verify instance needs_save status after move" );
print scalar $inst->list_changes, "\n" if $trace;
$inst->clear_changes;
is( $ol->fetch_with_id(0)->fetch_element('X')->fetch, undef, "check after move idx 0 in 2" );
is( $ol->fetch_with_id(0)->fetch_element('Y')->fetch, 'Av', "check after move" );
is( $ol->fetch_with_id(2)->fetch_element('Y')->fetch, undef, "check after move" );
is( $ol->fetch_with_id(2)->fetch_element('X')->fetch, 'Av', "check after move" );
for ( 0 .. 4 ) {
is( $ol->fetch_with_id($_)->index_value, $_, "Check moved index value $_" );
}
;
print $root->dump_tree( ) if $trace;
is( $ol->fetch_with_id(0)->fetch_element('X')->fetch, undef, "check before move" );
$ol->remove(0);
print $root->dump_tree( ) if $trace;
is( $ol->fetch_with_id(0)->fetch_element('X')->fetch, 'Bv', "check after move" );
# test node gist in an array display
my $olgist = $ol->fetch_with_id(5);
$olgist->fetch_element('X')->store('Av');
$olgist->fetch_element('Y')->store('Bv');
is($olgist->fetch_gist,'Av and Bv', "check get_display_key");
};
subtest "load method" => sub {
my $b = $root->fetch_element('bounded_list');
# test store
my @test = (
[ a1 => ['a1'] ],
[ '"a","b"' => [qw/a b/] ],
[ 'a,b' => [qw/a b/] ],
[ '"a\"a",b' => [qw/a"a b/] ],
[ '"a,a",b' => [ 'a,a', 'b' ] ],
[ '",a1"' => [',a1'] ],
);
foreach my $l (@test) {
$b->load( $l->[0] );
eq_or_diff( [ $b->fetch_all_values ], $l->[1], "test store $l->[0]" );
}
throws_ok { $b->load('a,,b'); } "Config::Model::Exception::Load", "fails load 'a,,b'";
};
subtest "preset mode" => sub {
$inst->preset_start;
my $pl = $root->fetch_element('plain_list');
$pl->fetch_with_id(0)->store('prefoo');
$pl->fetch_with_id(1)->store('prebar');
$inst->preset_stop;
ok( 1, "filled preset values" );
eq_or_diff( [ $pl->fetch_all_values ], [ 'prefoo', 'prebar' ],
"check that preset values are read" );
$pl->fetch_with_id(2)->store('bar');
eq_or_diff( [ $pl->fetch_all_values ], [ 'prefoo', 'prebar', 'bar' ],
"check that values are read" );
eq_or_diff( [ $pl->fetch_all_values( mode => 'custom' ) ],
['bar'], "check that custom values are read" );
$inst->clear_changes;
$pl->clear;
is( $inst->needs_save, 1, "verify instance needs_save status after clear array" );
eq_or_diff( [ $pl->fetch_all_indexes ], [], "check that array was cleared" );
eq_or_diff([$inst->list_changes],['plain_list: cleared all entries'],"check change message after clear");
};
subtest "default_with_init on leaf" => sub {
my $lwdwil = $root->fetch_element('list_with_default_with_init_leaf');
# note: calling fetch_all_indexes is required to trigger creation of default_with_init keys
eq_or_diff( [ $lwdwil->fetch_all_indexes ], [ 0, 1 ], "check default keys" );
is( $lwdwil->fetch_with_id(0)->fetch, 'def_1 stuff', "test default_with_init leaf 0" );
is( $lwdwil->fetch_with_id(1)->fetch, 'def_2 stuff', "test default_with_init leaf 1" );
# test default_with_init on node
my $lwdwin = $root->fetch_element('list_with_default_with_init_node');
eq_or_diff( [ $lwdwin->fetch_all_indexes ], [ 0, 1 ], "check default keys" );
is( $lwdwin->fetch_with_id(0)->fetch_element('X')->fetch, 'Bv', "test default_with_init node 0" );
is( $lwdwin->fetch_with_id(0)->fetch_element('Y')->fetch, 'Cv', "test default_with_init node 0" );
is( $lwdwin->fetch_with_id(1)->fetch_element('X')->fetch, 'Av', "test default_with_init node 0" );
};
foreach my $what (qw/forbid warn suppress/) {
my $elt_name = 'list_with_' . $what . '_duplicates';
subtest "test $elt_name" => sub {
my $lwd = $root->fetch_element( $elt_name );
$lwd->push(qw/string1 string2/);
$lwd->push('string1'); # does not trigger duplicate issues, yet
$lwd->push('string1'); # does not trigger duplicate issues, yet
# there we go
if ( $what eq 'forbid' ) {
is( $lwd->needs_content_check, 1, "verify needs_content_check is true" );
throws_ok { $lwd->fetch_all_values; } "Config::Model::Exception::WrongValue",
"fails forbidden duplicates";
is( $lwd->needs_content_check, 0, "verify needs_content_check after fetch_all_values" );
throws_ok { $lwd->fetch_all_values; } "Config::Model::Exception::WrongValue",
"fails forbidden duplicates even if needs_content_check is false";
is( $lwd->needs_content_check, 0, "verify again needs_content_check after fetch_all_values" );
$lwd->delete(2);
is( $lwd->needs_content_check, 1, "verify needs_content_check after list content modif" );
}
elsif ( $what eq 'warn' ) {
{
my $tlog = Test::Log::Log4perl->expect([ 'User' => warn => qr/Duplicated/ ]);
$lwd->fetch_all_values;
}
is ( $lwd->has_warning, 1, "detected duplicated values");
is( $lwd->has_fixes, 2, "check nb of fixes" );
$inst->apply_fixes;
{
# no warning expected
my $tlog = Test::Log::Log4perl->expect([]);
$lwd->fetch_all_values;
}
}
else {
$lwd->check_content;
}
is( $lwd->fetch_with_id(0)->fetch,
'string1', "check that original values is untouched after $what duplicates" );
};
}
$inst->clear_changes;
subtest "preset clear stuff" => sub {
my $pl = $root->fetch_element('plain_list');
# done after auto_create_ids tests, because preset_clear or layered_clear
# also clean up auto_create_ids (if there's no data in there)
$inst->preset_start;
$pl->fetch_with_id(0)->store('prefoo');
$pl->fetch_with_id(1)->store('prebar');
$inst->preset_stop;
eq_or_diff( [ $pl->fetch_all_indexes ], [ 0, 1 ], "check preset indexes" );
$pl->fetch_with_id(1)->store('bar');
$inst->preset_clear;
eq_or_diff( [ $pl->fetch_all_indexes ], [0], "check that only preset stuff was cleared" );
is( $pl->fetch_with_id(0)->fetch, 'bar', "check that bar was moved from 1 to 0" );
$pl->clear;
};
subtest "layered stuff" => sub {
my $pl = $root->fetch_element('plain_list');
$inst->layered_start;
$pl->fetch_with_id(0)->store('prefoo');
$pl->fetch_with_id(1)->store('prebar');
$inst->layered_stop;
eq_or_diff( [ $pl->fetch_all_indexes ], [ 0, 1 ], "check layered indexes" );
$pl->fetch_with_id(1)->store('bar');
$inst->layered_clear;
eq_or_diff( [ $pl->fetch_all_indexes ], [0], "check that only layered stuff was cleared" );
is( $pl->fetch_with_id(0)->fetch, 'bar', "check that bar was moved from 1 to 0" );
$pl->clear;
};
subtest "layered stuff " => sub {
# test done for https://github.com/dod38fr/config-model/issues/26#issuecomment-810572173
my $pl = $root->fetch_element('plain_list');
$inst->layered_start;
$pl->fetch_with_id(0)->store('prefoo');
$pl->fetch_with_id(1)->store('prebar');
$inst->layered_stop;
eq_or_diff( [ $pl->fetch_all_indexes ], [ 0, 1 ], "check layered indexes" );
$pl->fetch_with_id(2)->store('baz');
$inst->layered_clear;
eq_or_diff( [ $pl->fetch_all_indexes ], [0], "check that only layered stuff was cleared" );
is( $pl->fetch_with_id(0)->fetch, 'baz', "check that baz was moved from 2 to 0" );
$pl->clear;
};
subtest "notify change after implicit deletion (github #4)" => sub {
my $pl = $root->fetch_element('plain_list');
$pl->store_set(qw/j h g f k l/);
$inst->clear_changes;
my @set = qw/j h g f/;
$pl->store_set(@set);
is( $inst->c_count, 2, "check that store smaller set triggered a change" );
$inst->clear_changes;
};
subtest "sort" => sub {
my $pl = $root->fetch_element('plain_list');
my @set = qw/j h g f/;
$pl->store_set(@set);
$pl->sort;
eq_or_diff( [ $pl->fetch_all_values ], [ sort @set ], "check sort result" );
is( $inst->c_count, 1, "check that sort has triggered a change" );
$pl->sort;
is( $inst->c_count, 1, "check that redundant sort has not triggered a change" );
};
subtest "unshift" => sub {
my $pl = $root->fetch_element('plain_list');
$pl->store_set(qw/a b/);
$pl->unshift(qw/1 2 3 4/);
eq_or_diff( [ $pl->fetch_all_values ], [qw/1 2 3 4 a b/], "check unshift result" );
eq_or_diff( [ $pl->fetch_all_indexes ], [ ( 0 .. 5 ) ], "check that indexes are reset correctly" );
};
subtest "insert_at" => sub {
my $pl = $root->fetch_element('plain_list');
$pl->store_set(qw/a b/);
$pl->insert_at(qw/1 d e/);
eq_or_diff( [ $pl->fetch_all_values ], [qw/a d e b/], "check insert_at result" );
eq_or_diff( [ $pl->fetch_all_indexes ], [ ( 0 .. 3 ) ], "check that indexes are reset correctly" );
};
subtest "insert_before" => sub {
my $pl = $root->fetch_element('plain_list');
$pl->store_set(qw/foo baz/);
$pl->insert_before(qw/baz bar1 bar2/);
eq_or_diff( [ $pl->fetch_all_values ], [qw/foo bar1 bar2 baz/], "check insert_before result" );
$pl->insert_before( qr/z/, qw/bar3 bar4/ );
eq_or_diff(
[ $pl->fetch_all_values ],
[qw/foo bar1 bar2 bar3 bar4 baz/],
"check insert_before with regexp result"
);
};
subtest "insort" => sub {
my $pl = $root->fetch_element('plain_list');
my @set1 = qw/c1 e i1 j1 p1/;
my @set2 = qw/a2 z2 d2 e b2 k2/;
$pl->store_set(@set1);
$pl->sort;
$pl->insort(@set2);
eq_or_diff( [ $pl->fetch_all_values ], [ sort( @set1, @set2 ) ], "check insort result" );
};
subtest "load_data method" => sub {
my $b = $root->fetch_element('bounded_list');
# test store
my @test = (
[ a1 => ['a1'] ],
[ 'a b x' => [q/a b x/] ],
[ 'a b x' => [qw/a b x/], qr/ / ],
[ 'a b' => [qw/a b/], qr/ / ],
[ 'a,b,c' => [qw/a b c/], qr/,/ ],
[ 'a,too_long,b,c' => [qw/a b c/], qr/,/ ],
[ [qw/a c/] => [qw/a c/] ],
);
foreach my $l (@test) {
$b->load_data( data => $l->[0], split_reg => $l->[2], check => 'skip', silent => 1 );
eq_or_diff( [ $b->fetch_all_values ], $l->[1], "test store $l->[0]" );
}
throws_ok {
$b->load_data(plop=>'a,,b');
} "Config::Model::Exception::LoadData",
"fails load_data with wrong parameter";
$b->clear;
};
subtest "load_data method change tracking" => sub {
my $b = $root->fetch_element('bounded_list');
$b->load_data([qw/a b c/]);
$inst->clear_changes;
$b->load_data([qw/a b c/]);
is( $inst->needs_save, 0, "verify needs_save after loading same data" );
$b->load_data([qw/a b/]);
is( $inst->needs_save, 1, "verify needs_save after loading same data" );
print scalar $inst->list_changes, "\n" if $trace;
$inst->clear_changes;
$b->clear;
};
subtest "test load_data with node list" => sub {
my $ol = $root->fetch_element('olist');
$ol->clear;
$inst->clear_changes;
my $load_test = sub {
$ol->load_data(
data => shift,
check => 'skip',
silent => 1
);
};
$load_test->( [ ({X=>'Av',Y=>'Bv'}) x 3]);
is($ol->fetch_size,3,"check that 3 nodes was created");
# node 2 is removed because only 2 nodes are loaded
$load_test->( [{X=>'Av',Y=>'Bv'},{X=>'Av',Y=>'Bv'}]);
is($ol->fetch_size,2,"check that only 2 elements remain");
# node 1 is removed because all its data is bogus
$load_test->( [{X=>'Av_bogus',Y=>'Bv_bogus'},{X=>'Av',Y=>'Bv'}]);
is($ol->fetch_size,1,"check that only one element remains");
};
memory_cycle_ok( $model, "memory cycles" );
done_testing;
|