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