Skip to content

Commit

Permalink
add response's test
Browse files Browse the repository at this point in the history
  • Loading branch information
zigorou committed Sep 4, 2009
1 parent 45acc86 commit a72dda3
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 8 deletions.
11 changes: 7 additions & 4 deletions lib/Data/OpenSocial/Format/JSON.pm
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,21 @@ sub parse_object {
my %data;
for my $element ( keys %$object ) {
my $field = $class_type->element_to_field($element);

unless ($field) {
$data{$element} = $object->{$element};
next;
}

$data{$field} = $object->{$element};

my $type = $class_type->meta->get_attribute($field)->type_constraint;

if ( is_array_ref( $data{$field} ) ) {
}

if ( is_hash_ref( $data{$field} ) ) {
$data{$field} =
$class->parse_object(
$Data::OpenSocial::Types::COMPLEX_TYPES{$type}{class_type},
$object->{$field} );
$data{$field} );
}
}

Expand Down
3 changes: 3 additions & 0 deletions lib/Data/OpenSocial/Response.pm
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,21 @@ do {
field => 'media_item',
is => 'rw',
isa => 'OpenSocial.MediaItem',
coerce => 1,
},
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'message',
is => 'rw',
isa => 'OpenSocial.Message',
coerce => 1,
},
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'entry',
is => 'rw',
isa => 'OpenSocial.Entry.Collection',
coerce => 1,
},
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
Expand Down
18 changes: 18 additions & 0 deletions lib/Data/OpenSocial/Types.pm
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ our %COMPLEX_TYPES = (
via { Data::OpenSocial::Types->create_data( 'Drinker', $_ ); }
]
},
'OpenSocial.Entry' => +{
class_type => 'Data::OpenSocial::Entry',
coerce => [
from 'HashRef',
via { Data::OpenSocial::Types->create_data( 'Entry', $_ ); },
],
},
'OpenSocial.Group' => +{
class_type => 'Data::OpenSocial::Group',
coerce => [
Expand Down Expand Up @@ -241,6 +248,12 @@ our %COMPLEX_TYPES = (
coerce =>
[ from 'HashRef', via { Data::OpenSocial::Types->create_data('Presence', $_); } ],
},
'OpenSocial.Response' => +{
class_type => 'Data::OpenSocial::Response',
coerce => [
from 'HashRef', via { Data::OpenSocial::Types->create_date('Response', $_); },
],
},
'OpenSocial.Smoker' => +{
class_type => 'Data::OpenSocial::Smoker',
coerce =>
Expand Down Expand Up @@ -365,6 +378,11 @@ do {

subtype 'OpenSocial.Entry.Collection' => as
'ArrayRef[OpenSocial.Entry]';
coerce 'OpenSocial.Entry.Collection'
=> from 'ArrayRef[HashRef]'
=> via {
return [ map { Data::OpenSocial::Types->create_data('Entry', $_) } @$_ ];
};

subtype 'OpenSocial.Url.Collection' => as 'ArrayRef[OpenSocial.Url]';
coerce 'OpenSocial.Url.Collection'
Expand Down
7 changes: 3 additions & 4 deletions t/entry/10_format_json.t
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ our $json = JSON::Any->new;

{
my $src = +{
app_data => +{
appData => +{
pokes => 3,
last_poke => '2008-02-13T18:30:02Z',
},
Expand All @@ -88,7 +88,7 @@ our $json = JSON::Any->new;
Data::OpenSocial::Format::JSON->parse( 'Entry', $json->to_json($src) );

isa_ok( $data, 'Data::OpenSocial::Entry' );
isa_ok( $data->person, 'Data::OpenSocial::Appdata' );
isa_ok( $data->app_data, 'Data::OpenSocial::Appdata' );

my $json_str = Data::OpenSocial::Format::JSON->format($data);
note($json_str);
Expand All @@ -101,7 +101,6 @@ our $json = JSON::Any->new;
album => +{
id => '44332211',
thumbnailUrl => 'http://pages.example.org/albums/4433221-tn.png',

# caption => 'Example Album', # spec is wrong?
description =>
'This is an example album, and this text is an example description',
Expand All @@ -124,7 +123,7 @@ our $json = JSON::Any->new;

{
my $src = +{
media_item => +{
mediaItem => +{
id => '11223344',
thumbnailUrl => 'http://pages.example.org/images/11223344-tn.png',
mimeType => 'image/png',
Expand Down
250 changes: 250 additions & 0 deletions t/response/01_basic.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
use strict;
use Test::More;

use Data::OpenSocial::Response;

diag('response data basic testing');

{
my $src = +{
start_index => 1,
items_per_page => 10,
total_results => 100,
entry => [
+{
activity => +{
id =>
'http://example.org/activities/example.org:87ead8dead6beef/self/af3778',
title => '<a href="foo">some activity</a>',
updated => '2008-02-20T23:35:37',
body => 'Some details for some activity',
body_id => '383777272',
url => 'http://api.example.org/activity/feeds/.../af3778',
user_id => 'example.org:34KJDCSKJN2HHF0DW20394',
},
},
],
};

my $data = Data::OpenSocial::Response->new( $src );
isa_ok( $data, 'Data::OpenSocial::Response' );

is($data->start_index, $src->{start_index}, '//response/startIndex');
is($data->items_per_page, $src->{items_per_page}, '//response/itemsPerPage');
is($data->total_results, $src->{total_results}, '//response/totalResults');

my $activity = $data->entry->[0]->activity;

isa_ok($activity, 'Data::OpenSocial::Activity');
is($activity->id, $src->{entry}[0]{activity}{id}, '//response/entry[0]/person/id');
is($activity->title, $src->{entry}[0]{activity}{title}, '//response/entry[0]/person/title');
isa_ok($activity->updated, 'DateTime');
is($activity->body, $src->{entry}[0]{activity}{body}, '//response/entry[0]/person/body');
is($activity->body_id, $src->{entry}[0]{activity}{body_id}, '//response/entry[0]/person/bodyId');
is($activity->url, $src->{entry}[0]{activity}{url}, '//response/entry[0]/person/url');
}

{
my $src = +{
start_index => 1,
items_per_page => 10,
total_results => 100,
activity => +{
id =>
'http://example.org/activities/example.org:87ead8dead6beef/self/af3778',
title => '<a href="foo">some activity</a>',
updated => '2008-02-20T23:35:37',
body => 'Some details for some activity',
body_id => '383777272',
url => 'http://api.example.org/activity/feeds/.../af3778',
user_id => 'example.org:34KJDCSKJN2HHF0DW20394',
},
};

my $data = Data::OpenSocial::Response->new( $src );
isa_ok( $data, 'Data::OpenSocial::Response' );

is($data->start_index, $src->{start_index}, '//response/startIndex');
is($data->items_per_page, $src->{items_per_page}, '//response/itemsPerPage');
is($data->total_results, $src->{total_results}, '//response/totalResults');

my $activity = $data->activity;

isa_ok($activity, 'Data::OpenSocial::Activity');
is($activity->id, $src->{activity}{id}, '//response/activity/id');
is($activity->title, $src->{activity}{title}, '//response/activity/title');
isa_ok($activity->updated, 'DateTime');
is($activity->body, $src->{activity}{body}, '//response/activity/body');
is($activity->body_id, $src->{activity}{body_id}, '//response/activity/bodyId');
is($activity->url, $src->{activity}{url}, '//response/activity/url');
}

{
my $src = +{
start_index => 1,
items_per_page => 10,
total_results => 100,
entry => [
+{
person => +{
id => 'example.org:34KJDCSKJN2HHF0DW20394',
display_name => "Janey",
name => +{ formatted => 'Jane Doe' },
gender => 'female',
},
},
],
};

my $data = Data::OpenSocial::Response->new( $src );
isa_ok( $data, 'Data::OpenSocial::Response' );

is($data->start_index, $src->{start_index}, '//response/startIndex');
is($data->items_per_page, $src->{items_per_page}, '//response/itemsPerPage');
is($data->total_results, $src->{total_results}, '//response/totalResults');

my $person = $data->entry->[0]->person;

isa_ok($person, 'Data::OpenSocial::Person');
is($person->id, $src->{entry}[0]{person}{id}, '//response/entry[0]/person/id');
is($person->display_name, $src->{entry}[0]{person}{display_name}, '//response/entry[0]/person/displayName');
isa_ok($person->name, 'Data::OpenSocial::Name');
is($person->name->formatted, $src->{entry}[0]{person}{name}{formatted}, '//response/entry[0]/person/name/formatted');
is($person->gender, $src->{entry}[0]{person}{gender}, '//response/entry[0]/person/gender');
}

{
my $src = +{
start_index => 1,
items_per_page => 10,
total_results => 100,
person => +{
id => 'example.org:34KJDCSKJN2HHF0DW20394',
display_name => "Janey",
name => +{ formatted => 'Jane Doe' },
gender => 'female',
},
};

my $data = Data::OpenSocial::Response->new( $src );
isa_ok( $data, 'Data::OpenSocial::Response' );

is($data->start_index, $src->{start_index}, '//response/startIndex');
is($data->items_per_page, $src->{items_per_page}, '//response/itemsPerPage');
is($data->total_results, $src->{total_results}, '//response/totalResults');

my $person = $data->entry->[0]->person;

isa_ok($person, 'Data::OpenSocial::Person');
is($person->id, $src->{entry}[0]{person}{id}, '//response/entry[0]/person/id');
is($person->display_name, $src->{entry}[0]{person}{display_name}, '//response/entry[0]/person/displayName');
isa_ok($person->name, 'Data::OpenSocial::Name');
is($person->name->formatted, $src->{entry}[0]{person}{name}{formatted}, '//response/entry[0]/person/name/formatted');
is($person->gender, $src->{entry}[0]{person}{gender}, '//response/entry[0]/person/gender');
}

{
my $src = +{
start_index => 1,
items_per_page => 10,
total_results => 100,
entry => [
+{
group => +{
id => 'example.org:34KJDCSKJN2HHF0DW20394/friends',
title => 'Peeps',
},
},
],
};

my $data = Data::OpenSocial::Entry->new( group => $src );
isa_ok( $data, 'Data::OpenSocial::Entry' );

my $dst = $data->group;
isa_ok( $dst, 'Data::OpenSocial::Group' );

is( $dst->id, $src->{id}, '//entry/group/id' );
is( $dst->title, $src->{title}, '//entry/group/title' );
}

# {
# my $src = +{
# pokes => 3,
# last_poke => '2008-02-13T18:30:02Z',
# };

# my $data = Data::OpenSocial::Entry->new( app_data => $src );
# isa_ok( $data, 'Data::OpenSocial::Entry' );

# my $dst = $data->app_data;
# isa_ok( $dst, 'Data::OpenSocial::Appdata' );

# is( $dst->entry->[0]->key, 'last_poke', '//entry/appdata/entry[0]/key' );
# is( $dst->entry->[0]->value,
# '2008-02-13T18:30:02Z', '//entry/appdata/entry[0]/value' );
# is( $dst->entry->[1]->key, 'pokes', '//entry/appdata/entry[1]/key' );
# is( $dst->entry->[1]->value, 3, '//entry/appdata/entry[1]/value' );
# }

# {
# my $src = +{
# id => '44332211',
# thumbnail_url => 'http://pages.example.org/albums/4433221-tn.png',

# # caption => 'Example Album', # spec is wrong?
# description =>
# 'This is an example album, and this text is an example description',
# location => +{ latitude => 0, longitude => 0 },
# owner_id => 'example.org:55443322',
# };

# my $data = Data::OpenSocial::Entry->new( album => $src );
# isa_ok( $data, 'Data::OpenSocial::Entry' );

# my $dst = $data->album;
# isa_ok( $dst, 'Data::OpenSocial::Album' );

# is( $dst->id, $src->{id}, '//entry/album/id' );
# is( $dst->thumbnail_url, $src->{thumbnail_url},
# '//entry/album/thumbnailUrl' );
# is( $dst->description, $src->{description}, '//entry/album/description' );
# is(
# $dst->location->latitude,
# $src->{location}{latitude},
# '//entry/album/location/latitude'
# );
# is(
# $dst->location->longitude,
# $src->{location}{longitude},
# '//entry/album/location/longitude'
# );
# is( $dst->owner_id, $src->{owner_id}, '//entry/album/owner_id' );
# }

# {
# my $src = +{
# id => '11223344',
# thumbnail_url => 'http://pages.example.org/images/11223344-tn.png',
# mime_type => 'image/png',
# type => 'IMAGE',
# url => 'http://pages.example.org/images/11223344.png',
# album_id => '44332211',
# };

# my $data = Data::OpenSocial::Entry->new( media_item => $src );
# isa_ok( $data, 'Data::OpenSocial::Entry' );

# my $dst = $data->media_item;
# isa_ok( $dst, 'Data::OpenSocial::MediaItem' );

# is( $dst->id, $src->{id}, '//entry/mediaItem/id' );
# is( $dst->thumbnail_url, $src->{thumbnail_url},
# '//entry/mediaItem/thumbnailUrl' );
# is( $dst->mime_type, $src->{mime_type}, '//entry/mediaItem/mimeType' );
# is( $dst->type, $src->{type}, '//entry/mediaItem/type' );
# is( $dst->url, $src->{url}, '//entry/mediaItem/url' );
# is( $dst->album_id, $src->{album_id}, '//entry/mediaItem/albumId' );
# }

done_testing();

0 comments on commit a72dda3

Please sign in to comment.