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