Add some tests and fix up for the results..
Jess Robinson [Wed, 2 May 2007 13:33:47 +0000 (13:33 +0000)]
lib/DBIx/Class/Journal.pm
lib/DBIx/Class/Schema/Journal.pm
lib/DBIx/Class/Schema/Journal/DB.pm
lib/DBIx/Class/Schema/Journal/DB/AuditHistory.pm
lib/DBIx/Class/Schema/Journal/DB/AuditLog.pm
t/01test.t
t/lib/DBICTest/Schema.pm

index 93f0ccc..79c8e01 100644 (file)
@@ -4,29 +4,32 @@ use base qw/DBIx::Class/;
 
 ## On create/insert, add new entry to AuditLog
 
-sub new
-{
-    my ($class, $attrs, @rest) = @_;
+# sub new
+# {
+#     my ($class, $attrs, @rest) = @_;
 
-    $class->result_source->schema->_journal_schema->current_user(delete $attrs->{user_id});
+#     $class->result_source->schema->_journal_schema->current_user(delete $attrs->{user_id});
 
-    $class->next::method($attrs, @rest);
-}
+#     $class->next::method($attrs, @rest);
+# }
 
 sub insert
 {
     my ($self) = @_;
 
     ## create new transaction here?
-    $self->next::method();
+    my $res = $self->next::method();
     if($self->in_storage)
     {
-        my $s_name = $self->result_source_instance->name();
+        my $s_name = $self->result_source->source_name();
+        print STDERR "Schema: ", ref($self->result_source->schema), "\n";
         my $al = $self->result_source->schema->_journal_schema->resultset("${s_name}AuditLog");
         $al->create({
             changeset => $self->result_source->schema->_journal_schema->current_changeset(),
         });
     }
+
+    return $res;
 }
 
 ## On delete, update delete_id of AuditLog
index 36c4895..319477a 100644 (file)
@@ -16,7 +16,8 @@ sub connection
     my $self = shift;
     $self->next::method(@_);
 
-    my $journal_schema = (__PACKAGE__ . '::DB')->connect($self->journal_connection || $self->storage->connect_info);
+    my $journal_schema = DBIx::Class::Schema::Journal::DB->connect(@{ $self->journal_connection || $self->storage->connect_info });
+    print STDERR "conn", $journal_schema->storage->connect_info;
     if($self->journal_storage_type)
     {
         $journal_schema->storage_type($self->journal_storage_type);
@@ -31,7 +32,8 @@ sub connection
     {
         next unless($j_sources{$s_name});
         $self->create_journal_for($s_name);
-        $self->source($s_name)->load_component('Journal');
+        $self->class($s_name)->load_components('Journal');
+        print STDERR "$s_name :", $self->class($s_name), "\n";
     }
 
     ## Set up relationship between changeset->user_id and this schema's user
@@ -41,7 +43,7 @@ sub connection
         return;
     }
 
-    $self->_journal_schema->source('ChangeSet')->belongs_to('user', @{$self->journal_user});
+    $self->_journal_schema->class('ChangeSet')->belongs_to('user', @{$self->journal_user});
 }
 
 sub get_audit_log_class_name
@@ -65,17 +67,19 @@ sub create_journal_for
     my $source = $self->source($s_name);
     my $newclass = $self->get_audit_log_class_name($s_name);
     DBIx::Class::Componentised->inject_base($newclass, 'DBIx::Class::Schema::Journal::DB::AuditLog');
-#    $newclass->table(lc($s_name) . "_audit_log");
-    $newclass->result_source_instance->name(lc($s_name) . "_audit_log");
+    $newclass->table(lc($s_name) . "_audit_log");
+    $self->_journal_schema->register_class("${s_name}AuditLog", $newclass);
                            
 
-    my $histclass = $self->get_audit_hisory_class_name($s_name);
+    my $histclass = $self->get_audit_history_class_name($s_name);
     DBIx::Class::Componentised->inject_base($histclass, 'DBIx::Class::Schema::Journal::DB::AuditHistory');
-    $histclass->result_source_instance->name(lc($s_name) . "_audit_hisory");
+    $histclass->table(lc($s_name) . "_audit_history");
+#    $histclass->result_source_instance->name(lc($s_name) . "_audit_hisory");
     $histclass->add_columns(
                             map { $_ => $source->column_info($_) } $source->columns
                            );
                            
+    $self->_journal_schema->register_class("${s_name}AuditHistory", $histclass);
 }
 
 sub create_changeset
index 7a2a556..7e2ac82 100644 (file)
@@ -9,6 +9,8 @@ __PACKAGE__->mk_classdata('current_changeset');
 DBIx::Class::Schema::Journal::DB->load_classes(qw/
                                                ChangeSet
                                                Change
+                                               AuditLog
+                                               AuditHistory
                                                /);
 
 1;
index 2a8825a..dde946a 100644 (file)
@@ -1,6 +1,7 @@
 package DBIx::Class::Schema::Journal::DB::AuditHistory;
 
 use base 'DBIx::Class::Schema::Journal::DB::Base';
+__PACKAGE__->table(__PACKAGE__->table);
 
 __PACKAGE__->add_columns(
                            change_id => {
index 3026b80..6a37d38 100644 (file)
@@ -1,6 +1,7 @@
 package DBIx::Class::Schema::Journal::DB::AuditLog;
 
 use base 'DBIx::Class::Schema::Journal::DB::Base';
+__PACKAGE__->table(__PACKAGE__->table);
 
 __PACKAGE__->add_columns(
                            ID => {
index c5cf4e3..786f126 100644 (file)
@@ -10,11 +10,25 @@ BEGIN {
     eval "use DBD::SQLite";
     plan $@
         ? ( skip_all => 'needs DBD::SQLite for testing' )
-        : ( tests => 2 );
+        : ( tests => 6 );
 }
 
 my $schema = DBICTest->init_schema(no_populate => 1);
 
 ok($schema, 'Created a Schema');
-isa_ok($schema->_journal_schema, 'DBIx::Class::Schema::Journal::DB');
+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 $new_cd = $schema->resultset('CD')->create({
+    title => 'Angry young man',
+    artist => 0,
+    year => 2000,
+    });
+
+isa_ok($new_cd, 'DBIx::Class::Journal', 'Created CD object');
+
+my $search = $schema->_journal_schema->resultset('CDAuditLog')->search();
+ok($search->count, 'Created an entry in the CD audit log');
+
 
index 0807680..7349619 100644 (file)
@@ -5,8 +5,9 @@ use base qw/DBIx::Class::Schema/;
 
 __PACKAGE__->load_components(qw/+DBIx::Class::Schema::Journal/);
 
-no warnings qw/qw/;
+__PACKAGE__->journal_user(['DBICTest::Schema::Artist', {'foreign.artistid' => 'self.user_id'}]);
 
+no warnings qw/qw/;
 __PACKAGE__->load_classes(qw/
   Artist
   CD