annotate _test/Test/SQL/Schema.pm @ 375:441e84031c7b

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