8 unless (eval { require DBD::SQLite; 1 }) {
9 plan skip_all => 'Unable to load DBD::SQLite';
12 my $schema = My::Schema->connect('dbi:SQLite:dbname=:memory:');
16 my $people = $schema->resultset('Person');
18 my $bob = $people->create({
19 name => 'Bob Testuser',
22 $bob->create_related(assigned_tasks => {
26 $bob->create_related(assigned_tasks => {
30 $bob->create_related(assigned_tasks => {
35 subtest 'has_many' => sub {
37 my $join_with_min = sub {
38 return shift->with_parameterized_join(
39 urgent_assigned_tasks => { urgency_threshold => $_[0] },
43 my $join_with_range = sub {
44 return shift->with_parameterized_join(
45 tasks_in_urgency_range => {
52 my $search_count = sub {
53 return scalar shift->search(
54 { 'me.name' => { -like => 'Bob%' } },
59 '+as' => ['task_count'],
63 my $search = sub { $search_count->(shift, 'urgent_assigned_tasks.id') };
65 my $fetch_count = sub {
66 return shift->next->get_column('task_count');
69 subtest 'simple filter' => sub {
70 is $people->$join_with_min(19)->$search->$fetch_count,
72 is $people->$join_with_min(29)->$search->$fetch_count,
74 is $people->$join_with_min(39)->$search->$fetch_count,
78 subtest 'multiple filters' => sub {
79 my $rs1 = $people->$join_with_min(19)->$search;
80 my $rs2 = $people->$join_with_min(29)->$search;
81 is $rs1->$fetch_count, 2, 'first';
82 is $rs2->$fetch_count, 1, 'second';
85 subtest 'overrides' => sub {
92 }, qr{once.+per.+relation}i, 'throws error';
95 subtest 'multi parameter' => sub {
97 $search_count->(shift, 'tasks_in_urgency_range.id');
99 is $people->$join_with_range(10, 30)->$search->$fetch_count,
103 subtest 'multi join' => sub {
106 ->$join_with_range(10, 30)
112 subtest 'unconstrained' => sub {
114 ->with_parameterized_join(unconstrained_tasks => {})
115 ->$search_count('unconstrained_tasks.id')
117 3, 'unconstrained count';
120 subtest 'errors' => sub {
122 $people->with_parameterized_join(urgent_assigned_tasks => {})
125 }, qr{urgent_assigned_tasks.+urgency_threshold}, 'missing parameter';
127 $people->with_parameterized_join(__invalid__ => {})
130 }, qr{__invalid__}, 'unknown relation';
132 $people->with_parameterized_join(undef, {});
133 }, qr{relation.+name}i, 'missing relation name';
135 $people->with_parameterized_join(foo => []);
136 }, qr{parameters.+hash.+not.+ARRAY}i, 'invalid parameters';
138 $people->with_parameterized_join(foo => 23);
139 }, qr{parameters.+hash.+not.+non-reference}i, 'non ref parameters';
142 subtest 'declaration errors' => sub {
143 my $errors = \%My::Schema::Result::Person::ERROR;
144 like delete $errors->{no_args}, qr{Missing.+relation.+name}i,
146 like delete $errors->{no_source}, qr{Missing.+foreign.+source}i,
148 like delete $errors->{no_cond}, qr{Condition.+non-ref.+value}i,
150 like delete $errors->{invalid_cond}, qr{Condition.+SCALAR}i,
152 like delete $errors->{undef_args}, qr{Arguments.+array.+non-ref}i,
154 like delete $errors->{invalid_args}, qr{Arguments.+array.+SCALAR}i,
156 like delete $errors->{undef_builder}, qr{builder.+code.+non-ref}i,
158 like delete $errors->{invalid_builder}, qr{builder.+code.+ARRAY}i,
160 is_deeply $errors, {}, 'no more errors';