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