File: 02-create.t

package info (click to toggle)
libalzabo-perl 0.92-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,172 kB
  • ctags: 794
  • sloc: perl: 15,060; makefile: 4
file content (808 lines) | stat: -rw-r--r-- 26,908 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
#!/usr/bin/perl -w

use strict;

use File::Spec;

use lib '.', File::Spec->catdir( File::Spec->curdir, 't', 'lib' );

use Alzabo::Test::Utils;

use Test::More;


use Alzabo::Create;
use Alzabo::Config;


my @db;
my $tests = 0;

my $shared_tests = 150;
my $mysql_only_tests = 7;
my $pg_only_tests = 8;

if ( eval { require DBD::mysql } && ! $@ )
{
    push @db, 'MySQL';
    $tests += $shared_tests;
    $tests += $mysql_only_tests;
}

if ( eval { require DBD::Pg } && ! $@ )
{
    push @db, 'PostgreSQL';
    $tests += $shared_tests;
    $tests += $pg_only_tests;
}

unless ($tests)
{
    plan skip_all => 'no database drivers available';
    exit;
}

plan tests => $tests;

foreach my $db (@db)
{
    my $s = Alzabo::Create::Schema->new( name => "foo_$db",
					 rdbms => $db,
				       );

    ok( $s && ref $s,
	"Create a schema object" );

    ok( $s->name eq "foo_$db",
	"Make sure schema name is " . $s->name );

    isa_ok( $s->rules,"Alzabo::RDBMSRules::$db",
	    "Schema's rules object" );

    isa_ok( $s->driver,"Alzabo::Driver::$db",
	    "Schema's driver object" );

    my $dir = Alzabo::Config->schema_dir;
    {
	eval_ok( sub { $s->save_to_file },
		 "Call schema's save_to_file method" );

	my $base = File::Spec->catdir( $dir, $s->name );
	my $name = $s->name;
	ok( -d $base,
	    "'$base' should exist" );

	ok( -e "$base/$name.create.alz",
	    "'$base/$name.create.alz' file should exist" );
	ok( -e "$base/$name.runtime.alz",
	    "'$base/$name.runtime.alz' file should exist" );
	ok( -e "$base/$name.rdbms",
	    "'$base/$name.rdbms' file should exist" );
    }

    eval_ok( sub { $s->make_table( name => 'footab' ) } , "Make table 'footab'" );

    my $t1;
    eval_ok( sub { $t1 = $s->table('footab') }, "Retrieve 'footab' table from schema" );
    isa_ok( $t1, 'Alzabo::Create::Table',
	    "Object returned from \$s->table" );

    my $att = $db eq 'MySQL' ? 'unsigned' : 'check > 5';

    eval_ok( sub { $t1->make_column( name => 'foo_pk',
				     type => 'int',
				     attributes => [ $att ],
				     sequenced => 1,
				     nullable => 0,
				   ) },
	     "Make column 'foo_pk' in 'footab'" );


    eval { $s->tables( 'footab', 'does not exist' ) };
    like( $@, qr/Table does not exist doesn't exist/,
          "Make sure tables method catches missing tables" );

    eval { $s->table( 'does not exist' ) };
    isa_ok( $@, 'Alzabo::Exception::Params',
          "Make sure table() method catches missing tables" );

    eval { $t1->columns( 'foo_pk', 'does not exist' ) };
    like( $@, qr/Column does not exist doesn't exist/,
          "Make sure columns method catches missing columns" );

    my $t1_c1;
    eval_ok( sub { $t1_c1 = $t1->column('foo_pk') },
	     "Retrieve 'foo_pk' from 'footab'" );
    isa_ok( $t1_c1, 'Alzabo::Create::Column',
	    "Object returned from \$table->column" );

    is( $t1_c1->type, 'INTEGER',
	"foo_pk type should be 'INTEGER'" );
    is( scalar @{[$t1_c1->attributes]}, 1,
	"foo_pk should have one attribute" );
    is( ($t1_c1->attributes)[0], $att,
	"foo_pk's attribute should be $att" );
    ok( $t1_c1->has_attribute( attribute => uc $att ),
	"foo_pk should have attribute '\U$att\E' (case-insensitive check)" );
    ok( ! $t1_c1->has_attribute( attribute => uc $att, case_sensitive => 1 ),
	"foo_pk should _not_ have attribute '\U$att\E' (case-sensitive check)" );
    ok( ! $t1_c1->nullable,
	"foo_pk should not be nullable" );

    eval_ok( sub { $t1->add_primary_key($t1_c1) },
	     "Make 'foo_pk' a primary key for 'footab'" );

    ok( $t1_c1->is_primary_key,
	"'foo_pk' should be a primary key" );

    eval_ok( sub { $s->make_table( name => 'bartab' ) },
	     "Make table 'bartab'" );

    my $t2;
    eval_ok( sub { $t2 = $s->table('bartab') },
	     "Retrieve table 'bartab'" );
    isa_ok( $t2, 'Alzabo::Create::Table',
	    "'bartab'" );

    eval_ok( sub { $t2->make_column( name => 'bar_pk',
				     type => 'int',
				     default => 10,
				     sequenced => 1,
				     nullable => 0,
				   ) },
	     "Add 'bar_pk' to 'bartab'" );

    my $t2_c1;
    eval_ok( sub { $t2_c1 = $t2->column('bar_pk') },
	     "Retrieve 'bar_pk' from 'bartab'" );
    isa_ok( $t2_c1, 'Alzabo::Create::Column',
	    "'bar_pk'" );

    is( $t2_c1->default, '10',
	"bar_pk default should be '10'" );

    eval_ok( sub { $t2->add_primary_key($t2_c1) },
	     "Make 'bar_pk' a primary key for 'bartab'" );

    eval_ok( sub { $s->add_relationship( table_from => $t1,
					 table_to   => $t2,
					 cardinality => ['n', 'n'],
					 from_is_dependent => 0,
					 to_is_dependent => 0,
				       ) },
	     "Add many to many relationship from 'footab' to 'bartab'" );

    my $link;
    eval_ok( sub { $link = $s->table('footab_bartab') },
	     "Retrieve linking table 'footab_bartab'" );
    isa_ok( $link, 'Alzabo::Create::Table',
	    "'footab_bartab'" );

    my @t1_fk;
    eval_ok( sub { @t1_fk = $t1->foreign_keys( table => $link,
					       column => $t1_c1 ) },
	     "Retrieve foreign keys to linking table from 'footab'" );

    is( scalar @t1_fk, 1,
	"One and only one foreign key returned from 'footab'" );

    my $t1_fk = $t1_fk[0];

    isa_ok( $t1_fk, 'Alzabo::Create::ForeignKey',
	    "Return value from footab->foreign_keys" );

    is( $t1_fk->columns_from->name, 'foo_pk',
	"The foreign key from footab to the footab_bartab table's columns_from value should be 'foo_pk'" );
    is( $t1_fk->columns_from->table->name, 'footab',
	"The foreign key columns_from for the footab table should belong to the footab table" );
    is( $t1_fk->columns_to->name, 'foo_pk',
	"The foreign key from footab to the footab_bartab table's columns_to value should be 'foo_pk'" );
    is( $t1_fk->columns_to->table->name, 'footab_bartab',
	"The foreign key columns_to for the footab table should belong to the footab_bartab table" );
    is( $t1_fk->table_from->name, 'footab',
	"The table_from for the foreign key should be footab" );
    is( $t1_fk->table_to->name, 'footab_bartab',
	"The table_to for the foreign key should be footab_bartab" );

    my @t2_fk;
    eval_ok( sub { @t2_fk = $t2->foreign_keys( table => $link,
					       column => $t2_c1 ) },
	     "Retrieve foreign keys from 'bartab' to linking table" );

    is( scalar @t2_fk, 1,
	"Only one foreign key should be returned from 'bartab'" );

    my $t2_fk = $t2_fk[0];

    isa_ok( $t2_fk, 'Alzabo::Create::ForeignKey',
	    "Return value from bartab->foreign_keys" );

    is( $t2_fk->columns_from->name, 'bar_pk',
	"The foreign key from bartab to the table's columns_from value should be 'bar_pk'" );
    is( $t2_fk->columns_from->table->name, 'bartab',
	"The foreign key columns_from for the bartab table should belong to the bartab table" );
    is( $t2_fk->columns_to->name, 'bar_pk',
	"The foreign key from bartab to the linking table's columns_to value should be 'bar_pk'" );
    is( $t2_fk->columns_to->table->name, 'footab_bartab',
	"The foreign key columns_to for the bartab table should belong to the footab_bartab table" );
    is( $t2_fk->table_from->name, 'bartab',
	"The table_from for the foreign key should be bartab" );
    is( $t2_fk->table_to->name, 'footab_bartab',
	"The table_to for the foreign key should be footab_bartab" );

    my @link_fk;
    eval_ok( sub { @link_fk = $link->foreign_keys( table => $t1,
						   column => $link->column('foo_pk') ) },
	     "Retrieve foreign keys from 'footab_bartab' to 'footab'" );

    is( scalar @link_fk, 1,
	"Only one foreign key should be returned from 'footab_bartab'" );

    my $link_fk = $link_fk[0];

    is( $link_fk->columns_from->name, 'foo_pk',
	"The foreign key from footab_bartab to the table's columns_from value should be 'foo_pk'" );
    is( $link_fk->columns_from->table->name, 'footab_bartab',
	"The foreign key columns_from for the footab_bartab table should belong to the footab_bartab table" );
    is( $link_fk->columns_to->name, 'foo_pk',
	"The foreign key from footab_bartab to the linking table's columns_to value should be 'foo_pk'" );
    is( $link_fk->columns_to->table->name, 'footab',
	"The foreign key columns_to for the footab_bartab table should belong to the footab table" );
    is( $link_fk->table_from->name, 'footab_bartab',
	"The table_from for the foreign key should be footab_bartab" );
    is( $link_fk->table_to->name, 'footab',
	"The table_to for the foreign key should be footab" );

    eval_ok( sub { @link_fk = $link->foreign_keys( table => $t2,
						   column => $link->column('bar_pk') ) },
	     "Retrieve foreign keys from 'footab_bartab' to 'bartab'" );

    $link_fk = $link_fk[0];

    is( $link_fk[0]->columns_from->name, 'bar_pk',
	"The foreign key from footab_bartab to the table's columns_from value should be 'bar_pk'" );
    is( $link_fk[0]->columns_from->table->name, 'footab_bartab',
	"The foreign key columns_from for the footab_bartab table should belong to the footab_bartab table" );
    is( $link_fk[0]->columns_to->name, 'bar_pk',
	"The foreign key from footab_bartab to the linking table's columns_to value should be 'bar_pk'" );
    is( $link_fk[0]->columns_to->table->name, 'bartab',
	"The foreign key columns_to for the footab_bartab table should belong to the bartab table" );
    is( $link_fk[0]->table_from->name, 'footab_bartab',
	"The table_from for the foreign key should be footab_bartab" );
    is( $link_fk[0]->table_to->name, 'bartab',
	"The table_to for the foreign key should be bartab" );

    eval_ok( sub { $s->add_relationship( table_from => $t1,
					 table_to => $t2,
					 cardinality => [ 'n', 1 ],
					 from_is_dependent => 0,
					 to_is_dependent => 0,
				       ) },
	     "Create a many to one relation from 'footab' to 'bartab'" );

    my $new_col;
    eval_ok( sub { $new_col = $t1->column('bar_pk') },
	     "Retrieve the newly create 'bar_pk' column from 'footab'" );

    is( $new_col->definition, $t2->column('bar_pk')->definition,
	"bar_pk columns in footab and bartab should share the same definition object" );

    my @fk;
    eval { @fk = $t1->foreign_keys( table => $t2,
				    column => $new_col ); };
    ok( @fk,
	"footab should have a foreign key to bartab from bar_pk" );

    ok( @fk,
	"footab should only have one foreign key to bartab from bar_pk" );

    eval { @fk = $t2->foreign_keys( table => $t1,
				    column => $t2->column('bar_pk') ); };
    ok( @fk,
	"bartab should have a foreign key to footab from bar_pk" );

    ok( @fk,
	"bartab should only have one foreign key to footab from bar_pk" );

    eval_ok( sub { $s->add_relationship( table_from => $t1,
					 table_to => $t2,
					 cardinality => [ 1, 'n' ],
					 from_is_dependent => 0,
					 to_is_dependent => 0,
				       ) },
	     "Create a second relation (this time one to many) from footab to bartab" );

    eval_ok( sub { $new_col = $t2->column('foo_pk') },
	     "Retrieve the newly created foo_pk column from bartab" );

    is( $new_col->definition, $t1->column('foo_pk')->definition,
	"foo_pk columns in footab and bartab should share the same definition object" );

    eval { @fk = $t2->foreign_keys( table => $t1,
				    column => $new_col ); };

    ok( @fk,
	"bartab should have a foreign key to bartab from foo_pk" );

    ok( @fk,
	"bartab should only have one foreign key to bartab from foo_pk" );

    eval { @fk = $t1->foreign_keys( table => $t2,
				    column => $t1->column('foo_pk') ); };

    ok( @fk,
	"footab should have a foreign key to bartab from foo_pk" );

    ok( @fk,
	"footab should only have one foreign key to bartab from foo_pk" );

    $s->make_table( name => 'baztab' );
    my $t3 = $s->table('baztab');

    eval_ok( sub { $s->add_relationship( table_from => $t1,
					 table_to => $t3,
					 cardinality => [ 1, 'n' ],
					 from_is_dependent => 0,
					 to_is_dependent => 0,
				       ) },
	     "Add one to many relation from footab to baztab" );

    eval_ok( sub { $new_col = $t3->column('foo_pk') },
	     "Retrieve the foo_pk column from baztab" );

    is( $new_col->definition, $t1->column('foo_pk')->definition,
	"foo_pk columns in footab and baztab should share the same definition object" );

    eval { @fk = $t3->foreign_keys( table => $t1,
				    column => $new_col ); };
    ok( @fk,
	"baztab should have a foreign key to footab from foo_pk" );

    is( scalar @fk, 1,
	"baztab should only have one foreign key to footab from foo_ok" );

    eval { @fk = $t1->foreign_keys( table => $t3,
				    column => $t1->column('foo_pk') ); };
    ok( @fk,
	"footab should have foreign key to baztab from foo_pk" );

    is( scalar @fk, 1,
	"footab should only have one foreign key to baztab from foo_ok" );

    eval_ok( sub { $s->delete_table($link) },
	     "Delete foo_tab from schema" );

    @fk = $t1->all_foreign_keys;
    is( scalar @fk, 3,
	"footab table should have 3 foreign key after deleting footab_bartab table" );

    @fk = $t2->all_foreign_keys;
    is( scalar @fk, 2,
	"bartab table should have 2 foreign keys after deleting footab_bartab" );

    $s->delete_table($t1);

    @fk = $t3->all_foreign_keys;
    is( scalar @fk, 0,
	"baztab table should have 0 foreign keys after deleting footab table" );

    ok( ! exists $t2->{fk}{footab},
	"The \$t2 object's internal {fk} hash should not have a {footab} entry" );

    my $tc = $s->make_table( name => 'two_col_pk' );
    $tc->make_column( name => 'pk1',
		      type => 'int',
		      primary_key => 1 );

    eval_ok( sub { $tc->make_column( name => 'pk2',
				     type => 'int',
				     primary_key => 1 ) },
	     "Add a second primary column to two_col_pk" );

    my @pk = $tc->primary_key;
    is( scalar @pk, 2,
	"two_col_pk has two primary keys" );

    is( $pk[0]->name, 'pk1',
	"First primary column should be pk1" );

    is( $pk[1]->name, 'pk2',
	"Second primary column should be pk2" );


    $tc->make_column( name => 'non_pk',
		      type => 'varchar',
		      length => 2 );

    my $other = $s->make_table( name => 'other' );
    $other->make_column( name => 'other_pk',
			 type => 'int',
			 primary_key => 1 );
    $other->make_column( name => 'somethin',
			 type => 'text' );

    eval_ok( sub { $s->add_relationship( table_from => $tc,
					 table_to   => $other,
					 cardinality => [ 1, 'n' ],
					 from_is_dependent => 0,
					 to_is_dependent => 0,
				       ) },
	     "Add a one to many relationship from two_col_pk to other" );

    my @cols;
    eval_ok( sub { @cols = $other->columns( 'pk1', 'pk2' ) },
	     "Retrieve pk1 and pk2 column objects from other" );

    my $fk;
    eval_ok( sub { $fk = $other->foreign_keys( table => $tc,
					       column => $tc->column('pk1') ) },
	     "Retrieve the foreign from other to two_col_pk" );

    @cols = $fk->columns_from;

    is( scalar @cols, 2,
	"Foreign key object columns_from should return two objects" );

    is( $cols[0]->name, 'pk1',
	"The first column object should be pk1" );

    is( $cols[1]->name, 'pk2',
	"The second column object should be pk2" );

    is( $cols[0]->table->name, 'other',
	"The first column should belong to the other table" );

    is( $cols[1]->table->name, 'other',
	"The second column should belong to the other table" );

    @cols = $fk->columns_to;

    is( scalar @cols, 2,
	"Foreign key object columns_to should return two objects" );

    is( $cols[0]->name, 'pk1',
	"The first column object should be pk1" );

    is( $cols[1]->name, 'pk2',
	"The second column object should be pk2" );

    is( $cols[0]->table->name, 'two_col_pk',
	"The first column should belong to the two_col_pk table" );

    is( $cols[1]->table->name, 'two_col_pk',
	"The second column should belong to the two_col_pk table" );

    my @pairs = $fk->column_pairs;

    is( scalar @pairs, 2,
	"column_pairs method should return a two value array" );

    is( $pairs[0]->[0]->table->name, 'other',
	"\$pairs[0]->[0] should belong to other" );

    is( $pairs[0]->[0]->name, 'pk1',
	"\$pairs[0]->[0] should be pk1" );

    is( $pairs[0]->[1]->table->name, 'two_col_pk',
	"\$pairs[0]->[1] should belong to two_col_pk" );

    is( $pairs[0]->[1]->name, 'pk1',
	"\$pairs[0]->[1] should be pk1" );

    is( $pairs[1]->[0]->table->name, 'other',
	"\$pairs[1]->[0] should belong to other" );

    is( $pairs[1]->[0]->name, 'pk2',
	"\$pairs[1]->[0] should be pk2" );

    is( $pairs[1]->[1]->table->name, 'two_col_pk',
	"\$pairs[1]->[1] should belong to two_col_pk" );

    is( $pairs[1]->[1]->name, 'pk2',
	"\$pairs[1]->[1] should be pk2" );

    my $tbi = $t1->make_column( name => 'tbi',
				type => 'int',
				nullable => 0 );

    my $index;
    eval_ok( sub { $index = $t1->make_index( columns => [ { column => $tbi } ] ) },
	     "Make an index on tbi column in footab" );

    eval_ok( sub { $t1->set_name('newt1') },
	     "Change footab's name to newt1" );

    eval{ $t1->set_name('bartab') };
    like( $@, qr/Table bartab already exists/,
          "Make sure two tables cannot have the same name" );

    my $index2;
    eval_ok( sub { $index2 = $t1->index($index->id) },
	     "Retrieve index object from table based on \$index->id" );

    is( $index, $index2,
	"The index retrieved from newt1 should be the same as the one made earlier");

    $t1->column('foo_pk')->alter( type => 'varchar',
				  length => 20 );
    if ($db eq 'MySQL')
    {
	ok( ! $t1->column('foo_pk')->attributes,
	    "The unsigned attribute should not have survived the change from 'int' to 'varchar'" );
    }

    if ($db eq 'MySQL')
    {
	eval { $t1->column('foo_pk')->set_type('text') };
	my $e = $@;
	isa_ok( $e, 'Alzabo::Exception::RDBMSRules',
		"Exception thrown from attempt to set a primary key column to the 'text' type" );
    }

    if ($db eq 'PostgreSQL')
    {
	eval { $t1->column('tbi')->set_attributes('unique') };
	ok( ! $@,
	    "Postgres should allow a column to have a UNIQUE attribute" );
    }

    $tbi->alter( type => 'varchar',
		 length => 20 );
    $tbi->set_type('text');

    ok( ! defined $tbi->length,
	"Length should be undef after switching column type from 'varchar' to 'text'" );

    $tbi->alter( type => 'varchar',
		 length => 20 );
    $tbi->set_type('char');
    ok( $tbi->length,
	"Length should remain defined after switching column type from 'varchar' to 'char'" );

    is( $tbi->length, 20,
	"Length should remain set to 20 after switching column type from 'varchar' to 'char'" );

    eval_ok( sub { my @t = $s->tables( qw( bartab baztab ) ) },
	     "Retrieving two tables via the schema's tables method" );

    eval_ok( sub { $s->move_table( table => $other,
				   before => $s->table('bartab') ) },
	     "Move other table before bartab" );

    my @t = $s->tables;
    my $order_ok = 0;
    for (my $x = 0; $x < @t; $x++)
    {
	if ($t[$x]->name eq 'other')
	{
	    $order_ok = 1 if $t[$x+1] && $t[$x+1]->name eq 'bartab';
	    last;
	}
    }
    ok( $order_ok,
	"The move_table method should actually move the tables" );

    eval_ok( sub { $other->move_column( column => $other->column('somethin'),
					before => $other->column('other_pk') ) },
	     "Move a column in the other table" );

    my @c = $other->columns;
    $order_ok = 0;
    for (my $x = 0; $x < @c; $x++)
    {
	if ($c[$x]->name eq 'somethin')
	{
	    $order_ok = 1 if $c[$x+1] && $c[$x+1]->name eq 'other_pk';
	    last;
	}
    }
    ok( $order_ok,
	"The move_column method should actually move the columns" );

    eval_ok( sub { $tbi->set_name('newname') },
	     "Set tbi column name to newname" );

    eval{ $tbi->set_name('foo_pk') };
    like( $@, qr/Column foo_pk already exists/,
          "Make sure two column cannot have the same name" );

    eval_ok( sub { $s->make_table( name => 'YAtable',
				   before => scalar $s->table('other') ) },
	     "Call the make_table method on the schema with a before parameter" );

    eval_ok( sub { $other->make_column( name => 'othertest',
					type => 'int',
					before => scalar $other->column('other_pk') ) },
	     "Call the make_column method with a before parameter" );

    eval { $other->make_column( name => 'bad name',
				type => 'int' ) };
    my $e = $@;
    isa_ok( $e, 'Alzabo::Exception::RDBMSRules',
	    "Exception thrown making a column with a bad name" );

    ok( $other->column('othertest')->is_numeric,
        "Should return true from is_numeric" );
    ok( $other->column('othertest')->is_integer,
        "Should return true from is_integer" );
    is( $other->column('othertest')->generic_type, 'integer',
        "Should return 'integer' from generic_type" );


    eval_ok( sub { $s->add_relationship( columns_from => [ $tc->primary_key ],
					 columns_to   => [ $other->primary_key ],
					 cardinality => [ 'n', 'n' ],
					 from_is_dependent => 0,
					 to_is_dependent => 0,
				       ) },
	     "Add a many to many relationship without specifying tables" );

    {
        my $s2 = Alzabo::Create::Schema->new( name => "foo_$db",
                                              rdbms => $db,
                                            );

        my $t1 = $s2->make_table( name => 't1' );
        my $t2 = $s2->make_table( name => 't2' );
        my $t3 = $s2->make_table( name => 't3' );

        $t1->make_column( name => 't1_pk',
                          type => 'integer',
                          primary_key => 1 );
        $t2->make_column( name => 't2_pk',
                          type => 'integer',
                          primary_key => 1 );
        $t3->make_column( name => 't3_pk',
                          type => 'integer',
                          primary_key => 1 );

        $s2->add_relationship( table_from => $t1,
                               table_to   => $t2,
                               cardinality => [ 'n', '1' ],
                               from_is_dependent => 0,
                               to_is_dependent => 0,
                             );

        $s2->add_relationship( table_from => $t3,
                               table_to   => $t2,
                               cardinality => [ 'n', '1' ],
                               from_is_dependent => 0,
                               to_is_dependent => 0,
                             );

        $t1->delete_column( $t1->column('t2_pk') );

        my @fk = $t2->all_foreign_keys;
        is( scalar @fk, 1,
            "t2 should still have one foreign key" );
    }

    # test for bug when creating a relationship between two tables,
    # where one table has a VARCHAR/CHAR PK.  bug caused length of
    # created column to be undef.
    {
        my $s2 = Alzabo::Create::Schema->new( name => "foo_$db",
                                              rdbms => $db,
                                            );

        my $t1 = $s2->make_table( name => 't1' );
        my $t2 = $s2->make_table( name => 't2' );
        my $t3 = $s2->make_table( name => 't3' );

        $t1->make_column( name   => 't1_pk',
                          type   => 'varchar',
                          length => 50,
                          primary_key => 1 );

        $t2->make_column( name => 't2_pk',
                          type => 'integer',
                          primary_key => 1 );


        eval_ok( sub { $s2->add_relationship( table_from  => $t1,
                                              table_to    => $t2,
                                              cardinality => [ '1', 'n' ],
                                              from_is_dependent => 0,
                                              to_is_dependent   => 0,
                                            ) },
                 'Add a relationship between two columns where one has a VARCHAR pk',
               );

        ok( $t2->column('t1_pk'), 't2 now has a column called t1_pk' );
    }

    {
        my $t = $s->make_table( name => 'no_pk_table' );

        $t->make_column( name => 'not_a_pk',
                         type => 'integer',
                       );

        eval_ok( sub { my $pk = $t->primary_key },
                 "Calling primary_key on a table without a primary key should not fail" );

        my @pk = $t->primary_key;
        is( scalar @pk, 0,
            "Return val from primary_key on a table without a primary key should be an empty list" );
    }

    {
        my $t1 = $s->make_table( name => 'fk_table1' );
        my $t2 = $s->make_table( name => 'fk_table2' );

        $t1->make_column( name => 'fk_table1_pk',
                          type => 'int',
                          primary_key => 1,
                        );

        $t2->make_column( name => 'fk_table2_pk',
                          type => 'int',
                          primary_key => 1,
                        );

        $t2->make_column( name => 'fk_table1_pk',
                          type => 'int',
                        );

        eval { $s->add_relationship( table_from => $t1,
                                     table_to   => $t2,
                                     cardinality => [ '1', 'n' ],
                                     from_is_dependent => 0,
                                     to_is_dependent => 1,
                                   ) };
        ok( ! $@,
            "call add_relationship where column in table_to already exists" );
    }

    if ( $db eq 'MySQL' )
    {
        $t1->set_attributes( 'TYPE = INNODB' );

        my @att = $t1->attributes;

        is( @att, 1, 't1 has 1 attribute' );
        is( $att[0], 'TYPE = INNODB', 'attribute is "TYPE = INNODB"' );

        my $att_t = $s->make_table( name => 'has_attributes',
                                    attributes =>
                                    [ 'TYPE = INNODB',
                                      'PACK_KEYS = 1 ' ],
                                  );

        @att = $att_t->attributes;

        is( @att, 2, 't1 has 2 attributes' );
        is( $att[0], 'TYPE = INNODB', 'first attribute is "TYPE = INNODB"' );
        is( $att[1], 'PACK_KEYS = 1', 'second attribute is "PACK_KEYS = 1"' );
    }
    else
    {
        $t1->set_attributes( 'WITH OIDS' );

        my @att = $t1->attributes;

        is( @att, 1, 't1 has 1 attribute' );
        is( $att[0], 'WITH OIDS', 'attribute is "WITH OIDS"' );

        my $att_t = $s->make_table( name => 'has_attributes',
                                    attributes =>
                                    [ 'WITH OIDS',
                                      'INHERITS footab' ],
                                  );

        @att = $att_t->attributes;

        is( @att, 2, 't1 has 2 attributes' );
        is( $att[0], 'WITH OIDS', 'first attribute is "WITH OIDS"' );
        is( $att[1], 'INHERITS footab', 'second attribute is "INHERITS footab"' );

        my $i;

        eval_ok( sub { $i = $tc->make_index( columns  => [ $tc->column('non_pk') ],
                                             function => 'LOWER(non_pk)',
                                           ) },
                 "make a function index" );

        is( $i->function, 'LOWER(non_pk)', "index function is LOWER(non_pk)" );
    }
}