add_relationship, relationship_info, relationships moved to ResultSource
[dbsrgits/DBIx-Class.git] / t / cdbi-t / 01-columns.t
CommitLineData
ea2e61bf 1use strict;
2
d7156e50 3use Test::More tests => 24;
ea2e61bf 4
5#-----------------------------------------------------------------------
6# Make sure that we can set up columns properly
7#-----------------------------------------------------------------------
8package State;
9
8452e496 10use base 'DBIx::Class::Test::SQLite';
ea2e61bf 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
8452e496 36use base 'DBIx::Class::Test::SQLite';
ea2e61bf 37
38City->table('City');
39City->columns(All => qw/Name State Population/);
12bbb339 40City->has_a(State => 'State');
ea2e61bf 41
42
43#-------------------------------------------------------------------------
44package CD;
8452e496 45use base 'DBIx::Class::Test::SQLite';
ea2e61bf 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: {
8fe001e1 90 skip "No column objects", 1;
ea2e61bf 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";
9bc6db13 98 @grps = sort @grps; # Because the underlying API is hash-based
ea2e61bf 99 is $grps[0], 'Other', " - Other";
100 is $grps[1], 'Weather', " - Weather";
101}
102
d7156e50 103#{
104#
105# package DieTest;
106# @DieTest::ISA = qw(DBIx::Class);
107# DieTest->load_components(qw/CDBICompat::Retrieve Core/);
108# package main;
109# local $SIG{__WARN__} = sub { };
110# eval { DieTest->retrieve(1) };
111# like $@, qr/unless primary columns are defined/, "Need primary key for retrieve";
112#}
ea2e61bf 113
114#-----------------------------------------------------------------------
115# Make sure that columns inherit properly
116#-----------------------------------------------------------------------
117package State;
118
119package A;
120@A::ISA = qw(DBIx::Class);
126042ee 121__PACKAGE__->load_components(qw/CDBICompat Core/);
ec77fadc 122__PACKAGE__->table('dummy');
ea2e61bf 123__PACKAGE__->columns(Primary => 'id');
124
125package A::B;
126@A::B::ISA = 'A';
ec77fadc 127__PACKAGE__->table('dummy2');
ea2e61bf 128__PACKAGE__->columns(All => qw(id b1));
129
130package A::C;
131@A::C::ISA = 'A';
ec77fadc 132__PACKAGE__->table('dummy3');
ea2e61bf 133__PACKAGE__->columns(All => qw(id c1 c2 c3));
134
135package main;
136is join (' ', sort A->columns), 'id', "A columns";
137is join (' ', sort A::B->columns), 'b1 id', "A::B columns";
138is join (' ', sort A::C->columns), 'c1 c2 c3 id', "A::C columns";
139