Debian Codegraph @

t/cdbi-t-ocache/01-columns.t

raw
1 use strict;
2 use Test::More tests => 26;
3
4 #-----------------------------------------------------------------------
5 # Make sure that we can set up columns properly
6 #-----------------------------------------------------------------------
7 package State;
8
9 use base 'Class::DBI::Sweet';
10 use Class::DBI::Column;
11
12 State->table('State');
13 State->columns(Essential => qw/Abbreviation Name/);
14 State->columns(Primary => 'Name');
15 State->columns(Weather => qw/Snowfall/,
16 Class::DBI::Column->new('Rain', { accessor => 'Rainfall' })
17 );
18 State->columns(Other => qw/Capital Population/);
19 State->has_many(cities => "City");
20
21 sub mutator_name_for {
22 my ($class, $column) = @_;
23 return "set_" . $column->accessor;
24 }
25
26 sub Snowfall { 1 }
27
28 package City;
29
30 use base 'Class::DBI::Sweet';
31
32 City->table('City');
33 City->columns(All => qw/Name State Population/);
34 City->has_a(State => 'State');
35
36 #-------------------------------------------------------------------------
37 package CD;
38 use base 'Class::DBI::Sweet';
39
40 CD->table('CD');
41 CD->columns('All' => qw/artist title length/);
42
43 #-------------------------------------------------------------------------
44
45 package main;
46
47 is(State->table, 'State', 'State table()');
48 is(State->primary_column, 'name', 'State primary()');
49 is_deeply [ State->columns('Primary') ] => [qw/name/],
50 'State Primary:' . join ", ", State->columns('Primary');
51 is_deeply [ sort State->columns('Essential') ] => [qw/abbreviation name/],
52 'State Essential:' . join ", ", State->columns('Essential');
53 is_deeply [ sort State->columns('All') ] =>
54 [ sort qw/name abbreviation rain snowfall capital population/ ],
55 'State All:' . join ", ", State->columns('All');
56
57 is(CD->primary_column, 'artist', 'CD primary()');
58 is_deeply [ CD->columns('Primary') ] => [qw/artist/],
59 'CD primary:' . join ", ", CD->columns('Primary');
60 is_deeply [ sort CD->columns('All') ] => [qw/artist length title/],
61 'CD all:' . join ", ", CD->columns('All');
62 is_deeply [ sort CD->columns('Essential') ] => [qw/artist/],
63 'CD essential:' . join ", ", CD->columns('Essential');
64
65 {
66 local $SIG{__WARN__} = sub { ok 1, "Error thrown" };
67 ok(!State->columns('Nonsense'), "No Nonsense group");
68 }
69 ok(State->find_column('Rain'), 'find_column Rain');
70 ok(State->find_column('rain'), 'find_column rain');
71 ok(!State->find_column('HGLAGAGlAG'), '!find_column HGLAGAGlAG');
72
73 can_ok +State => qw/Rainfall _Rainfall_accessor set_Rainfall
74 _set_Rainfall_accessor Snowfall _Snowfall_accessor set_Snowfall
75 _set_Snowfall_accessor/;
76
77 foreach my $method (qw/Rain _Rain_accessor rain snowfall/) {
78 ok !State->can($method), "State can't $method";
79 }
80
81 {
82 eval { my @grps = State->__grouper->groups_for("Huh"); };
83 ok $@, "Huh not in groups";
84
85 my @grps =
86 sort State->__grouper->groups_for(State->_find_columns(qw/rain capital/));
87 is @grps, 2, "Rain and Capital = 2 groups";
88 is $grps[0], 'Other', " - Other";
89 is $grps[1], 'Weather', " - Weather";
90 }
91
92 {
93 local $SIG{__WARN__} = sub { };
94 eval { Class::DBI::Sweet->retrieve(1) };
95 like $@, qr/Can't retrieve unless primary columns are defined/,
96 "Need primary key for retrieve";
97 }
98
99 #-----------------------------------------------------------------------
100 # Make sure that columns inherit properly
101 #-----------------------------------------------------------------------
102 package State;
103
104 package A;
105 @A::ISA = qw(Class::DBI::Sweet);
106 __PACKAGE__->columns(Primary => 'id');
107
108 package A::B;
109 @A::B::ISA = 'A';
110 __PACKAGE__->columns(All => qw(id b1));
111
112 package A::C;
113 @A::C::ISA = 'A';
114 __PACKAGE__->columns(All => qw(id c1 c2 c3));
115
116 package main;
117 is join(' ', sort A->columns), 'id', "A columns";
118 is join(' ', sort A::B->columns), 'b1 id', "A::B columns";
119 is join(' ', sort A::C->columns), 'c1 c2 c3 id', "A::C columns";