Switch CDBICompat and its tests to OptDeps
[dbsrgits/DBIx-Class.git] / t / cdbi / 04-lazy.t
CommitLineData
83eef562 1use DBIx::Class::Optional::Dependencies -skip_all_without => 'cdbicompat';
2
510ca912 3use strict;
f54428ab 4use warnings;
83eef562 5
510ca912 6use Test::More;
a6c52738 7use Test::Warn;
289ba852 8
510ca912 9#----------------------------------------------------------------------
10# Test lazy loading
11#----------------------------------------------------------------------
12
a40329c4 13use lib 't/cdbi/testlib';
14use Lazy;
510ca912 15
16is_deeply [ Lazy->columns('Primary') ], [qw/this/], "Pri";
17is_deeply [ sort Lazy->columns('Essential') ], [qw/opop this/], "Essential";
18is_deeply [ sort Lazy->columns('things') ], [qw/that this/], "things";
19is_deeply [ sort Lazy->columns('horizon') ], [qw/eep orp/], "horizon";
20is_deeply [ sort Lazy->columns('vertical') ], [qw/oop opop/], "vertical";
21is_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
33Lazy->create({ this => 1, that => 2, oop => 3, opop => 4, eep => 5 });
34
35ok(my $obj = Lazy->retrieve(1), 'Retrieve by Primary');
36ok($obj->_attribute_exists('this'), "Gets primary");
37ok($obj->_attribute_exists('opop'), "Gets other essential");
38ok(!$obj->_attribute_exists('that'), "But other things");
39ok(!$obj->_attribute_exists('eep'), " nor eep");
40ok(!$obj->_attribute_exists('orp'), " nor orp");
41ok(!$obj->_attribute_exists('oop'), " nor oop");
42
43ok(my $val = $obj->eep, 'Fetch eep');
44ok($obj->_attribute_exists('orp'), 'Gets orp too');
45ok(!$obj->_attribute_exists('oop'), 'But still not oop');
46ok(!$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
59eval { # Need a hashref
6a3bf251 60 Lazy->create(this => 10, that => 20, oop => 30, opop => 40, eep => 50);
510ca912 61};
62ok($@, $@);
63
64eval { # False column
6a3bf251 65 Lazy->create({ this => 10, that => 20, theother => 30 });
510ca912 66};
67ok($@, $@);
68
69eval { # Multiple false columns
6a3bf251 70 Lazy->create({ this => 10, that => 20, theother => 30, andanother => 40 });
510ca912 71};
72ok($@, $@);
73
c0fcc63f 74
2c656b2b 75warning_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 112SKIP: {
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
176done_testing;