2e37827ae405e1cfc47c6475995be17423cc756d
[dbsrgits/DBIx-Class.git] / t / cdbi / 04-lazy.t
1 use DBIx::Class::Optional::Dependencies -skip_all_without => 'cdbicompat';
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Warn;
8
9 #----------------------------------------------------------------------
10 # Test lazy loading
11 #----------------------------------------------------------------------
12
13 use lib 't/cdbi/testlib';
14 use Lazy;
15
16 is_deeply [ Lazy->columns('Primary') ],        [qw/this/],      "Pri";
17 is_deeply [ sort Lazy->columns('Essential') ], [qw/opop this/], "Essential";
18 is_deeply [ sort Lazy->columns('things') ],    [qw/that this/], "things";
19 is_deeply [ sort Lazy->columns('horizon') ],   [qw/eep orp/],   "horizon";
20 is_deeply [ sort Lazy->columns('vertical') ],  [qw/oop opop/],  "vertical";
21 is_deeply [ sort Lazy->columns('All') ], [qw/eep oop opop orp that this/], "All";
22
23 {
24   my @groups = Lazy->__grouper->groups_for(Lazy->find_column('this'));
25   is_deeply [ sort @groups ], [sort qw/things Essential Primary/], "this (@groups)";
26 }
27
28 {
29   my @groups = Lazy->__grouper->groups_for(Lazy->find_column('that'));
30   is_deeply \@groups, [qw/things/], "that (@groups)";
31 }
32
33 Lazy->create({ this => 1, that => 2, oop => 3, opop => 4, eep => 5 });
34
35 ok(my $obj = Lazy->retrieve(1), 'Retrieve by Primary');
36 ok($obj->_attribute_exists('this'),  "Gets primary");
37 ok($obj->_attribute_exists('opop'),  "Gets other essential");
38 ok(!$obj->_attribute_exists('that'), "But other things");
39 ok(!$obj->_attribute_exists('eep'),  " nor eep");
40 ok(!$obj->_attribute_exists('orp'),  " nor orp");
41 ok(!$obj->_attribute_exists('oop'),  " nor oop");
42
43 ok(my $val = $obj->eep, 'Fetch eep');
44 ok($obj->_attribute_exists('orp'),   'Gets orp too');
45 ok(!$obj->_attribute_exists('oop'),  'But still not oop');
46 ok(!$obj->_attribute_exists('that'), 'nor that');
47
48 {
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";
55 }
56
57 # Test contructor breaking.
58
59 eval {    # Need a hashref
60   Lazy->create(this => 10, that => 20, oop => 30, opop => 40, eep => 50);
61 };
62 ok($@, $@);
63
64 eval {    # False column
65   Lazy->create({ this => 10, that => 20, theother => 30 });
66 };
67 ok($@, $@);
68
69 eval {    # Multiple false columns
70   Lazy->create({ this => 10, that => 20, theother => 30, andanother => 40 });
71 };
72 ok($@, $@);
73
74
75 warning_like {
76     Lazy->columns( TEMP => qw(that) );
77 } qr/Declaring column that as TEMP but it already exists/;
78
79 # Test that create() and update() throws out columns that changed
80 {
81     my $l = Lazy->create({
82         this => 99,
83         that => 2,
84         oop  => 3,
85         opop => 4,
86     });
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
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;
106
107     $l->delete;
108 }
109
110
111 # Now again for inflated values
112 SKIP: {
113     DBIx::Class::Optional::Dependencies->skip_without( 'Date::Simple>=3.03' );
114     Lazy->has_a(
115         orp     => 'Date::Simple',
116         inflate => sub { Date::Simple->new($_[0] . '-01-01') },
117         deflate => 'format'
118     );
119
120     my $l = Lazy->create({
121         this => 89,
122         that => 2,
123         orp  => 1998,
124     });
125
126     ok $l->db_Main->do(qq{
127         UPDATE @{[ $l->table ]}
128         SET    orp  = ?
129         WHERE  this = ?
130     }, undef, 1987, $l->this);
131
132     is $l->orp, '1987-01-01';
133
134     $l->orp(2007);
135     is $l->orp, '2007-01-01';   # make sure it's inflated
136     $l->update;
137
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';
145
146     $l->delete;
147 }
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     });
159
160     # Delete the object without it knowing.
161     Lazy->db_Main->do(qq[
162         DELETE
163         FROM   @{[ Lazy->table ]}
164         WHERE  this = 99
165     ]);
166
167     $l->eep;
168
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 }
175
176 done_testing;