X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F01test.t;h=8f60c2b21bf1d39516e7a50074a35456bae2fc5b;hb=bf4f93f703bf4ff48b44fa828f2cba1640ed7958;hp=8de5a54828abd4f75abf8659f2383ee9c9e05a19;hpb=aba934919d04916618ce9b441b210ed516d5b067;p=dbsrgits%2FDBIx-Class-Journal.git diff --git a/t/01test.t b/t/01test.t index 8de5a54..8f60c2b 100644 --- a/t/01test.t +++ b/t/01test.t @@ -1,36 +1,54 @@ use strict; -use warnings; +use warnings; use Test::More; use lib qw(t/lib); use DBICTest; -use Data::Dumper; BEGIN { - eval "use DBD::SQLite"; + eval "use DBD::SQLite; use SQL::Translator"; plan $@ - ? ( skip_all => 'needs DBD::SQLite for testing' ) - : ( tests => 15 ); + ? ( skip_all => 'needs DBD::SQLite and SQL::Translator for testing' ) + : ( tests => 21 ); } my $schema = DBICTest->init_schema(no_populate => 1); ok($schema, 'Created a Schema'); -isa_ok($schema->_journal_schema, 'DBIx::Class::Schema::Journal::DB', 'Actually have a schema object for the journaling'); -isa_ok($schema->_journal_schema->source('CDAuditHistory'), 'DBIx::Class::ResultSource', 'CDAuditHistory source exists'); -isa_ok($schema->_journal_schema->source('ArtistAuditLog'), 'DBIx::Class::ResultSource', 'ArtistAuditLog source exists'); + +isa_ok( + $schema->_journal_schema, + 'DBIx::Class::Schema::Journal::DB', + 'Actually have a schema object for the journaling' +); + +isa_ok( + $schema->_journal_schema->source('CDAuditHistory'), + 'DBIx::Class::ResultSource', + 'CDAuditHistory source exists' +); + +isa_ok( + $schema->_journal_schema->source('ArtistAuditLog'), + 'DBIx::Class::ResultSource', + 'ArtistAuditLog source exists' +); my $artist; my $new_cd = $schema->txn_do( sub { - my $current_changeset = $schema->_journal_schema->current_changeset; - ok( $current_changeset, "have a current changeset" ); + my $current_changeset = $schema->_journal_schema->_current_changeset; + ok( $current_changeset, 'have a current changeset' ); $artist = $schema->resultset('Artist')->create({ name => 'Fred Bloggs', }); $schema->txn_do(sub { - is( $current_changeset, $schema->_journal_schema->current_changeset, "nested txn doesn't create a new changeset" ); + is( + $current_changeset, + $schema->_journal_schema->_current_changeset, + q{nested txn doesn't create a new changeset} + ); return $schema->resultset('CD')->create({ title => 'Angry young man', artist => $artist, @@ -38,20 +56,29 @@ my $new_cd = $schema->txn_do( sub { }); }); }); -isa_ok($new_cd, 'DBIx::Class::Journal', 'Created CD object'); - -is( $schema->_journal_schema->current_changeset, undef, "no current changeset" ); - -my $search = $schema->_journal_schema->resultset('CDAuditLog')->search(); +isa_ok( + $new_cd, + 'DBIx::Class::Journal', + 'Created CD object' +); + +is( + $schema->_journal_schema->_current_changeset, + undef, 'no current changeset' +); +eval { $schema->_journal_schema->current_changeset }; +ok( $@, 'causes error' ); + +my $search = $schema->_journal_schema->resultset('CDAuditLog')->search; ok($search->count, 'Created an entry in the CD audit log'); -$schema->txn_do( sub { +$schema->txn_do(sub { $new_cd->year(2003); $new_cd->update; -} ); +}); is($new_cd->year, 2003, 'Changed year to 2003'); -my $cdah = $schema->_journal_schema->resultset('CDAuditHistory')->search(); +my $cdah = $schema->_journal_schema->resultset('CDAuditHistory')->search; ok($cdah->count, 'Created an entry in the CD audit history'); $schema->txn_do( sub { @@ -60,14 +87,43 @@ $schema->txn_do( sub { artist => $artist, year => 1999, }); -} ); +}); + + +my %id = map { $_ => $new_cd->get_column($_) } $new_cd->primary_columns; $schema->txn_do( sub { $new_cd->delete; -} ); +}); + +{ + my $alentry = $search->find(\%id); + ok($alentry, 'got log entry'); + ok(defined($alentry->deleted), 'Deleted set in audit_log'); + cmp_ok( + $alentry->deleted->id, '>', $alentry->created->id, + 'deleted is after created' + ); +} -my $alentry = $search->find({ ID => $new_cd->get_column($new_cd->primary_columns) }); -ok(defined($alentry->deleted), 'Deleted set in audit_log'); +$new_cd = $schema->txn_do( sub { + $schema->resultset('CD')->create({ + %id, + title => 'lalala', + artist => $artist, + year => 2000, + }); +}); + +{ + my $alentry = $search->find(\%id); + ok($alentry, 'got log entry'); + ok(defined($alentry->deleted), 'Deleted set in audit_log'); + cmp_ok( + $alentry->deleted->id, '<', $alentry->created->id, + 'deleted is before created (recreated)' + ); +} $schema->changeset_user(1); $schema->txn_do( sub { @@ -76,12 +132,12 @@ $schema->txn_do( sub { artist => $artist, year => 1999, }); -} ); +}); ok($search->count > 1, 'Created an second entry in the CD audit history'); -my $cset = $schema->_journal_schema->resultset('ChangeSet')->find(5); -is($cset->user_id, 1, 'Set user id for 5th changeset'); +my $cset = $schema->_journal_schema->resultset('ChangeSet')->find(6); +is($cset->user_id, 1, 'Set user id for 6th changeset'); $schema->changeset_session(1); $schema->txn_do( sub { @@ -92,6 +148,8 @@ $schema->txn_do( sub { }); } ); -my $cset2 = $schema->_journal_schema->resultset('ChangeSet')->find(6); -is($cset2->session_id, 1, 'Set session id for 6th changeset'); +my $cset2 = $schema->_journal_schema->resultset('ChangeSet')->find(7); +is($cset2->session_id, 1, 'Set session id for 7th changeset'); + +