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