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