File: 19_view.t

package info (click to toggle)
liborlite-perl 1.97-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 524 kB
  • sloc: perl: 3,693; sql: 97; makefile: 2
file content (200 lines) | stat: -rw-r--r-- 5,405 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/perl

# Tests support for views

use strict;

BEGIN {
	$|  = 1;
	$^W = 1;
}

use Test::More tests => 81;
use File::Spec::Functions ':ALL';
use t::lib::Test;

# Set up again
my $file = test_db();
my $dbh  = create_ok(
	file    => catfile(qw{ t 19_view.sql }),
	connect => [ "dbi:SQLite:$file" ],
);

# Create the test package
eval <<"END_PERL"; die $@ if $@;
package Foo::Bar;

use strict;
use ORLite '$file';

1;
END_PERL

# Simple null transaction to stimulate any errors
Foo::Bar->begin;
Foo::Bar->rollback;

# Check the file name
$file = rel2abs($file);
is( Foo::Bar->sqlite, $file,              '->sqlite ok' );
is( Foo::Bar->dsn,    "dbi:SQLite:$file", '->dsn ok'    );

# Check the schema version
is( Foo::Bar->pragma('user_version'), 0, '->user_version ok' );

# Check metadata methods in the test table
is( Foo::Bar::ViewOne->base, 'Foo::Bar', '->base ok' );
is( Foo::Bar::ViewOne->table, 'view_one', '->table ok' );
my $columns = Foo::Bar::ViewOne->table_info;
is_deeply( $columns, [
	{
		cid        => 0,
		dflt_value => undef,
		name       => 'col1',
		notnull    => 0,
		pk         => 0,
		type       => 'integer',
	},
	{
		cid        => 1,
		dflt_value => undef,
		name       => 'col2',
		notnull    => 0,
		pk         => 0,
		type       => 'string',
	},
], '->table_info ok' );
is( Foo::Bar::TableOne->count, 0, '->count(table) is zero' );
is( Foo::Bar::ViewOne->count, 0, '->count(view) is zero' );

# Populate the test table
ok(
	Foo::Bar::TableOne->create( col1 => 1, col2 => 'foo' ),
	'Created row 1',
);
is( Foo::Bar::TableOne->count, 1, '->count(table) is one' );
is( Foo::Bar::ViewOne->count, 1, '->count(view) is one' );
isa_ok( Foo::Bar::TableOne->load(1), 'Foo::Bar::TableOne' );
my $new = Foo::Bar::TableOne->create( col2 => 'bar' );
isa_ok( $new, 'Foo::Bar::TableOne'  );
is( $new->col1, 2, '->col1 ok' );
is( $new->col2, 'bar', '->col2 ok' );
is( $new->rowid, 2, '->rowid ok' );
ok(
	Foo::Bar::TableOne->create( col2 => 'bar' ),
	'Created row 3',
);

# Check the ->count method
is( Foo::Bar::TableOne->count, 3, 'Found 3 table rows' );
is( Foo::Bar::ViewOne->count, 3, 'Found 3 view rows' );
is( Foo::Bar::TableOne->count('where col2 = ?', 'bar'), 2, 'Table condition count works' );
is( Foo::Bar::ViewOne->count('where col2 = ?', 'bar'), 2, 'View condition count works' );

sub test_ones {
	my $ones = shift;
	is( scalar(@$ones), 3, 'Got 3 objects' );
	isa_ok( $ones->[0], 'Foo::Bar::ViewOne' );
	is( $ones->[0]->col1, 1,     '->col1 ok' );
	is( $ones->[0]->col2, 'foo', '->col2 ok' );
	isa_ok( $ones->[1], 'Foo::Bar::ViewOne' );
	is( $ones->[1]->col1, 2,     '->col1 ok' );
	is( $ones->[1]->col2, 'bar', '->col2 ok' );
	isa_ok( $ones->[2], 'Foo::Bar::ViewOne' );
	is( $ones->[2]->col1, 3,     '->col1 ok' );
	is( $ones->[2]->col2, 'bar', '->col2 ok' );
}

# Fetch the rows (list context)
test_ones(
	[ Foo::Bar::ViewOne->select('order by col1') ]
);

# Fetch the rows (scalar context)
test_ones(
	scalar Foo::Bar::ViewOne->select('order by col1')
);

SCOPE: {
	# Emulate select via iterate
	my $ones = [];
	Foo::Bar::ViewOne->iterate( 'order by col1', sub {
		push @$ones, $_;
	} );
	test_ones( $ones );

	# Partial fetch
	my $short = [];
	Foo::Bar::ViewOne->iterate( 'order by col1', sub {
		push @$short, $_;
		return 0;
	} );
	is( scalar(@$short), 1, 'Found only one record' );
	is_deeply( $short->[0], $ones->[0], 'Found the same first record' );

	# Lower level equivalent
	my $short2 = [];
	Foo::Bar->iterate( 'select * from view_one order by col1', sub {
		push @$short2, $_;
		return 0;
	} );
	is( scalar(@$short2), 1, 'Found only one record' );
	is_deeply( $short2->[0], [ 1, 'foo' ], 'Found correct alternative' );

	# Delete one of the objects via the class delete method
	my @delete = Foo::Bar::TableOne->select('where col2 = ?', 'bar');
	$_->delete foreach @delete;
	is( Foo::Bar::ViewOne->count, 1, 'Confirm 2 rows were deleted' );

	# Truncate so we can continue
	ok( Foo::Bar::TableOne->truncate, '->truncate ok' );
	is( Foo::Bar::ViewOne->count, 0, 'Confirm table/view are empty' );
}

# Database should now be empty
SCOPE: {
	my @none = Foo::Bar::ViewOne->select;
	is_deeply( \@none, [ ], '->select ok with nothing' );

	my $none = Foo::Bar::ViewOne->select;
	is_deeply( $none, [ ], '->select ok with nothing' );
}

# Transaction testing
SCOPE: {
	is( Foo::Bar->connected, !1, '->connected is false' );
	ok( Foo::Bar->begin, '->begin' );
	is( Foo::Bar->connected, 1,  '->connected is true' );
	isa_ok( Foo::Bar::TableOne->create, 'Foo::Bar::TableOne' );
	is( Foo::Bar::ViewOne->count, 1, 'One row created' );
	ok( Foo::Bar->rollback, '->rollback' );
	is( Foo::Bar->connected, !1, '->connected is false' );
	is( Foo::Bar::ViewOne->count, 0, 'Commit ok' );

	ok( Foo::Bar->begin, '->begin' );
	isa_ok( Foo::Bar::TableOne->create, 'Foo::Bar::TableOne' );
	is( Foo::Bar::ViewOne->count, 1, 'One row created' );
	ok( Foo::Bar->commit, '->commit' );
	is( Foo::Bar::ViewOne->count, 1, 'Commit ok' );
}

# Truncate
SCOPE: {
	ok( Foo::Bar::TableOne->truncate, '->truncate ok' );
	is( Foo::Bar::ViewOne->count, 0, 'Commit ok' );	
}





######################################################################
# Exceptions

# Load an object that does not exist
SCOPE: {
	# There should not be any of the state-altering methods
	foreach ( qw{ load insert update delete truncate } ) {
		is( Foo::Bar::ViewOne->can($_), undef, "Method $_ does not exist" );
	}
}