Fixup several tests silently broken by 12e7015a
[dbsrgits/DBIx-Class.git] / t / lib / DBICTest / Schema / Artist.pm
1 package # hide from PAUSE
2     DBICTest::Schema::Artist;
3
4 use warnings;
5 use strict;
6
7 use base 'DBICTest::BaseResult';
8 use mro 'c3';
9
10 use DBICTest::Util 'check_customcond_args';
11
12 __PACKAGE__->table('artist');
13 __PACKAGE__->source_info({
14     "source_info_key_A" => "source_info_value_A",
15     "source_info_key_B" => "source_info_value_B",
16     "source_info_key_C" => "source_info_value_C",
17 });
18 __PACKAGE__->add_columns(
19   'artistid' => {
20     data_type => 'integer',
21     is_auto_increment => 1,
22   },
23   'name' => {
24     data_type => 'varchar',
25     size      => 100,
26     is_nullable => 1,
27   },
28   rank => {
29     data_type => 'integer',
30     default_value => 13,
31   },
32   charfield => {
33     data_type => 'char',
34     size => 10,
35     is_nullable => 1,
36   },
37 );
38 __PACKAGE__->set_primary_key('artistid');
39 __PACKAGE__->add_unique_constraint(['name']);
40 __PACKAGE__->add_unique_constraint(artist => ['artistid']); # do not remove, part of a test
41 __PACKAGE__->add_unique_constraint(u_nullable => [qw/charfield rank/]);
42
43
44 __PACKAGE__->mk_classdata('field_name_for', {
45     artistid    => 'primary key',
46     name        => 'artist name',
47 });
48
49 # the undef condition in this rel is *deliberate*
50 # tests oddball legacy syntax
51 __PACKAGE__->has_many(
52     cds => 'DBICTest::Schema::CD', undef,
53     { order_by => { -asc => 'year'} },
54 );
55
56 __PACKAGE__->has_many(
57   cds_cref_cond => 'DBICTest::Schema::CD',
58   sub {
59     # This is for test purposes only. A regular user does not
60     # need to sanity check the passed-in arguments, this is what
61     # the tests are for :)
62     my $args = &check_customcond_args;
63
64     return (
65       { "$args->{foreign_alias}.artist" => { '=' => { -ident => "$args->{self_alias}.artistid"} },
66       },
67       $args->{self_result_object} && {
68         "$args->{foreign_alias}.artist" => $args->{self_rowobj}->artistid,  # keep old rowobj syntax as a test
69       }
70     );
71   },
72 );
73
74 __PACKAGE__->has_many(
75   cds_80s => 'DBICTest::Schema::CD',
76   sub {
77     # This is for test purposes only. A regular user does not
78     # need to sanity check the passed-in arguments, this is what
79     # the tests are for :)
80     my $args = &check_customcond_args;
81
82     return (
83       { "$args->{foreign_alias}.artist" => { '=' => \ "$args->{self_alias}.artistid" },
84         "$args->{foreign_alias}.year"   => { '>' => 1979, '<' => 1990 },
85       },
86       $args->{self_result_object} && {
87         "$args->{foreign_alias}.artist" => { '=' => \[ '?',  $args->{self_result_object}->artistid ] },
88         "$args->{foreign_alias}.year"   => { '>' => 1979, '<' => 1990 },
89       }
90     );
91   },
92 );
93
94
95 __PACKAGE__->has_many(
96   cds_84 => 'DBICTest::Schema::CD',
97   sub {
98     # This is for test purposes only. A regular user does not
99     # need to sanity check the passed-in arguments, this is what
100     # the tests are for :)
101     my $args = &check_customcond_args;
102
103     return (
104       { "$args->{foreign_alias}.artist" => { -ident => "$args->{self_alias}.artistid" },
105         "$args->{foreign_alias}.year"   => 1984,
106       },
107       $args->{self_result_object} && {
108         "$args->{foreign_alias}.artist" => $args->{self_result_object}->artistid,
109         "$args->{foreign_alias}.year"   => 1984,
110       }
111     );
112   }
113 );
114
115
116 __PACKAGE__->has_many(
117   cds_90s => 'DBICTest::Schema::CD',
118   sub {
119     # This is for test purposes only. A regular user does not
120     # need to sanity check the passed-in arguments, this is what
121     # the tests are for :)
122     my $args = &check_customcond_args;
123
124     return (
125       { "$args->{foreign_alias}.artist" => { -ident => "$args->{self_alias}.artistid" },
126         "$args->{foreign_alias}.year"   => { '>' => 1989, '<' => 2000 },
127       }
128     );
129   }
130 );
131
132
133 __PACKAGE__->has_many(
134     cds_unordered => 'DBICTest::Schema::CD'
135 );
136 __PACKAGE__->has_many(
137     cds_very_very_very_long_relationship_name => 'DBICTest::Schema::CD'
138 );
139
140 __PACKAGE__->has_many( twokeys => 'DBICTest::Schema::TwoKeys' );
141 __PACKAGE__->has_many( onekeys => 'DBICTest::Schema::OneKey' );
142
143 __PACKAGE__->has_many(
144   artist_undirected_maps => 'DBICTest::Schema::ArtistUndirectedMap',
145   [ {'foreign.id1' => 'self.artistid'}, {'foreign.id2' => 'self.artistid'} ],
146   { cascade_copy => 0 } # this would *so* not make sense
147 );
148
149 __PACKAGE__->has_many(
150     artwork_to_artist => 'DBICTest::Schema::Artwork_to_Artist' => 'artist_id'
151 );
152 __PACKAGE__->many_to_many('artworks', 'artwork_to_artist', 'artwork');
153
154 __PACKAGE__->has_many(
155     cds_without_genre => 'DBICTest::Schema::CD',
156     sub {
157         # This is for test purposes only. A regular user does not
158         # need to sanity check the passed-in arguments, this is what
159         # the tests are for :)
160         my $args = &check_customcond_args;
161
162         return (
163           {
164             "$args->{foreign_alias}.artist" => { -ident => "$args->{self_alias}.artistid" },
165             "$args->{foreign_alias}.genreid" => undef,
166           }, $args->{self_result_object} && {
167             "$args->{foreign_alias}.artist" => $args->{self_result_object}->artistid,
168             "$args->{foreign_alias}.genreid" => undef,
169           }
170         ),
171     },
172 );
173
174 sub sqlt_deploy_hook {
175   my ($self, $sqlt_table) = @_;
176
177   if ($sqlt_table->schema->translator->producer_type =~ /SQLite$/ ) {
178     $sqlt_table->add_index( name => 'artist_name_hookidx', fields => ['name'] )
179       or die $sqlt_table->error;
180   }
181 }
182
183 sub store_column {
184   my ($self, $name, $value) = @_;
185   $value = 'X '.$value if ($name eq 'name' && defined $value && $value =~ /(X )?store_column test/);
186   $self->next::method($name, $value);
187 }
188
189
190 1;