Institute a central "load this first in testing" package
[dbsrgits/DBIx-Class.git] / t / 100extra_source.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Warn;
8
9 use DBICTest;
10
11 {
12   package DBICTest::ArtistRS;
13   use strict;
14   use warnings;
15   use base qw/DBIx::Class::ResultSet/;
16 }
17
18 my $schema = DBICTest->init_schema();
19 my $artist_source = $schema->source('Artist');
20
21 my $new_source = DBIx::Class::ResultSource::Table->new({
22   %$artist_source,
23   name            => 'artist_preview',
24   resultset_class => 'DBICTest::ArtistRS',
25   _relationships  => {}, # copying them as-is is bad taste
26 });
27 $new_source->add_column('other_col' => { data_type => 'integer', default_value => 1 });
28
29 {
30   $schema->register_extra_source( 'artist->extra' => $new_source );
31
32   my $primary_source = $schema->source('DBICTest::Artist');
33   is($primary_source->source_name, 'Artist', 'original source still primary source');
34   ok(! $primary_source->has_column('other_col'), 'column definition did not leak to original source');
35   isa_ok($schema->resultset ('artist->extra'), 'DBICTest::ArtistRS');
36 }
37
38 warnings_are (sub {
39   my $source = $schema->source('DBICTest::Artist');
40   $schema->register_source($source->source_name, $source);
41 }, [], 're-registering an existing source under the same name causes no warnings' );
42
43 warnings_like (
44   sub {
45     my $new_source_name = 'Artist->preview(artist_preview)';
46     $schema->register_source( $new_source_name => $new_source );
47
48     my $primary_source = $schema->source('DBICTest::Artist');
49     is($primary_source->source_name, $new_source_name, 'new source is primary source');
50     ok($primary_source->has_column('other_col'), 'column correctly defined on new source');
51
52     isa_ok ($schema->resultset ($new_source_name), 'DBICTest::ArtistRS');
53
54     my $original_source = $schema->source('Artist');
55     ok(! $original_source->has_column('other_col'), 'column definition did not leak to original source');
56     isa_ok ($original_source->resultset, 'DBIx::Class::ResultSet');
57     isa_ok ($schema->resultset('Artist'), 'DBIx::Class::ResultSet');
58   },
59   [
60     qr/DBICTest::Artist already had a registered source which was replaced by this call/
61   ],
62   'registering source to an existing result warns'
63 );
64
65 done_testing;