35d52dd496dd873961594036752589a77a9e4e94
[dbsrgits/DBIx-Class-Journal.git] / t / 03populate.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 BEGIN {
6     eval "use DBD::SQLite; use SQL::Translator";
7     plan $@
8         ? ( skip_all => 'needs DBD::SQLite and SQL::Translator for testing' )
9         : ( tests => 17 );
10 }
11
12 use lib qw(t/lib);
13 use DBICTest;
14
15 # connect to db and deploy only the original db schema, not journal schema
16 my $schema = DBICTest->init_schema(no_populate => 1, no_deploy => 1);
17
18 ok($schema, 'Created a Schema');
19 $schema->deploy;
20
21 # check we have no journal
22 my $count = eval {
23     $schema->_journal_schema->resultset('ChangeLog')->count;
24 };
25 my $e = $@;
26
27 is( $count, undef, 'no count' );
28 like( $e, qr/table.*changelog/, 'missing table error' );
29
30 # insert two rows -not- in txn
31 my ($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
48 is( $@, '', "no error" );
49 is( $count, 0, "count is 0 (changelog)" );
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
57 is( $@, '', "no error" );
58 is( $count, 1, "count is 1 (changeset)" );
59
60 # check it contains two inserts
61 $count = eval { $schema->_journal_schema->resultset('ChangeLog')->count };
62
63 is( $@, '', "no error" );
64 is( $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
69 is( $@, '', "no error" );
70 is( $count, 2, "count is 2 (auditlog)" );
71
72 # now delete a row
73 eval {
74     my $deleted = $schema->txn_do(sub {
75         $artistA->delete;
76     });
77 };
78
79 is( $@, '', "no error from deletion journal (create_id not null)" );
80 is( $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
85 is( $@, '', "no error" );
86 is( $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
97 is( $@, '', "no error" );
98 is( $count, 1, "count is 1 (delete_id)" );
99