Alzabo

 view release on metacpan or  search on metacpan

t/03-runtime.t  view on Meta::CPAN

unless (@rdbms_names)
{
    plan skip_all => 'no test config provided';
    exit;
}

my $tests_per_run = 340;
my $test_count = $tests_per_run * @rdbms_names;

my %SINGLE_RDBMS_TESTS = ( mysql => 23,
			   pg => 11,
			 );

foreach my $rdbms ( keys %SINGLE_RDBMS_TESTS )
{
    next unless grep { $_ eq $rdbms } @rdbms_names;

    $test_count += $SINGLE_RDBMS_TESTS{$rdbms};
}

plan tests => $test_count;


Alzabo::Test::Utils->remove_all_schemas;


foreach my $rdbms (@rdbms_names)
{
    if ( $rdbms eq 'mysql' )
    {
        # prevent subroutine redefinition warnings
        local $^W = 0;
	eval 'use Alzabo::SQLMaker::MySQL qw(:all)';
    }
    elsif ( $rdbms eq 'pg' )
    {
        local $^W = 0;
	eval 'use Alzabo::SQLMaker::PostgreSQL qw(:all)';
    }

    Alzabo::Test::Utils->make_schema($rdbms);

    run_tests($rdbms);

    Alzabo::Test::Utils->remove_schema($rdbms);
}

sub run_tests
{
    my $rdbms = shift;

    my $config = Alzabo::Test::Utils->test_config_for($rdbms);

    my $s = Alzabo::Runtime::Schema->load_from_file( name => $config->{schema_name} );

    # tests setting basic parameters and connecting to RDBMS
    {
        eval_ok( sub { $s->set_user('foo') },
                 "Set user for schema to foo" );

        eval_ok( sub { $s->set_password('foo') },
                 "Set password for schema to foo" );

        eval_ok( sub { $s->set_host('foo') },
                 "Set host for schema to foo" );

        eval_ok( sub { $s->set_port(1234) },
                 "Set port for schema to 1234" );

        $s->$_(undef) foreach qw( set_user set_password set_host set_port );

        $s->connect( Alzabo::Test::Utils->connect_params_for($rdbms) );

        $s->set_referential_integrity(1);
    }

    {
        my $dbh = $s->driver->handle;
        isa_ok( $dbh, ref $s->driver->{dbh},
                "Object returned by \$s->driver->handle method" );

        eval_ok( sub { $s->driver->handle($dbh) },
                 "Set \$s->driver->handle" );
    }

    my $emp_t = $s->table('employee');
    my $dep_t = $s->table('department');
    my $proj_t = $s->table('project');
    my $emp_proj_t = $s->table('employee_project');

    my %dep;
    eval_ok( sub { $dep{borg} = $dep_t->insert( values => { name => 'borging' } ) },
	     "Insert borging row into department table" );

    is( $dep{borg}->select('name'), 'borging',
	"The borg department name should be 'borging'" );

    {
	my @all = $dep{borg}->select;
	is( @all, 3,
	    "select with no columns should return all the values" );
	is( $all[1], 'borging',
	    "The second value should be the department name" );

	my %all = $dep{borg}->select_hash;
	is( keys %all, 3,
	    "select_hash with no columns should return two keys" );
	ok( exists $all{department_id},
	    "The returned hash should have a department_id key" );
	ok( exists $all{name},
	    "The returned hash should have a department_id key" );
	is( $all{name}, 'borging',
	    "The value of the name key be the department name" );
    }


    $dep{lying} = $dep_t->insert( values => { name => 'lying to the public' } );

    my $borg_id = $dep{borg}->select('department_id');
    delete $dep{borg};

    eval_ok( sub { $dep{borg} = $dep_t->row_by_pk( pk => $borg_id ) },
	     "Retrieve borg department row via row_by_pk method" );

    isa_ok( $dep{borg}, 'Alzabo::Runtime::Row',
	    "Borg department" );

    is( $dep{borg}->select('name'), 'borging',
	"Department's name should be 'borging'" );



( run in 0.665 second using v1.01-cache-2.11-cpan-5a3173703d6 )