more tests
Robert Sedlacek [Wed, 30 Sep 2015 03:06:42 +0000 (03:06 +0000)]
t/00basic.t

index 23fe945..fa997e3 100644 (file)
@@ -2,6 +2,7 @@ use strict;
 use warnings;
 use lib 't/lib';
 use Test::More;
+use Test::Fatal;
 use My::Schema;
 
 my $schema = My::Schema->connect('dbi:SQLite:dbname=:memory:');
@@ -35,26 +36,27 @@ subtest 'has_many' => sub {
         );
     };
 
-#    my $join_with_range = sub {
-#        return shift->with_parameterized_join(
-#            tasks_in_urgency_range => {
-#                min => $_[0],
-#                max => $_[1],
-#            },
-#        );
-#    };
+    my $join_with_range = sub {
+        return shift->with_parameterized_join(
+            tasks_in_urgency_range => {
+                min => $_[0],
+                max => $_[1],
+            },
+        );
+    };
 
-    my $search = sub {
+    my $search_count = sub {
         return scalar shift->search(
             { 'me.name' => { -like => 'Bob%' } },
             {
                 '+select' => [{
-                    count => \['urgent_assigned_tasks.id'],
+                    count => \[shift],
                 }],
                 '+as' => ['task_count'],
             },
         );
     };
+    my $search = sub { $search_count->(shift, 'urgent_assigned_tasks.id') };
 
     my $fetch_count = sub {
         return shift->next->get_column('task_count');
@@ -78,17 +80,42 @@ subtest 'has_many' => sub {
 
     subtest 'overrides' => sub {
         is $people
-            ->$join_with_min(190)
             ->$join_with_min(19)
+            ->$join_with_min(29)
             ->$search
             ->$fetch_count,
-            2, 'overridden parameter';
+            1, 'overridden parameter';
     };
 
-#    subtest 'multi parameter' => sub {
-#        is $people->$join_with_range(10, 30)->$search->$fetch_count,
-#            3, 'full range';
-#    };
+    subtest 'multi parameter' => sub {
+        my $search = sub {
+            $search_count->(shift, 'tasks_in_urgency_range.id');
+        };
+        is $people->$join_with_range(10, 30)->$search->$fetch_count,
+            3, 'full range';
+    };
+
+    subtest 'multi join' => sub {
+        is $people
+            ->$join_with_min(19)
+            ->$join_with_range(10, 30)
+            ->$search
+            ->$fetch_count,
+            2*3, 'full count';
+    };
+
+    subtest 'errors' => sub {
+        like exception {
+            $people->with_parameterized_join(urgent_assigned_tasks => {})
+                ->$search
+                ->$fetch_count;
+        }, qr{urgent_assigned_tasks.+urgency_threshold}, 'missing parameter';
+        like exception {
+            $people->with_parameterized_join(__invalid__ => {})
+                ->$search
+                ->$fetch_count;
+        }, qr{__invalid__}, 'unknown relation';
+    };
 };
 
 done_testing;