Commit | Line | Data |
b5851590 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use lib qw(t/lib); |
6 | use DBICTest; |
7 | use Data::Dumper; |
8 | |
9 | BEGIN { |
f020d600 |
10 | eval "use DBD::SQLite; use SQL::Translator"; |
b5851590 |
11 | plan $@ |
f020d600 |
12 | ? ( skip_all => 'needs DBD::SQLite and SQL::Translator for testing' ) |
5c9840d8 |
13 | : ( tests => 21 ); |
b5851590 |
14 | } |
15 | |
16 | my $schema = DBICTest->init_schema(no_populate => 1); |
17 | |
18 | ok($schema, 'Created a Schema'); |
c5fba518 |
19 | isa_ok($schema->_journal_schema, 'DBIx::Class::Schema::Journal::DB', 'Actually have a schema object for the journaling'); |
20 | isa_ok($schema->_journal_schema->source('CDAuditHistory'), 'DBIx::Class::ResultSource', 'CDAuditHistory source exists'); |
21 | isa_ok($schema->_journal_schema->source('ArtistAuditLog'), 'DBIx::Class::ResultSource', 'ArtistAuditLog source exists'); |
22 | |
1e996809 |
23 | my $artist; |
f4f0b7c9 |
24 | my $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 |
41 | isa_ok($new_cd, 'DBIx::Class::Journal', 'Created CD object'); |
42 | |
be32516d |
43 | is( $schema->_journal_schema->_current_changeset, undef, "no current changeset" ); |
44 | eval { $schema->_journal_schema->current_changeset }; |
45 | ok( $@, "causes error" ); |
aba93491 |
46 | |
c5fba518 |
47 | my $search = $schema->_journal_schema->resultset('CDAuditLog')->search(); |
48 | ok($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 | |
55 | is($new_cd->year, 2003, 'Changed year to 2003'); |
56 | my $cdah = $schema->_journal_schema->resultset('CDAuditHistory')->search(); |
57 | ok($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 | |
5c9840d8 |
67 | |
68 | my %id = map { $_ => $new_cd->get_column($_) } $new_cd->primary_columns; |
69 | |
1e996809 |
70 | $schema->txn_do( sub { |
71 | $new_cd->delete; |
5c9840d8 |
72 | }); |
73 | |
74 | { |
75 | my $alentry = $search->find(\%id); |
76 | ok($alentry, "got log entry"); |
77 | ok(defined($alentry->deleted), 'Deleted set in audit_log'); |
78 | cmp_ok( $alentry->deleted->id, ">", $alentry->created->id, "deleted is after created" ); |
79 | } |
80 | |
81 | $new_cd = $schema->txn_do( sub { |
82 | $schema->resultset('CD')->create({ |
83 | %id, |
84 | title => 'lalala', |
85 | artist => $artist, |
86 | year => 2000, |
87 | }); |
88 | }); |
1e996809 |
89 | |
5c9840d8 |
90 | { |
91 | my $alentry = $search->find(\%id); |
92 | ok($alentry, "got log entry"); |
93 | ok(defined($alentry->deleted), 'Deleted set in audit_log'); |
94 | cmp_ok( $alentry->deleted->id, "<", $alentry->created->id, "deleted is before created (recreated)" ); |
95 | } |
b5851590 |
96 | |
ec16e73a |
97 | $schema->changeset_user(1); |
98 | $schema->txn_do( sub { |
99 | $schema->resultset('CD')->create({ |
100 | title => 'Something 2', |
101 | artist => $artist, |
102 | year => 1999, |
103 | }); |
5c9840d8 |
104 | }); |
ec16e73a |
105 | |
106 | ok($search->count > 1, 'Created an second entry in the CD audit history'); |
107 | |
5c9840d8 |
108 | my $cset = $schema->_journal_schema->resultset('ChangeSet')->find(6); |
109 | is($cset->user_id, 1, 'Set user id for 6th changeset'); |
ec16e73a |
110 | |
111 | $schema->changeset_session(1); |
112 | $schema->txn_do( sub { |
113 | $schema->resultset('CD')->create({ |
114 | title => 'Something 3', |
115 | artist => $artist, |
116 | year => 1999, |
117 | }); |
118 | } ); |
119 | |
5c9840d8 |
120 | my $cset2 = $schema->_journal_schema->resultset('ChangeSet')->find(7); |
121 | is($cset2->session_id, 1, 'Set session id for 7th changeset'); |
122 | |
123 | |
ec16e73a |
124 | |