Bugzilla – Attachment 19338 Details for
Bug 10508
UT: C4::Branch.pm needs unit tests
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 10508 : C4::Branch.pm needs unit tests.
Bug-10508--C4Branchpm-needs-unit-tests.patch (text/plain), 11.77 KB, created by
kenza
on 2013-07-02 12:26:57 UTC
(
hide
)
Description:
Bug 10508 : C4::Branch.pm needs unit tests.
Filename:
MIME Type:
Creator:
kenza
Created:
2013-07-02 12:26:57 UTC
Size:
11.77 KB
patch
obsolete
>From 010681355ce36e81a8154d0cc1dfb6e23556050f Mon Sep 17 00:00:00 2001 >From: Kenza Zaki <kenza.zaki@biblibre.com> >Date: Tue, 2 Jul 2013 14:24:08 +0200 >Subject: [PATCH] Bug 10508 : C4::Branch.pm needs unit tests. > >Unit tests are wrap in a transaction. > >NOTE: the field "issuing" isn't used in the database > >To test: >prove t/db_dependent/Branches.t >t/db_dependent/Branch.t .. ok >All tests successful. >Files=1, Tests=31, 0 wallclock secs ( 0.03 usr 0.00 sys + 0.13 cusr 0.00 csys = 0.16 CPU) >Result: PASS >--- > C4/Branch.pm | 10 -- > t/db_dependent/Branch.t | 302 +++++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 302 insertions(+), 10 deletions(-) > create mode 100755 t/db_dependent/Branch.t > >diff --git a/C4/Branch.pm b/C4/Branch.pm >index 5783de9..216c4cf 100644 >--- a/C4/Branch.pm >+++ b/C4/Branch.pm >@@ -182,7 +182,6 @@ sub GetBranchName { > $sth = $dbh->prepare("Select branchname from branches where branchcode=?"); > $sth->execute($branchcode); > my $branchname = $sth->fetchrow_array; >- $sth->finish; > return ($branchname); > } > >@@ -278,7 +277,6 @@ sub ModBranch { > "insert into branchrelations (branchcode, categorycode) values(?, ?)" > ); > $sth->execute( $branchcode, $cat ); >- $sth->finish; > } > foreach my $cat (@removecats) { > my $sth = >@@ -286,7 +284,6 @@ sub ModBranch { > "delete from branchrelations where branchcode=? and categorycode=?" > ); > $sth->execute( $branchcode, $cat ); >- $sth->finish; > } > } > >@@ -422,7 +419,6 @@ sub GetBranchesInCategory { > while (my $branch = $sth->fetchrow) { > push @branches, $branch; > } >- $sth->finish(); > return( \@branches ); > } > >@@ -467,11 +463,9 @@ sub GetBranchInfo { > while ( my ($cat) = $nsth->fetchrow_array ) { > push( @cats, $cat ); > } >- $nsth->finish; > $data->{'categories'} = \@cats; > push( @results, $data ); > } >- $sth->finish; > return \@results; > } > >@@ -486,7 +480,6 @@ sub DelBranch { > my $dbh = C4::Context->dbh; > my $sth = $dbh->prepare("delete from branches where branchcode = ?"); > $sth->execute($branchcode); >- $sth->finish; > } > > =head2 ModBranchCategoryInfo >@@ -503,13 +496,11 @@ sub ModBranchCategoryInfo { > # we are doing an insert > my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype,show_in_pulldown) VALUES (?,?,?,?,?)"); > $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'} ); >- $sth->finish(); > } > else { > # modifying > my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=?,show_in_pulldown=? WHERE categorycode=?"); > $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'},uc( $data->{'categorycode'} ) ); >- $sth->finish(); > } > } > >@@ -546,7 +537,6 @@ sub DelBranchCategory { > my $dbh = C4::Context->dbh; > my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?"); > $sth->execute($categorycode); >- $sth->finish; > } > > =head2 CheckBranchCategorycode >diff --git a/t/db_dependent/Branch.t b/t/db_dependent/Branch.t >new file mode 100755 >index 0000000..3f80a28 >--- /dev/null >+++ b/t/db_dependent/Branch.t >@@ -0,0 +1,302 @@ >+#!/usr/bin/perl >+ >+use Modern::Perl; >+use Test::More tests => 31; >+use C4::Context; >+ >+BEGIN { >+ use_ok('C4::Branch'); >+} >+ >+can_ok( >+ 'C4::Branch', qw( >+ GetBranchCategory >+ GetBranchName >+ GetBranch >+ GetBranches >+ GetBranchesLoop >+ GetBranchDetail >+ get_branchinfos_of >+ ModBranch >+ CheckBranchCategorycode >+ GetBranchInfo >+ GetCategoryTypes >+ GetBranchCategories >+ GetBranchesInCategory >+ ModBranchCategoryInfo >+ DelBranch >+ DelBranchCategory >+ CheckCategoryUnique >+ mybranch >+ GetBranchesCount) >+); >+ >+# Start transaction >+my $dbh = C4::Context->dbh; >+$dbh->{RaiseError} = 1; >+$dbh->{AutoCommit} = 0; >+ >+# Start test >+ >+my $count = GetBranchesCount; >+like( $count, '/^\d+$/', "the count is a number" ); >+ >+#add 2 branches >+my $b1 = { >+ add => 1, >+ branchcode => 'BRA', >+ branchname => 'BranchA', >+ branchaddress1 => 'adr1A', >+ branchaddress2 => 'adr2A', >+ branchaddress3 => 'adr3A', >+ branchzip => 'zipA', >+ branchcity => 'cityA', >+ branchstate => 'stateA', >+ branchcountry => 'countryA', >+ branchphone => 'phoneA', >+ branchfax => 'faxA', >+ branchemail => 'emailA', >+ branchurl => 'urlA', >+ branchip => 'ipA', >+ branchprinter => 'printerA', >+ branchnotes => 'noteA', >+ opac_info => 'opacA' >+}; >+my $b2 = { >+ branchcode => 'BRB', >+ branchname => 'BranchB', >+ branchaddress1 => 'adr1B', >+ branchaddress2 => 'adr2B', >+ branchaddress3 => 'adr3B', >+ branchzip => 'zipB', >+ branchcity => 'cityB', >+ branchstate => 'stateB', >+ branchcountry => 'countryB', >+ branchphone => 'phoneB', >+ branchfax => 'faxB', >+ branchemail => 'emailB', >+ branchurl => 'urlB', >+ branchip => 'ipB', >+ branchprinter => 'printerB', >+ branchnotes => 'noteB', >+ opac_info => 'opacB', >+}; >+ModBranch($b1); >+is( ModBranch($b2), undef, 'the field add is missing' ); >+ >+$b2->{add} = 1; >+ModBranch($b2); >+is( GetBranchesCount, $count + 2, "two branches added" ); >+ >+#Test DelBranch >+ >+is( DelBranch( $b2->{branchcode} ), 1, "One row affected" ); >+is( GetBranchesCount, $count + 1, "branch BRB deleted" ); >+ >+#Test GetBranchName >+is( GetBranchName( $b1->{branchcode} ), >+ $b1->{branchname}, "GetBranchName returns the right name" ); >+ >+#Test GetBranchDetail >+my $branchdetail = GetBranchDetail( $b1->{branchcode} ); >+$branchdetail->{add} = 1; >+$b1->{issuing} = undef; # Not used in DB >+is_deeply( $branchdetail, $b1, 'branchdetail is right' ); >+ >+#Test Getbranches >+my $branches = GetBranches; >+is( scalar( keys %$branches ), >+ GetBranchesCount, "GetBranches returns the right number of branches" ); >+ >+#Test ModBranch >+ >+$b1 = { >+ branchcode => 'BRA', >+ branchname => 'BranchA modified', >+ branchaddress1 => 'adr1A modified', >+ branchaddress2 => 'adr2A modified', >+ branchaddress3 => 'adr3A modified', >+ branchzip => 'zipA modified', >+ branchcity => 'cityA modified', >+ branchstate => 'stateA modified', >+ branchcountry => 'countryA modified', >+ branchphone => 'phoneA modified', >+ branchfax => 'faxA modified', >+ branchemail => 'emailA modified', >+ branchurl => 'urlA modified', >+ branchip => 'ipA modified', >+ branchprinter => 'printerA modified', >+ branchnotes => 'notesA modified', >+ opac_info => 'opacA modified' >+}; >+ >+ModBranch($b1); >+is( GetBranchesCount, $count + 1, >+ "A branch has been modified, no new branch added" ); >+$branchdetail = GetBranchDetail( $b1->{branchcode} ); >+$b1->{issuing} = undef; >+is_deeply( $branchdetail, $b1 ); >+ >+#Test categories >+my $categories = GetBranchCategories; >+my $count_cat = scalar( keys $categories ); >+ >+my $cat1 = { >+ add => 1, >+ categorycode => 'CAT1', >+ categoryname => 'catname1', >+ codedescription => 'catdesc1', >+ categorytype => 'cattype1', >+ show_in_pulldown => 1 >+}; >+my $cat2 = { >+ add => 1, >+ categorycode => 'CAT2', >+ categoryname => 'catname2', >+ categorytype => 'catype2', >+ codedescription => 'catdesc2', >+ show_in_pulldown => 1 >+}; >+ >+ModBranchCategoryInfo($cat1); >+ModBranchCategoryInfo($cat2); >+ >+$categories = GetBranchCategories; >+is( scalar( keys $categories ), $count_cat + 2, "Two categories added" ); >+ >+#test GetBranchCategory >+my $cat1detail = GetBranchCategory( $cat1->{categorycode} ); >+delete $cat1->{add}; >+is_deeply( $cat1detail, $cat1, 'CAT1 details are right' ); >+ >+#Test DelBranchCategory >+my $del = DelBranchCategory( $cat2->{categorycode} ); >+is( $del, 1, 'One row affected' ); >+$categories = GetBranchCategories; >+is( scalar( keys $categories ), $count_cat + 1, "Category CAT2 deleted" ); >+ >+my $cat2detail = GetBranchCategory( $cat2->{categorycode} ); >+is( $cat2detail, undef, 'CAT2 doesnt exist' ); >+ >+#Test CheckBranchCategoryCode >+my $check1 = CheckBranchCategorycode( $cat1->{categorycode} ); >+my $check2 = CheckBranchCategorycode( $cat2->{categorycode} ); >+like( $check1, '/^\d+$/', "CheckBranchCategorycode returns a number" ); >+ >+$b2->{CAT1} = 1; >+ModBranch($b2); >+is( GetBranchesCount, $count + 2, 'BRB added' ); >+is( >+ CheckBranchCategorycode( $cat1->{categorycode} ), >+ $check1 + 1, >+ 'BRB added to CAT1' >+); >+ >+#Test GetBranchInfo >+my $b1info = GetBranchInfo( $b1->{branchcode} ); >+$b1->{categories} = []; >+is_deeply( @$b1info[0], $b1, 'BRA has no categories' ); >+ >+my $b2info = GetBranchInfo( $b2->{branchcode} ); >+my @cat = ( $cat1->{categorycode} ); >+delete $b2->{add}; >+delete $b2->{CAT1}; >+$b2->{issuing} = undef; >+$b2->{categories} = \@cat; >+is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1' ); >+ >+ModBranchCategoryInfo($cat2); >+$b2 = { >+ branchcode => 'BRB', >+ branchname => 'BranchB', >+ branchaddress1 => 'adr1B', >+ branchaddress2 => 'adr2B', >+ branchaddress3 => 'adr3B', >+ branchzip => 'zipB', >+ branchcity => 'cityB', >+ branchstate => 'stateB', >+ branchcountry => 'countryB', >+ branchphone => 'phoneB', >+ branchfax => 'faxB', >+ branchemail => 'emailB', >+ branchurl => 'urlB', >+ branchip => 'ipB', >+ branchprinter => 'printerB', >+ branchnotes => 'noteB', >+ opac_info => 'opacB', >+ CAT1 => 1, >+ CAT2 => 1 >+}; >+ModBranch($b2); >+$b2info = GetBranchInfo( $b2->{branchcode} ); >+is( >+ CheckBranchCategorycode( $cat2->{categorycode} ), >+ $check2 + 1, >+ 'BRB added to CAT2' >+); >+push( @cat, $cat2->{categorycode} ); >+delete $b2->{CAT1}; >+delete $b2->{CAT2}; >+$b2->{issuing} = undef; >+$b2->{categories} = \@cat; >+is_deeply( @$b2info[0], $b2, 'BRB has the category CAT1 and CAT2' ); >+ >+#Test GetBranchesInCategory >+my $brCat1 = GetBranchesInCategory( $cat1->{categorycode} ); >+my @b = ( $b2->{branchcode} ); >+is_deeply( $brCat1, \@b, 'CAT1 has branch BRB' ); >+ >+my $b3 = { >+ add => 1, >+ branchcode => 'BRC', >+ branchname => 'BranchC', >+ branchaddress1 => 'adr1C', >+ branchaddress2 => 'adr2C', >+ branchaddress3 => 'adr3C', >+ branchzip => 'zipC', >+ branchcity => 'cityC', >+ branchstate => 'stateC', >+ branchcountry => 'countryC', >+ branchphone => 'phoneC', >+ branchfax => 'faxC', >+ branchemail => 'emailC', >+ branchurl => 'urlC', >+ branchip => 'ipC', >+ branchprinter => 'printerC', >+ branchnotes => 'noteC', >+ opac_info => 'opacC', >+ CAT1 => 1, >+ CAT2 => 1 >+}; >+ModBranch($b3); >+$brCat1 = GetBranchesInCategory( $cat1->{categorycode} ); >+push( @b, $b3->{branchcode} ); >+is_deeply( $brCat1, \@b, 'CAT1 has branch BRB and BRC' ); >+is( >+ CheckBranchCategorycode( $cat1->{categorycode} ), >+ $check1 + 2, >+ 'BRC has been added to CAT1' >+); >+ >+#Test CheckCategoryUnique >+is( CheckCategoryUnique('CAT2'), 0, 'CAT2 exists' ); >+is( CheckCategoryUnique('CAT_NO_EXISTS'), 1, 'CAT_NO_EXISTS doesnt exist' ); >+ >+#Test GetCategoryTypes >+my @Types = GetCategoryTypes; >+is_deeply( >+ \@Types, >+ [ 'searchdomain', 'properties' ], >+ "the type is properties and searchdomain" >+); >+ >+#TODO later: test mybranchine and onlymine >+# Actually we cannot mock C4::Context->userenv in unit tests >+ >+#Test GetBranchesLoop >+my $loop = GetBranchesLoop; >+is( scalar(@$loop), GetBranchesCount, 'There is the right number of branches' ); >+ >+# End transaction >+$dbh->rollback; >+ >-- >1.7.9.5
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 10508
:
19338
|
19546
|
19687
|
19688
|
19771
|
19780
|
19781
|
19853
|
19864
|
19900
|
20029
|
20626
|
20638
|
20639
|
20663
|
20664