[Jifty-commit] r6619 - in jifty/trunk: t/TestApp-Plugin-REST/t

Jifty commits jifty-commit at lists.jifty.org
Tue Mar 17 11:15:14 EDT 2009


Author: sartak
Date: Tue Mar 17 11:15:13 2009
New Revision: 6619

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/t/TestApp-Plugin-REST/t/03-format.t

Log:
 r81244 at onn:  sartak | 2009-03-17 10:56:45 -0400
 Generalize the "test the results of some REST method" so we can add formats with DRY


Modified: jifty/trunk/t/TestApp-Plugin-REST/t/03-format.t
==============================================================================
--- jifty/trunk/t/TestApp-Plugin-REST/t/03-format.t	(original)
+++ jifty/trunk/t/TestApp-Plugin-REST/t/03-format.t	Tue Mar 17 11:15:13 2009
@@ -2,7 +2,7 @@
 use warnings;
 use strict;
 
-use Jifty::Test::Dist tests => 20;
+use Jifty::Test::Dist tests => 24;
 use Jifty::Test::WWW::Mechanize;
 
 my $server  = Jifty::Test->make_server;
@@ -19,75 +19,66 @@
 $u1->create(name => 'test', email => 'test at example.com');
 ok($u1->id);
 
-my %loader = (
-    yml  => \&Jifty::YAML::Load,
-    json => \&Jifty::JSON::jsonToObj,
-);
-sub fetch {
+sub result_of {
     local $Test::Builder::Level = $Test::Builder::Level + 1;
-    my ($url, $format) = @_;
+    my $request = shift;
+    my $test    = shift;
 
-    if (!exists($loader{$format})) {
-        die "Invalid format '$format'. Valid formats are: "
-          . join ', ', sort keys %loader;
+    if (!ref($request)) {
+        $request = {
+            mech_method => 'get',
+            url         => $request,
+        };
     }
 
-    $mech->get_ok("$URL$url.$format", "Get $url in $format");
-    is($mech->status, 200, "HTTP response status for $URL$url.$format");
-    my $from_dot_format = $loader{$format}->($mech->content);
-
-    return $from_dot_format;
+    my %loaders = (
+        yml  => \&Jifty::YAML::Load,
+        json => \&Jifty::JSON::jsonToObj,
+    );
+
+    for my $format (keys %loaders) {
+        my $url = $URL . $request->{url} . '.' . $format;
+
+        my $method = $request->{mech_method};
+        $mech->$method($url, @{ $request->{mech_args} || [] });
+
+        is($mech->status, 200, "HTTP response status for $url");
+
+        my $loaded = $loaders{$format}->($mech->content);
+        local $Test::Builder::Level = $Test::Builder::Level + 1;
+        $test->($loaded);
+    }
 }
 
-my $list = fetch('/=/model', 'yml');
-is(scalar @$list, 2, 'Got two models in YAML');
-is($list->[0],'TestApp.Plugin.REST.Model.Group');
-is($list->[1],'TestApp.Plugin.REST.Model.User');
-
-$list = fetch('/=/model', 'json');
-is(scalar @$list, 2, 'Got two models in JSON');
-is($list->[0],'TestApp.Plugin.REST.Model.Group');
-is($list->[1],'TestApp.Plugin.REST.Model.User');
-
-my $user = fetch('/=/model/User', 'yml');
-is(scalar keys %$user, 4, 'four keys in the user record, YAML');
-
-$user = fetch('/=/model/User', 'json');
-is(scalar keys %$user, 4, 'four keys in the user record, JSON');
-
-__END__
-
-$mech->get_ok('/=/model/user');
-is($mech->status,'200');
-$mech->get_ok('/=/model/TestApp::Plugin::REST::Model::User');
-is($mech->status,'200');
-$mech->get_ok('/=/model/TestApp.Plugin.REST.Model.User');
-is($mech->status,'200');
-$mech->get_ok('/=/model/testapp.plugin.rest.model.user');
-is($mech->status,'200');
-
-
-$mech->get_ok('/=/model/User.yml');
-my %keys =  %{get_content()};
-
-is((0+keys(%keys)), 4, "The model has 4 keys");
-is_deeply([sort keys %keys], [sort qw/id name email tasty/]);
+result_of '/=/model' => sub {
+    is_deeply($_[0], [
+        'TestApp.Plugin.REST.Model.Group',
+        'TestApp.Plugin.REST.Model.User',
+    ]);
+};
 
+result_of '/=/model/User' => sub {
+    is(scalar keys %{ $_[0] }, 4, 'four keys in the user record');
+};
 
-# on GET    '/=/model/*/*'   => \&list_model_items;
-$mech->get_ok('/=/model/user/id.yml');
-my @rows = @{get_content()};
-is($#rows,0);
+result_of '/=/model/user/id' => sub {
+    is(@{ $_[0] }, 1, "one user");
+};
 
+result_of '/=/model/user/id/1' => sub {
+    is_deeply($_[0], {
+        name  => 'test',
+        email => 'test at example.com',
+        id    => 1,
+        tasty => undef,
+    });
+};
 
-# on GET    '/=/model/*/*/*' => \&show_item;
-$mech->get_ok('/=/model/user/id/1.yml');
-my %content = %{get_content()};
-is_deeply(\%content, { name => 'test', email => 'test at example.com', id => 1, tasty => undef });
+result_of '/=/model/user/id/1/email' => sub {
+    is($_[0], 'test at example.com');
+};
 
-# on GET    '/=/model/*/*/*/*' => \&show_item_Field;
-$mech->get_ok('/=/model/user/id/1/email.yml');
-is(get_content(), 'test at example.com');
+__END__
 
 # on PUT    '/=/model/*/*/*' => \&replace_item;
 # on DELETE '/=/model/*/*/*' => \&delete_item;


More information about the Jifty-commit mailing list