From ce0a362fd03c0bfae763a445fcb151ef9ea2b9d0 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 1 Jun 2024 02:48:21 -0400 Subject: [PATCH] group_hosts_by: Add tests --- Makefile.am | 1 + t/group_hosts_by.pl | 95 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 t/group_hosts_by.pl diff --git a/Makefile.am b/Makefile.am index 459931e..5b61f08 100644 --- a/Makefile.am +++ b/Makefile.am @@ -65,6 +65,7 @@ handwritten_tests = \ t/builtinfw_query.pl \ t/get_ip_from_if.pl \ t/geturl_connectivity.pl \ + t/group_hosts_by.pl \ t/interval_expired.pl \ t/is-and-extract-ipv4.pl \ t/is-and-extract-ipv6.pl \ diff --git a/t/group_hosts_by.pl b/t/group_hosts_by.pl new file mode 100644 index 0000000..cc2cc09 --- /dev/null +++ b/t/group_hosts_by.pl @@ -0,0 +1,95 @@ +use Test::More; +SKIP: { eval { require Test::Warnings; } or skip($@, 1); } +eval { require 'ddclient'; } or BAIL_OUT($@); +eval { require Data::Dumper; } or skip($@, 1); +Data::Dumper->import(); + +my $h1 = 'h1'; +my $h2 = 'h2'; +my $h3 = 'h3'; + +$ddclient::config{$h1} = { + common => 'common', + h1h2 => 'h1 and h2', + unique => 'h1', + falsy => 0, + maybeunset => 'unique', +}; +$ddclient::config{$h2} = { + common => 'common', + h1h2 => 'h1 and h2', + unique => 'h2', + falsy => '', + maybeunset => undef, # should not be grouped with unset +}; +$ddclient::config{$h3} = { + common => 'common', + h1h2 => 'unique', + unique => 'h3', + falsy => undef, + # maybeunset is intentionally not set +}; + +my @test_cases = ( + { + desc => 'empty attribute set yields single group with all hosts', + groupby => [qw()], + want => [[$h1, $h2, $h3]], + }, + { + desc => 'common attribute yields single group with all hosts', + groupby => [qw(common)], + want => [[$h1, $h2, $h3]], + }, + { + desc => 'subset share a value', + groupby => [qw(h1h2)], + want => [[$h1, $h2], [$h3]], + }, + { + desc => 'all unique', + groupby => [qw(unique)], + want => [[$h1], [$h2], [$h3]], + }, + { + desc => 'combination', + groupby => [qw(common h1h2)], + want => [[$h1, $h2], [$h3]], + }, + { + desc => 'falsy values', + groupby => [qw(falsy)], + want => [[$h1], [$h2], [$h3]], + todo => 'support for undef not yet added', + }, + { + desc => 'set, unset, undef', + groupby => [qw(maybeunset)], + want => [[$h1], [$h2], [$h3]], + todo => 'support for unset and undef not yet added', + }, + { + desc => 'missing attribute', + groupby => [qw(thisdoesnotexist)], + want => [[$h1, $h2, $h3]], + }, +); + +for my $tc (@test_cases) { + my %got = ddclient::group_hosts_by([$h1, $h2, $h3], $tc->{groupby}); + # %got is used as a set of sets. Sort everything to make comparison easier. + my @got = sort({ + for (my $i = 0; $i < @$a && $i < @$b; ++$i) { + my $x = $a->[$i] cmp $b->[$i]; + return $x if $x != 0; + } + return @$a <=> @$b; + } map({ [sort(@$_)]; } values(%got))); + TODO: { + local $TODO = $tc->{todo}; + is_deeply(\@got, $tc->{want}, $tc->{desc}) + or diag(Data::Dumper->Dump([\@got, $tc->{want}], [qw(got want)])); + } +} + +done_testing();