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