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 use IMPL::Class::Property::Direct;
|
|
10
|
|
11 use IMPL::Test qw(test shared failed);
|
|
12
|
|
13 BEGIN {
|
|
14 shared public property schemaDB => prop_all;
|
|
15 }
|
|
16
|
|
17 require IMPL::SQL::Schema;
|
|
18 require IMPL::SQL::Schema::Constraint::Unique;
|
|
19
|
|
20 use IMPL::SQL::Types qw(Integer Varchar);
|
|
21
|
|
22 test CreateSchema => sub {
|
|
23 my ($this) = @_;
|
|
24
|
165
|
25 my $schema = new IMPL::SQL::Schema(name => 'dbTest', version => 1) or failed "Failed to create schema";
|
49
|
26
|
165
|
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;
|
49
|
29
|
|
30 $this->schemaDB($schema);
|
|
31 };
|
|
32
|
|
33 test AddTable => sub {
|
|
34 my ($this) = @_;
|
|
35
|
165
|
36 my $table = $this->schemaDB->AddTable({name => 'User'}) or failed "Failed to add a table to the schema";
|
49
|
37 $table->InsertColumn({
|
165
|
38 name => 'Id',
|
|
39 type => Integer
|
49
|
40 });
|
|
41 $table->InsertColumn({
|
165
|
42 name => 'Login',
|
|
43 type => Varchar(255)
|
49
|
44 });
|
|
45 $table->InsertColumn({
|
165
|
46 name => 'DisplayName',
|
|
47 canBeNull => 1,
|
|
48 type => Varchar(255)
|
49
|
49 });
|
|
50 $table->InsertColumn({
|
165
|
51 name => 'RoleId',
|
|
52 canBeNull => 1,
|
|
53 type => Integer
|
49
|
54 });
|
165
|
55
|
|
56 my $colCount = @{$table->columns};
|
49
|
57
|
|
58 failed "Failed to add columns", "Expected: 4", "Got: ".$colCount unless $colCount == 4;
|
|
59
|
165
|
60 my $table2 = $this->schemaDB->AddTable({name => 'Role'});
|
49
|
61 $table2->InsertColumn({
|
165
|
62 name => 'Id',
|
|
63 type => Integer
|
49
|
64 });
|
|
65 $table2->InsertColumn({
|
165
|
66 name => 'Description',
|
|
67 type => Varchar(255)
|
49
|
68 });
|
|
69 $table2->InsertColumn({
|
165
|
70 name => 'ObsoleteId',
|
|
71 type => Integer
|
49
|
72 });
|
|
73
|
165
|
74 };
|
|
75
|
|
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
|
49
|
88 };
|
|
89
|
165
|
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 {
|
49
|
102 my ($this) = @_;
|
|
103
|
165
|
104 my $table = $this->schemaDB->GetTable('Role') or failed "Failed to get a table";
|
49
|
105
|
|
106 my $constraint = $table->AddConstraint(
|
|
107 new IMPL::SQL::Schema::Constraint::Unique(
|
165
|
108 name => 'Role_ObsoleteId_Uniq',
|
|
109 table => $table,
|
|
110 columns => ['ObsoleteId']
|
49
|
111 )
|
|
112 ) or failed "Failed to add constraint";
|
|
113
|
|
114 failed "Failed to retrieve a constraint" unless ($table->GetColumnConstraints('ObsoleteId'))[0] == $constraint;
|
|
115
|
165
|
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');
|
49
|
130
|
|
131 failed "A constraint remains alive after column deletion" unless $constraint->isDisposed;
|
165
|
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";
|
49
|
157 };
|
|
158
|
|
159 test Dispose => sub {
|
|
160 my ($this) = @_;
|
|
161
|
|
162 $this->schemaDB->Dispose();
|
|
163 };
|
|
164
|
|
165
|
|
166 1;
|