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