Mercurial > pub > Impl
comparison _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 |
comparison
equal
deleted
inserted
replaced
164:eb3e9861a761 | 165:76515373dac0 |
---|---|
1 package Test::SQL::Schema; | 1 package Test::SQL::Schema; |
2 use strict; | 2 use strict; |
3 use warnings; | 3 use warnings; |
4 | 4 |
5 use base qw(IMPL::Test::Unit); | 5 use parent qw(IMPL::Test::Unit); |
6 __PACKAGE__->PassThroughArgs; | 6 __PACKAGE__->PassThroughArgs; |
7 | 7 |
8 use IMPL::Class::Property; | 8 use IMPL::Class::Property; |
9 use IMPL::Class::Property::Direct; | 9 use IMPL::Class::Property::Direct; |
10 | 10 |
20 use IMPL::SQL::Types qw(Integer Varchar); | 20 use IMPL::SQL::Types qw(Integer Varchar); |
21 | 21 |
22 test CreateSchema => sub { | 22 test CreateSchema => sub { |
23 my ($this) = @_; | 23 my ($this) = @_; |
24 | 24 |
25 my $schema = new IMPL::SQL::Schema(Name => 'dbTest', Version => 1) or failed "Failed to create schema"; | 25 my $schema = new IMPL::SQL::Schema(name => 'dbTest', version => 1) or failed "Failed to create schema"; |
26 | 26 |
27 failed "Failed to set a schema name" unless $schema->Name eq 'dbTest'; | 27 failed "Failed to set a schema name" unless $schema->name eq 'dbTest'; |
28 failed "Failed to set a schema version" unless $schema->Version == 1; | 28 failed "Failed to set a schema version" unless $schema->version == 1; |
29 | 29 |
30 $this->schemaDB($schema); | 30 $this->schemaDB($schema); |
31 }; | 31 }; |
32 | 32 |
33 test AddTable => sub { | 33 test AddTable => sub { |
34 my ($this) = @_; | 34 my ($this) = @_; |
35 | 35 |
36 my $table = $this->schemaDB->AddTable({Name => 'User'}) or failed "Failed to add a table to the schema"; | 36 my $table = $this->schemaDB->AddTable({name => 'User'}) or failed "Failed to add a table to the schema"; |
37 $table->InsertColumn({ | 37 $table->InsertColumn({ |
38 Name => 'Id', | 38 name => 'Id', |
39 Type => Integer | 39 type => Integer |
40 }); | 40 }); |
41 $table->InsertColumn({ | 41 $table->InsertColumn({ |
42 Name => 'Login', | 42 name => 'Login', |
43 Type => Varchar(255) | 43 type => Varchar(255) |
44 }); | 44 }); |
45 $table->InsertColumn({ | 45 $table->InsertColumn({ |
46 Name => 'DisplayName', | 46 name => 'DisplayName', |
47 CanBeNull => 1, | 47 canBeNull => 1, |
48 Type => Varchar(255) | 48 type => Varchar(255) |
49 }); | 49 }); |
50 $table->InsertColumn({ | 50 $table->InsertColumn({ |
51 Name => 'RoleId', | 51 name => 'RoleId', |
52 CanBeNull => 1, | 52 canBeNull => 1, |
53 Type => Integer | 53 type => Integer |
54 }); | |
55 | |
56 my $colCount = @{$table->columns}; | |
57 | |
58 failed "Failed to add columns", "Expected: 4", "Got: ".$colCount unless $colCount == 4; | |
59 | |
60 my $table2 = $this->schemaDB->AddTable({name => 'Role'}); | |
61 $table2->InsertColumn({ | |
62 name => 'Id', | |
63 type => Integer | |
64 }); | |
65 $table2->InsertColumn({ | |
66 name => 'Description', | |
67 type => Varchar(255) | |
68 }); | |
69 $table2->InsertColumn({ | |
70 name => 'ObsoleteId', | |
71 type => Integer | |
54 }); | 72 }); |
55 | 73 |
56 $table->SetPrimaryKey('Id'); | |
57 | |
58 my $colCount = @{$table->Columns}; | |
59 | |
60 failed "Failed to add columns", "Expected: 4", "Got: ".$colCount unless $colCount == 4; | |
61 failed "Failed to set a primary key" unless $table->PrimaryKey; | |
62 | |
63 my $table2 = $this->schemaDB->AddTable({Name => 'Role'}); | |
64 $table2->InsertColumn({ | |
65 Name => 'Id', | |
66 Type => Integer | |
67 }); | |
68 $table2->InsertColumn({ | |
69 Name => 'Description', | |
70 Type => Varchar(255) | |
71 }); | |
72 $table2->InsertColumn({ | |
73 Name => 'ObsoleteId', | |
74 Type => Integer | |
75 }); | |
76 | |
77 $table2->SetPrimaryKey('Id'); | |
78 | |
79 $table->LinkTo($table2,'RoleId'); | |
80 }; | 74 }; |
81 | 75 |
82 test Constraints => sub { | 76 test SetPrimaryKey => sub { |
77 my ($this) = @_; | |
78 | |
79 my $tableUser = $this->schemaDB->GetTable('User'); | |
80 my $tableRole = $this->schemaDB->GetTable('Role'); | |
81 | |
82 $tableUser->SetPrimaryKey('Id'); | |
83 $tableRole->SetPrimaryKey('Id'); | |
84 | |
85 $tableUser->primaryKey->HasColumn('Id') or failed "A primary key of 'User' table should contain 'Id' column"; | |
86 $tableRole->primaryKey->HasColumn('Id') or failed "A primary key of 'Role' table should contain 'Id' column"; | |
87 | |
88 }; | |
89 | |
90 test LinkTables => sub { | |
91 my ($this) = @_; | |
92 | |
93 my $tableUser = $this->schemaDB->GetTable('User'); | |
94 my $tableRole = $this->schemaDB->GetTable('Role'); | |
95 | |
96 $tableUser->LinkTo($tableRole,'RoleId'); | |
97 | |
98 $tableUser->GetColumnConstraints('RoleId') == 1 or failed "Wrong constraints count for 'RoleId' column", $tableUser->GetColumnConstraints('RoleId'); | |
99 }; | |
100 | |
101 test AddConstraint => sub { | |
83 my ($this) = @_; | 102 my ($this) = @_; |
84 | 103 |
85 my $table = $this->schemaDB->Tables->{Role} or failed "Failed to get a table"; | 104 my $table = $this->schemaDB->GetTable('Role') or failed "Failed to get a table"; |
86 | 105 |
87 my $constraint = $table->AddConstraint( | 106 my $constraint = $table->AddConstraint( |
88 new IMPL::SQL::Schema::Constraint::Unique( | 107 new IMPL::SQL::Schema::Constraint::Unique( |
89 Name => 'Role_ObsoleteId_Uniq', | 108 name => 'Role_ObsoleteId_Uniq', |
90 Table => $table, | 109 table => $table, |
91 Columns => ['ObsoleteId'] | 110 columns => ['ObsoleteId'] |
92 ) | 111 ) |
93 ) or failed "Failed to add constraint"; | 112 ) or failed "Failed to add constraint"; |
94 | 113 |
95 failed "Failed to retrieve a constraint" unless ($table->GetColumnConstraints('ObsoleteId'))[0] == $constraint; | 114 failed "Failed to retrieve a constraint" unless ($table->GetColumnConstraints('ObsoleteId'))[0] == $constraint; |
96 | 115 |
97 $table->RemoveColumn('ObsoleteId',1); | 116 }; |
117 | |
118 test RemoveConstraint => sub { | |
119 my ($this) = @_; | |
120 | |
121 my $table = $this->schemaDB->GetTable('Role') or failed "Failed to get a table"; | |
122 my $constraint = $table->GetConstraint('Role_ObsoleteId_Uniq'); | |
123 | |
124 eval { | |
125 $table->RemoveColumn('ObsoleteId'); | |
126 1; | |
127 } and failed "Should not remove column with constraint"; | |
128 | |
129 $table->RemoveColumn('ObsoleteId','force'); | |
98 | 130 |
99 failed "A constraint remains alive after column deletion" unless $constraint->isDisposed; | 131 failed "A constraint remains alive after column deletion" unless $constraint->isDisposed; |
100 | 132 |
133 }; | |
134 | |
135 test RemoveTable => sub { | |
136 my ($this) = @_; | |
137 | |
138 my $table = $this->schemaDB->GetTable('Role') or failed "Failed to get a table"; | |
139 | |
140 $this->schemaDB->RemoveTable('Role'); | |
141 | |
142 $table->isDisposed or failed "A table remains alive after deletion"; | |
143 | |
144 my $table2 = $this->schemaDB->GetTable('User'); | |
145 | |
146 $table2->GetColumnConstraints('RoleId') == 0 or failed "A foreign key keept alive"; | |
147 }; | |
148 | |
149 test Clone => sub { | |
150 my ($this) = @_; | |
151 | |
152 my $clone1 = $this->schemaDB->Clone(); | |
153 | |
154 $clone1->Dispose(); | |
155 | |
156 $this->schemaDB->isDisposed and failed "An original schema should not be disposed"; | |
101 }; | 157 }; |
102 | 158 |
103 test Dispose => sub { | 159 test Dispose => sub { |
104 my ($this) = @_; | 160 my ($this) = @_; |
105 | 161 |