f7afb6f18e78b4f5623f45b260645410eb0a810c
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 46relationships_multi_m2m.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use lib qw(t/lib);
5 use make_dbictest_db_multi_m2m;
6 use Devel::Dwarn;
7
8 use DBIx::Class::Schema::Loader;
9
10 my $schema_counter = 0;
11
12 {
13     my $hashmap = schema_with(
14         rel_name_map => {
15             foos_2s => "other_foos",
16             bars_2s => "other_bars",
17         },
18     );
19
20     foreach ([qw(Foo bars)], [qw(Bar foos)]) {
21         my ($source, $rel) = @{$_};
22         my $row = $hashmap->resultset($source)->find(1);
23         foreach my $link ("", "other_") {
24             can_ok $row, "${link}${rel}";
25         }
26     }
27 }
28 {
29     my $submap = schema_with(
30         rel_name_map => sub {
31             my ($args) = @_;
32             if ($args->{type} eq "many_to_many") {
33                 like $args->{link_class},
34                     qr/\ADBICTest::Schema::${schema_counter}::Result::FooBar(?:One|Two)\z/,
35                     "link_class";
36                 like $args->{link_moniker}, qr/\AFooBar(?:One|Two)\z/,
37                     "link_moniker";
38                 like $args->{link_rel_name}, qr/\Afoo_bar_(?:ones|twos)\z/,
39                     "link_rel_name";
40
41                 return $args->{name}."_".(split /_/, $args->{link_rel_name})[-1];
42             }
43         },
44     );
45     foreach ([qw(Foo bars)], [qw(Bar foos)]) {
46         my ($source, $rel) = @{$_};
47         my $row = $submap->resultset($source)->find(1);
48         foreach ([ones => 1], [twos => 2]) {
49             my ($link, $count) = @{$_};
50             my $m2m = "${rel}_${link}";
51             can_ok $row, $m2m;
52             is $row->$m2m->count, $count, "$m2m count";
53         }
54     }
55 }
56
57 done_testing;
58
59 #### generates a new schema with the given opts every time it's called
60 sub schema_with {
61     $schema_counter++;
62     DBIx::Class::Schema::Loader::make_schema_at(
63             'DBICTest::Schema::'.$schema_counter,
64             { naming => 'current', @_ },
65             [ $make_dbictest_db_multi_m2m::dsn ],
66     );
67     "DBICTest::Schema::$schema_counter"->clone;
68 }