#!/usr/bin/perl -w  

use Test::More tests => 14;

use TestLib;
use EPrints;
use Test::MockObject;
use strict;








my $mocksth;
my $mockdb;
my $mockdbh;
my $mocksession;
my $mockrepository;


# testing get_index_ids

$mocksth = Test::MockObject->new();
$mocksth->set_true( 'finish' );

$mockdb = Test::MockObject->new();
$mockdb->set_true( 'execute' );
$mockdb->set_always( 'prepare', $mocksth );
#ok( $mockdb->somemethod() );

$mocksth->set_series( 'fetchrow_array', ('100:200:300'), () );
ok( eq_set( EPrints::Database::get_index_ids( $mockdb, 'aaa', 'bbb' ), [100,200,300] ), 'Simple case' );

$mocksth->set_series( 'fetchrow_array', (join( ":",1..20000), () ) );
ok( eq_set( EPrints::Database::get_index_ids( $mockdb, 'aaa', 'bbb' ), [1..20000] ), '20000 items in one row' );

$mocksth->set_series( 'fetchrow_array', ('1:2:3:4:5:6:100:200:4:5'), () );
ok( eq_set( EPrints::Database::get_index_ids( $mockdb, 'aaa', 'bbb' ), [1,2,3,4,5,6,100,200] ), 'Duplicates' );

$mocksth->set_series( 'fetchrow_array', ('1'), () );
ok( eq_set( EPrints::Database::get_index_ids( $mockdb, 'aaa', 'bbb' ), [1] ), 'Single item' );

$mocksth->set_series( 'fetchrow_array', () );
ok( eq_set( EPrints::Database::get_index_ids( $mockdb, 'aaa', 'bbb' ), [] ), 'No items' );

$mocksth->set_series( 'fetchrow_array', ('1:2:3'),('4:5:6'),('7:8:9'),()) ;
ok( eq_set( EPrints::Database::get_index_ids( $mockdb, 'aaa', 'bbb' ), [1,2,3,4,5,6,7,8,9] ), 'Multiple rows' );

####################

my @args;

# test SQL regexp callback: sub do

$mockdb = Test::MockObject->new();
$mockdbh = Test::MockObject->new();
$mocksession = Test::MockObject->new();
$mockrepository = Test::MockObject->new();
$mockdb->{dbh} = $mockdbh;
$mockdb->{session} = $mocksession;
$mocksession->set_always( 'get_repository',$mockrepository );
$mockrepository->set_always( 'can_call', 0 );
$mockrepository->set_always( 'get_conf', undef );
$mockdbh->set_true( 'do' );
ok( EPrints::Database::do( $mockdb, "some_sql" ), "basic call to sub do" );
@args = $mockdbh->call_args( 0 );
ok( eq_array( \@args, [$mockdbh,'some_sql'] ), "passed SQL to database OK" );

$mockdb = Test::MockObject->new();
$mockdbh = Test::MockObject->new();
$mocksession = Test::MockObject->new();
$mockrepository = Test::MockObject->new();
$mockdb->{dbh} = $mockdbh;
$mockdb->{session} = $mocksession;
$mocksession->set_always( 'get_repository',$mockrepository );
$mockrepository->set_always( 'can_call', 0 );
$mockrepository->set_always( 'get_conf', sub { my( $sql ) = @_; $sql="\U$sql"; return $sql;} );
$mockdbh->set_true( 'do' );
ok( EPrints::Database::do( $mockdb, "some_sql" ), "(with callback) basic call to sub do" );
@args = $mockdbh->call_args( 0 );
ok( eq_array( \@args, [$mockdbh,'SOME_SQL'] ), "(with callback) passed SQL to database OK" );


# test SQL regexp callback: sub prepare
$mockdb = Test::MockObject->new();
$mockdbh = Test::MockObject->new();
$mocksth = Test::MockObject->new();
$mocksession = Test::MockObject->new();
$mockrepository = Test::MockObject->new();
$mockrepository->set_always( 'can_call', 0 );
$mockdb->{dbh} = $mockdbh;
$mockdb->{session} = $mocksession;
$mocksession->set_always( 'get_repository',$mockrepository );
$mockrepository->set_always( 'get_conf', undef );
$mockdbh->set_always( 'prepare', $mocksth );
is( EPrints::Database::prepare( $mockdb, "some_sql" ), $mocksth, "basic call to sub prepare" );
@args = $mockdbh->call_args( 0 );
ok( eq_array( \@args, [$mockdbh,'some_sql'] ), "passed SQL to database OK" );


$mockdb = Test::MockObject->new();
$mockdbh = Test::MockObject->new();
$mocksth = Test::MockObject->new();
$mocksession = Test::MockObject->new();
$mockrepository = Test::MockObject->new();
$mockrepository->set_always( 'can_call', 0 );
$mockdb->{dbh} = $mockdbh;
$mockdb->{session} = $mocksession;
$mocksession->set_always( 'get_repository',$mockrepository );
$mockrepository->set_always( 'get_conf', sub { my( $sql ) = @_; $sql="\U$sql"; return $sql;} );
$mockdbh->set_always( 'prepare', $mocksth );
is( EPrints::Database::prepare( $mockdb, "some_sql" ), $mocksth, "(with callback) basic call to sub prepare" );
@args = $mockdbh->call_args( 0 );
ok( eq_array( \@args, [$mockdbh,'SOME_SQL'] ), "(with callback) passed SQL to database OK" );


=head1 COPYRIGHT

=for COPYRIGHT BEGIN

Copyright 2022 University of Southampton.
EPrints 3.4 is supplied by EPrints Services.

http://www.eprints.org/eprints-3.4/

=for COPYRIGHT END

=for LICENSE BEGIN

This file is part of EPrints 3.4 L<http://www.eprints.org/>.

EPrints 3.4 and this file are released under the terms of the
GNU Lesser General Public License version 3 as published by
the Free Software Foundation unless otherwise stated.

EPrints 3.4 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with EPrints 3.4.
If not, see L<http://www.gnu.org/licenses/>.

=for LICENSE END

