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