work even without journal_user
[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' )
ec16e73a 13 : ( tests => 12 );
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 {
1e996809 25 $artist = $schema->resultset('Artist')->create({
f3602465 26 name => 'Fred Bloggs',
27 });
f4f0b7c9 28 return $schema->resultset('CD')->create({
29 title => 'Angry young man',
f3602465 30 artist => $artist,
f4f0b7c9 31 year => 2000,
c5fba518 32 });
f4f0b7c9 33});
c5fba518 34isa_ok($new_cd, 'DBIx::Class::Journal', 'Created CD object');
35
36my $search = $schema->_journal_schema->resultset('CDAuditLog')->search();
37ok($search->count, 'Created an entry in the CD audit log');
38
1e996809 39$schema->txn_do( sub {
40 $new_cd->year(2003);
41 $new_cd->update;
42} );
43
44is($new_cd->year, 2003, 'Changed year to 2003');
45my $cdah = $schema->_journal_schema->resultset('CDAuditHistory')->search();
46ok($cdah->count, 'Created an entry in the CD audit history');
47
48$schema->txn_do( sub {
49 $schema->resultset('CD')->create({
50 title => 'Something',
51 artist => $artist,
52 year => 1999,
53 });
54} );
55
56$schema->txn_do( sub {
57 $new_cd->delete;
58} );
59
60my $alentry = $search->find({ ID => $new_cd->get_column($new_cd->primary_columns) });
61ok(defined($alentry->deleted), 'Deleted set in audit_log');
b5851590 62
ec16e73a 63$schema->changeset_user(1);
64$schema->txn_do( sub {
65 $schema->resultset('CD')->create({
66 title => 'Something 2',
67 artist => $artist,
68 year => 1999,
69 });
70} );
71
72ok($search->count > 1, 'Created an second entry in the CD audit history');
73
74my $cset = $schema->_journal_schema->resultset('ChangeSet')->find(5);
75is($cset->user_id, 1, 'Set user id for 5th changeset');
76
77$schema->changeset_session(1);
78$schema->txn_do( sub {
79 $schema->resultset('CD')->create({
80 title => 'Something 3',
81 artist => $artist,
82 year => 1999,
83 });
84} );
85
86my $cset2 = $schema->_journal_schema->resultset('ChangeSet')->find(6);
87is($cset2->session_id, 1, 'Set session id for 6th changeset');
88