Fix create() in the same way as update() so it throws out the new values and
[dbsrgits/DBIx-Class.git] / t / cdbi-t / 04-lazy.t
1 use strict;
2 use Test::More;
3
4
5 #----------------------------------------------------------------------
6 # Test lazy loading
7 #----------------------------------------------------------------------
8
9 BEGIN {
10   eval "use DBIx::Class::CDBICompat;";
11   if ($@) {
12     plan (skip_all => 'Class::Trigger and DBIx::ContextualFetch required');
13     next;
14   }
15         eval "use DBD::SQLite";
16         plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 34);
17 }
18
19 INIT {
20         use lib 't/testlib';
21         use Lazy;
22 }
23
24 is_deeply [ Lazy->columns('Primary') ],        [qw/this/],      "Pri";
25 is_deeply [ sort Lazy->columns('Essential') ], [qw/opop this/], "Essential";
26 is_deeply [ sort Lazy->columns('things') ],    [qw/that this/], "things";
27 is_deeply [ sort Lazy->columns('horizon') ],   [qw/eep orp/],   "horizon";
28 is_deeply [ sort Lazy->columns('vertical') ],  [qw/oop opop/],  "vertical";
29 is_deeply [ sort Lazy->columns('All') ], [qw/eep oop opop orp that this/], "All";
30
31 {
32         my @groups = Lazy->__grouper->groups_for(Lazy->find_column('this'));
33         is_deeply [ sort @groups ], [sort qw/things Essential Primary/], "this (@groups)";
34 }
35
36 {
37         my @groups = Lazy->__grouper->groups_for(Lazy->find_column('that'));
38         is_deeply \@groups, [qw/things/], "that (@groups)";
39 }
40
41 Lazy->create({ this => 1, that => 2, oop => 3, opop => 4, eep => 5 });
42
43 ok(my $obj = Lazy->retrieve(1), 'Retrieve by Primary');
44 ok($obj->_attribute_exists('this'),  "Gets primary");
45 ok($obj->_attribute_exists('opop'),  "Gets other essential");
46 ok(!$obj->_attribute_exists('that'), "But other things");
47 ok(!$obj->_attribute_exists('eep'),  " nor eep");
48 ok(!$obj->_attribute_exists('orp'),  " nor orp");
49 ok(!$obj->_attribute_exists('oop'),  " nor oop");
50
51 ok(my $val = $obj->eep, 'Fetch eep');
52 ok($obj->_attribute_exists('orp'),   'Gets orp too');
53 ok(!$obj->_attribute_exists('oop'),  'But still not oop');
54 ok(!$obj->_attribute_exists('that'), 'nor that');
55
56 {
57         Lazy->columns(All => qw/this that eep orp oop opop/);
58         ok(my $obj = Lazy->retrieve(1), 'Retrieve by Primary');
59         ok !$obj->_attribute_exists('oop'), " Don't have oop";
60         my $null = $obj->eep;
61         ok !$obj->_attribute_exists('oop'),
62                 " Don't have oop - even after getting eep";
63 }
64
65 # Test contructor breaking.
66
67 eval {    # Need a hashref
68         Lazy->create(this => 10, that => 20, oop => 30, opop => 40, eep => 50);
69 };
70 ok($@, $@);
71
72 eval {    # False column
73         Lazy->create({ this => 10, that => 20, theother => 30 });
74 };
75 ok($@, $@);
76
77 eval {    # Multiple false columns
78         Lazy->create({ this => 10, that => 20, theother => 30, andanother => 40 });
79 };
80 ok($@, $@);
81
82
83 # Test that create() and update() throws out columns that changed
84 {
85     my $l = Lazy->create({
86         this => 99,
87         that => 2,
88         oop  => 3,
89         opop => 4,
90     });
91
92     ok $l->db_Main->do(qq{
93         UPDATE @{[ $l->table ]}
94         SET    oop  = ?
95         WHERE  this = ?
96     }, undef, 87, $l->this);
97
98     is $l->oop, 87;
99
100     $l->oop(32);
101     $l->update;
102
103     ok $l->db_Main->do(qq{
104         UPDATE @{[ $l->table ]}
105         SET    oop  = ?
106         WHERE  this = ?
107     }, undef, 23, $l->this);
108
109     is $l->oop, 23;
110     
111     $l->delete;
112 }
113
114
115 # Now again for inflated values
116 {
117     Lazy->has_a(
118         orp     => 'Date::Simple',
119         inflate => sub { Date::Simple->new($_[0] . '-01-01') },
120         deflate => 'format'
121     );
122     
123     my $l = Lazy->create({
124         this => 89,
125         that => 2,
126         orp  => 1998,
127     });
128
129     ok $l->db_Main->do(qq{
130         UPDATE @{[ $l->table ]}
131         SET    orp  = ?
132         WHERE  this = ?
133     }, undef, 1987, $l->this);
134     
135     is $l->orp, '1987-01-01';
136
137     $l->orp(2007);
138     is $l->orp, '2007-01-01';   # make sure it's inflated
139     $l->update;
140     
141     ok $l->db_Main->do(qq{
142         UPDATE @{[ $l->table ]}
143         SET    orp  = ?
144         WHERE  this = ?
145     }, undef, 1942, $l->this);
146
147     is $l->orp, '1942-01-01';
148     
149     $l->delete;
150 }