Ensuring the first characters and db population work with a test.

This commit is contained in:
sergiotarxz 2024-06-27 12:43:57 +02:00
parent 97b79f4dcc
commit c35f414aa2
4 changed files with 40 additions and 4 deletions

View File

@ -35,13 +35,16 @@ WHERE name = ?', {}, $option_populated
sub _populate_type( $self, $type ) {
my $basic_character_resultset =
JapaChar::Schema->Schema->resultset('BasicCharacter');
my @array_for_insertion;
for my $char ( @{ $self->_get_characters_of_type($type) } ) {
my $kana = $char->{kana};
my $romanji = $char->{roumaji};
next if $romanji =~ /pause/i;
$basic_character_resultset->new(
{ value => $kana, romanji => $romanji, type => $type, } )->insert;
push @array_for_insertion, { value => $kana, romanji => $romanji, type => $type };
}
$basic_character_resultset->populate([
@array_for_insertion
]);
}
sub _get_characters_of_type( $self, $type ) {

View File

@ -16,6 +16,10 @@ use Data::Dumper;
my $dbh;
sub reset_dbh {
undef $dbh;
}
sub connect {
if ( defined $dbh ) {
return $dbh;

View File

@ -29,4 +29,8 @@ sub Schema ($class) {
}
return $schema;
}
sub reset_schema {
undef $schema;
}
1;

View File

@ -5,7 +5,7 @@ use v5.38.2;
use strict;
use warnings;
use Test::Most tests => 3;
use Test::Most tests => 5;
use Test::MockModule;
use Path::Tiny;
@ -15,6 +15,7 @@ use lib dirname(dirname(__FILE__)).'/lib';
use JapaChar::DB;
use JapaChar::Random;
use JapaChar::Schema;
BEGIN {
use_ok 'JapaChar::Characters';
@ -37,5 +38,29 @@ BEGIN {
});
my $next_char = JapaChar::Characters->new->next_char;
ok defined($next_char), 'The next char is defined.';
is_deeply $next_review_char, $next_char, 'The next char is a review one.';
is_deeply $next_char, $next_review_char, 'The next char is a review one when all characters are learned.';
}
{
JapaChar::Schema::reset_schema();
JapaChar::DB::reset_dbh();
my $mock_db = Test::MockModule->new('JapaChar::DB');
my $tempdir = Path::Tiny->tempdir;
my $db_file = $tempdir->child('doesnt-exist.db');
$mock_db->mock(_db_path => sub {
return $db_file;
});
my $mock_random = Test::MockModule->new('JapaChar::Random');
$mock_random->mock(get => sub {
return 19;
});
my $mock_characters = Test::MockModule->new('JapaChar::Characters');
my $next_learning = undef;
$mock_characters->mock(_next_learning_char => sub {
$next_learning = $mock_characters->original('_next_learning_char')->(@_);
return $next_learning;
});
my $next_char = JapaChar::Characters->new->next_char;
ok defined($next_char), 'The next char is defined.';
is_deeply $next_char, $next_learning, 'The next char is a learning one when there is no progress.';
}