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