9db9e27dd5252c2c0fa1ea2f283bc1d93663cc79
[dbsrgits/DBIx-Class.git] / t / cdbi-t / 04-lazy.t
1 use strict;
2 use Test::More;
3
4 #----------------------------------------------------------------------
5 # Test lazy loading
6 #----------------------------------------------------------------------
7
8 BEGIN {
9         eval "use DBD::SQLite";
10         plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 25);
11 }
12
13 INIT {
14         use lib 't/testlib';
15         use Lazy;
16 }
17
18 is_deeply [ Lazy->columns('Primary') ],        [qw/this/],      "Pri";
19 is_deeply [ sort Lazy->columns('Essential') ], [qw/opop this/], "Essential";
20 is_deeply [ sort Lazy->columns('things') ],    [qw/that this/], "things";
21 is_deeply [ sort Lazy->columns('horizon') ],   [qw/eep orp/],   "horizon";
22 is_deeply [ sort Lazy->columns('vertical') ],  [qw/oop opop/],  "vertical";
23 is_deeply [ sort Lazy->columns('All') ], [qw/eep oop opop orp that this/], "All";
24
25 {
26         my @groups = Lazy->__grouper->groups_for(Lazy->find_column('this'));
27         is_deeply [ sort @groups ], [sort qw/things Essential Primary/], "this (@groups)";
28 }
29
30 {
31         my @groups = Lazy->__grouper->groups_for(Lazy->find_column('that'));
32         is_deeply \@groups, [qw/things/], "that (@groups)";
33 }
34
35 Lazy->create({ this => 1, that => 2, oop => 3, opop => 4, eep => 5 });
36
37 ok(my $obj = Lazy->retrieve(1), 'Retrieve by Primary');
38 ok($obj->_attribute_exists('this'),  "Gets primary");
39 ok($obj->_attribute_exists('opop'),  "Gets other essential");
40 ok(!$obj->_attribute_exists('that'), "But other things");
41 ok(!$obj->_attribute_exists('eep'),  " nor eep");
42 ok(!$obj->_attribute_exists('orp'),  " nor orp");
43 ok(!$obj->_attribute_exists('oop'),  " nor oop");
44
45 ok(my $val = $obj->eep, 'Fetch eep');
46 ok($obj->_attribute_exists('orp'),   'Gets orp too');
47 ok(!$obj->_attribute_exists('oop'),  'But still not oop');
48 ok(!$obj->_attribute_exists('that'), 'nor that');
49
50 {
51         Lazy->columns(All => qw/this that eep orp oop opop/);
52         ok(my $obj = Lazy->retrieve(1), 'Retrieve by Primary');
53         ok !$obj->_attribute_exists('oop'), " Don't have oop";
54         my $null = $obj->eep;
55         ok !$obj->_attribute_exists('oop'),
56                 " Don't have oop - even after getting eep";
57 }
58
59 # Test contructor breaking.
60
61 eval {    # Need a hashref
62         Lazy->create(this => 10, that => 20, oop => 30, opop => 40, eep => 50);
63 };
64 ok($@, $@);
65
66 eval {    # False column
67         Lazy->create({ this => 10, that => 20, theother => 30 });
68 };
69 ok($@, $@);
70
71 eval {    # Multiple false columns
72         Lazy->create({ this => 10, that => 20, theother => 30, andanother => 40 });
73 };
74 ok($@, $@);
75