5 use Support::TestSetup;
10 use SL::Dev::Part qw(new_assembly);
11 use SL::DB::Helper::ValidateAssembly;
13 Support::TestSetup::login();
14 $::locale = Locale->new("en");
19 is( SL::DB::Manager::Part->get_all_count(), 4, "total number of parts created by reset_state() is 4");
21 my $assembly_part = SL::DB::Manager::Part->find_by( partnumber => '19000' ) || die "Can't find assembly 19000";
22 my $assembly_item_part = SL::DB::Manager::Part->find_by( partnumber => '19000 1' ) || die "Can't find assembly item part '19000 1'";
24 is($assembly_part->part_type, 'assembly', 'assembly has correct type');
25 is( scalar @{$assembly_part->assemblies}, 3, 'assembly consists of three parts' );
27 # fetch assembly item corresponding to partnumber 19000
28 my $assembly_items = $assembly_part->find_assemblies( { parts_id => $assembly_item_part->id } ) || die "can't find assembly_item";
29 my $assembly_item = $assembly_items->[0];
30 is($assembly_item->part->partnumber, '19000 1', 'assembly part part relation works');
31 is($assembly_item->assembly_part->partnumber, '19000', 'assembly part assembly part relation works');
35 my $assembly2_part = new_assembly( partnumber => '20000', assnumber => 'as2' )->save;
36 my $retval = validate_assembly($assembly_part,$assembly2_part);
37 ok(!defined $retval, 'assembly 19000 can be child of assembly 20000' );
38 $assembly2_part->add_assemblies(SL::DB::Assembly->new(parts_id => $assembly_part->id, qty => 3, bom => 1));
39 $assembly2_part->save;
41 my $assembly3_part = new_assembly( partnumber => '30000', assnumber => 'as3' )->save;
42 $retval = validate_assembly($assembly3_part,$assembly_part);
43 ok(!defined $retval, 'assembly 30000 can be child of assembly 19000' );
45 $retval = validate_assembly($assembly3_part,$assembly2_part);
46 ok(!defined $retval, 'assembly 30000 can be child of assembly 20000' );
48 $assembly_part->add_assemblies(SL::DB::Assembly->new(parts_id => $assembly3_part->id, qty => 4, bom => 1));
51 $retval = validate_assembly($assembly3_part,$assembly2_part);
52 ok(!defined $retval, 'assembly 30000 can be child of assembly 20000' );
54 $assembly2_part->add_assemblies(SL::DB::Assembly->new(parts_id => $assembly3_part->id, qty => 5, bom => 1));
55 $assembly2_part->save;
57 # fetch assembly item corresponding to partnumber 20000
58 my $assembly2_items = $assembly2_part->find_assemblies() || die "can't find assembly_item";
59 is( scalar @{$assembly2_items}, 5, 'assembly2 consists of ive parts' );
60 my $assembly2_item = $assembly2_items->[3];
61 is($assembly2_item->qty, 3, 'qty of 3rd assembly item is 3' );
62 is($assembly2_item->part->part_type, 'assembly', '3rd assembly item \'' . $assembly2_item->part->partnumber . '\' is also an assembly');
63 my $assembly3_items = $assembly2_item->part->find_assemblies() || die "can't find assembly_item";
64 is( scalar @{$assembly3_items}, 4, 'assembly3 consists of four parts' );
68 # check loop to itself
69 $retval = validate_assembly($assembly_part,$assembly_part);
70 is( $retval,"The assembly '19000' cannot be a part from itself.", 'assembly loops to itself' );
71 if (!$retval && $assembly_part->add_assemblies( SL::DB::Assembly->new(parts_id => $assembly_part->id, qty => 8, bom => 1))) {
74 is( scalar @{$assembly_part->assemblies}, 4, 'assembly consists of four parts' );
77 $retval = validate_assembly($assembly2_part,$assembly_part);
78 ok( $retval, 'assembly indirect loop' );
79 if (!$retval && $assembly_part->add_assemblies( SL::DB::Assembly->new(parts_id => $assembly2_part->id, qty => 9, bom => 1))) {
82 is( scalar @{$assembly_part->assemblies}, 4, 'assembly consists of four parts' );
88 SL::DB::Manager::Assembly->delete_all(all => 1);
89 SL::DB::Manager::Part->delete_all( all => 1);
95 my $assembly = new_assembly( assnumber => '19000', partnumber => '19000' )->save;