Bug 8300: Add mechanized unit test for batch import
[koha.git] / t / db_dependent / SQLHelper.t
1 #!/usr/bin/perl
2 #
3 # This Koha test module is a stub!
4 # Add more tests here!!!
5
6 use strict;
7 use warnings;
8 use YAML;
9
10 use C4::Debug;
11 use C4::SQLHelper qw(:all);
12
13 use Test::More tests => 20;
14
15 use_ok('C4::SQLHelper');
16
17 use C4::Category;
18 use C4::Branch;
19 my @categories=C4::Category->all;
20 my $insert;
21 ok(($insert=InsertInTable("branches",{branchcode=>"ZZZZ",branchname=>"Brancheinconnue",city=>" ",zipcode=>" "},1))==0,"AddBranch (Insert In Table with primary key defined)");
22 my $branches=C4::Branch::GetBranches;
23 my @branchcodes=keys %$branches;
24 my ($borrid, $borrtmp);
25 ok($borrid=InsertInTable("borrowers",{firstname=>"Jean",surname=>"Valjean",city=>" ",zipcode=>" ",email=>"email",categorycode=>$categories[0]->{categorycode}, branchcode=>$branchcodes[0]}),"Insert In Table");
26 $borrtmp=InsertInTable("borrowers",{firstname=>"Jean",surname=>"cocteau",city=>" ",zipcode=>" ",email=>"email",categorycode=>$categories[0]->{categorycode}, branchcode=>$branchcodes[0]});
27 ok(my $status=UpdateInTable("borrowers",{borrowernumber=>$borrid,firstname=>"Jean",surname=>"Valjean",city=>"Dampierre",zipcode=>" ",email=>"email", branchcode=>$branchcodes[1]}),"Update In Table");
28 my $borrowers=SearchInTable("borrowers");
29 ok(@$borrowers>0, "Search In Table All values");
30 $borrowers=SearchInTable("borrowers",{borrowernumber=>$borrid});
31 ok(@$borrowers==1, "Search In Table by primary key on table");
32 $borrowers=SearchInTable("borrowers",{firstname=>"Jean"});
33 ok(@$borrowers>0, "Search In Table hashref");
34 $borrowers=SearchInTable("borrowers",{firstname=>"Jean"},[{firstname=>1},{borrowernumber=>1}],undef, [qw(borrowernumber)]);
35 ok(($$borrowers[0]{borrowernumber} + 0) > ($$borrowers[1]{borrowernumber} + 0), "Search In Table Order");
36 $borrowers=SearchInTable("borrowers",{firstname=>"Jean"},[{surname=>0},{firstname=>1}], undef, [qw(firstname surname)]);
37 ok(uc($$borrowers[0]{surname}) lt uc($$borrowers[1]{surname}), "Search In Table Order");
38 $borrowers=SearchInTable("borrowers","Jean");
39 ok(@$borrowers>0, "Search In Table string");
40 #FIXME : When searching on All the fields of the table, seems to return Junk
41 eval{$borrowers=SearchInTable("borrowers","Jean Valjean",undef,undef,undef,[qw(firstname surname borrowernumber cardnumber)],"start_with")};
42 #eval{$borrowers=SearchInTable("borrowers","Jean Valjean",undef,undef,undef,undef,"start_with")};
43 # This would not be much efficient because of "numbers" special treatment : We return stuff if empty or '' as soon as search is NOT exact
44 # This behaviour is implemented because of branchcode and numbers can be null
45 $debug && warn Dump(@$borrowers);
46 ok(scalar(@$borrowers)==1 && !($@), "Search In Table does an implicit AND of all the words in strings");
47 $borrowers=SearchInTable("borrowers",["Valjean",{firstname=>"Jean"}]);
48 ok(@$borrowers>0, "Search In Table arrayref");
49 $borrowers=SearchInTable("borrowers",["Valjean",{firstname=>"Jean"}],undef,undef,[qw(borrowernumber)]);
50 ok(keys %{$$borrowers[0]} ==1, "Search In Table columns out limit");
51 $borrowers=SearchInTable("borrowers",["Valjean",{firstname=>"Jean"}],undef,undef,[qw(borrowernumber)],[qw(firstname surname title)]);
52 ok(@$borrowers>0, "Search In Table columns out limit to borrowernumber AND filter firstname surname title");
53 $borrowers=SearchInTable("borrowers",["Val",{firstname=>"Jean"}],undef,undef,[qw(borrowernumber)],[qw(surname)],"start_with");
54 ok(@$borrowers>0, "Search In Table columns filter surname  Val on a wide search found ");
55 $borrowers=eval{SearchInTable("borrowers",["Val",{member=>"Jean"}],undef,undef,[qw(borrowernumber)],[qw(firstname title)],"exact")};
56 ok(@$borrowers==0 && !($@), "Search In Table fails gracefully when no correct field passed in hash");
57 $borrowers=eval{SearchInTable("borrowers",["Jea"],undef,undef,undef,[qw(firstname surname borrowernumber)],"start_with")};
58 ok(@$borrowers>0 && !($@), "Search on simple value in firstname");
59
60 $status=DeleteInTable("borrowers",{borrowernumber=>$borrid});
61 ok($status>0 && !($@), "DeleteInTable OK");
62 $status=DeleteInTable("borrowers",{borrowernumber=>$borrtmp});
63 ok($status>0 && !($@), "DeleteInTable OK");
64 $status=DeleteInTable("branches", {branchcode => 'ZZZZ'});
65 ok($status>0 && !($@), "DeleteInTable (branch) OK");