- Add predump_hook and prepopulate_hook tests add-hooks
mikew [Fri, 11 Oct 2013 15:37:39 +0000 (11:37 -0400)]
t/19-hooks.t [new file with mode: 0644]

diff --git a/t/19-hooks.t b/t/19-hooks.t
new file mode 100644 (file)
index 0000000..2714665
--- /dev/null
@@ -0,0 +1,58 @@
+#!perl
+
+use DBIx::Class::Fixtures;
+use lib qw(t/lib);
+use DBICTest;
+use Path::Class;
+use Data::Dumper;
+use Test::More tests => 7;
+
+# set up and populate schema
+ok(my $schema = DBICTest->init_schema(), 'got schema');
+my $config_dir = 't/var/configs';
+
+# This also tests multiple calls to dump and populate using
+# the same fixture object
+ok(my $fixtures = DBIx::Class::Fixtures->new({ 
+    config_dir => $config_dir, 
+    debug => 0 
+  }), 'object created with correct config dir'
+);
+
+# predump_hook tests
+DBICTest->clear_schema($schema);
+DBICTest->populate_schema($schema);
+ok($fixtures->dump({ 
+  config => "fetch.json", 
+  schema => $schema, 
+  directory => 't/var/fixtures',
+  predump_hook => sub {
+      my ($src,$row) = @_;
+      $row->{name} = 'predump_modified'
+        if ($src->name eq 'artist' && $row->{artistid} == 1);
+  }
+}), "dump with predump_hook ok");
+
+DBICTest->clear_schema($schema);
+ok($fixtures->populate({ 
+    ddl => 't/lib/sqlite.sql', 
+    connection_details => ['dbi:SQLite:t/var/DBIxClass.db', '', ''], 
+    directory => 't/var/fixtures',
+}), 'populate after predump_hook ok');
+
+is($schema->resultset('Artist')->find(1)->name, 'predump_modified', 'predump_hook ok');
+
+## prepopulate_hook tests
+DBICTest->clear_schema($schema);
+ok($fixtures->populate({ 
+    ddl => 't/lib/sqlite.sql', 
+    connection_details => ['dbi:SQLite:t/var/DBIxClass.db', '', ''], 
+    directory => 't/var/fixtures',
+    prepopulate_hook => sub { 
+        my ($src,$row) = @_;
+        $row->{name} = 'prepopulate_modified'
+          if ($src->name eq 'artist' && $row->{artistid} == 1);
+    }
+}), 'populate after prepopulate_hook ok');
+
+is($schema->resultset('Artist')->find(1)->name, 'prepopulate_modified', 'prepopulate_hook ok');