add class attribute test
[dbsrgits/DBIx-Data-Store-old.git] / t / 01basic_collection.t
CommitLineData
65b76960 1use Test::More;
2use DBIx::Data::Store;
3use DBIx::Data::Store::CRUD;
4use DBIx::Data::Collection::Set;
65b76960 5use DBI;
6
1e948dd4 7use strict;
8use warnings FATAL => 'all';
9
65b76960 10my $dsn = 'dbi:SQLite:tmp.db';
11
12my @expect;
13
14{
15 unlink('tmp.db');
16 my $dbh = DBI->connect($dsn);
17 $dbh->do(q{
18 CREATE TABLE person (
19 id INTEGER NOT NULL PRIMARY KEY,
20 name VARCHAR(255) NOT NULL
21 )
22 });
23 my $pop = $dbh->prepare(q{INSERT INTO person (name) VALUES (?)});
24 my @names = qw(Joe Jim Bob Pterry);
25 $pop->execute($_) for @names;
26 @expect = do { my $id = 0; map +{ id => ++$id, name => $_ }, @names };
27}
28
29sub make_set {
1e948dd4 30 my ($set, $crud) = @_;
65b76960 31 DBIx::Data::Collection::Set->new(
32 store => DBIx::Data::Store::CRUD->new(
33 raw_store => DBIx::Data::Store->connect($dsn),
34 select_sql => q{SELECT id, name FROM person},
3347c67e 35 select_column_order => [ qw(id name) ],
1e948dd4 36 %$crud,
65b76960 37 ),
1e948dd4 38 %$set
65b76960 39 );
40}
41
42my $set = make_set;
43
44is_deeply([ $set->flatten ], \@expect, 'Basic data out ok (flatten)');
45
46{
47 my $stream = $set->as_stream;
48
49 my @got; while (my ($next) = $stream->next) { push @got, $next }
50
51 is_deeply(\@got, \@expect, 'Basic data out ok (stream)');
52}
53
1e948dd4 54$set = make_set { class => 'Spoon' };
55
56is_deeply(
57 [ $set->flatten ],
58 [ map { bless({ %$_ }, 'Spoon') } @expect ],
59 'Basic data with class out ok'
60);
61
65b76960 62done_testing;