use 5.024;
use strict;
use warnings;
use Test2::Bundle::Extended;
use experimental 'signatures';
use Path::Tiny;
use AniDB::Datastore;
my $tempdir = Path::Tiny->tempdir;
my $db_file = $tempdir->child('test.db');
my $ds;
subtest 'construction' => sub {
try_ok {
$ds = AniDB::Datastore->new({database=>$db_file})
} 'builds';
isa_ok(
$ds->dbh,
['DBI::db'],
'gets a dbh',
);
};
my $test_file = $tempdir->child('some.video');
$test_file->spew_raw('data');
my $stat = $test_file->stat;
subtest 'reading non-existent records' => sub {
is(
$ds->path_info_for($test_file),
undef,
'path record',
);
is(
$ds->episode_info_for({hash=>1,size=>1}),
undef,
'episode record',
);
is(
$ds->anime_info_for({aid=>1}),
undef,
'anime record',
);
};
subtest 'insert data' => sub {
try_ok {
ok $ds->update_path_info(
$test_file => {
size => 1,
mtime => 1,
hash => 'abc',
},
);
} 'path info';
try_ok {
ok $ds->update_episode_info(
{
size => 1,
hash => 'abc',
},
{ random => 'info', aid => 5 },
);
} 'episode info';
try_ok {
ok $ds->update_anime_info(
{ aid => 5 },
{ more => 'random' },
);
} 'anime info';
};
subtest 'update data' => sub {
try_ok {
ok $ds->update_path_info(
$test_file => {
size => 3,
mtime => 5,
hash => 'abc',
},
);
} 'path info';
try_ok {
ok $ds->update_episode_info(
{
size => 3,
hash => 'abc',
},
{ random => 'new-info', aid => 5 },
);
} 'episode info';
try_ok {
ok $ds->update_anime_info(
{ aid => 5 },
{ more => 'new-random' },
);
} 'anime info';
};
subtest 'retrieve data' => sub {
is(
$ds->path_info_for($test_file),
my $path_info = {
name => $test_file->stringify,
size => 3,
mtime => 5,
hash => 'abc',
},
'path info',
);
is(
$ds->episode_info_for({size=>3,hash=>'abc'}),
my $episode_info ={ random => 'new-info', aid => 5 },
'episode info',
);
is(
$ds->anime_info_for({ aid => 5 }),
my $anime_info = { more => 'new-random' },
'anime info',
);
is(
$ds->full_info_for($test_file),
{
path => $path_info,
episode => $episode_info,
anime => $anime_info,
},
'full info',
);
};
subtest 'changed' => sub {
ok $ds->has_changed($test_file,$stat);
$ds->update_path_info(
$test_file => {
size => $stat->size,
mtime => $stat->mtime,
hash => 'abcd',
},
);
ok not $ds->has_changed($test_file,$stat);
};
subtest 'rename' => sub {
my $new_name = path('/tmp/another');
$ds->rename($test_file,$new_name);
is(
$ds->path_info_for($new_name),
{
name => $new_name->stringify,
size => $stat->size,
mtime => $stat->mtime,
hash => 'abcd',
},
'path info',
);
};
done_testing;