roll back accidental case change
[dbsrgits/DBIx-Class-Journal.git] / t / 01test.t
CommitLineData
b5851590 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7use Data::Dumper;
8
9BEGIN {
10 eval "use DBD::SQLite";
11 plan $@
12 ? ( skip_all => 'needs DBD::SQLite for testing' )
aba93491 13 : ( tests => 15 );
b5851590 14}
15
16my $schema = DBICTest->init_schema(no_populate => 1);
17
18ok($schema, 'Created a Schema');
c5fba518 19isa_ok($schema->_journal_schema, 'DBIx::Class::Schema::Journal::DB', 'Actually have a schema object for the journaling');
20isa_ok($schema->_journal_schema->source('CDAuditHistory'), 'DBIx::Class::ResultSource', 'CDAuditHistory source exists');
21isa_ok($schema->_journal_schema->source('ArtistAuditLog'), 'DBIx::Class::ResultSource', 'ArtistAuditLog source exists');
22
1e996809 23my $artist;
f4f0b7c9 24my $new_cd = $schema->txn_do( sub {
aba93491 25 my $current_changeset = $schema->_journal_schema->current_changeset;
26 ok( $current_changeset, "have a current changeset" );
27
1e996809 28 $artist = $schema->resultset('Artist')->create({
f3602465 29 name => 'Fred Bloggs',
30 });
aba93491 31
32 $schema->txn_do(sub {
33 is( $current_changeset, $schema->_journal_schema->current_changeset, "nested txn doesn't create a new changeset" );
34 return $schema->resultset('CD')->create({
35 title => 'Angry young man',
36 artist => $artist,
37 year => 2000,
38 });
c5fba518 39 });
f4f0b7c9 40});
c5fba518 41isa_ok($new_cd, 'DBIx::Class::Journal', 'Created CD object');
42
aba93491 43is( $schema->_journal_schema->current_changeset, undef, "no current changeset" );
44
c5fba518 45my $search = $schema->_journal_schema->resultset('CDAuditLog')->search();
46ok($search->count, 'Created an entry in the CD audit log');
47
1e996809 48$schema->txn_do( sub {
49 $new_cd->year(2003);
50 $new_cd->update;
51} );
52
53is($new_cd->year, 2003, 'Changed year to 2003');
54my $cdah = $schema->_journal_schema->resultset('CDAuditHistory')->search();
55ok($cdah->count, 'Created an entry in the CD audit history');
56
57$schema->txn_do( sub {
58 $schema->resultset('CD')->create({
59 title => 'Something',
60 artist => $artist,
61 year => 1999,
62 });
63} );
64
65$schema->txn_do( sub {
66 $new_cd->delete;
67} );
68
69my $alentry = $search->find({ ID => $new_cd->get_column($new_cd->primary_columns) });
70ok(defined($alentry->deleted), 'Deleted set in audit_log');
b5851590 71
ec16e73a 72$schema->changeset_user(1);
73$schema->txn_do( sub {
74 $schema->resultset('CD')->create({
75 title => 'Something 2',
76 artist => $artist,
77 year => 1999,
78 });
79} );
80
81ok($search->count > 1, 'Created an second entry in the CD audit history');
82
83my $cset = $schema->_journal_schema->resultset('ChangeSet')->find(5);
84is($cset->user_id, 1, 'Set user id for 5th changeset');
85
86$schema->changeset_session(1);
87$schema->txn_do( sub {
88 $schema->resultset('CD')->create({
89 title => 'Something 3',
90 artist => $artist,
91 year => 1999,
92 });
93} );
94
95my $cset2 = $schema->_journal_schema->resultset('ChangeSet')->find(6);
96is($cset2->session_id, 1, 'Set session id for 6th changeset');
97