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