minor change to POD, added limitation for CASCADE
[dbsrgits/DBIx-Class-Journal.git] / t / 01test.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 BEGIN {
9     eval "use DBD::SQLite; use SQL::Translator";
10     plan $@
11         ? ( skip_all => 'needs DBD::SQLite and SQL::Translator for testing' )
12         : ( tests => 21 );
13 }
14
15 my $schema = DBICTest->init_schema(no_populate => 1);
16
17 ok($schema, 'Created a Schema');
18
19 isa_ok(
20    $schema->_journal_schema,
21    'DBIx::Class::Schema::Journal::DB',
22    'Actually have a schema object for the journaling'
23 );
24
25 isa_ok(
26    $schema->_journal_schema->source('CDAuditHistory'),
27    'DBIx::Class::ResultSource',
28    'CDAuditHistory source exists'
29 );
30
31 isa_ok(
32    $schema->_journal_schema->source('ArtistAuditLog'),
33    'DBIx::Class::ResultSource',
34    'ArtistAuditLog source exists'
35 );
36
37 my $artist;
38 my $new_cd = $schema->txn_do( sub {
39     my $current_changeset = $schema->_journal_schema->_current_changeset;
40     ok( $current_changeset, 'have a current changeset' );
41
42     $artist = $schema->resultset('Artist')->create({
43         name => 'Fred Bloggs',
44     });
45
46     $schema->txn_do(sub {
47         is(
48            $current_changeset,
49            $schema->_journal_schema->_current_changeset,
50            q{nested txn doesn't create a new changeset}
51         );
52         return $schema->resultset('CD')->create({
53             title => 'Angry young man',
54             artist => $artist,
55             year => 2000,
56         });
57     });
58 });
59 isa_ok(
60    $new_cd,
61    'DBIx::Class::Journal',
62    'Created CD object'
63 );
64
65 is(
66    $schema->_journal_schema->_current_changeset,
67    undef, 'no current changeset'
68 );
69 eval { $schema->_journal_schema->current_changeset };
70 ok( $@, 'causes error' );
71
72 my $search = $schema->_journal_schema->resultset('CDAuditLog')->search;
73 ok($search->count, 'Created an entry in the CD audit log');
74
75 $schema->txn_do(sub {
76     $new_cd->year(2003);
77     $new_cd->update;
78 });
79
80 is($new_cd->year, 2003,  'Changed year to 2003');
81 my $cdah = $schema->_journal_schema->resultset('CDAuditHistory')->search;
82 ok($cdah->count, 'Created an entry in the CD audit history');
83
84 $schema->txn_do( sub {
85     $schema->resultset('CD')->create({
86         title => 'Something',
87         artist => $artist,
88         year => 1999,
89     });
90 });
91
92
93 my %id = map { $_ => $new_cd->get_column($_) } $new_cd->primary_columns;
94
95 $schema->txn_do( sub {
96     $new_cd->delete;
97 });
98
99 {
100     my $alentry = $search->find(\%id);
101     ok($alentry, 'got log entry');
102     ok(defined($alentry->deleted), 'Deleted set in audit_log');
103     cmp_ok(
104        $alentry->deleted->id, '>', $alentry->created->id,
105        'deleted is after created'
106     );
107 }
108
109 $new_cd = $schema->txn_do( sub {
110     $schema->resultset('CD')->create({
111         %id,
112         title => 'lalala',
113         artist => $artist,
114         year => 2000,
115     });
116 });
117
118 {
119     my $alentry = $search->find(\%id);
120     ok($alentry, 'got log entry');
121     ok(defined($alentry->deleted), 'Deleted set in audit_log');
122     cmp_ok(
123        $alentry->deleted->id, '<', $alentry->created->id,
124        'deleted is before created (recreated)'
125     );
126 }
127
128 $schema->changeset_user(1);
129 $schema->txn_do( sub {
130     $schema->resultset('CD')->create({
131         title => 'Something 2',
132         artist => $artist,
133         year => 1999,
134     });
135 });
136
137 ok($search->count > 1, 'Created an second entry in the CD audit history');
138
139 my $cset = $schema->_journal_schema->resultset('ChangeSet')->find(6);
140 is($cset->user_id, 1, 'Set user id for 6th changeset');
141
142 $schema->changeset_session(1);
143 $schema->txn_do( sub {
144     $schema->resultset('CD')->create({
145         title => 'Something 3',
146         artist => $artist,
147         year => 1999,
148     });
149 } );
150
151 my $cset2 = $schema->_journal_schema->resultset('ChangeSet')->find(7);
152 is($cset2->session_id, 1, 'Set session id for 7th changeset');
153
154
155