add rel_name_map option
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 45relationships.t
index f3ae061..b2eb18a 100644 (file)
@@ -1,11 +1,13 @@
 use strict;
-use Test::More tests => 6;
+use Test::More tests => 12;
 use Test::Exception;
 use lib qw(t/lib);
 use make_dbictest_db;
 
 use DBIx::Class::Schema::Loader;
 
+my $schema_counter = 0;
+
 # test skip_relationships
 my $regular = schema_with();
 is( ref($regular->source('Bar')->relationship_info('fooref')), 'HASH',
@@ -16,6 +18,82 @@ is_deeply( $skip_rel->source('Bar')->relationship_info('fooref'), undef,
     'skip_relationships blocks generation of fooref rel',
   );
 
+# test hashref as rel_name_map
+my $hash_relationship = schema_with(
+    rel_name_map => {
+        fooref => "got_fooref",
+        bars   => "ignored",
+        Foo    => {
+            bars => "got_bars",
+            fooref => "ignored",
+        },
+    }
+);
+is( ref($hash_relationship->source('Foo')->relationship_info('got_bars')),
+    'HASH',
+    'single level hash in rel_name_map picked up correctly'
+  );
+is( ref($hash_relationship->source('Bar')->relationship_info('got_fooref')),
+    'HASH',
+    'double level hash in rel_name_map picked up correctly'
+  );
+
+# test coderef as rel_name_map
+my $code_relationship = schema_with(
+    rel_name_map => sub {
+        my ($args) = @_;
+
+        if ($args->{local_moniker} eq 'Foo') {
+            is_deeply(
+                $args,
+                {
+                   name           => 'bars',
+                   type           => 'has_many',
+                    local_class    =>
+                        "DBICTest::Schema::${schema_counter}::Result::Foo",
+                   local_moniker  => 'Foo',
+                   local_columns  => ['fooid'],
+                    remote_class   =>
+                        "DBICTest::Schema::${schema_counter}::Result::Bar",
+                   remote_moniker => 'Bar',
+                   remote_columns => ['fooref'],
+               },
+               'correct args for Foo passed'
+              );
+           return 'bars_caught';
+        }
+       elsif ($args->{local_moniker} eq 'Bar') {
+            is_deeply(
+                $args,
+                {
+                   name           => 'fooref',
+                   type           => 'belongs_to',
+                    local_class    =>
+                        "DBICTest::Schema::${schema_counter}::Result::Bar",
+                   local_moniker  => 'Bar',
+                   local_columns  => ['fooref'],
+                    remote_class   =>
+                        "DBICTest::Schema::${schema_counter}::Result::Foo",
+                   remote_moniker => 'Foo',
+                   remote_columns => ['fooid'],
+               },
+               'correct args for Foo passed'
+              );
+       
+            return 'fooref_caught';
+       }
+    }
+  );
+is( ref($code_relationship->source('Foo')->relationship_info('bars_caught')),
+    'HASH',
+    'rel_name_map overrode local_info correctly'
+  );
+is( ref($code_relationship->source('Bar')->relationship_info('fooref_caught')),
+    'HASH',
+    'rel_name_map overrode remote_info correctly'
+  );
+
+
 
 # test relationship_attrs
 throws_ok {
@@ -46,9 +124,7 @@ throws_ok {
       );
 }
 
-
 #### generates a new schema with the given opts every time it's called
-my $schema_counter = 0;
 sub schema_with {
     $schema_counter++;
     DBIx::Class::Schema::Loader::make_schema_at(