From: Rafael Kitover Date: Tue, 23 Mar 2010 00:57:48 +0000 (-0400) Subject: test for clashing monikers error X-Git-Tag: 0.06000~33 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f812ef60d51e2783567d12938dfb9f534e2a4cc4;p=dbsrgits%2FDBIx-Class-Schema-Loader.git test for clashing monikers error --- diff --git a/t/23dumpmore.t b/t/23dumpmore.t index 9aae2b1..8ca8acc 100644 --- a/t/23dumpmore.t +++ b/t/23dumpmore.t @@ -6,10 +6,11 @@ use Data::Dumper::Concise; use DBIx::Class::Schema::Loader (); use File::Temp 'tempfile'; use lib qw(t/lib); -use make_dbictest_db; my $DUMP_PATH = './t/_dump'; +my $TEST_DB_CLASS = 'make_dbictest_db'; + sub dump_directly { my %tdata = @_; @@ -22,7 +23,7 @@ sub dump_directly { my @warns; eval { local $SIG{__WARN__} = sub { push(@warns, @_) }; - $schema_class->connect($make_dbictest_db::dsn); + $schema_class->connect(get_dsn(\%tdata)); }; my $err = $@; $schema_class->storage->disconnect if !$err && $schema_class->storage; @@ -44,7 +45,7 @@ sub dump_dbicdump { push @cmd, '-o', "$opt=$val"; } - push @cmd, $tdata{classname}, $make_dbictest_db::dsn; + push @cmd, $tdata{classname}, get_dsn(\%tdata); # make sure our current @INC gets used by dbicdump use Config; @@ -69,6 +70,22 @@ sub dump_dbicdump { return @warns; } +sub get_dsn { + my $opts = shift; + + my $test_db_class = $opts->{test_db_class} || $TEST_DB_CLASS; + + eval "require $test_db_class;"; + die $@ if $@; + + my $dsn = do { + no strict 'refs'; + ${$test_db_class . '::dsn'}; + }; + + return $dsn; +} + sub check_error { my ($got, $expected) = @_; @@ -112,7 +129,9 @@ sub test_dumps { my $schema_path = $DUMP_PATH . '/' . $schema_class; $schema_path =~ s{::}{/}g; - dump_file_like($schema_path . '.pm', @$schema_regexes); + + dump_file_like($schema_path . '.pm', @$schema_regexes) if $schema_regexes; + foreach my $src (keys %$file_regexes) { my $src_file = $schema_path . '/' . $src . '.pm'; dump_file_like($src_file, @{$file_regexes->{$src}}); @@ -212,6 +231,16 @@ do_dump_test( unlink $config_file; +rmtree($DUMP_PATH, 1, 1); + +do_dump_test( + classname => 'DBICTest::Schema::14', + test_db_class => 'make_dbictest_db_clashing_monikers', + error => qr/tables 'bar', 'bars' reduced to the same source moniker 'Bar'/, +); + +rmtree($DUMP_PATH, 1, 1); + # test out the POD do_dump_test( @@ -236,8 +265,8 @@ do_dump_test( qr/package DBICTest::DumpMore::1::Foo;/, qr/=head1 NAME\n\nDBICTest::DumpMore::1::Foo\n\n=cut\n\n/, qr/=head1 ACCESSORS\n\n/, -qr/=head2 fooid\n\n data_type: 'INTEGER'\n default_value: undef\n is_nullable: 1\n size: undef\n\n/, -qr/=head2 footext\n\n data_type: 'TEXT'\n default_value: 'footext'\n extra: {is_footext => 1}\n is_nullable: 1\n size: undef\n\n/, +qr/=head2 fooid\n\n data_type: 'INTEGER'\n is_nullable: 1\n\n/, +qr/=head2 footext\n\n data_type: 'TEXT'\n default_value: 'footext'\n extra: {is_footext => 1}\n is_nullable: 1\n\n/, qr/->set_primary_key/, qr/=head1 RELATIONS\n\n/, qr/=head2 bars\n\nType: has_many\n\nRelated object: L\n\n=cut\n\n/, @@ -247,8 +276,8 @@ qr/1;\n$/, qr/package DBICTest::DumpMore::1::Bar;/, qr/=head1 NAME\n\nDBICTest::DumpMore::1::Bar\n\n=cut\n\n/, qr/=head1 ACCESSORS\n\n/, -qr/=head2 barid\n\n data_type: 'INTEGER'\n default_value: undef\n is_nullable: 1\n size: undef\n\n/, -qr/=head2 fooref\n\n data_type: 'INTEGER'\n default_value: undef\n is_foreign_key: 1\n is_nullable: 1\n size: undef\n\n/, +qr/=head2 barid\n\n data_type: 'INTEGER'\n is_nullable: 1\n\n/, +qr/=head2 fooref\n\n data_type: 'INTEGER'\n is_foreign_key: 1\n is_nullable: 1\n\n/, qr/->set_primary_key/, qr/=head1 RELATIONS\n\n/, qr/=head2 fooref\n\nType: belongs_to\n\nRelated object: L\n\n=cut\n\n/, diff --git a/t/lib/make_dbictest_db_clashing_monikers.pm b/t/lib/make_dbictest_db_clashing_monikers.pm new file mode 100644 index 0000000..5f37fea --- /dev/null +++ b/t/lib/make_dbictest_db_clashing_monikers.pm @@ -0,0 +1,43 @@ +package make_dbictest_db_clashing_monikers; + +use strict; +use warnings; +use DBI; + +eval { require DBD::SQLite }; +my $class = $@ ? 'SQLite2' : 'SQLite'; + +my $fn = './t/dbictest_clashing_tables.db'; + +unlink($fn); +our $dsn = "dbi:$class:dbname=$fn"; +my $dbh = DBI->connect($dsn); + +$dbh->do($_) for ( + q|CREATE TABLE foo ( + fooid INTEGER PRIMARY KEY, + footext TEXT DEFAULT 'footext', + foodt TIMESTAMP DEFAULT CURRENT_TIMESTAMP + )|, + q|CREATE TABLE bar ( + barid INTEGER PRIMARY KEY, + fooref INTEGER REFERENCES foo(fooid) + )|, +# this will cause a singularized moniker clash + q|CREATE TABLE bars ( + barid INTEGER PRIMARY KEY, + fooref INTEGER REFERENCES foo(fooid) + )|, + q|INSERT INTO foo (fooid, footext) VALUES (1,'Foo text for number 1')|, + q|INSERT INTO foo (fooid, footext) VALUES (2,'Foo record associated with the Bar with barid 3')|, + q|INSERT INTO foo (fooid, footext) VALUES (3,'Foo text for number 3')|, + q|INSERT INTO foo (fooid, footext) VALUES (4,'Foo text for number 4')|, + q|INSERT INTO bar VALUES (1,4)|, + q|INSERT INTO bar VALUES (2,3)|, + q|INSERT INTO bar VALUES (3,2)|, + q|INSERT INTO bar VALUES (4,1)|, +); + +END { unlink($fn); } + +1; diff --git a/t/lib/make_dbictest_db_with_unique.pm b/t/lib/make_dbictest_db_with_unique.pm index 4eb8516..62f6f9a 100644 --- a/t/lib/make_dbictest_db_with_unique.pm +++ b/t/lib/make_dbictest_db_with_unique.pm @@ -7,7 +7,7 @@ use DBI; eval { require DBD::SQLite }; my $class = $@ ? 'SQLite2' : 'SQLite'; -my $fn = './t/dbictest.db'; +my $fn = './t/dbictest_with_unique.db'; unlink($fn); our $dsn = "dbi:$class:dbname=$fn";