Alzabo

 view release on metacpan or  search on metacpan

t/02-create.t  view on Meta::CPAN

		 "Call schema's save_to_file method" );

	my $base = File::Spec->catdir( $dir, $s->name );
	my $name = $s->name;
	ok( -d $base,
	    "'$base' should exist" );

	ok( -e "$base/$name.create.alz",
	    "'$base/$name.create.alz' file should exist" );
	ok( -e "$base/$name.runtime.alz",
	    "'$base/$name.runtime.alz' file should exist" );
	ok( -e "$base/$name.rdbms",
	    "'$base/$name.rdbms' file should exist" );
    }

    eval_ok( sub { $s->make_table( name => 'footab' ) } , "Make table 'footab'" );

    my $t1;
    eval_ok( sub { $t1 = $s->table('footab') }, "Retrieve 'footab' table from schema" );
    isa_ok( $t1, 'Alzabo::Create::Table',
	    "Object returned from \$s->table" );

    my $att = $db eq 'MySQL' ? 'unsigned' : 'check > 5';

    eval_ok( sub { $t1->make_column( name => 'foo_pk',
				     type => 'int',
				     attributes => [ $att ],
				     sequenced => 1,
				     nullable => 0,
				   ) },
	     "Make column 'foo_pk' in 'footab'" );


    eval { $s->tables( 'footab', 'does not exist' ) };
    like( $@, qr/Table does not exist doesn't exist/,
          "Make sure tables method catches missing tables" );

    eval { $s->table( 'does not exist' ) };
    isa_ok( $@, 'Alzabo::Exception::Params',
          "Make sure table() method catches missing tables" );

    eval { $t1->columns( 'foo_pk', 'does not exist' ) };
    like( $@, qr/Column does not exist doesn't exist/,
          "Make sure columns method catches missing columns" );

    my $t1_c1;
    eval_ok( sub { $t1_c1 = $t1->column('foo_pk') },
	     "Retrieve 'foo_pk' from 'footab'" );
    isa_ok( $t1_c1, 'Alzabo::Create::Column',
	    "Object returned from \$table->column" );

    is( $t1_c1->type, 'INTEGER',
	"foo_pk type should be 'INTEGER'" );
    is( scalar @{[$t1_c1->attributes]}, 1,
	"foo_pk should have one attribute" );
    is( ($t1_c1->attributes)[0], $att,
	"foo_pk's attribute should be $att" );
    ok( $t1_c1->has_attribute( attribute => uc $att ),
	"foo_pk should have attribute '\U$att\E' (case-insensitive check)" );
    ok( ! $t1_c1->has_attribute( attribute => uc $att, case_sensitive => 1 ),
	"foo_pk should _not_ have attribute '\U$att\E' (case-sensitive check)" );
    ok( ! $t1_c1->nullable,
	"foo_pk should not be nullable" );

    eval_ok( sub { $t1->add_primary_key($t1_c1) },
	     "Make 'foo_pk' a primary key for 'footab'" );

    ok( $t1_c1->is_primary_key,
	"'foo_pk' should be a primary key" );

    eval_ok( sub { $s->make_table( name => 'bartab' ) },
	     "Make table 'bartab'" );

    my $t2;
    eval_ok( sub { $t2 = $s->table('bartab') },
	     "Retrieve table 'bartab'" );
    isa_ok( $t2, 'Alzabo::Create::Table',
	    "'bartab'" );

    eval_ok( sub { $t2->make_column( name => 'bar_pk',
				     type => 'int',
				     default => 10,
				     sequenced => 1,
				     nullable => 0,
				   ) },
	     "Add 'bar_pk' to 'bartab'" );

    my $t2_c1;
    eval_ok( sub { $t2_c1 = $t2->column('bar_pk') },
	     "Retrieve 'bar_pk' from 'bartab'" );
    isa_ok( $t2_c1, 'Alzabo::Create::Column',
	    "'bar_pk'" );

    is( $t2_c1->default, '10',
	"bar_pk default should be '10'" );

    eval_ok( sub { $t2->add_primary_key($t2_c1) },
	     "Make 'bar_pk' a primary key for 'bartab'" );

    eval_ok( sub { $s->add_relationship( table_from => $t1,
					 table_to   => $t2,
					 cardinality => ['n', 'n'],
					 from_is_dependent => 0,
					 to_is_dependent => 0,
				       ) },
	     "Add many to many relationship from 'footab' to 'bartab'" );

    my $link;
    eval_ok( sub { $link = $s->table('footab_bartab') },
	     "Retrieve linking table 'footab_bartab'" );
    isa_ok( $link, 'Alzabo::Create::Table',
	    "'footab_bartab'" );

    my @t1_fk;
    eval_ok( sub { @t1_fk = $t1->foreign_keys( table => $link,
					       column => $t1_c1 ) },
	     "Retrieve foreign keys to linking table from 'footab'" );

    is( scalar @t1_fk, 1,
	"One and only one foreign key returned from 'footab'" );

t/02-create.t  view on Meta::CPAN

                          primary_key => 1 );

        $s2->add_relationship( table_from => $t1,
                               table_to   => $t2,
                               cardinality => [ 'n', '1' ],
                               from_is_dependent => 0,
                               to_is_dependent => 0,
                             );

        $s2->add_relationship( table_from => $t3,
                               table_to   => $t2,
                               cardinality => [ 'n', '1' ],
                               from_is_dependent => 0,
                               to_is_dependent => 0,
                             );

        $t1->delete_column( $t1->column('t2_pk') );

        my @fk = $t2->all_foreign_keys;
        is( scalar @fk, 1,
            "t2 should still have one foreign key" );
    }

    # test for bug when creating a relationship between two tables,
    # where one table has a VARCHAR/CHAR PK.  bug caused length of
    # created column to be undef.
    {
        my $s2 = Alzabo::Create::Schema->new( name => "foo_$db",
                                              rdbms => $db,
                                            );

        my $t1 = $s2->make_table( name => 't1' );
        my $t2 = $s2->make_table( name => 't2' );
        my $t3 = $s2->make_table( name => 't3' );

        $t1->make_column( name   => 't1_pk',
                          type   => 'varchar',
                          length => 50,
                          primary_key => 1 );

        $t2->make_column( name => 't2_pk',
                          type => 'integer',
                          primary_key => 1 );


        eval_ok( sub { $s2->add_relationship( table_from  => $t1,
                                              table_to    => $t2,
                                              cardinality => [ '1', 'n' ],
                                              from_is_dependent => 0,
                                              to_is_dependent   => 0,
                                            ) },
                 'Add a relationship between two columns where one has a VARCHAR pk',
               );

        ok( $t2->column('t1_pk'), 't2 now has a column called t1_pk' );
    }

    {
        my $t = $s->make_table( name => 'no_pk_table' );

        $t->make_column( name => 'not_a_pk',
                         type => 'integer',
                       );

        eval_ok( sub { my $pk = $t->primary_key },
                 "Calling primary_key on a table without a primary key should not fail" );

        my @pk = $t->primary_key;
        is( scalar @pk, 0,
            "Return val from primary_key on a table without a primary key should be an empty list" );
    }

    {
        my $t1 = $s->make_table( name => 'fk_table1' );
        my $t2 = $s->make_table( name => 'fk_table2' );

        $t1->make_column( name => 'fk_table1_pk',
                          type => 'int',
                          primary_key => 1,
                        );

        $t2->make_column( name => 'fk_table2_pk',
                          type => 'int',
                          primary_key => 1,
                        );

        $t2->make_column( name => 'fk_table1_pk',
                          type => 'int',
                        );

        eval { $s->add_relationship( table_from => $t1,
                                     table_to   => $t2,
                                     cardinality => [ '1', 'n' ],
                                     from_is_dependent => 0,
                                     to_is_dependent => 1,
                                   ) };
        ok( ! $@,
            "call add_relationship where column in table_to already exists" );
    }

    if ( $db eq 'MySQL' )
    {
        $t1->set_attributes( 'TYPE = INNODB' );

        my @att = $t1->attributes;

        is( @att, 1, 't1 has 1 attribute' );
        is( $att[0], 'TYPE = INNODB', 'attribute is "TYPE = INNODB"' );

        my $att_t = $s->make_table( name => 'has_attributes',
                                    attributes =>
                                    [ 'TYPE = INNODB',
                                      'PACK_KEYS = 1 ' ],
                                  );

        @att = $att_t->attributes;

        is( @att, 2, 't1 has 2 attributes' );
        is( $att[0], 'TYPE = INNODB', 'first attribute is "TYPE = INNODB"' );
        is( $att[1], 'PACK_KEYS = 1', 'second attribute is "PACK_KEYS = 1"' );
    }



( run in 0.862 second using v1.01-cache-2.11-cpan-ceb78f64989 )