[Jifty-commit] r4996 - in jifty/trunk: lib/Jifty/Plugin/Attributes/Mixin t/TestApp-Plugin-Attributes/lib/TestApp/Plugin/Attributes/Model t/TestApp-Plugin-Attributes/t

Jifty commits jifty-commit at lists.jifty.org
Tue Feb 5 12:58:30 EST 2008


Author: sartak
Date: Tue Feb  5 12:58:28 2008
New Revision: 4996

Added:
   jifty/trunk/t/TestApp-Plugin-Attributes/lib/TestApp/Plugin/Attributes/Model/Song.pm
   jifty/trunk/t/TestApp-Plugin-Attributes/t/01-content.t
   jifty/trunk/t/TestApp-Plugin-Attributes/t/02-crud-methods.t
   jifty/trunk/t/TestApp-Plugin-Attributes/t/03-permissions.t
Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Plugin/Attributes/Mixin/Attributes.pm
   jifty/trunk/t/TestApp-Plugin-Attributes/t/00-basic.t

Log:
 r51363 at onn:  sartak | 2008-02-05 12:57:59 -0500
 Forgot to commit TestApp::Plugin::Attributes::Model::Song
 Clean up the return values of the attribute mixin methods
 A bunch more tests


Modified: jifty/trunk/lib/Jifty/Plugin/Attributes/Mixin/Attributes.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Attributes/Mixin/Attributes.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/Attributes/Mixin/Attributes.pm	Tue Feb  5 12:58:28 2008
@@ -24,7 +24,8 @@
 
 =head2 first_attribute name
 
-Returns the first attribute on this object with the given name.
+Returns the first attribute on this object with the given name, or C<undef> if
+none exist.
 
 =cut
 
@@ -37,8 +38,8 @@
 
 =head2 add_attribute PARAMHASH
 
-Adds the given attribute to this object. The following fields must be
-provided:
+Adds the given attribute to this object. Returns the new attribute if
+successful, C<undef> otherwise. The following fields must be provided:
 
 =over 4
 
@@ -69,12 +70,15 @@
         description => $args{description},
         content     => $args{content},
     );
+
+    return $attr->id ? $attr : undef;
 }
 
 =head2 set_attribute PARAMHASH
 
 Sets the given attribute on the object. Note that all existing attributes of
-that name will be removed. The following fields must be provided:
+that name will be removed. Returns the new attribute or C<undef> if one could
+not be created. The following fields must be provided:
 
 =over 4
 
@@ -117,18 +121,23 @@
 
 =head2 delete_attribute name
 
-Deletes all attributes of the given name
+Deletes all attributes of the given name. Returns a true value if all attributes
+were deleted, a false if any could not be.
 
 =cut
 
 sub delete_attribute {
     my $self = shift;
     my $name = shift;
+    my $ok = 1;
 
     my $attrs = $self->attributes->named($name);
     while (my $attr = $attrs->next) {
-        $attr->delete;
+        $attr->delete
+            or $ok = 0;
     }
+
+    return $ok;
 }
 
 1;

Added: jifty/trunk/t/TestApp-Plugin-Attributes/lib/TestApp/Plugin/Attributes/Model/Song.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp-Plugin-Attributes/lib/TestApp/Plugin/Attributes/Model/Song.pm	Tue Feb  5 12:58:28 2008
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl
+package TestApp::Plugin::Attributes::Model::Song;
+use strict;
+use warnings;
+
+use Jifty::Plugin::Attributes::Mixin::Attributes;
+use Jifty::DBI::Schema;
+use Jifty::Record schema {
+    column 'name' =>
+        type is 'text',
+        is mandatory;
+
+    column 'artist' =>
+        type is 'text',
+        is mandatory;
+
+    column 'album' =>
+        type is 'text',
+        is mandatory;
+};
+
+our %rights;
+
+sub current_user_can {
+    my $self = shift;
+    my $right = shift;
+    my %args = @_;
+
+    return $rights{$right} if exists $rights{$right};
+
+    $self->SUPER::current_user_can($right, @_);
+}
+
+sub set_right {
+    my $self = shift;
+    my $right = shift;
+    my $val = shift;
+
+    return delete $rights{$right} if !defined($val);
+    $rights{$right} = $val;
+}
+
+1;
+

Modified: jifty/trunk/t/TestApp-Plugin-Attributes/t/00-basic.t
==============================================================================
--- jifty/trunk/t/TestApp-Plugin-Attributes/t/00-basic.t	(original)
+++ jifty/trunk/t/TestApp-Plugin-Attributes/t/00-basic.t	Tue Feb  5 12:58:28 2008
@@ -3,7 +3,7 @@
 use strict;
 use lib 't/lib';
 use Jifty::SubTest;
-use Jifty::Test tests => 10;
+use Jifty::Test tests => 12;
 
 my $song = TestApp::Plugin::Attributes::Model::Song->new;
 my ($ok, $msg) = $song->create(
@@ -34,3 +34,13 @@
 is($attr->description,  'Is this song an instrumental?', "description of the attribute was saved");
 is($attr->content, 1, "content of the attribute was saved");
 
+my $song2 = TestApp::Plugin::Attributes::Model::Song->new;
+($ok, $msg) = $song2->create(
+    name   => 'A Passage to Bangkok',
+    artist => 'Rush',
+    album  => '2112',
+);
+ok($ok, $msg);
+
+ok(!defined($song2->first_attribute('is_instrumental')), "second song has no is_instrumental attribute");
+

Added: jifty/trunk/t/TestApp-Plugin-Attributes/t/01-content.t
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp-Plugin-Attributes/t/01-content.t	Tue Feb  5 12:58:28 2008
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+use lib 't/lib';
+use Jifty::SubTest;
+use Jifty::Test tests => 7;
+
+my $song = TestApp::Plugin::Attributes::Model::Song->new;
+my ($ok, $msg) = $song->create(
+    name   => 'Home',
+    artist => 'Dream Theater',
+    album  => 'Scenes from a Memory',
+);
+ok($ok, $msg);
+
+$song->add_attribute(name => 'artists', content => [qw/LaBrie Myung Petrucci Portroy Rudess/]);
+is_deeply($song->first_attribute('artists')->content, [qw/LaBrie Myung Petrucci Portroy Rudess/], "attribute content can be an arrayref");
+
+$song->add_attribute(name => 'guests', content => {Thomason => "additional vocals", Brown => "hypnotherapist"});
+is_deeply($song->first_attribute('guests')->content, {Thomason => "additional vocals", Brown => "hypnotherapist"}, "attribute content can be a hashref");
+
+is($song->attributes->count, 2, "two attributes");
+is($song->attributes->named('artists')->count, 1, "one attribute named artists");
+is($song->attributes->named('guests')->count, 1, "one attribute named guests");
+
+my $complex = {
+    a => [qw/a b c/],
+    b => {
+        c => 'd',
+        e => [qw/f g h/],
+        i => {
+            j => 'k',
+            l => 'm',
+        },
+        n => [],
+    },
+    o => undef,
+};
+
+$song->add_attribute(
+    name => 'complex',
+    content => $complex,
+);
+
+is_deeply($song->first_attribute('complex')->content, $complex, "complex content can be saved");
+

Added: jifty/trunk/t/TestApp-Plugin-Attributes/t/02-crud-methods.t
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp-Plugin-Attributes/t/02-crud-methods.t	Tue Feb  5 12:58:28 2008
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+use lib 't/lib';
+use Jifty::SubTest;
+use Jifty::Test tests => 8;
+
+my $song = TestApp::Plugin::Attributes::Model::Song->new;
+my ($ok, $msg) = $song->create(
+    name   => 'Backdrifts',
+    artist => 'Radiohead',
+    album  => 'Hail to the Thief',
+);
+ok($ok, $msg);
+
+$song->add("radiohead");
+$song->has_tags(qw/radiohead/);
+
+$song->add("2003");
+$song->has_tags(qw/radiohead 2003/);
+
+$song->set("httt");
+$song->has_tags(qw/httt/);
+
+$song->add("radiohead");
+$song->has_tags(qw/httt radiohead/);
+
+$song->add("2003");
+$song->has_tags(qw/httt radiohead 2003/);
+
+$song->delete_attribute('tag');
+$song->has_tags(qw//);
+
+$song->add("radiohead");
+$song->has_tags(qw/radiohead/);
+
+sub TestApp::Plugin::Attributes::Model::Song::add {
+    $_[0]->add_attribute(name => 'tag', content => $_[1]);
+}
+
+sub TestApp::Plugin::Attributes::Model::Song::set {
+    $_[0]->set_attribute(name => 'tag', content => $_[1]);
+}
+
+sub TestApp::Plugin::Attributes::Model::Song::has_tags {
+    my $self     = shift;
+    my %expected = map { $_ => 1 } @_;
+    my %got      = map { $_->content => 1 }
+                   @{ $self->attributes->named("tag")->items_array_ref };
+
+    ::is_deeply(\%got, \%expected, "attributes set correctly");
+}
+

Added: jifty/trunk/t/TestApp-Plugin-Attributes/t/03-permissions.t
==============================================================================
--- (empty file)
+++ jifty/trunk/t/TestApp-Plugin-Attributes/t/03-permissions.t	Tue Feb  5 12:58:28 2008
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+use lib 't/lib';
+use Jifty::SubTest;
+use Jifty::Test tests => 30;
+
+my $song = TestApp::Plugin::Attributes::Model::Song->new;
+my ($ok, $msg) = $song->create(
+    name   => 'Hysteria',
+    artist => 'Muse',
+    album  => 'Absolution',
+);
+ok($ok, $msg);
+
+ok($song->add_attribute(name => 'stars', content => 5), "can add attributes");
+my $attr = $song->first_attribute('stars');
+
+has_right($_) for qw/create read update delete/;
+
+$song->set_right(create => 0);
+has_right($_) for qw/create read update delete/;
+
+$song->set_right(delete => 0);
+has_right($_) for qw/create read update delete/;
+
+$song->set_right(update => 0);
+lacks_right($_, "$_ checks object's update right") for qw/create update delete/;
+has_right('read', "read checks object's read right");
+
+$song->set_right(read => 0);
+lacks_right($_) for qw/create read update delete/;
+
+$song->set_right(update => undef);
+has_right($_, "$_ checks object's update right") for qw/create update delete/;
+lacks_right('read', "read checks object's read right");
+
+$song->set_right(read => undef);
+has_right($_) for qw/create read update delete/;
+
+sub has_right {
+    my $right = shift;
+    my $has_right = $attr->current_user_can($right, object => $song);
+
+    local $Test::Builder::Level = $Test::Builder::Level + 1;
+    ok($has_right, shift || "current_user_can $right");
+}
+
+sub lacks_right {
+    my $right = shift;
+    my $has_right = $attr->current_user_can($right, object => $song);
+
+    local $Test::Builder::Level = $Test::Builder::Level + 1;
+    ok(!$has_right, shift || "current_user_cannot $right");
+}
+


More information about the Jifty-commit mailing list