Test::TempDir
[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' )
be32516d 13 : ( tests => 16 );
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 {
be32516d 25 my $current_changeset = $schema->_journal_schema->_current_changeset;
aba93491 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 {
be32516d 33 is( $current_changeset, $schema->_journal_schema->_current_changeset, "nested txn doesn't create a new changeset" );
aba93491 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
be32516d 43is( $schema->_journal_schema->_current_changeset, undef, "no current changeset" );
44eval { $schema->_journal_schema->current_changeset };
45ok( $@, "causes error" );
aba93491 46
c5fba518 47my $search = $schema->_journal_schema->resultset('CDAuditLog')->search();
48ok($search->count, 'Created an entry in the CD audit log');
49
1e996809 50$schema->txn_do( sub {
51 $new_cd->year(2003);
52 $new_cd->update;
53} );
54
55is($new_cd->year, 2003, 'Changed year to 2003');
56my $cdah = $schema->_journal_schema->resultset('CDAuditHistory')->search();
57ok($cdah->count, 'Created an entry in the CD audit history');
58
59$schema->txn_do( sub {
60 $schema->resultset('CD')->create({
61 title => 'Something',
62 artist => $artist,
63 year => 1999,
64 });
65} );
66
67$schema->txn_do( sub {
68 $new_cd->delete;
69} );
70
30a4f241 71my $alentry = $search->find({ map { $_ => $new_cd->get_column($_) } $new_cd->primary_columns });
1e996809 72ok(defined($alentry->deleted), 'Deleted set in audit_log');
b5851590 73
ec16e73a 74$schema->changeset_user(1);
75$schema->txn_do( sub {
76 $schema->resultset('CD')->create({
77 title => 'Something 2',
78 artist => $artist,
79 year => 1999,
80 });
81} );
82
83ok($search->count > 1, 'Created an second entry in the CD audit history');
84
85my $cset = $schema->_journal_schema->resultset('ChangeSet')->find(5);
86is($cset->user_id, 1, 'Set user id for 5th changeset');
87
88$schema->changeset_session(1);
89$schema->txn_do( sub {
90 $schema->resultset('CD')->create({
91 title => 'Something 3',
92 artist => $artist,
93 year => 1999,
94 });
95} );
96
97my $cset2 = $schema->_journal_schema->resultset('ChangeSet')->find(6);
98is($cset2->session_id, 1, 'Set session id for 6th changeset');
99