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 {
|
194
|
15 shared public property schema => prop_all;
|
165
|
16 }
|
|
17
|
|
18 sub StartUnit {
|
194
|
19 return {
|
|
20 schema => new IMPL::SQL::Schema( name => 'testTraits', version => 1 )
|
|
21 };
|
165
|
22 }
|
|
23
|
|
24 test CreateTable => sub {
|
194
|
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
|
165
|
35 };
|
|
36
|
|
37 test InsertColumn => sub {
|
194
|
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
|
|
48 my $column = $table->GetColumn('id') or failed "Column not found";
|
|
49
|
|
50 assert( $column->name eq 'id');
|
|
51 assert( $column->type->SameValue(Integer()) );
|
|
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
|
|
61 $column = $table->GetColumn('name');
|
|
62
|
|
63 assert($column);
|
|
64 assert($column->name eq 'name');
|
|
65 assert($column->type->SameValue(Varchar(255)));
|
|
66 assert($column->isNullable);
|
165
|
67 };
|
|
68
|
|
69 test CreateTableWithColumns => sub {
|
194
|
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
|
|
84 assert( my $column = $table->GetColumn('id') );
|
|
85 assert($column->type->SameValue(Varchar(64)));
|
|
86 assert(not $column->isNullable);
|
|
87
|
|
88 assert( $column = $table->GetColumn('role') );
|
|
89 assert( $column->defaultValue eq 'user' );
|
165
|
90 };
|
|
91
|
|
92 sub FinishUnit {
|
194
|
93 my ($self,$session) = @_;
|
|
94
|
|
95 $self->supercall::FinishUnit();
|
|
96
|
|
97 $session->{schema}->Dispose();
|
165
|
98 }
|
|
99
|
180
|
100 1;
|