Simplified column handling code, moved primary key defs to Table.pm
[dbsrgits/DBIx-Class.git] / t / cdbi-t / 01-columns.t
1 use strict;
2
3 use Test::More tests => 24;
4
5 #-----------------------------------------------------------------------
6 # Make sure that we can set up columns properly
7 #-----------------------------------------------------------------------
8 package State;
9
10 use base 'DBIx::Class';
11 State->load_components(qw/CDBICompat Core/);
12
13 State->table('State');
14 State->columns(Essential => qw/Abbreviation Name/);
15 State->columns(Primary =>   'Name');
16 State->columns(Weather =>   qw/Rain Snowfall/);
17 State->columns(Other =>     qw/Capital Population/);
18 #State->has_many(cities => "City");
19
20 sub accessor_name {
21         my ($class, $column) = @_;
22         my $return = $column eq "Rain" ? "Rainfall" : $column;
23         return $return;
24 }
25
26 sub mutator_name {
27         my ($class, $column) = @_;
28         my $return = $column eq "Rain" ? "set_Rainfall" : "set_$column";
29         return $return;
30 }
31
32 sub Snowfall { 1 }
33
34
35 package City;
36
37 use base 'DBIx::Class';
38 City->load_components(qw/CDBICompat Core/);
39
40 City->table('City');
41 City->columns(All => qw/Name State Population/);
42 City->has_a(State => 'State');
43
44
45 #-------------------------------------------------------------------------
46 package CD;
47 use base 'DBIx::Class';
48 CD->load_components(qw/CDBICompat Core/);
49
50 CD->table('CD');
51 CD->columns('All' => qw/artist title length/);
52
53 #-------------------------------------------------------------------------
54
55 package main;
56
57 is(State->table,          'State', 'State table()');
58 is(State->primary_column, 'name',  'State primary()');
59 is_deeply [ State->columns('Primary') ] => [qw/name/],
60         'State Primary:' . join ", ", State->columns('Primary');
61 is_deeply [ sort State->columns('Essential') ] => [qw/abbreviation name/],
62         'State Essential:' . join ", ", State->columns('Essential');
63 is_deeply [ sort State->columns('All') ] =>
64         [ sort qw/name abbreviation rain snowfall capital population/ ],
65         'State All:' . join ", ", State->columns('All');
66
67 is(CD->primary_column, 'artist', 'CD primary()');
68 is_deeply [ CD->columns('Primary') ] => [qw/artist/],
69         'CD primary:' . join ", ", CD->columns('Primary');
70 is_deeply [ sort CD->columns('All') ] => [qw/artist length title/],
71         'CD all:' . join ", ", CD->columns('All');
72 is_deeply [ sort CD->columns('Essential') ] => [qw/artist/],
73         'CD essential:' . join ", ", CD->columns('Essential');
74
75 ok(State->find_column('Rain'), 'find_column Rain');
76 ok(State->find_column('rain'), 'find_column rain');
77 ok(!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: {
93           skip "No column objects", 1;
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";
101         @grps = sort @grps; # Because the underlying API is hash-based
102         is $grps[0], 'Other',   " - Other";
103         is $grps[1], 'Weather', " - Weather";
104 }
105
106 #{
107 #        
108 #        package DieTest;
109 #        @DieTest::ISA = qw(DBIx::Class);
110 #        DieTest->load_components(qw/CDBICompat::Retrieve Core/);
111 #        package main;
112 #       local $SIG{__WARN__} = sub { };
113 #       eval { DieTest->retrieve(1) };
114 #       like $@, qr/unless primary columns are defined/, "Need primary key for retrieve";
115 #}
116
117 #-----------------------------------------------------------------------
118 # Make sure that columns inherit properly
119 #-----------------------------------------------------------------------
120 package State;
121
122 package A;
123 @A::ISA = qw(DBIx::Class);
124 __PACKAGE__->load_components(qw/CDBICompat Core/);
125 __PACKAGE__->table('dummy');
126 __PACKAGE__->columns(Primary => 'id');
127
128 package A::B;
129 @A::B::ISA = 'A';
130 __PACKAGE__->table('dummy2');
131 __PACKAGE__->columns(All => qw(id b1));
132
133 package A::C;
134 @A::C::ISA = 'A';
135 __PACKAGE__->table('dummy3');
136 __PACKAGE__->columns(All => qw(id c1 c2 c3));
137
138 package main;
139 is join (' ', sort A->columns),    'id',          "A columns";
140 is join (' ', sort A::B->columns), 'b1 id',       "A::B columns";
141 is join (' ', sort A::C->columns), 'c1 c2 c3 id', "A::C columns";
142