165
|
1 package Test::SQL::Traits;
|
|
2 use parent qw(IMPL::Test::Unit);
|
|
3
|
|
4 __PACKAGE__->PassThroughArgs;
|
|
5
|
|
6 use IMPL::lang;
|
|
7 use IMPL::Class::Property;
|
|
8 use IMPL::Test qw(test failed shared assert);
|
|
9
|
|
10 use IMPL::SQL::Schema;
|
|
11 use IMPL::SQL::Schema::Traits;
|
|
12 use IMPL::SQL::Types qw(Integer Varchar DateTime);
|
|
13
|
|
14 BEGIN {
|
|
15 shared public property schema => prop_all;
|
|
16 }
|
|
17
|
|
18 sub StartUnit {
|
|
19 return {
|
|
20 schema => new IMPL::SQL::Schema( name => 'testTraits', version => 1 )
|
|
21 };
|
|
22 }
|
|
23
|
|
24 test CreateTable => sub {
|
|
25 my ($this) = @_;
|
|
26
|
|
27 my $table = $this->schema->AddTable(
|
|
28 new IMPL::SQL::Schema::Traits::Table(
|
|
29 'user'
|
|
30 )
|
|
31 ) or failed "Failed to create table";
|
|
32
|
|
33 $this->schema->GetTable('user') or failed "Can't get a created table";
|
|
34
|
|
35 };
|
|
36
|
|
37 test InsertColumn => sub {
|
|
38 my ($this) = @_;
|
|
39
|
|
40 my $table = $this->schema->GetTable('user');
|
|
41
|
|
42 $table->InsertColumn(
|
|
43 new IMPL::SQL::Schema::Traits::Column(
|
|
44 id => Integer, tag => { auto_increment => 1 }
|
|
45 )
|
|
46 );
|
|
47
|
168
|
48 my $column = $table->GetColumn('id') or failed "Column not found";
|
165
|
49
|
|
50 assert( $column->name eq 'id');
|
167
|
51 assert( $column->type->SameValue(Integer()) );
|
165
|
52 assert( not $column->isNullable );
|
|
53 assert( $column->tag->{auto_increment} );
|
|
54
|
|
55 $table->InsertColumn(
|
|
56 new IMPL::SQL::Schema::Traits::Column(
|
|
57 name => Varchar(255), isNullable => 1
|
|
58 )
|
|
59 );
|
|
60
|
168
|
61 $column = $table->GetColumn('name');
|
165
|
62
|
|
63 assert($column);
|
|
64 assert($column->name eq 'name');
|
167
|
65 assert($column->type->SameValue(Varchar(255)));
|
165
|
66 assert($column->isNullable);
|
|
67 };
|
|
68
|
|
69 test CreateTableWithColumns => sub {
|
|
70 my ($this) = @_;
|
|
71
|
|
72 my $table = $this->schema->AddTable(
|
|
73 new IMPL::SQL::Schema::Traits::Table(
|
|
74 session => [
|
|
75 new IMPL::SQL::Schema::Traits::Column( id => Varchar(64)),
|
|
76 new IMPL::SQL::Schema::Traits::Column( expires => DateTime ),
|
|
77 new IMPL::SQL::Schema::Traits::Column( role => Varchar(64), defaultValue => 'user' )
|
|
78 ]
|
|
79 )
|
|
80 ) or failed "Failed to create table";
|
|
81
|
|
82 assert( $table->ColumnsCount == 3 );
|
|
83
|
168
|
84 assert( my $column = $table->GetColumn('id') );
|
167
|
85 assert($column->type->SameValue(Varchar(64)));
|
165
|
86 assert(not $column->isNullable);
|
|
87
|
168
|
88 assert( $column = $table->GetColumn('role') );
|
165
|
89 assert( $column->defaultValue eq 'user' );
|
|
90 };
|
|
91
|
|
92 sub FinishUnit {
|
|
93 my ($self,$session) = @_;
|
|
94
|
|
95 $self->supercall::FinishUnit();
|
|
96
|
|
97 $session->{schema}->Dispose();
|
|
98 }
|
|
99
|
180
|
100 1;
|