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