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