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