From 691799eb26751b7899f9d46fb87b7ebb5e67657f Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 18 Dec 2017 13:47:53 -0300 Subject: [PATCH] Bug 19828: Unit tests This patch introduces unit tests for the changes this bug introduces to Koha::Object->store. To test: - Apply this patch - Run: $ kshell k$ prove t/db_dependent/Koha/Object.t => FAIL: Tests should fail because the changes are not implemented on this patch Signed-off-by: Kyle M Hall Signed-off-by: Josef Moravec --- t/db_dependent/Koha/Object.t | 93 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Object.t b/t/db_dependent/Koha/Object.t index 5f91415..542b1e2 100755 --- a/t/db_dependent/Koha/Object.t +++ b/t/db_dependent/Koha/Object.t @@ -17,7 +17,8 @@ use Modern::Perl; -use Test::More tests => 9; +use Test::More tests => 10; +use Test::Exception; use Test::Warn; use C4::Context; @@ -218,3 +219,93 @@ subtest "Test update method" => sub { $schema->storage->txn_rollback; }; + +subtest 'store() tests' => sub { + + plan tests => 10; + + $schema->storage->txn_begin; + + # Create a category to make sure its ID doesn't exist on the DB + my $category = $builder->build_object({ class => 'Koha::Patron::Categories' }); + my $category_id = $category->id; + $category->delete; + + my $patron = Koha::Patron->new({ categorycode => $category_id }); + + my $print_error = $schema->storage->dbh->{PrintError}; + $schema->storage->dbh->{PrintError} = 0; + throws_ok + { $patron->store } + 'Koha::Exceptions::Object::FKConstraint', + 'Exception is thrown correctly'; + is( + $@->message, + "Broken FK constraint", + 'Exception message is correct' + ); + is( + $@->broken_fk, + 'categorycode', + 'Exception field is correct' + ); + + my $library = $builder->build_object({ class => 'Koha::Libraries' }); + $category = $builder->build_object({ class => 'Koha::Patron::Categories' }); + $patron = $builder->build_object({ class => 'Koha::Patrons' }); + + my $new_patron = Koha::Patron->new({ + branchcode => $library->id, + cardnumber => $patron->cardnumber, + categorycode => $category->id + }); + + throws_ok + { $new_patron->store } + 'Koha::Exceptions::Object::DuplicateID', + 'Exception is thrown correctly'; + + is( + $@->message, + 'Duplicate ID', + 'Exception message is correct' + ); + + is( + $@->duplicate_id, + 'cardnumber', + 'Exception field is correct' + ); + + $new_patron = Koha::Patron->new({ + branchcode => $library->id, + userid => $patron->userid, + categorycode => $category->id + }); + + throws_ok + { $new_patron->store } + 'Koha::Exceptions::Object::DuplicateID', + 'Exception is thrown correctly'; + + is( + $@->message, + 'Duplicate ID', + 'Exception message is correct' + ); + + is( + $@->duplicate_id, + 'userid', + 'Exception field is correct' + ); + + $schema->storage->dbh->{PrintError} = $print_error; + + # Successful test + $patron->set({ firstname => 'Manuel' }); + my $ret = $patron->store; + is( ref($ret), 'Koha::Patron', 'store() returns the object on success' ); + + $schema->storage->txn_rollback; +}; -- 2.1.4