1use strict;
2
3use Test::More;
4use lib 't/cdbi/testlib';
5
6BEGIN {
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#-----------------------------------------------------------------------
15package State;
16
17use base 'DBIC::Test::SQLite';
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
26sub accessor_name_for {
27  my ($class, $column) = @_;
28  my $return = $column eq "Rain" ? "Rainfall" : $column;
29  return $return;
30}
31
32sub mutator_name_for {
33  my ($class, $column) = @_;
34  my $return = $column eq "Rain" ? "set_Rainfall" : "set_$column";
35  return $return;
36}
37
38sub Snowfall { 1 }
39
40
41package City;
42
43use base 'DBIC::Test::SQLite';
44
45City->table('City');
46City->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#-------------------------------------------------------------------------
59package CD;
60use base 'DBIC::Test::SQLite';
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/],
72  'State Primary:' . join ", ", State->columns('Primary');
73is_deeply [ sort State->columns('Essential') ] => [qw/abbreviation name/],
74  'State Essential:' . join ", ", State->columns('Essential');
75is_deeply [ sort State->columns('All') ] =>
76  [ sort qw/name abbreviation rain snowfall capital population/ ],
77  'State All:' . join ", ", State->columns('All');
78
79is(CD->primary_column, 'artist', 'CD primary()');
80is_deeply [ CD->columns('Primary') ] => [qw/artist/],
81  'CD primary:' . join ", ", CD->columns('Primary');
82is_deeply [ sort CD->columns('All') ] => [qw/artist length title/],
83  'CD all:' . join ", ", CD->columns('All');
84is_deeply [ sort CD->columns('Essential') ] => [qw/artist/],
85  'CD essential:' . join ", ", CD->columns('Essential');
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{
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#-----------------------------------------------------------------------
132package State;
133
134package A;
135@A::ISA = qw(DBIx::Class);
136__PACKAGE__->load_components(qw/CDBICompat Core/);
137__PACKAGE__->table('dummy');
138__PACKAGE__->columns(Primary => 'id');
139
140package A::B;
141@A::B::ISA = 'A';
142__PACKAGE__->table('dummy2');
143__PACKAGE__->columns(All => qw(id b1));
144
145package A::C;
146@A::C::ISA = 'A';
147__PACKAGE__->table('dummy3');
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
155