From 5de06b87e29f12250a35df3239372a7ed0754811 Mon Sep 17 00:00:00 2001 From: Kenza Zaki Date: Fri, 26 Jul 2013 09:55:43 +0200 Subject: [PATCH] Bug 10644: add unit tests for ClassSource.pm The tests are wrapped in a database transaction. To test: prove t/db_dependent/ClassSource.t t/db_dependent/ClassSource.t .. 1/24 prepare_cached(SELECT * FROM `class_sort_rules` WHERE `class_sort_rule` = ?) statement handle DBI::st=HASH(0x92dbedc) still Active at C4/ClassSource.pm line 249 prepare_cached(SELECT * FROM `class_sources` WHERE cn_source = ?) statement handle DBI::st=HASH(0x926876c) still Active at C4/ClassSource.pm line 137 attempting to use non-existent class sorting routine routine1 prepare_cached(SELECT * FROM `class_sort_rules` WHERE `class_sort_rule` = ?) statement handle DBI::st=HASH(0x92dbedc) still Active at C4/ClassSource.pm line 249 prepare_cached(SELECT * FROM `class_sources` WHERE cn_source = ?) statement handle DBI::st=HASH(0x926876c) still Active at C4/ClassSource.pm line 137 t/db_dependent/ClassSource.t .. ok All tests successful. Files=1, Tests=24, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.12 cusr 0.01 csys = 0.15 CPU) Result: PASS Signed-off-by: Srdjan Signed-off-by: Katrin Fischer Signed-off-by: Galen Charlton --- t/db_dependent/ClassSource.t | 197 +++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 t/db_dependent/ClassSource.t diff --git a/t/db_dependent/ClassSource.t b/t/db_dependent/ClassSource.t new file mode 100644 index 0000000000..13090e4a05 --- /dev/null +++ b/t/db_dependent/ClassSource.t @@ -0,0 +1,197 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use Test::More tests => 24; + +use C4::Context; + +BEGIN { + use_ok('C4::ClassSource'); +} +can_ok( 'C4::ClassSource', + qw( AddClassSortRule + AddClassSource + GetClassSort + GetClassSortRule + GetClassSortRules + GetClassSource + GetClassSources + DelClassSortRule + DelClassSource + GetSourcesForSortRule + ModClassSortRule + ModClassSource) +); + +#Start transaction +my $dbh = C4::Context->dbh; +$dbh->{RaiseError} = 1; +$dbh->{AutoCommit} = 0; + +#Start tests +$dbh->do(q|DELETE FROM class_sources|); +$dbh->do(q|DELETE FROM class_sort_rules|); + +#Test AddClassSortRule +my $countSources = scalar( keys(GetClassSources) ); +my $countSources2 = scalar( keys(GetClassSortRules) ); +AddClassSortRule( 'sortrule1', 'description1', 'routine1' ); +AddClassSortRule( 'sortrule2', 'description2', 'routine2' ); +is( + scalar( keys(GetClassSortRules) ), + $countSources + 2, + "SortRule1 and SortRules2 have been added" +); + +#Test AddClassSource +AddClassSource( 'source1', 'Description_source1', 1, 'sortrule1' ); +AddClassSource( 'source2', 'Description_source2', 0, 'sortrule1' ); +is( + scalar( keys(GetClassSources) ), + $countSources2 + 2, + "Source1 and source2 have been added" +); + +#Test GetClassSortRule +is_deeply( + GetClassSortRule('sortrule1'), + { + class_sort_rule => 'sortrule1', + description => 'description1', + sort_routine => 'routine1' + }, + "GetClassSort gives sortrule1's informations" +); +is_deeply( GetClassSortRule(), undef, + "GetClassSort without params returns undef" ); +is_deeply( GetClassSortRule('doesnt_exist'), + undef, "GetClassSort with an id which doesn't exist returns undef" ); + +#Test GetClassSortRules +my $getsortrules = GetClassSortRules(); +is_deeply( + $getsortrules, + { + sortrule1 => { + class_sort_rule => 'sortrule1', + description => 'description1', + sort_routine => 'routine1' + }, + sortrule2 => { + class_sort_rule => 'sortrule2', + description => 'description2', + sort_routine => 'routine2' + } + }, + "GetClassSortRules returns the id off all SortRule and there informations" +); + +#Test GetClassSource +my $getsource1 = GetClassSource('source1'); +is_deeply( + $getsource1, + { + cn_source => 'source1', + description => 'Description_source1', + used => 1, + class_sort_rule => 'sortrule1' + }, + "GetClassSource gives source1's informations" +); +is_deeply( GetClassSource(), undef, + "GetClassSource without params returns undef" ); +is_deeply( GetClassSource('doesnt_exist'), + undef, "GetClassSource with an id which doesn't exist returns undef" ); + +#Test GetClassSources +my $getsources = GetClassSources(); +is_deeply( + $getsources, + { + source1 => { + cn_source => 'source1', + description => 'Description_source1', + used => 1, + class_sort_rule => 'sortrule1' + }, + source2 => { + cn_source => 'source2', + description => 'Description_source2', + used => 0, + class_sort_rule => 'sortrule1' + } + }, + "GetClassSources returns the id off all sources and there informations" +); + +#Test GetClassSort +my $getclassSort = GetClassSort( 'source1', 'sortrule1', 'item1' ) + ; #Note: Create a warning:" attempting to use non-existent class sorting routine $sort_routine" +is( $getclassSort, "SORTRULE1_ITEM1", +" the sort key corresponding to Source1 and sortrule1 and item1 is SORTRULE1_ITEM1" +); + +#Test GetSourcesForSorSortRule +my @sources = GetSourcesForSortRule('sortrule1'); +is_deeply( + \@sources, + [ 'source1', 'source2' ], + "Sortrule1 has source1 and source2" +); +@sources = GetSourcesForSortRule(); +is_deeply( \@sources, [], + "Without params GetSourcesForSortRule returns an empty array" ); +@sources = GetSourcesForSortRule('doesnt_exist'); +is_deeply( \@sources, [], + "With a wrong params GetSourcesForSortRule returns an empty array" ); + +#Test DelClassSortRule +#DelClassSortRule ('sortrule1'); +#is(scalar (keys (GetClassSortRules)),1,"SortRule1 has been deleted");#FIXME : impossible if some sources exist +DelClassSortRule('sortrule2'); +is( scalar( keys(GetClassSortRules) ), 1, "SortRule2 has been deleted" ); +DelClassSortRule(); +is( scalar( keys(GetClassSortRules) ), + 1, "Without params DelClassSortRule doesn't do anything" ); +DelClassSortRule('doesnt_exist'); +is( scalar( keys(GetClassSortRules) ), + 1, "Without wrong id, DelClassSortRule doesn't do anything" ); + +#Test DelClassSource +DelClassSource('source2'); +is( scalar( keys(GetClassSources) ), 1, "Source2 has been deleted" ); +DelClassSource(); +is( scalar( keys(GetClassSources) ), + 1, "Without params DelClassSource doesn't do anything" ); +DelClassSource('doesnt_exist'); +is( scalar( keys(GetClassSources) ), + 1, "Without wrong id, DelClassSource doesn't do anything" ); + +#Test ModClassSortRule +ModClassSortRule( 'sortrule1', 'description1_modified', 'routine1_modified' ); +is_deeply( + GetClassSortRule('sortrule1'), + { + class_sort_rule => 'sortrule1', + description => 'description1_modified', + sort_routine => 'routine1_modified' + }, + "Sortrule1 has been modified" +); + +#Test ModClassSource +ModClassSource( 'source1', 'Description_source1_modified', 0, 'sortrule1' ); +is_deeply( + GetClassSource('source1'), + { + cn_source => 'source1', + description => 'Description_source1_modified', + used => 0, + class_sort_rule => 'sortrule1' + }, + "Source1 has been modified" +); + +#End transaction +$dbh->rollback; -- 2.39.5