5fd18f2c1a819b4049c3cd4d32759af6a2df9f4e
[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 => 27);
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 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;
102 }