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