Commit | Line | Data |
ea2e61bf |
1 | use strict; |
2 | |
289ba852 |
3 | use Test::More; |
97d61088 |
4 | use lib 't/cdbi/testlib'; |
289ba852 |
5 | |
ea2e61bf |
6 | |
7 | #----------------------------------------------------------------------- |
8 | # Make sure that we can set up columns properly |
9 | #----------------------------------------------------------------------- |
10 | package State; |
11 | |
97d61088 |
12 | use base 'DBIC::Test::SQLite'; |
ea2e61bf |
13 | |
14 | State->table('State'); |
15 | State->columns(Essential => qw/Abbreviation Name/); |
16 | State->columns(Primary => 'Name'); |
17 | State->columns(Weather => qw/Rain Snowfall/); |
18 | State->columns(Other => qw/Capital Population/); |
19 | #State->has_many(cities => "City"); |
20 | |
05ccf064 |
21 | sub accessor_name_for { |
6a3bf251 |
22 | my ($class, $column) = @_; |
23 | my $return = $column eq "Rain" ? "Rainfall" : $column; |
24 | return $return; |
ea2e61bf |
25 | } |
26 | |
05ccf064 |
27 | sub mutator_name_for { |
6a3bf251 |
28 | my ($class, $column) = @_; |
29 | my $return = $column eq "Rain" ? "set_Rainfall" : "set_$column"; |
30 | return $return; |
ea2e61bf |
31 | } |
32 | |
33 | sub Snowfall { 1 } |
34 | |
35 | |
36 | package City; |
37 | |
97d61088 |
38 | use base 'DBIC::Test::SQLite'; |
ea2e61bf |
39 | |
40 | City->table('City'); |
41 | City->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 | #------------------------------------------------------------------------- |
54 | package CD; |
97d61088 |
55 | use base 'DBIC::Test::SQLite'; |
ea2e61bf |
56 | |
57 | CD->table('CD'); |
58 | CD->columns('All' => qw/artist title length/); |
59 | |
60 | #------------------------------------------------------------------------- |
61 | |
62 | package main; |
63 | |
64 | is(State->table, 'State', 'State table()'); |
65 | is(State->primary_column, 'name', 'State primary()'); |
66 | is_deeply [ State->columns('Primary') ] => [qw/name/], |
6a3bf251 |
67 | 'State Primary:' . join ", ", State->columns('Primary'); |
ea2e61bf |
68 | is_deeply [ sort State->columns('Essential') ] => [qw/abbreviation name/], |
6a3bf251 |
69 | 'State Essential:' . join ", ", State->columns('Essential'); |
ea2e61bf |
70 | is_deeply [ sort State->columns('All') ] => |
6a3bf251 |
71 | [ sort qw/name abbreviation rain snowfall capital population/ ], |
72 | 'State All:' . join ", ", State->columns('All'); |
ea2e61bf |
73 | |
74 | is(CD->primary_column, 'artist', 'CD primary()'); |
75 | is_deeply [ CD->columns('Primary') ] => [qw/artist/], |
6a3bf251 |
76 | 'CD primary:' . join ", ", CD->columns('Primary'); |
ea2e61bf |
77 | is_deeply [ sort CD->columns('All') ] => [qw/artist length title/], |
6a3bf251 |
78 | 'CD all:' . join ", ", CD->columns('All'); |
ea2e61bf |
79 | is_deeply [ sort CD->columns('Essential') ] => [qw/artist/], |
6a3bf251 |
80 | 'CD essential:' . join ", ", CD->columns('Essential'); |
ea2e61bf |
81 | |
82 | ok(State->find_column('Rain'), 'find_column Rain'); |
83 | ok(State->find_column('rain'), 'find_column rain'); |
84 | ok(!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 | #----------------------------------------------------------------------- |
127 | package State; |
128 | |
129 | package 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 | |
135 | package A::B; |
136 | @A::B::ISA = 'A'; |
ec77fadc |
137 | __PACKAGE__->table('dummy2'); |
ea2e61bf |
138 | __PACKAGE__->columns(All => qw(id b1)); |
139 | |
140 | package A::C; |
141 | @A::C::ISA = 'A'; |
ec77fadc |
142 | __PACKAGE__->table('dummy3'); |
ea2e61bf |
143 | __PACKAGE__->columns(All => qw(id c1 c2 c3)); |
144 | |
145 | package main; |
146 | is join (' ', sort A->columns), 'id', "A columns"; |
147 | is join (' ', sort A::B->columns), 'b1 id', "A::B columns"; |
148 | is join (' ', sort A::C->columns), 'c1 c2 c3 id', "A::C columns"; |
149 | |
d9bd5195 |
150 | done_testing; |