From a8496c4d15f5c82396da74eb33a36db058289b12 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Thu, 18 Mar 2010 15:06:17 -0700 Subject: [PATCH] Convert Comments role to use Types. Build a test for it. --- lib/WebGUI/Role/Asset/Comments.pm | 10 +--- t/Role/Asset/Comments.t | 88 +++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 t/Role/Asset/Comments.t diff --git a/lib/WebGUI/Role/Asset/Comments.pm b/lib/WebGUI/Role/Asset/Comments.pm index 3fdb5cf4f..0d1493589 100644 --- a/lib/WebGUI/Role/Asset/Comments.pm +++ b/lib/WebGUI/Role/Asset/Comments.pm @@ -16,6 +16,7 @@ package WebGUI::Role::Asset::Comments; use Moose::Role; use WebGUI::Definition::Asset; +use WebGUI::Types; define tableName => 'assetAspectComments'; property comments => ( noFormPost => 1, @@ -31,7 +32,6 @@ property averageCommentRating => ( default => 0, ); -use JSON; use WebGUI::Exception; use WebGUI::Form; use WebGUI::HTML; @@ -131,14 +131,6 @@ sub canComment { } -#------------------------------------------------------------------- - -=head2 definition - -Extends the definition to add the comments and averageCommentRating fields. - -=cut - #------------------------------------------------------------------- =head2 deleteComment ( id ) diff --git a/t/Role/Asset/Comments.t b/t/Role/Asset/Comments.t new file mode 100644 index 000000000..bf6aabfee --- /dev/null +++ b/t/Role/Asset/Comments.t @@ -0,0 +1,88 @@ +# vim:syntax=perl +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2009 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------ +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------ + +# Write a little about what this script tests. +# +# + +use FindBin; +use strict; +use lib "$FindBin::Bin/../../lib"; +use Test::More; +use Test::Deep; +use Test::MockObject; + +use WebGUI::Test; # Must use this before any other WebGUI modules +use WebGUI::Session; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; +$session->db->dbh->do('drop table if exists dummyTable'); +$session->db->dbh->do(< 'dummyTable'; +with 'WebGUI::Role::Asset::Comments'; + +package main; + +use WebGUI::Asset; + +my $mock = Test::MockObject->new(); +$mock->fake_module('WebGUI::Asset::DummyComments', '__DUMMY__DUMMY__' => sub {}, ); + +#---------------------------------------------------------------------------- +# put your tests here + +my $dummy = WebGUI::Asset->getDefault($session)->addChild({ + className => 'WebGUI::Asset::DummyComments', + url => '/home/shawshank', + title => 'Dummy Title', + synopsis => 'Dummy Synopsis', + description => 'Dummy Description', +}); +my $tag = WebGUI::VersionTag->getWorking($session); +addToCleanup($tag); + +ok $dummy->does('WebGUI::Role::Asset::Comments'), 'dummy object does the right role'; +$dummy->comments([{ television => 'drop', misdemeanor => 'felony', }]); +$dummy->write(); + +my $json = $session->db->quickScalar('select comments from assetAspectComments where assetId=?', [$dummy->assetId]); +like $json, qr/"television":"drop"/, 'checking serialize to json in the db'; + +my $dummy2 = $dummy->cloneFromDb(); +cmp_deeply( + $dummy2->comments(), + [ { television => 'drop', misdemeanor => 'felony', }], + 'checking JSON and deserialize from db' +); + +done_testing(); + +#---------------------------------------------------------------------------- +# Cleanup +END { + $session->db->dbh->do('drop table if exists dummyTable'); +} +#vim:ft=perl