switch to hashref based for command args and results
[dbsrgits/DBIx-Data-Store-old.git] / t / 01basic_collection.t
1 use strict;
2 use warnings FATAL => 'all';
3 use Test::More;
4 use DBIx::Data::Store;
5 use DBIx::Data::Store::CRUD;
6 use DBIx::Data::Collection::Set;
7
8 use DBI;
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   DBIx::Data::Collection::Set->new(
31     store => DBIx::Data::Store::CRUD->new(
32       raw_store => DBIx::Data::Store->connect($dsn),
33       select_sql => q{SELECT id, name FROM person},
34       select_column_order => [ qw(id name) ],
35     ),
36   );
37 }
38
39 my $set = make_set;
40
41 is_deeply([ $set->flatten ], \@expect, 'Basic data out ok (flatten)');
42
43 {
44   my $stream = $set->as_stream;
45
46   my @got; while (my ($next) = $stream->next) { push @got, $next }
47
48   is_deeply(\@got, \@expect, 'Basic data out ok (stream)');
49 }
50
51 done_testing;