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