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