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