Bug 17755: Unit tests

This patch introduces unit tests for Koha::Object::Library::Limit. It is
done this way because it needs to be instantiated to be usable.

To test:
- Run:
  $ prove t/db_dependent/Koha/Patron/Attribute/Types.t
=> SUCCESS: Tests pass
- Sign off :-D

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
This commit is contained in:
Tomás Cohen Arazi 2016-12-12 11:15:45 -03:00 committed by Kyle M Hall
parent bd01041ac6
commit 62249ff573

View file

@ -0,0 +1,250 @@
#!/usr/bin/perl
# Copyright 2016 Koha Development team
#
# This file is part of Koha
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 5;
use t::lib::TestBuilder;
use t::lib::Mocks;
use Test::Exception;
use C4::Context;
use Koha::Database;
use Koha::Patron::Attribute::Type;
use Koha::Patron::Attribute::Types;
my $schema = Koha::Database->new->schema;
my $dbh = C4::Context->dbh;
my $builder = t::lib::TestBuilder->new;
subtest 'new() tests' => sub {
plan tests => 2;
$schema->storage->txn_begin;
# Cleanup before running the tests
Koha::Patron::Attribute::Types->search()->delete();
my $attribute_type = Koha::Patron::Attribute::Type->new(
{ code => 'code',
description => 'description',
repeatable => 0
}
)->store();
is( Koha::Patron::Attribute::Types->search()->count,
1, 'Only one object created' );
my $cateogory_code
= $builder->build( { source => 'Category' } )->{categorycode};
my $attribute_type_with_category = Koha::Patron::Attribute::Type->new(
{ code => 'code_2',
description => 'description',
category_code => $cateogory_code,
repeatable => 0
}
)->store();
is( Koha::Patron::Attribute::Types->search()->count,
2, 'Two objects created' );
$schema->storage->txn_rollback;
};
subtest 'library_limits() tests' => sub {
plan tests => 5;
$schema->storage->txn_begin;
# Cleanup before running the tests
Koha::Patron::Attribute::Types->search()->delete();
my $attribute_type = Koha::Patron::Attribute::Type->new(
{ code => 'code',
description => 'description',
repeatable => 0
}
)->store();
my $library = $builder->build( { source => 'Branch' } )->{branchcode};
my $library_limits = $attribute_type->library_limits();
is_deeply( $library_limits, [],
'No branch limitations defined for attribute type' );
my $print_error = $dbh->{PrintError};
$dbh->{PrintError} = 0;
throws_ok {
$library_limits = $attribute_type->library_limits( ['fake'] );
}
'Koha::Exceptions::CannotAddLibraryLimit',
'Exception thrown on single invalid branchcode';
throws_ok {
$library_limits
= $attribute_type->library_limits( [ 'fake', $library ] );
}
'Koha::Exceptions::CannotAddLibraryLimit',
'Exception thrown on invalid branchcode present';
$dbh->{PrintError} = $print_error;
$library_limits = $attribute_type->library_limits( [$library] );
is_deeply( $library_limits, [1], 'Library limits correctly set' );
my $another_library
= $builder->build( { source => 'Branch' } )->{branchcode};
$library_limits
= $attribute_type->library_limits( [ $library, $another_library ] );
is_deeply( $library_limits, [ 1, 1 ], 'Library limits correctly set' );
$schema->storage->txn_rollback;
};
subtest 'add_library_limit() tests' => sub {
plan tests => 3;
$schema->storage->txn_begin;
# Cleanup before running the tests
Koha::Patron::Attribute::Types->search()->delete();
my $attribute_type = Koha::Patron::Attribute::Type->new(
{ code => 'code',
description => 'description',
repeatable => 0
}
)->store();
throws_ok { $attribute_type->add_library_limit() }
'Koha::Exceptions::MissingParameter',
'branchcode parameter is mandatory';
my $library = $builder->build( { source => 'Branch' } )->{branchcode};
is( $attribute_type->add_library_limit($library),
1, 'Library limit successfully added' );
my $print_error = $dbh->{PrintError};
$dbh->{PrintError} = 0;
throws_ok {
$attribute_type->add_library_limit('fake');
}
'Koha::Exceptions::CannotAddLibraryLimit',
'Exception thrown on invalid branchcode';
$dbh->{PrintError} = $print_error;
$schema->storage->txn_rollback;
};
subtest 'del_library_limit() tests' => sub {
plan tests => 4;
$schema->storage->txn_begin;
# Cleanup before running the tests
Koha::Patron::Attribute::Types->search()->delete();
my $attribute_type = Koha::Patron::Attribute::Type->new(
{ code => 'code',
description => 'description',
repeatable => 0
}
)->store();
throws_ok { $attribute_type->del_library_limit() }
'Koha::Exceptions::MissingParameter',
'branchcode parameter is mandatory';
my $library = $builder->build( { source => 'Branch' } )->{branchcode};
$attribute_type->add_library_limit($library);
is( $attribute_type->del_library_limit($library),
1, 'Library limit successfully deleted' );
my $print_error = $dbh->{PrintError};
$dbh->{PrintError} = 0;
throws_ok {
$attribute_type->del_library_limit($library);
}
'Koha::Exceptions::ObjectNotFound',
'Exception thrown on non-existent library limit';
throws_ok {
$attribute_type->del_library_limit('fake');
}
'Koha::Exceptions::ObjectNotFound',
'Exception thrown on non-existent library limit';
$dbh->{PrintError} = $print_error;
$schema->storage->txn_rollback;
};
subtest 'replace_library_limits() tests' => sub {
plan tests => 6;
$schema->storage->txn_begin;
# Cleanup before running the tests
Koha::Patron::Attribute::Types->search()->delete();
my $attribute_type = Koha::Patron::Attribute::Type->new(
{ code => 'code',
description => 'description',
repeatable => 0
}
)->store();
is_deeply( $attribute_type->replace_library_limits( [] ),
[], 'Replacing with empty array returns an empty array as expected' );
is_deeply( $attribute_type->library_limits(),
[], 'Replacing with empty array yields no library limits' );
my $library_1 = $builder->build({ source => 'Branch'})->{branchcode};
my $library_2 = $builder->build({ source => 'Branch'})->{branchcode};
is_deeply( $attribute_type->replace_library_limits( [$library_1] ),
[ 1 ], 'Successfully adds a single library limit' );
is_deeply( $attribute_type->library_limits(),
[ $library_1 ], 'Library limit correctly set' );
is_deeply( $attribute_type->replace_library_limits( [$library_1, $library_2] ),
[ 1, 1 ], 'Successfully adds two library limit' );
is_deeply( $attribute_type->library_limits(),
[ $library_1, $library_2 ], 'Library limit correctly set' );
$schema->storage->txn_rollback;
};
1;