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