add class attribute test
[dbsrgits/DBIx-Data-Store-old.git] / t / 01basic_collection.t
1 use Test::More;
2 use DBIx::Data::Store;
3 use DBIx::Data::Store::CRUD;
4 use DBIx::Data::Collection::Set;
5 use DBI;
6
7 use strict;
8 use warnings FATAL => 'all';
9
10 my $dsn = 'dbi:SQLite:tmp.db';
11
12 my @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
29 sub make_set {
30   my ($set, $crud) = @_;
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},
35       select_column_order => [ qw(id name) ],
36       %$crud,
37     ),
38     %$set
39   );
40 }
41
42 my $set = make_set;
43
44 is_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
54 $set = make_set { class => 'Spoon' };
55
56 is_deeply(
57   [ $set->flatten ],
58   [ map { bless({ %$_ }, 'Spoon') } @expect ],
59   'Basic data with class out ok'
60 );
61
62 done_testing;