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