annotate _test/Test/SQL/Schema.pm @ 218:358f867712b4

sync
author sergey
date Mon, 20 Aug 2012 17:24:48 +0400
parents 4d0e1962161c
children 4ddb27ff4a0b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
1 package Test::SQL::Schema;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
2 use strict;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
3 use warnings;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
4
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
5 use parent qw(IMPL::Test::Unit);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
6 __PACKAGE__->PassThroughArgs;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
7
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
8 use IMPL::Class::Property;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
9 use IMPL::Class::Property::Direct;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
10
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
11 use IMPL::Test qw(test shared failed);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
12
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
13 BEGIN {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
14 shared public property schemaDB => prop_all;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
15 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
16
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
17 require IMPL::SQL::Schema;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
18 require IMPL::SQL::Schema::Constraint::Unique;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
19
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
20 use IMPL::SQL::Types qw(Integer Varchar);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
21
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
22 test CreateSchema => sub {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
23 my ($this) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
24
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
25 my $schema = new IMPL::SQL::Schema(name => 'dbTest', version => 1) or failed "Failed to create schema";
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
26
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
27 failed "Failed to set a schema name" unless $schema->name eq 'dbTest';
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
28 failed "Failed to set a schema version" unless $schema->version == 1;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
29
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
30 $this->schemaDB($schema);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
31 };
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
32
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
33 test AddTable => sub {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
34 my ($this) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
35
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
36 my $table = $this->schemaDB->AddTable({name => 'User'}) or failed "Failed to add a table to the schema";
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
37 $table->InsertColumn({
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
38 name => 'Id',
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
39 type => Integer
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
40 });
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
41 $table->InsertColumn({
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
42 name => 'Login',
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
43 type => Varchar(255)
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
44 });
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
45 $table->InsertColumn({
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
46 name => 'DisplayName',
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
47 canBeNull => 1,
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
48 type => Varchar(255)
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
49 });
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
50 $table->InsertColumn({
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
51 name => 'RoleId',
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
52 canBeNull => 1,
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
53 type => Integer
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
54 });
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
55
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
56 my $colCount = @{$table->columns};
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
57
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
58 failed "Failed to add columns", "Expected: 4", "Got: ".$colCount unless $colCount == 4;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
59
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
60 my $table2 = $this->schemaDB->AddTable({name => 'Role'});
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
61 $table2->InsertColumn({
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
62 name => 'Id',
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
63 type => Integer
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
64 });
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
65 $table2->InsertColumn({
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
66 name => 'Description',
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
67 type => Varchar(255)
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
68 });
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
69 $table2->InsertColumn({
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
70 name => 'ObsoleteId',
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
71 type => Integer
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
72 });
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
73
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
74 };
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
75
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
76 test SetPrimaryKey => sub {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
77 my ($this) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
78
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
79 my $tableUser = $this->schemaDB->GetTable('User');
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
80 my $tableRole = $this->schemaDB->GetTable('Role');
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
81
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
82 $tableUser->AddConstraint( pk => { columns => ['Id'], name => 'PK' });
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
83 $tableRole->SetPrimaryKey('Id');
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
84
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
85 $tableUser->primaryKey->HasColumn('Id') or failed "A primary key of 'User' table should contain 'Id' column";
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
86 $tableRole->primaryKey->HasColumn('Id') or failed "A primary key of 'Role' table should contain 'Id' column";
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
87
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
88 };
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
89
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
90 test LinkTables => sub {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
91 my ($this) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
92
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
93 my $tableUser = $this->schemaDB->GetTable('User');
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
94 my $tableRole = $this->schemaDB->GetTable('Role');
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
95
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
96 $tableUser->LinkTo($tableRole,'RoleId');
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
97
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
98 $tableUser->GetColumnConstraints('RoleId') == 1 or failed "Wrong constraints count for 'RoleId' column", $tableUser->GetColumnConstraints('RoleId');
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
99 };
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
100
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
101 test AddConstraint => sub {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
102 my ($this) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
103
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
104 my $table = $this->schemaDB->GetTable('Role') or failed "Failed to get a table";
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
105
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
106 my $constraint = $table->AddConstraint(
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
107 new IMPL::SQL::Schema::Constraint::Unique(
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
108 name => 'Role_ObsoleteId_Uniq',
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
109 table => $table,
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
110 columns => ['ObsoleteId']
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
111 )
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
112 ) or failed "Failed to add constraint";
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
113
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
114 failed "Failed to retrieve a constraint" unless ($table->GetColumnConstraints('ObsoleteId'))[0] == $constraint;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
115
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
116 };
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
117
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
118 test RemoveConstraint => sub {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
119 my ($this) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
120
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
121 my $table = $this->schemaDB->GetTable('Role') or failed "Failed to get a table";
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
122 my $constraint = $table->GetConstraint('Role_ObsoleteId_Uniq');
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
123
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
124 eval {
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
125 $table->RemoveColumn('ObsoleteId');
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
126 1;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
127 } and failed "Should not remove column with constraint";
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
128
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
129 $table->RemoveColumn('ObsoleteId','force');
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
130
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
131 failed "A constraint remains alive after column deletion" unless $constraint->isDisposed;
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
132
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
133 };
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
134
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
135 test RemoveTable => sub {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
136 my ($this) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
137
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
138 my $table = $this->schemaDB->GetTable('Role') or failed "Failed to get a table";
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
139
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
140 $this->schemaDB->RemoveTable('Role');
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
141
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
142 $table->isDisposed or failed "A table remains alive after deletion";
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
143
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
144 my $table2 = $this->schemaDB->GetTable('User');
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
145
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
146 $table2->GetColumnConstraints('RoleId') == 0 or failed "A foreign key keept alive";
165
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
147 };
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
148
76515373dac0 Added Class::Template,
wizard
parents: 49
diff changeset
149 test Clone => sub {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
150 my ($this) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
151
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
152 my $clone1 = $this->schemaDB->Clone();
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
153
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
154 $clone1->Dispose();
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
155
4d0e1962161c Replaced tabs with spaces
cin
parents: 168
diff changeset
156 $this->schemaDB->isDisposed and failed "An original schema should not be disposed";
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
157 };
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
158
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
159 test Dispose => sub {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
160 my ($this) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
161
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
162 $this->schemaDB->Dispose();
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
163 };
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
164
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
165
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 33
diff changeset
166 1;