kivitendo/t/part/assembly.t @ a536d0ec
8aea132f | Geoffrey Richardson | use strict;
|
||
use Test::More;
|
||||
use lib 't';
|
||||
use Support::TestSetup;
|
||||
use Test::Exception;
|
||||
use SL::DB::Unit;
|
||||
use SL::DB::Part;
|
||||
use SL::DB::Assembly;
|
||||
63410aa6 | Sven Schöling | use SL::Dev::Part qw(new_assembly);
|
||
5d711a25 | Martin Helmling | use SL::DB::Helper::ValidateAssembly;
|
||
8aea132f | Geoffrey Richardson | |||
Support::TestSetup::login();
|
||||
5d711a25 | Martin Helmling | $::locale = Locale->new("en");
|
||
8aea132f | Geoffrey Richardson | |||
clear_up();
|
||||
reset_state();
|
||||
981bfe17 | Geoffrey Richardson | is( SL::DB::Manager::Part->get_all_count(), 4, "total number of parts created by reset_state() is 4");
|
||
8aea132f | Geoffrey Richardson | |||
981bfe17 | Geoffrey Richardson | my $assembly_part = SL::DB::Manager::Part->find_by( partnumber => '19000' ) || die "Can't find assembly 19000";
|
||
my $assembly_item_part = SL::DB::Manager::Part->find_by( partnumber => '19000 1' ) || die "Can't find assembly item part '19000 1'";
|
||||
8aea132f | Geoffrey Richardson | |||
98b64fe1 | Geoffrey Richardson | is($assembly_part->part_type, 'assembly', 'assembly has correct type');
|
||
5d711a25 | Martin Helmling | is( scalar @{$assembly_part->assemblies}, 3, 'assembly consists of three parts' );
|
||
5222d83a | Geoffrey Richardson | |||
# fetch assembly item corresponding to partnumber 19000
|
||||
my $assembly_items = $assembly_part->find_assemblies( { parts_id => $assembly_item_part->id } ) || die "can't find assembly_item";
|
||||
my $assembly_item = $assembly_items->[0];
|
||||
981bfe17 | Geoffrey Richardson | is($assembly_item->part->partnumber, '19000 1', 'assembly part part relation works');
|
||
35777fef | Geoffrey Richardson | is($assembly_item->assembly_part->partnumber, '19000', 'assembly part assembly part relation works');
|
||
8aea132f | Geoffrey Richardson | |||
5d711a25 | Martin Helmling | |||
63410aa6 | Sven Schöling | my $assembly2_part = new_assembly( partnumber => '20000', assnumber => 'as2' )->save;
|
||
5d711a25 | Martin Helmling | my $retval = validate_assembly($assembly_part,$assembly2_part);
|
||
e63e6914 | Sven Schöling | ok(!defined $retval, 'assembly 19000 can be child of assembly 20000' );
|
||
5d711a25 | Martin Helmling | $assembly2_part->add_assemblies(SL::DB::Assembly->new(parts_id => $assembly_part->id, qty => 3, bom => 1));
|
||
$assembly2_part->save;
|
||||
63410aa6 | Sven Schöling | my $assembly3_part = new_assembly( partnumber => '30000', assnumber => 'as3' )->save;
|
||
5d711a25 | Martin Helmling | $retval = validate_assembly($assembly3_part,$assembly_part);
|
||
e63e6914 | Sven Schöling | ok(!defined $retval, 'assembly 30000 can be child of assembly 19000' );
|
||
5d711a25 | Martin Helmling | |||
$retval = validate_assembly($assembly3_part,$assembly2_part);
|
||||
e63e6914 | Sven Schöling | ok(!defined $retval, 'assembly 30000 can be child of assembly 20000' );
|
||
5d711a25 | Martin Helmling | |||
$assembly_part->add_assemblies(SL::DB::Assembly->new(parts_id => $assembly3_part->id, qty => 4, bom => 1));
|
||||
$assembly_part->save;
|
||||
$retval = validate_assembly($assembly3_part,$assembly2_part);
|
||||
e63e6914 | Sven Schöling | ok(!defined $retval, 'assembly 30000 can be child of assembly 20000' );
|
||
5d711a25 | Martin Helmling | |||
$assembly2_part->add_assemblies(SL::DB::Assembly->new(parts_id => $assembly3_part->id, qty => 5, bom => 1));
|
||||
$assembly2_part->save;
|
||||
# fetch assembly item corresponding to partnumber 20000
|
||||
my $assembly2_items = $assembly2_part->find_assemblies() || die "can't find assembly_item";
|
||||
981bfe17 | Geoffrey Richardson | is( scalar @{$assembly2_items}, 5, 'assembly2 consists of ive parts' );
|
||
5d711a25 | Martin Helmling | my $assembly2_item = $assembly2_items->[3];
|
||
981bfe17 | Geoffrey Richardson | is($assembly2_item->qty, 3, 'qty of 3rd assembly item is 3' );
|
||
is($assembly2_item->part->part_type, 'assembly', '3rd assembly item \'' . $assembly2_item->part->partnumber . '\' is also an assembly');
|
||||
5d711a25 | Martin Helmling | my $assembly3_items = $assembly2_item->part->find_assemblies() || die "can't find assembly_item";
|
||
981bfe17 | Geoffrey Richardson | is( scalar @{$assembly3_items}, 4, 'assembly3 consists of four parts' );
|
||
5d711a25 | Martin Helmling | |||
# check loop to itself
|
||||
$retval = validate_assembly($assembly_part,$assembly_part);
|
||||
is( $retval,"The assembly '19000' cannot be a part from itself.", 'assembly loops to itself' );
|
||||
if (!$retval && $assembly_part->add_assemblies( SL::DB::Assembly->new(parts_id => $assembly_part->id, qty => 8, bom => 1))) {
|
||||
$assembly_part->save;
|
||||
}
|
||||
981bfe17 | Geoffrey Richardson | is( scalar @{$assembly_part->assemblies}, 4, 'assembly consists of four parts' );
|
||
5d711a25 | Martin Helmling | |||
# check indirekt loop
|
||||
$retval = validate_assembly($assembly2_part,$assembly_part);
|
||||
ok( $retval, 'assembly indirect loop' );
|
||||
if (!$retval && $assembly_part->add_assemblies( SL::DB::Assembly->new(parts_id => $assembly2_part->id, qty => 9, bom => 1))) {
|
||||
$assembly_part->save;
|
||||
}
|
||||
981bfe17 | Geoffrey Richardson | is( scalar @{$assembly_part->assemblies}, 4, 'assembly consists of four parts' );
|
||
5d711a25 | Martin Helmling | |||
8aea132f | Geoffrey Richardson | clear_up();
|
||
done_testing;
|
||||
sub clear_up {
|
||||
SL::DB::Manager::Assembly->delete_all(all => 1);
|
||||
SL::DB::Manager::Part->delete_all( all => 1);
|
||||
};
|
||||
sub reset_state {
|
||||
my %params = @_;
|
||||
63410aa6 | Sven Schöling | my $assembly = new_assembly( assnumber => '19000', partnumber => '19000' )->save;
|
||
8aea132f | Geoffrey Richardson | };
|
||
1;
|