Skip to content

Commit

Permalink
Issue #7: migrate a test to using Test2::V0
Browse files Browse the repository at this point in the history
and introducing subtests
  • Loading branch information
bschmalhofer committed Mar 17, 2022
1 parent c35ddbb commit 480485d
Showing 1 changed file with 46 additions and 77 deletions.
123 changes: 46 additions & 77 deletions scripts/test/FAQ/RelatedArticle.t
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ use warnings;
# core modules

# CPAN modules
use Test2::V0;

# OTOBO modules
use Kernel::System::UnitTest::RegisterDriver; # Set up $Self and $Kernel::OM

our $Self;
use Kernel::System::UnitTest::RegisterDriver; # Set up $Kernel::OM

$Kernel::OM->ObjectParamAdd(
'Kernel::System::UnitTest::Helper' => {
Expand Down Expand Up @@ -174,10 +173,7 @@ for my $FAQItem (@FAQItems) {
%{$FAQItem},
);

$Self->True(
$FAQItemID,
"FAQAdd() successful for test - FAQItemID $FAQItemID",
);
ok( $FAQItemID, "FAQAdd() successful for test - FAQItemID $FAQItemID" );

push @FAQItemIDs, $FAQItemID;
}
Expand Down Expand Up @@ -431,41 +427,30 @@ for my $Test (@Tests) {
# check FAQKeywordArticleList attribute
if ( !$Test->{FAQKeywordArticleList} || ref $Test->{FAQKeywordArticleList} ne 'HASH' ) {

$Self->True(
0,
"Test $TestCount: No FAQKeywordArticleList found for this test.",
);
fail("Test $TestCount: No FAQKeywordArticleList found for this test.");

next TEST;
}

# print test case description
if ( $Test->{Description} ) {
$Self->True(
1,
"Test $TestCount: $Test->{Description}",
);
}
subtest "Test $TestCount: $Test->{Description}" => sub {

my %FAQKeywordArticleList = $FAQObject->FAQKeywordArticleList(
%{ $Test->{FAQKeywordArticleList} },
);

if ( $Test->{Fails} ) {
$Self->False(
%FAQKeywordArticleList ? 1 : 0,
"Test $TestCount: FAQKeywordArticleList() - should fail.",
my %FAQKeywordArticleList = $FAQObject->FAQKeywordArticleList(
$Test->{FAQKeywordArticleList}->%*,
);
}
else {

for my $Keyword ( sort keys %{ $Test->{ReferenceData} } ) {
if ( $Test->{Fails} ) {
ok( !%FAQKeywordArticleList, "Test $TestCount: FAQKeywordArticleList() - should fail." );
}
else {

$Self->IsDeeply(
$FAQKeywordArticleList{$Keyword} || [],
$Test->{ReferenceData}->{$Keyword},
"Test $TestCount: FAQKeywordArticleList() - $Keyword - test the result",
);
for my $Keyword ( sort keys %{ $Test->{ReferenceData} } ) {

is(
$FAQKeywordArticleList{$Keyword} || [],
$Test->{ReferenceData}->{$Keyword},
"Test $TestCount: FAQKeywordArticleList() - $Keyword - test the result",
);
}
}
}
}
Expand Down Expand Up @@ -504,14 +489,11 @@ my $LastFAQKeywordArticleListCache = $Kernel::OM->Get('Kernel::System::Cache')->
Key => $CacheKey,
);

$Self->True(
1,
"Test $TestCount: Test the cache for last function call",
);
diag("Test $TestCount: Test the cache for last function call");

for my $Keyword ( sort keys %{ $Tests[-1]->{ReferenceData} } ) {

$Self->IsDeeply(
is(
$LastFAQKeywordArticleListCache->{$Keyword} || [],
$Tests[-1]->{ReferenceData}->{$Keyword},
"Test $TestCount: Cache - FAQKeywordArticleList() - $Keyword - test the result",
Expand Down Expand Up @@ -803,10 +785,7 @@ for my $Test (@Tests) {
)
{

$Self->True(
0,
"Test $TestCount: No RelatedAgentArticleList or RelatedCustomerArticleList found for this test.",
);
fail("Test $TestCount: No RelatedAgentArticleList or RelatedCustomerArticleList found for this test.");

next TEST;
}
Expand All @@ -820,47 +799,37 @@ for my $Test (@Tests) {
$RelatedArticleFunction = 'RelatedAgentArticleList';
}

# print test case description
if ( $Test->{Description} ) {
$Self->True(
1,
"Test $TestCount: $Test->{Description}",
);
}
subtest "Test $TestCount: $Test->{Description}" => sub {
my @RelatedArticleList;

my @RelatedArticleList;

if ( $Test->{RelatedCustomerArticleList} ) {

@RelatedArticleList = $FAQObject->$RelatedArticleFunction(
%{ $Test->{RelatedCustomerArticleList} },
);
}
else {
@RelatedArticleList = $FAQObject->$RelatedArticleFunction(
%{ $Test->{RelatedAgentArticleList} },
);
}
if ( $Test->{RelatedCustomerArticleList} ) {
@RelatedArticleList = $FAQObject->$RelatedArticleFunction(
%{ $Test->{RelatedCustomerArticleList} },
);
}
else {
@RelatedArticleList = $FAQObject->$RelatedArticleFunction(
%{ $Test->{RelatedAgentArticleList} },
);
}

if ( $Test->{Fails} ) {
$Self->False(
@RelatedArticleList ? 1 : 0,
"Test $TestCount: $RelatedArticleFunction() - should fail.",
);
}
else {
if ( $Test->{Fails} ) {
ok( !@RelatedArticleList, "Test $TestCount: $RelatedArticleFunction() - should fail." );
}
else {

my @RelatedFAQArticleIDs = map { $_->{ItemID} } @RelatedArticleList;
my @RelatedFAQArticleIDs = map { $_->{ItemID} } @RelatedArticleList;

$Self->IsDeeply(
\@RelatedFAQArticleIDs,
$Test->{ReferenceData},
"Test $TestCount: $RelatedArticleFunction() - test the result",
);
is(
\@RelatedFAQArticleIDs,
$Test->{ReferenceData},
"Test $TestCount: $RelatedArticleFunction() - test the result",
);
}
}
}
continue {
$TestCount++;
}

$Self->DoneTesting();
done_testing();

0 comments on commit 480485d

Please sign in to comment.