found a bug with use of id not $pk - thanks tests!!
[dbsrgits/DBIx-Class-Journal.git] / t / 03populate.t
CommitLineData
e32a5dbb 1use strict;
2use warnings;
3
4use Test::More;
5BEGIN {
6 eval "use DBD::SQLite; use SQL::Translator";
7 plan $@
8 ? ( skip_all => 'needs DBD::SQLite and SQL::Translator for testing' )
1891bb5f 9 : ( tests => 17 );
e32a5dbb 10}
11
12use lib qw(t/lib);
13use DBICTest;
14
15# connect to db and deploy only the original db schema, not journal schema
16my $schema = DBICTest->init_schema(no_populate => 1, no_deploy => 1);
e32a5dbb 17
18ok($schema, 'Created a Schema');
1891bb5f 19$schema->deploy;
e32a5dbb 20
21# check we have no journal
22my $count = eval {
23 $schema->_journal_schema->resultset('ChangeLog')->count;
24};
25my $e = $@;
26
27is( $count, undef, 'no count' );
28like( $e, qr/table.*changelog/, 'missing table error' );
29
30# insert two rows -not- in txn
31my ($artistA, $artistB);
32#$schema->txn_do(sub {
33 $artistA = $schema->resultset('Artist')->create({
34 name => 'Fred Bloggs A',
35 });
36
37 $artistB = $schema->resultset('Artist')->create({
38 name => 'Fred Bloggs B',
39 });
40#});
41
42# create the journal
43$schema->journal_schema_deploy();
44
45# check it is empty
46$count = eval { $schema->_journal_schema->resultset('ChangeLog')->count };
47
48is( $@, '', "no error" );
49is( $count, 0, "count is 0 (chagnelog)" );
50
51# run populate
52$schema->prepopulate_journal();
53
54# check there is only one changeset
55$count = eval { $schema->_journal_schema->resultset('ChangeSet')->count };
56
57is( $@, '', "no error" );
58is( $count, 1, "count is 1 (changeset)" );
59
60# check it contains two inserts
61$count = eval { $schema->_journal_schema->resultset('ChangeLog')->count };
62
63is( $@, '', "no error" );
64is( $count, 2, "count is 2 (changelog)" );
65
66# check audit log has two rows for two inserts
67$count = eval { $schema->_journal_schema->resultset('ArtistAuditLog')->count };
68
69is( $@, '', "no error" );
70is( $count, 2, "count is 2 (auditlog)" );
71
72# now delete a row
73eval {
74 my $deleted = $schema->txn_do(sub {
75 $artistA->delete;
76 });
77};
78
79is( $@, '', "no error from deletion journal (create_id not null)" );
80is( $artistA->in_storage, 0, "row was deleted" );
81
82# check journal log still has two rows
83$count = eval { $schema->_journal_schema->resultset('ArtistAuditLog')->count };
84
85is( $@, '', "no error" );
86is( $count, 2, "count is 2 (auditlog 2)" );
87
88# and that one of them has a delete_id
89$count = eval {
90 $schema->_journal_schema->resultset('ArtistAuditLog')
91 ->search({
92 artistid => $artistA->id,
93 delete_id => { '-not' => undef }
94 })->count;
95};
96
97is( $@, '', "no error" );
98is( $count, 1, "count is 1 (delete_id)" );
99