skeleton insert code
[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
d8eb0a3f 14sub sort_set {
15 sort { $a->{name} cmp $b->{name} } @_
16}
17
65b76960 18{
19 unlink('tmp.db');
20 my $dbh = DBI->connect($dsn);
21 $dbh->do(q{
22 CREATE TABLE person (
23 id INTEGER NOT NULL PRIMARY KEY,
24 name VARCHAR(255) NOT NULL
25 )
26 });
27 my $pop = $dbh->prepare(q{INSERT INTO person (name) VALUES (?)});
28 my @names = qw(Joe Jim Bob Pterry);
29 $pop->execute($_) for @names;
d8eb0a3f 30 @expect = sort_set do {
31 my $id = 0; map +{ id => ++$id, name => $_ }, @names
32 };
65b76960 33}
34
35sub make_set {
1e948dd4 36 my ($set, $crud) = @_;
65b76960 37 DBIx::Data::Collection::Set->new(
3a2e7c1c 38 set_over => [ 'id' ],
65b76960 39 store => DBIx::Data::Store::CRUD->new(
40 raw_store => DBIx::Data::Store->connect($dsn),
41 select_sql => q{SELECT id, name FROM person},
3347c67e 42 select_column_order => [ qw(id name) ],
1e948dd4 43 %$crud,
65b76960 44 ),
1e948dd4 45 %$set
65b76960 46 );
47}
48
49my $set = make_set;
50
d8eb0a3f 51is_deeply([ sort_set $set->flatten ], \@expect, 'Basic data out ok (flatten)');
65b76960 52
53{
54 my $stream = $set->as_stream;
55
56 my @got; while (my ($next) = $stream->next) { push @got, $next }
57
d8eb0a3f 58 is_deeply([ sort_set @got ], \@expect, 'Basic data out ok (stream)');
65b76960 59}
60
1e948dd4 61$set = make_set { class => 'Spoon' };
62
63is_deeply(
d8eb0a3f 64 [ sort_set $set->flatten ],
1e948dd4 65 [ map { bless({ %$_ }, 'Spoon') } @expect ],
66 'Basic data with class out ok'
67);
68
3a2e7c1c 69$set = make_set {}, {
70 insert_sql => q{INSERT INTO person (name) VALUES (?) },
71 insert_argument_order => [ 'name' ],
72 insert_command_constructor => sub {
73 require DBIx::Data::Store::Command::Insert::LastInsertId;
74 my $self = shift;
75 DBIx::Data::Store::Command::Insert::LastInsertId->new(
76 id_column => 'id',
77 raw_store => $self->raw_store,
78 insert_call_command => $self->raw_store->new_call_command(@_)
79 );
80 }
81};
82
83my $doug = $set->add({ name => 'Doug' });
84
85use Devel::Dwarn;
86
87Dwarn $doug;
88
65b76960 89done_testing;