32
|
1 package Test::SQL::Schema;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
|
5 use base qw(IMPL::Test::Unit);
|
|
6 __PACKAGE__->PassThroughArgs;
|
|
7
|
33
|
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
|
|
25 my $schema = new IMPL::SQL::Schema(Name => 'dbTest', Version => 1) or failed "Failed to create schema";
|
|
26
|
|
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;
|
|
29
|
|
30 $this->schemaDB($schema);
|
|
31 };
|
|
32
|
|
33 test AddTable => sub {
|
|
34 my ($this) = @_;
|
|
35
|
|
36 my $table = $this->schemaDB->AddTable({Name => 'User'}) or failed "Failed to add a table to the schema";
|
|
37 $table->InsertColumn({
|
|
38 Name => 'Id',
|
|
39 Type => Integer
|
|
40 });
|
|
41 $table->InsertColumn({
|
|
42 Name => 'Login',
|
|
43 Type => Varchar(255)
|
|
44 });
|
|
45 $table->InsertColumn({
|
|
46 Name => 'DisplayName',
|
|
47 CanBeNull => 1,
|
|
48 Type => Varchar(255)
|
|
49 });
|
|
50 $table->InsertColumn({
|
|
51 Name => 'RoleId',
|
|
52 CanBeNull => 1,
|
|
53 Type => Integer
|
|
54 });
|
|
55
|
|
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 };
|
|
81
|
|
82 test Constraints => sub {
|
|
83 my ($this) = @_;
|
|
84
|
|
85 my $table = $this->schemaDB->Tables->{Role} or failed "Failed to get a table";
|
|
86
|
|
87 my $constraint = $table->AddConstraint(
|
|
88 new IMPL::SQL::Schema::Constraint::Unique(
|
|
89 Name => 'Role_ObsoleteId_Uniq',
|
|
90 Table => $table,
|
|
91 Columns => ['ObsoleteId']
|
|
92 )
|
|
93 ) or failed "Failed to add constraint";
|
|
94
|
|
95 failed "Failed to retrieve a constraint" unless ($table->GetColumnConstraints('ObsoleteId'))[0] == $constraint;
|
|
96
|
|
97 $table->RemoveColumn('ObsoleteId',1);
|
|
98
|
|
99 failed "A constraint remains alive after column deletion" unless $constraint->isDisposed;
|
|
100
|
|
101 };
|
|
102
|
|
103 test Dispose => sub {
|
|
104 my ($this) = @_;
|
|
105
|
|
106 $this->schemaDB->Dispose();
|
|
107 };
|
|
108
|
|
109
|
32
|
110 1;
|