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