Merge 'trunk' into 'chaining_fixes'
[dbsrgits/DBIx-Class.git] / t / cdbi / 01-columns.t
CommitLineData
ea2e61bf 1use strict;
2
289ba852 3use Test::More;
97d61088 4use lib 't/cdbi/testlib';
289ba852 5
6BEGIN {
7 eval "use DBIx::Class::CDBICompat;";
e60dc79f 8 plan $@ ? (skip_all => "Class::Trigger and DBIx::ContextualFetch required: $@") : (tests=> 24);
289ba852 9}
10
ea2e61bf 11
12#-----------------------------------------------------------------------
13# Make sure that we can set up columns properly
14#-----------------------------------------------------------------------
15package State;
16
97d61088 17use base 'DBIC::Test::SQLite';
ea2e61bf 18
19State->table('State');
20State->columns(Essential => qw/Abbreviation Name/);
21State->columns(Primary => 'Name');
22State->columns(Weather => qw/Rain Snowfall/);
23State->columns(Other => qw/Capital Population/);
24#State->has_many(cities => "City");
25
05ccf064 26sub accessor_name_for {
6a3bf251 27 my ($class, $column) = @_;
28 my $return = $column eq "Rain" ? "Rainfall" : $column;
29 return $return;
ea2e61bf 30}
31
05ccf064 32sub mutator_name_for {
6a3bf251 33 my ($class, $column) = @_;
34 my $return = $column eq "Rain" ? "set_Rainfall" : "set_$column";
35 return $return;
ea2e61bf 36}
37
38sub Snowfall { 1 }
39
40
41package City;
42
97d61088 43use base 'DBIC::Test::SQLite';
ea2e61bf 44
45City->table('City');
46City->columns(All => qw/Name State Population/);
ea2e61bf 47
97ac49db 48{
49 # Disable the `no such table' warning
50 local $SIG{__WARN__} = sub {
51 my $warning = shift;
52 warn $warning unless ($warning =~ /\Qno such table: City(1)\E/);
53 };
54
55 City->has_a(State => 'State');
56}
ea2e61bf 57
58#-------------------------------------------------------------------------
59package CD;
97d61088 60use base 'DBIC::Test::SQLite';
ea2e61bf 61
62CD->table('CD');
63CD->columns('All' => qw/artist title length/);
64
65#-------------------------------------------------------------------------
66
67package main;
68
69is(State->table, 'State', 'State table()');
70is(State->primary_column, 'name', 'State primary()');
71is_deeply [ State->columns('Primary') ] => [qw/name/],
6a3bf251 72 'State Primary:' . join ", ", State->columns('Primary');
ea2e61bf 73is_deeply [ sort State->columns('Essential') ] => [qw/abbreviation name/],
6a3bf251 74 'State Essential:' . join ", ", State->columns('Essential');
ea2e61bf 75is_deeply [ sort State->columns('All') ] =>
6a3bf251 76 [ sort qw/name abbreviation rain snowfall capital population/ ],
77 'State All:' . join ", ", State->columns('All');
ea2e61bf 78
79is(CD->primary_column, 'artist', 'CD primary()');
80is_deeply [ CD->columns('Primary') ] => [qw/artist/],
6a3bf251 81 'CD primary:' . join ", ", CD->columns('Primary');
ea2e61bf 82is_deeply [ sort CD->columns('All') ] => [qw/artist length title/],
6a3bf251 83 'CD all:' . join ", ", CD->columns('All');
ea2e61bf 84is_deeply [ sort CD->columns('Essential') ] => [qw/artist/],
6a3bf251 85 'CD essential:' . join ", ", CD->columns('Essential');
ea2e61bf 86
87ok(State->find_column('Rain'), 'find_column Rain');
88ok(State->find_column('rain'), 'find_column rain');
89ok(!State->find_column('HGLAGAGlAG'), '!find_column HGLAGAGlAG');
90
91{
6a3bf251 92
ea2e61bf 93 can_ok +State => qw/Rainfall _Rainfall_accessor set_Rainfall
6a3bf251 94 _set_Rainfall_accessor Snowfall _Snowfall_accessor set_Snowfall
95 _set_Snowfall_accessor/;
96
97 foreach my $method (qw/Rain _Rain_accessor rain snowfall/) {
98 ok !State->can($method), "State can't $method";
ea2e61bf 99 }
100
101}
102
103{
6a3bf251 104 SKIP: {
105 skip "No column objects", 1;
ea2e61bf 106
6a3bf251 107 eval { my @grps = State->__grouper->groups_for("Huh"); };
108 ok $@, "Huh not in groups";
109 }
ea2e61bf 110
6a3bf251 111 my @grps = sort State->__grouper->groups_for(State->_find_columns(qw/rain capital/));
112 is @grps, 2, "Rain and Capital = 2 groups";
9bc6db13 113 @grps = sort @grps; # Because the underlying API is hash-based
6a3bf251 114 is $grps[0], 'Other', " - Other";
115 is $grps[1], 'Weather', " - Weather";
ea2e61bf 116}
117
d7156e50 118#{
6a3bf251 119#
d7156e50 120# package DieTest;
121# @DieTest::ISA = qw(DBIx::Class);
122# DieTest->load_components(qw/CDBICompat::Retrieve Core/);
123# package main;
6a3bf251 124# local $SIG{__WARN__} = sub { };
125# eval { DieTest->retrieve(1) };
126# like $@, qr/unless primary columns are defined/, "Need primary key for retrieve";
d7156e50 127#}
ea2e61bf 128
129#-----------------------------------------------------------------------
130# Make sure that columns inherit properly
131#-----------------------------------------------------------------------
132package State;
133
134package A;
135@A::ISA = qw(DBIx::Class);
126042ee 136__PACKAGE__->load_components(qw/CDBICompat Core/);
ec77fadc 137__PACKAGE__->table('dummy');
ea2e61bf 138__PACKAGE__->columns(Primary => 'id');
139
140package A::B;
141@A::B::ISA = 'A';
ec77fadc 142__PACKAGE__->table('dummy2');
ea2e61bf 143__PACKAGE__->columns(All => qw(id b1));
144
145package A::C;
146@A::C::ISA = 'A';
ec77fadc 147__PACKAGE__->table('dummy3');
ea2e61bf 148__PACKAGE__->columns(All => qw(id c1 c2 c3));
149
150package main;
151is join (' ', sort A->columns), 'id', "A columns";
152is join (' ', sort A::B->columns), 'b1 id', "A::B columns";
153is join (' ', sort A::C->columns), 'c1 c2 c3 id', "A::C columns";
154