Fix the test to skip if MyFoo won't load.
[dbsrgits/DBIx-Class.git] / t / cdbi-t / 04-lazy.t
CommitLineData
510ca912 1use strict;
2use Test::More;
3
289ba852 4
510ca912 5#----------------------------------------------------------------------
6# Test lazy loading
7#----------------------------------------------------------------------
8
9BEGIN {
48e8a885 10 eval "use DBIx::Class::CDBICompat;";
11 if ($@) {
12 plan (skip_all => 'Class::Trigger and DBIx::ContextualFetch required');
13 next;
14 }
510ca912 15 eval "use DBD::SQLite";
b57940c5 16 plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 30);
510ca912 17}
18
19INIT {
20 use lib 't/testlib';
21 use Lazy;
22}
23
24is_deeply [ Lazy->columns('Primary') ], [qw/this/], "Pri";
25is_deeply [ sort Lazy->columns('Essential') ], [qw/opop this/], "Essential";
26is_deeply [ sort Lazy->columns('things') ], [qw/that this/], "things";
27is_deeply [ sort Lazy->columns('horizon') ], [qw/eep orp/], "horizon";
28is_deeply [ sort Lazy->columns('vertical') ], [qw/oop opop/], "vertical";
29is_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
41Lazy->create({ this => 1, that => 2, oop => 3, opop => 4, eep => 5 });
42
43ok(my $obj = Lazy->retrieve(1), 'Retrieve by Primary');
44ok($obj->_attribute_exists('this'), "Gets primary");
45ok($obj->_attribute_exists('opop'), "Gets other essential");
46ok(!$obj->_attribute_exists('that'), "But other things");
47ok(!$obj->_attribute_exists('eep'), " nor eep");
48ok(!$obj->_attribute_exists('orp'), " nor orp");
49ok(!$obj->_attribute_exists('oop'), " nor oop");
50
51ok(my $val = $obj->eep, 'Fetch eep');
52ok($obj->_attribute_exists('orp'), 'Gets orp too');
53ok(!$obj->_attribute_exists('oop'), 'But still not oop');
54ok(!$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
67eval { # Need a hashref
68 Lazy->create(this => 10, that => 20, oop => 30, opop => 40, eep => 50);
69};
70ok($@, $@);
71
72eval { # False column
73 Lazy->create({ this => 10, that => 20, theother => 30 });
74};
75ok($@, $@);
76
77eval { # Multiple false columns
78 Lazy->create({ this => 10, that => 20, theother => 30, andanother => 40 });
79};
80ok($@, $@);
81
c0fcc63f 82
83# Test that 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 $l->oop(32);
93 $l->update;
94
95 ok $l->db_Main->do(qq{
96 UPDATE @{[ $l->table ]}
97 SET oop = ?
98 WHERE this = ?
99 }, undef, 23, $l->this);
100
101 is $l->oop, 23;
b57940c5 102
103 $l->delete;
104}
105
106
107# Now again for inflated values
108{
109 Lazy->has_a(
110 orp => 'Date::Simple',
2801ef58 111 inflate => sub { Date::Simple->new($_[0] . '-01-01') },
b57940c5 112 deflate => 'format'
113 );
114
115 my $l = Lazy->create({
116 this => 89,
117 that => 2,
118 orp => 1998,
119 });
120
121 $l->orp(2007);
122 is $l->orp, '2007-01-01'; # make sure it's inflated
123 $l->update;
124
125 ok $l->db_Main->do(qq{
126 UPDATE @{[ $l->table ]}
127 SET orp = ?
128 WHERE this = ?
129 }, undef, 1942, $l->this);
130
131 is $l->orp, '1942-01-01';
132
133 $l->delete;
c0fcc63f 134}