Commit | Line | Data |
83eef562 |
1 | use DBIx::Class::Optional::Dependencies -skip_all_without => 'cdbicompat'; |
2 | |
510ca912 |
3 | use strict; |
f54428ab |
4 | use warnings; |
83eef562 |
5 | |
510ca912 |
6 | use Test::More; |
a6c52738 |
7 | use Test::Warn; |
289ba852 |
8 | |
510ca912 |
9 | #---------------------------------------------------------------------- |
10 | # Test lazy loading |
11 | #---------------------------------------------------------------------- |
12 | |
a40329c4 |
13 | use lib 't/cdbi/testlib'; |
14 | use Lazy; |
510ca912 |
15 | |
16 | is_deeply [ Lazy->columns('Primary') ], [qw/this/], "Pri"; |
17 | is_deeply [ sort Lazy->columns('Essential') ], [qw/opop this/], "Essential"; |
18 | is_deeply [ sort Lazy->columns('things') ], [qw/that this/], "things"; |
19 | is_deeply [ sort Lazy->columns('horizon') ], [qw/eep orp/], "horizon"; |
20 | is_deeply [ sort Lazy->columns('vertical') ], [qw/oop opop/], "vertical"; |
21 | is_deeply [ sort Lazy->columns('All') ], [qw/eep oop opop orp that this/], "All"; |
22 | |
23 | { |
6a3bf251 |
24 | my @groups = Lazy->__grouper->groups_for(Lazy->find_column('this')); |
25 | is_deeply [ sort @groups ], [sort qw/things Essential Primary/], "this (@groups)"; |
510ca912 |
26 | } |
27 | |
28 | { |
6a3bf251 |
29 | my @groups = Lazy->__grouper->groups_for(Lazy->find_column('that')); |
30 | is_deeply \@groups, [qw/things/], "that (@groups)"; |
510ca912 |
31 | } |
32 | |
33 | Lazy->create({ this => 1, that => 2, oop => 3, opop => 4, eep => 5 }); |
34 | |
35 | ok(my $obj = Lazy->retrieve(1), 'Retrieve by Primary'); |
36 | ok($obj->_attribute_exists('this'), "Gets primary"); |
37 | ok($obj->_attribute_exists('opop'), "Gets other essential"); |
38 | ok(!$obj->_attribute_exists('that'), "But other things"); |
39 | ok(!$obj->_attribute_exists('eep'), " nor eep"); |
40 | ok(!$obj->_attribute_exists('orp'), " nor orp"); |
41 | ok(!$obj->_attribute_exists('oop'), " nor oop"); |
42 | |
43 | ok(my $val = $obj->eep, 'Fetch eep'); |
44 | ok($obj->_attribute_exists('orp'), 'Gets orp too'); |
45 | ok(!$obj->_attribute_exists('oop'), 'But still not oop'); |
46 | ok(!$obj->_attribute_exists('that'), 'nor that'); |
47 | |
48 | { |
6a3bf251 |
49 | Lazy->columns(All => qw/this that eep orp oop opop/); |
50 | ok(my $obj = Lazy->retrieve(1), 'Retrieve by Primary'); |
51 | ok !$obj->_attribute_exists('oop'), " Don't have oop"; |
52 | my $null = $obj->eep; |
53 | ok !$obj->_attribute_exists('oop'), |
54 | " Don't have oop - even after getting eep"; |
510ca912 |
55 | } |
56 | |
57 | # Test contructor breaking. |
58 | |
59 | eval { # Need a hashref |
6a3bf251 |
60 | Lazy->create(this => 10, that => 20, oop => 30, opop => 40, eep => 50); |
510ca912 |
61 | }; |
62 | ok($@, $@); |
63 | |
64 | eval { # False column |
6a3bf251 |
65 | Lazy->create({ this => 10, that => 20, theother => 30 }); |
510ca912 |
66 | }; |
67 | ok($@, $@); |
68 | |
69 | eval { # Multiple false columns |
6a3bf251 |
70 | Lazy->create({ this => 10, that => 20, theother => 30, andanother => 40 }); |
510ca912 |
71 | }; |
72 | ok($@, $@); |
73 | |
c0fcc63f |
74 | |
2c656b2b |
75 | warning_like { |
a6c52738 |
76 | Lazy->columns( TEMP => qw(that) ); |
2c656b2b |
77 | } qr/Declaring column that as TEMP but it already exists/; |
a6c52738 |
78 | |
1d7e89b8 |
79 | # Test that create() and update() throws out columns that changed |
c0fcc63f |
80 | { |
81 | my $l = Lazy->create({ |
82 | this => 99, |
83 | that => 2, |
84 | oop => 3, |
85 | opop => 4, |
86 | }); |
1d7e89b8 |
87 | |
88 | ok $l->db_Main->do(qq{ |
89 | UPDATE @{[ $l->table ]} |
90 | SET oop = ? |
91 | WHERE this = ? |
92 | }, undef, 87, $l->this); |
93 | |
94 | is $l->oop, 87; |
95 | |
c0fcc63f |
96 | $l->oop(32); |
97 | $l->update; |
98 | |
99 | ok $l->db_Main->do(qq{ |
100 | UPDATE @{[ $l->table ]} |
101 | SET oop = ? |
102 | WHERE this = ? |
103 | }, undef, 23, $l->this); |
104 | |
105 | is $l->oop, 23; |
8273e845 |
106 | |
b57940c5 |
107 | $l->delete; |
108 | } |
109 | |
110 | |
111 | # Now again for inflated values |
b8dec578 |
112 | SKIP: { |
461e818a |
113 | DBIx::Class::Optional::Dependencies->skip_without( 'Date::Simple>=3.03' ); |
b57940c5 |
114 | Lazy->has_a( |
115 | orp => 'Date::Simple', |
2801ef58 |
116 | inflate => sub { Date::Simple->new($_[0] . '-01-01') }, |
b57940c5 |
117 | deflate => 'format' |
118 | ); |
8273e845 |
119 | |
b57940c5 |
120 | my $l = Lazy->create({ |
121 | this => 89, |
122 | that => 2, |
123 | orp => 1998, |
124 | }); |
1d7e89b8 |
125 | |
126 | ok $l->db_Main->do(qq{ |
127 | UPDATE @{[ $l->table ]} |
128 | SET orp = ? |
129 | WHERE this = ? |
130 | }, undef, 1987, $l->this); |
8273e845 |
131 | |
1d7e89b8 |
132 | is $l->orp, '1987-01-01'; |
133 | |
b57940c5 |
134 | $l->orp(2007); |
135 | is $l->orp, '2007-01-01'; # make sure it's inflated |
136 | $l->update; |
8273e845 |
137 | |
b57940c5 |
138 | ok $l->db_Main->do(qq{ |
139 | UPDATE @{[ $l->table ]} |
140 | SET orp = ? |
141 | WHERE this = ? |
142 | }, undef, 1942, $l->this); |
143 | |
144 | is $l->orp, '1942-01-01'; |
8273e845 |
145 | |
b57940c5 |
146 | $l->delete; |
c0fcc63f |
147 | } |
b33ae6a0 |
148 | |
149 | |
150 | # Test that a deleted object works |
151 | { |
152 | Lazy->search()->delete_all; |
153 | my $l = Lazy->create({ |
154 | this => 99, |
155 | that => 2, |
156 | oop => 3, |
157 | opop => 4, |
158 | }); |
8273e845 |
159 | |
b33ae6a0 |
160 | # Delete the object without it knowing. |
161 | Lazy->db_Main->do(qq[ |
162 | DELETE |
163 | FROM @{[ Lazy->table ]} |
164 | WHERE this = 99 |
165 | ]); |
8273e845 |
166 | |
b33ae6a0 |
167 | $l->eep; |
8273e845 |
168 | |
b33ae6a0 |
169 | # The problem was when an object had an inflated object |
170 | # loaded. _flesh() would set _column_data to undef and |
171 | # get_column() would think nothing was there. |
172 | # I'm too lazy to set up the proper inflation test. |
173 | ok !exists $l->{_column_data}{orp}; |
174 | } |
d9bd5195 |
175 | |
176 | done_testing; |