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