BEGIN {
sub
new {
my
(
$class
,
%args
) =
@_
;
return
bless
\
%args
,
$class
;
}
sub
selectall_hashref {
return
[
{
first
=>
'John'
,
last
=>
'Smith'
,
url
=>
'example.com/john_smith'
},
{
first
=>
'Jane'
,
last
=>
'Smith'
,
url
=>
'example.com/jane_smith'
},
];
}
sub
fetchrow_hashref {
return
{
first
=>
'John'
,
last
=>
'Smith'
,
url
=>
'example.com/john_smith'
};
}
}
my
$temp_dir
= tempdir(
CLEANUP
=> 1);
my
$obj
= Genealogy::Wills->new(
directory
=>
$temp_dir
);
ok(
$obj
,
'Object created successfully'
);
my
$invalid_dir_obj
= Genealogy::Wills->new(
directory
=>
'/invalid/directory'
);
ok(!
defined
(
$invalid_dir_obj
),
'Object creation fails with invalid directory'
);
eval
{ Genealogy::Wills->new(
'foo'
) };
like($@,
qr/Invalid arguments passed/
,
'Fails gracefully when required arguments are missing in `new`'
);
is(
$obj
->{
'directory'
},
$temp_dir
,
'Directory property set correctly'
);
my
@results
=
$obj
->search(
last
=>
'Smith'
);
is(
scalar
(
@results
), 2,
'Search returned correct number of results'
);
is(
$results
[0]->{
'first'
},
'John'
,
'First result matches expected value'
);
like(
$results
[0]->{
'url'
},
qr/^https:\/
\//,
'URL in results is correctly formatted'
);
dies_ok(
sub
{
$obj
->search() },
'Search with missing parameters dies'
);
my
$cloned_obj
=
$obj
->new(
logger
=>
sub
{
print
@_
});
ok(
$cloned_obj
,
'Successfully cloned an existing object'
);
is(
$cloned_obj
->{
'directory'
},
$obj
->{
'directory'
},
'Cloned object retains original directory'
);
my
$custom_obj
= Genealogy::Wills->new(
directory
=>
$temp_dir
,
cache_duration
=>
'2 days'
);
is(
$custom_obj
->{
'cache_duration'
},
'2 days'
,
'Custom cache_duration property is set correctly'
);
{
no
warnings
'redefine'
;
*Genealogy::Wills::wills::selectall_hashref
=
sub
{ [] };
my
@no_results
=
$obj
->search(
last
=>
'Nonexistent'
);
is(
scalar
(
@no_results
), 0,
'Search with non-existent name returns no results'
);
}
{
no
warnings
'redefine'
;
*Genealogy::Wills::wills::selectall_hashref
=
sub
{
return
[{
first
=>
'John'
,
last
=>
'Smith'
,
url
=>
'example.com/john_smith'
}];
};
my
@results
=
$obj
->search(
last
=>
'Smith'
);
like(
$results
[0]->{
'url'
},
qr/^https:\/
\//,
'URL formatting correctly prepends https://'
);
}