Mercurial > pub > Impl
diff _test/Test/SQL/Schema.pm @ 165:76515373dac0
Added Class::Template,
Rewritten SQL::Schema
'use parent' directive instead of 'use base'
author | wizard |
---|---|
date | Sat, 23 Apr 2011 23:06:48 +0400 |
parents | 16ada169ca75 |
children | 6148f89bb7bf |
line wrap: on
line diff
--- a/_test/Test/SQL/Schema.pm Mon Mar 28 01:36:24 2011 +0400 +++ b/_test/Test/SQL/Schema.pm Sat Apr 23 23:06:48 2011 +0400 @@ -2,7 +2,7 @@ use strict; use warnings; -use base qw(IMPL::Test::Unit); +use parent qw(IMPL::Test::Unit); __PACKAGE__->PassThroughArgs; use IMPL::Class::Property; @@ -22,10 +22,10 @@ test CreateSchema => sub { my ($this) = @_; - my $schema = new IMPL::SQL::Schema(Name => 'dbTest', Version => 1) or failed "Failed to create schema"; + my $schema = new IMPL::SQL::Schema(name => 'dbTest', version => 1) or failed "Failed to create schema"; - failed "Failed to set a schema name" unless $schema->Name eq 'dbTest'; - failed "Failed to set a schema version" unless $schema->Version == 1; + failed "Failed to set a schema name" unless $schema->name eq 'dbTest'; + failed "Failed to set a schema version" unless $schema->version == 1; $this->schemaDB($schema); }; @@ -33,71 +33,127 @@ test AddTable => sub { my ($this) = @_; - my $table = $this->schemaDB->AddTable({Name => 'User'}) or failed "Failed to add a table to the schema"; + my $table = $this->schemaDB->AddTable({name => 'User'}) or failed "Failed to add a table to the schema"; $table->InsertColumn({ - Name => 'Id', - Type => Integer + name => 'Id', + type => Integer }); $table->InsertColumn({ - Name => 'Login', - Type => Varchar(255) + name => 'Login', + type => Varchar(255) }); $table->InsertColumn({ - Name => 'DisplayName', - CanBeNull => 1, - Type => Varchar(255) + name => 'DisplayName', + canBeNull => 1, + type => Varchar(255) }); $table->InsertColumn({ - Name => 'RoleId', - CanBeNull => 1, - Type => Integer + name => 'RoleId', + canBeNull => 1, + type => Integer }); - - $table->SetPrimaryKey('Id'); - - my $colCount = @{$table->Columns}; + + my $colCount = @{$table->columns}; failed "Failed to add columns", "Expected: 4", "Got: ".$colCount unless $colCount == 4; - failed "Failed to set a primary key" unless $table->PrimaryKey; - my $table2 = $this->schemaDB->AddTable({Name => 'Role'}); + my $table2 = $this->schemaDB->AddTable({name => 'Role'}); $table2->InsertColumn({ - Name => 'Id', - Type => Integer + name => 'Id', + type => Integer }); $table2->InsertColumn({ - Name => 'Description', - Type => Varchar(255) + name => 'Description', + type => Varchar(255) }); $table2->InsertColumn({ - Name => 'ObsoleteId', - Type => Integer + name => 'ObsoleteId', + type => Integer }); - $table2->SetPrimaryKey('Id'); - - $table->LinkTo($table2,'RoleId'); +}; + +test SetPrimaryKey => sub { + my ($this) = @_; + + my $tableUser = $this->schemaDB->GetTable('User'); + my $tableRole = $this->schemaDB->GetTable('Role'); + + $tableUser->SetPrimaryKey('Id'); + $tableRole->SetPrimaryKey('Id'); + + $tableUser->primaryKey->HasColumn('Id') or failed "A primary key of 'User' table should contain 'Id' column"; + $tableRole->primaryKey->HasColumn('Id') or failed "A primary key of 'Role' table should contain 'Id' column"; + }; -test Constraints => sub { +test LinkTables => sub { + my ($this) = @_; + + my $tableUser = $this->schemaDB->GetTable('User'); + my $tableRole = $this->schemaDB->GetTable('Role'); + + $tableUser->LinkTo($tableRole,'RoleId'); + + $tableUser->GetColumnConstraints('RoleId') == 1 or failed "Wrong constraints count for 'RoleId' column", $tableUser->GetColumnConstraints('RoleId'); +}; + +test AddConstraint => sub { my ($this) = @_; - my $table = $this->schemaDB->Tables->{Role} or failed "Failed to get a table"; + my $table = $this->schemaDB->GetTable('Role') or failed "Failed to get a table"; my $constraint = $table->AddConstraint( new IMPL::SQL::Schema::Constraint::Unique( - Name => 'Role_ObsoleteId_Uniq', - Table => $table, - Columns => ['ObsoleteId'] + name => 'Role_ObsoleteId_Uniq', + table => $table, + columns => ['ObsoleteId'] ) ) or failed "Failed to add constraint"; failed "Failed to retrieve a constraint" unless ($table->GetColumnConstraints('ObsoleteId'))[0] == $constraint; - $table->RemoveColumn('ObsoleteId',1); +}; + +test RemoveConstraint => sub { + my ($this) = @_; + + my $table = $this->schemaDB->GetTable('Role') or failed "Failed to get a table"; + my $constraint = $table->GetConstraint('Role_ObsoleteId_Uniq'); + + eval { + $table->RemoveColumn('ObsoleteId'); + 1; + } and failed "Should not remove column with constraint"; + + $table->RemoveColumn('ObsoleteId','force'); failed "A constraint remains alive after column deletion" unless $constraint->isDisposed; - + +}; + +test RemoveTable => sub { + my ($this) = @_; + + my $table = $this->schemaDB->GetTable('Role') or failed "Failed to get a table"; + + $this->schemaDB->RemoveTable('Role'); + + $table->isDisposed or failed "A table remains alive after deletion"; + + my $table2 = $this->schemaDB->GetTable('User'); + + $table2->GetColumnConstraints('RoleId') == 0 or failed "A foreign key keept alive"; +}; + +test Clone => sub { + my ($this) = @_; + + my $clone1 = $this->schemaDB->Clone(); + + $clone1->Dispose(); + + $this->schemaDB->isDisposed and failed "An original schema should not be disposed"; }; test Dispose => sub {