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