From 607d49b5a3ed131be681e1abe5db3650e4168eb4 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 3 Aug 2015 11:37:36 -0300 Subject: [PATCH] Bug 14639: (regression tests) Extend Koha::MetadataRecord to handle serialization format In order to use Koha::MetadataRecord as a container for moving records around it is important to let it carry the serialization format of the record object it was built with, so it is easier and cheaper to make decisions about records. This patch introduces regression tests for the changes to be made. The 'format' param is introduced, and also sets default values: schema => 'marc21' format => 'usmarc' so the default behaviour is preserved, and no changes are needed in places Koha::MetadataRecord is used. ->new also returns undef if no record is passed, and raises a carped warning. To test: - Apply the patch - Run the new tests $ prove t/Koha_MetadataRecord.t => FAIL: Tests shoud fail as the changes are not implemented on Koha::MetadataRecord --- t/Koha_MetadataRecord.t | 53 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/t/Koha_MetadataRecord.t b/t/Koha_MetadataRecord.t index 33cdeb1..2b2ce95 100755 --- a/t/Koha_MetadataRecord.t +++ b/t/Koha_MetadataRecord.t @@ -17,10 +17,10 @@ # You should have received a copy of the GNU General Public License # along with Koha; if not, see . -use strict; -use warnings; +use Modern::Perl; -use Test::More tests => 4; +use Test::More tests => 5; +use Test::Warn; BEGIN { use_ok('Koha::MetadataRecord'); @@ -96,3 +96,50 @@ foreach my $field (@$hash) { is_deeply($hash, $samplehash, 'Generated hash correctly'); my $dupkeys = grep { $_ > 1 } values %fieldkeys; is($dupkeys, 0, 'No duplicate keys'); + + +subtest "new() tests" => sub { + + plan tests => 10; + + # Test default values with a MARC::Record record + my $record = MARC::Record->new(); + my $metadata_record = new Koha::MetadataRecord({ + record => $record, + schema => undef, + format => undef + }); + + is( ref($metadata_record), 'Koha::MetadataRecord', 'Type correct'); + is( ref($metadata_record->record), 'MARC::Record', 'Record type preserved'); + is( $metadata_record->schema, 'marc21', 'Metadata schema defaults to marc21'); + is( $metadata_record->format, 'usmarc', 'Serializacion format defaults to usmarc'); + + # Test passed values, also no constraint on record type + my $weird_record = {}; + bless $weird_record, 'Weird::Class'; + + $metadata_record = new Koha::MetadataRecord({ + record => $weird_record, + schema => 'something', + format => 'else' + }); + + is( ref($metadata_record), 'Koha::MetadataRecord', 'Type correct'); + is( ref($metadata_record->record), 'Weird::Class', 'Record type preserved'); + is( $metadata_record->schema, 'something', 'Metadata schema correctly set'); + is( $metadata_record->format, 'else', 'Serializacion format correctly set'); + + # Having a record object is mandatory + warning_is { $metadata_record = new Koha::MetadataRecord({ + record => undef, + schema => 'something', + format => 'else' + }) } + { carped => 'No record passed' }, + 'Undefined record raises carped warning'; + + is( $metadata_record, undef, 'record object mandatory') +}; + +1; -- 2.5.0