Initial commit of DBIx::Class (experimental Class::DBI-inspired ORM)
[dbsrgits/DBIx-Class.git] / 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';
11
12State->table('State');
13State->columns(Essential => qw/Abbreviation Name/);
14State->columns(Primary => 'Name');
15State->columns(Weather => qw/Rain Snowfall/);
16State->columns(Other => qw/Capital Population/);
17#State->has_many(cities => "City");
18
19sub accessor_name {
20 my ($class, $column) = @_;
21 my $return = $column eq "Rain" ? "Rainfall" : $column;
22 return $return;
23}
24
25sub mutator_name {
26 my ($class, $column) = @_;
27 my $return = $column eq "Rain" ? "set_Rainfall" : "set_$column";
28 return $return;
29}
30
31sub Snowfall { 1 }
32
33
34package City;
35
36use base 'DBIx::Class';
37
38City->table('City');
39City->columns(All => qw/Name State Population/);
40#City->has_a(State => 'State');
41
42
43#-------------------------------------------------------------------------
44package CD;
45use base 'DBIx::Class';
46
47CD->table('CD');
48CD->columns('All' => qw/artist title length/);
49
50#-------------------------------------------------------------------------
51
52package main;
53
54is(State->table, 'State', 'State table()');
55is(State->primary_column, 'name', 'State primary()');
56is_deeply [ State->columns('Primary') ] => [qw/name/],
57 'State Primary:' . join ", ", State->columns('Primary');
58is_deeply [ sort State->columns('Essential') ] => [qw/abbreviation name/],
59 'State Essential:' . join ", ", State->columns('Essential');
60is_deeply [ sort State->columns('All') ] =>
61 [ sort qw/name abbreviation rain snowfall capital population/ ],
62 'State All:' . join ", ", State->columns('All');
63
64is(CD->primary_column, 'artist', 'CD primary()');
65is_deeply [ CD->columns('Primary') ] => [qw/artist/],
66 'CD primary:' . join ", ", CD->columns('Primary');
67is_deeply [ sort CD->columns('All') ] => [qw/artist length title/],
68 'CD all:' . join ", ", CD->columns('All');
69is_deeply [ sort CD->columns('Essential') ] => [qw/artist/],
70 'CD essential:' . join ", ", CD->columns('Essential');
71
72ok(State->find_column('Rain'), 'find_column Rain');
73ok(State->find_column('rain'), 'find_column rain');
74ok(!State->find_column('HGLAGAGlAG'), '!find_column HGLAGAGlAG');
75
76{
77
78 can_ok +State => qw/Rainfall _Rainfall_accessor set_Rainfall
79 _set_Rainfall_accessor Snowfall _Snowfall_accessor set_Snowfall
80 _set_Snowfall_accessor/;
81
82 foreach my $method (qw/Rain _Rain_accessor rain snowfall/) {
83 ok !State->can($method), "State can't $method";
84 }
85
86}
87
88{
89 SKIP: {
90 skip "Different error message", 1;
91
92 eval { my @grps = State->__grouper->groups_for("Huh"); };
93 ok $@, "Huh not in groups";
94 }
95
96 my @grps = sort State->__grouper->groups_for(State->_find_columns(qw/rain capital/));
97 is @grps, 2, "Rain and Capital = 2 groups";
98 my @grps = sort @grps; # Because DBIx::Class is hash-based
99 is $grps[0], 'Other', " - Other";
100 is $grps[1], 'Weather', " - Weather";
101}
102
103SKIP: {
104 skip "->retrieve not yet supported by CDBI compat", 1;
105 local $SIG{__WARN__} = sub { };
106 eval { DBIx::Class->retrieve(1) };
107 like $@, qr/Can't retrieve unless primary columns are defined/, "Need primary key for retrieve";
108}
109
110#-----------------------------------------------------------------------
111# Make sure that columns inherit properly
112#-----------------------------------------------------------------------
113package State;
114
115package A;
116@A::ISA = qw(DBIx::Class);
117__PACKAGE__->columns(Primary => 'id');
118
119package A::B;
120@A::B::ISA = 'A';
121__PACKAGE__->columns(All => qw(id b1));
122
123package A::C;
124@A::C::ISA = 'A';
125__PACKAGE__->columns(All => qw(id c1 c2 c3));
126
127package main;
128is join (' ', sort A->columns), 'id', "A columns";
129is join (' ', sort A::B->columns), 'b1 id', "A::B columns";
130is join (' ', sort A::C->columns), 'c1 c2 c3 id', "A::C columns";
131