Commit | Line | Data |
32746a09 |
1 | use strict; |
2 | use warnings FATAL => 'all'; |
3 | use Test::More; |
4 | |
5 | BEGIN { |
6 | package BasicCollection; |
330d0949 |
7 | require 't/dbix-data-set/01basic_collection.t'; |
32746a09 |
8 | } |
9 | |
10 | sub setup_dbh { BasicCollection::setup_dbh } |
11 | |
12 | my $dbh; |
13 | |
14 | sub setup_db { |
15 | $dbh = setup_dbh; |
16 | $dbh->do("CREATE TABLE spoon ( |
17 | id INTEGER NOT NULL PRIMARY KEY, |
18 | last_frobnicated DATE NOT NULL |
19 | )"); |
20 | $dbh->do("INSERT INTO spoon (last_frobnicated) VALUES ('2009-03-30')") |
21 | for 1 .. 5; |
22 | } |
23 | |
24 | setup_db; |
25 | |
26 | my $store = DBIx::Data::Store::CRUD->new( |
27 | raw_store => BasicCollection::raw_store, |
28 | select_sql => 'SELECT id, last_frobnicated FROM spoon', |
29 | select_column_order => [ qw(id last_frobbed) ], |
30 | update_sql => 'UPDATE spoon SET last_frobnicated = ?', |
31 | update_argument_order => [ qw(last_frobbed) ], |
32 | delete_sql => 'DELETE FROM spoon', |
33 | ); |
34 | |
35 | sub make_set { |
36 | DBIx::Data::Collection::Set->new( |
37 | store => $store, |
38 | set_over => [ 'id' ], |
39 | ); |
40 | } |
41 | |
42 | sub sort_set { sort { $a->{id} <=> $b->{id} } @_ } |
43 | |
44 | my $set = make_set; |
45 | |
46 | my $x = 0; |
47 | |
48 | my @expected = map +{ id => ++$x, last_frobbed => '2009-03-30' }, 1 .. 5; |
49 | |
50 | my @data = $set->flatten; |
51 | |
52 | is_deeply( |
53 | \@data, |
54 | \@expected, |
55 | 'Simple fetch' |
56 | ); |
57 | |
58 | $set->_update_set_in_store({ last_frobbed => '2009-04-01' }); |
59 | |
60 | $_->{last_frobbed} = '2009-04-01' for @expected; |
61 | |
62 | is_deeply( |
63 | \@data, |
64 | \@expected, |
65 | 'After update' |
66 | ); |
67 | |
68 | is_deeply( |
69 | [ sort_set make_set->flatten ], |
70 | \@expected, |
71 | 'Refetch' |
72 | ); |
73 | |
74 | $set->_remove_set_from_store; |
75 | |
76 | @expected = (); |
77 | |
78 | @data = $set->flatten; |
79 | |
80 | is_deeply( |
81 | \@data, |
82 | \@expected, |
83 | 'After delete' |
84 | ); |
85 | |
86 | is_deeply( |
87 | [ sort_set make_set->flatten ], |
88 | \@expected, |
89 | 'Refetch' |
90 | ); |
91 | |
92 | done_testing; |