8 my $schema = My::Schema->connect('dbi:SQLite:dbname=:memory:');
12 my $people = $schema->resultset('Person');
14 my $bob = $people->create({
15 name => 'Bob Testuser',
18 $bob->create_related(assigned_tasks => {
22 $bob->create_related(assigned_tasks => {
26 $bob->create_related(assigned_tasks => {
31 subtest 'has_many' => sub {
33 my $join_with_min = sub {
34 return shift->with_parameterized_join(
35 urgent_assigned_tasks => { urgency_threshold => $_[0] },
39 my $join_with_range = sub {
40 return shift->with_parameterized_join(
41 tasks_in_urgency_range => {
48 my $search_count = sub {
49 return scalar shift->search(
50 { 'me.name' => { -like => 'Bob%' } },
55 '+as' => ['task_count'],
59 my $search = sub { $search_count->(shift, 'urgent_assigned_tasks.id') };
61 my $fetch_count = sub {
62 return shift->next->get_column('task_count');
65 subtest 'simple filter' => sub {
66 is $people->$join_with_min(19)->$search->$fetch_count,
68 is $people->$join_with_min(29)->$search->$fetch_count,
70 is $people->$join_with_min(39)->$search->$fetch_count,
74 subtest 'multiple filters' => sub {
75 my $rs1 = $people->$join_with_min(19)->$search;
76 my $rs2 = $people->$join_with_min(29)->$search;
77 is $rs1->$fetch_count, 2, 'first';
78 is $rs2->$fetch_count, 1, 'second';
81 subtest 'overrides' => sub {
87 1, 'overridden parameter';
90 subtest 'multi parameter' => sub {
92 $search_count->(shift, 'tasks_in_urgency_range.id');
94 is $people->$join_with_range(10, 30)->$search->$fetch_count,
98 subtest 'multi join' => sub {
101 ->$join_with_range(10, 30)
107 subtest 'errors' => sub {
109 $people->with_parameterized_join(urgent_assigned_tasks => {})
112 }, qr{urgent_assigned_tasks.+urgency_threshold}, 'missing parameter';
114 $people->with_parameterized_join(__invalid__ => {})
117 }, qr{__invalid__}, 'unknown relation';