Added tests for chained sqlt_deploy_hook()'s
[dbsrgits/DBIx-Class.git] / t / 86sqlt.t
index a832325..961ca27 100644 (file)
@@ -5,6 +5,8 @@ use Test::More;
 use lib qw(t/lib);
 use DBICTest;
 
+use Scalar::Util 'blessed';
+
 BEGIN {
   require DBIx::Class;
   plan skip_all =>
@@ -12,6 +14,14 @@ BEGIN {
     unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy')
 }
 
+my $custom_deployment_statements_called = 0;
+
+sub DBICTest::Schema::deployment_statements {
+  $custom_deployment_statements_called = 1;
+  my $self = shift;
+  return $self->next::method(@_);
+}
+
 my $schema = DBICTest->init_schema (no_deploy => 1);
 
 
@@ -19,7 +29,6 @@ my $schema = DBICTest->init_schema (no_deploy => 1);
 {
   my $not_first_table_creation_re = qr/CREATE TABLE fourkeys_to_twokeys/;
 
-
   my $statements = $schema->deployment_statements;
   like (
     $statements,
@@ -42,28 +51,67 @@ my $schema = DBICTest->init_schema (no_deploy => 1);
   );
 }
 
+{
+  # use our own throw-away schema, since we'll be deploying twice
+  my $schema = DBICTest->init_schema (no_deploy => 1);
 
+  my $deploy_hook_called = 0;
+  $custom_deployment_statements_called = 0;
 
-# replace the sqlt calback with a custom version ading an index
-$schema->source('Track')->sqlt_deploy_callback(sub {
-  my ($self, $sqlt_table) = @_;
+  # add a temporary sqlt_deploy_hook to a source
+  local $DBICTest::Schema::Track::hook_cb = sub {
+    my ($class, $sqlt_table) = @_;
 
-  is (
-    $sqlt_table->schema->translator->producer_type,
-    join ('::', 'SQL::Translator::Producer', $schema->storage->sqlt_type),
-    'Production type passed to translator object',
-  );
+    $deploy_hook_called = 1;
 
-  if ($schema->storage->sqlt_type eq 'SQLite' ) {
-    $sqlt_table->add_index( name => 'track_title', fields => ['title'] )
-      or die $sqlt_table->error;
-  }
+    is ($class, 'DBICTest::Track', 'Result class passed to plain hook');
 
-  $self->default_sqlt_deploy_hook($sqlt_table);
-});
+    is (
+      $sqlt_table->schema->translator->producer_type,
+      join ('::', 'SQL::Translator::Producer', $schema->storage->sqlt_type),
+      'Production type passed to translator object',
+    );
+  };
 
-$schema->deploy; # do not remove, this fires the is() test in the callback above
+  my $component_deploy_hook_called = 0;
+  local $DBICTest::DeployComponent::hook_cb = sub {
+    $component_deploy_hook_called = 1;
+  };
+
+  $schema->deploy; # do not remove, this fires the is() test in the callback above
+  ok($deploy_hook_called, 'deploy hook got called');
+  ok($custom_deployment_statements_called, '->deploy used the schemas deploy_statements method');
+  ok($component_deploy_hook_called, 'component deploy hook got called');
+}
 
+{
+  my $deploy_hook_called = 0;
+  $custom_deployment_statements_called = 0;
+
+  # replace the sqlt calback with a custom version ading an index
+  $schema->source('Track')->sqlt_deploy_callback(sub {
+    my ($self, $sqlt_table) = @_;
+
+    $deploy_hook_called = 1;
+
+    is (
+      $sqlt_table->schema->translator->producer_type,
+      join ('::', 'SQL::Translator::Producer', $schema->storage->sqlt_type),
+      'Production type passed to translator object',
+    );
+
+    if ($schema->storage->sqlt_type eq 'SQLite' ) {
+      $sqlt_table->add_index( name => 'track_title', fields => ['title'] )
+        or die $sqlt_table->error;
+    }
+
+    $self->default_sqlt_deploy_hook($sqlt_table);
+  });
+
+  $schema->deploy; # do not remove, this fires the is() test in the callback above
+  ok($deploy_hook_called, 'deploy hook got called');
+  ok($custom_deployment_statements_called, '->deploy used the schemas deploy_statements method');
+}
 
 
 my $translator = SQL::Translator->new(