Testy Moose

It test review day again here in the Moose-Pen

Today I was having a look at coverage that the '58_parenthes.t' test case gives and with only seven tests it is rather sparse, time to add a few more in.

What I want to first improve is the coverage for 'functions' and 'expressions' to make sure the nested nature or the left and right attributes are correct. I started with this formula 'element' make up of a number of 'expressions','functions', 'params' and 'elements' that use parentheses;

(abs((People.salary + .05) * 1.5))*People.overtime+(abs(People.salary+.05) *2)*People.doubletime)
The above would be expressed as the following hash;

 my $expression =  {
            expression => '+',
            left       => {
                expression        => '*',
                open_parentheses  => 1,
                close_parentheses => 1,
                left              => {
                    expression        => '*',
                    open_parentheses  => 1,
                    close_parentheses => 1,
                    left              => {
                        function => 'abs',
                        left     => {
                            expression => '+',
                            left       => { name => 'salary' },
                            right      => { value => '0.5' }
                        },
                    },
                    right => { value => '1.5' },
                },
                right => { name => 'overtime' },
            },
            right => {
                expression        => '*',
                open_parentheses  => 1,
                close_parentheses => 1,
                left              => {
                    expression        => '*',
                    open_parentheses  => 1,
                    close_parentheses => 1,
                    left              => {
                        function => 'abs',
                        left     => {
                            expression => '+',
                            left       => { name => 'salary' },
                            right      => { value => '0.5' }
                        },
                    },
                    right => { value => '2' },
                },
                right => { name => 'doubletime' },
            },
        };
Which I can then use as the first item on my elements array-refs like this;

$in_hash = {
    view     => { name => 'People' },
    elements => [$expression]
};
Now I can use the above to do some standard tests. As a side note I guess my grade 11 Maths master was right after-all. I will find a use of reverse polish notation or at least its poor cousin someday. The first test was just to see if it passes in the above state;

 ok($da->retrieve( Data::Test->new(), $return ),"Balanced nested elements parentheses");
and the next I removed one of the 'open_parentheses'

delete( $in_hash->{elements}->[0]->{left}->{open_parentheses} );
and did this;

$da = Database::Accessor->new($in_hash);

like(
    exception { $da->retrieve( Data::Test->new() ) },
    qr /Unbalanced parentheses in your static or dynamic attributes/,
    "Caught unbalanced nested elements parentheses"
);
and that one passed; I just carried on creating a few tests with the following outcome
ok 1 - Balanced nested elements parentheses ok 2 - Caught left open parentheses missing ok 3 - Caught left close parentheses missing ok 4 - Caught right left close parentheses missing ok 5 - Caught right left open parentheses missing
Next I did the same sort of thing for conditions by just adding in the first element as a left value in a condition;
 
 $in_hash = {
    view     => { name => 'People' },
    elements => [ {
            name => 'first_name',
            view => 'People'
        },
        {
            name => 'last_name',
            view => 'People'
        },
        {
            name => 'user_id',
            view => 'People'
        }]
};
$da = Database::Accessor->new($in_hash);
$da->add_condition(
    {
        left              =>  $expression ,
        right             => { value => '201' },
        operator          => '=',
        condition         => 'AND',
    }
);

ok(
    $da->retrieve( Data::Test->new(), $return ),
    "Balanced nested elements on condition"
);
$da->reset_conditions();
delete( $expression->{left}->{open_parentheses} );
$da->add_condition(
    {
        left              =>  $expression ,
        right             => { value => '201' },
        operator          => '=',
        condition         => 'AND',
    }
);
like(
    exception { $da->retrieve( Data::Test->new() ) },
    qr /Unbalanced parentheses in your static or dynamic attributes/,
    "Caught left open parentheses missing"
);
and I got
... ok 6 - Balanced nested elements on condition ok 7 - Caught left open parentheses missing
Now I an not going to bore you with many many more lines like the above few tests. What I continued to do was add more and more tests similar to the one above and I got the this as a final result.
ok 1 - Balanced nested elements parentheses ok 2 - Caught left open parentheses missing ok 3 - Caught left close parentheses missing ok 4 - Caught right left close parentheses missing ok 5 - Caught right left open parentheses missing ok 6 - Balanced nested elements on condition ok 7 - Conditions caught left open parentheses missing ok 8 - Conditions caught left close parentheses missing ok 9 - Conditions caught right left close parentheses missing ok 10 - Conditions caught right left open parentheses missing ok 11 - Balanced nested elements on link ok 12 - Link caught left open parentheses missing ok 13 - Link caught left close parentheses missing ok 14 - Link caught right left close parentheses missing ok 15 - Link caught right left open parentheses missing ok 16 - Balanced nested elements on Gather ok 17 - Gather caught left open parentheses missing ok 18 - Gather caught left close parentheses missing ok 19 - Gather caught right left close parentheses missing ok 20 - Gather caught right left open parentheses missing ok 21 - Balanced nested elements on Sort ok 22 - Sort caught left open parentheses missing ok 23 - Sort caught left close parentheses missing ok 24 - Sort caught right left close parentheses missing ok 25 - Sort caught right left open parentheses missing
so things are looking good.

sr90-55.jpg

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations