App-ElasticSearch-Utilities
view release on metacpan or search on metacpan
t/01-querystring.t view on Meta::CPAN
#!perl
#
use strict;
use warnings;
use App::ElasticSearch::Utilities::QueryString;
use CLI::Helpers qw(:output);
use Data::Dumper;
use File::Temp qw(tempfile);
use Test::More;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
# Test Data File;
my ($fh,$tempfile) = tempfile(SUFFIX => '.dat');
my @csv_data=();
while(<DATA>) {
chomp;
my @cols = split /\s+/, $_;
printf $fh "%s\n", join("\t", @cols);
push @csv_data, [ @cols ];
}
close($fh);
my ($csv,$csvfile) = tempfile( SUFFIX => '.csv' );
for(@csv_data) {
printf $csv "%s\n", join(',', @{ $_ });
}
close($csv);
END {
debug({color=>'red'},"[test_cleanup] Removing $tempfile");
unlink($tempfile);
debug({color=>'red'},"[test_cleanup] Removing $csvfile");
unlink($csvfile);
}
# Query String Parser Testing
my %tests = (
'00-barewords' => [
[qw(src_ip:1.2.3.4 and not dst:www.example.com)],
{
'bool' => {
'must' => [
{
'query_string' => {
'query' => 'src_ip:1.2.3.4 AND NOT dst:www.example.com'
}
}
]
}
},
],
'01-ip-cidr-expansion' => [
[qw(src_ip:10.0.0.0/8)],
{
'bool' => {
'must' => [
{
'range' => {
'src_ip' => { gte => '10.0.0.0', lte => '10.255.255.255' }
}
}
]
}
},
],
'02-underscored' => [
["_prefix_:useragent=Go "],
{
'bool' => {
'must' => [
{
'prefix' => {
'useragent' => 'Go '
}
}
]
}
},
],
'03-file-expansion-dat' => [
[sprintf "src_ip:%s[-1]", $tempfile],
{
'bool' => {
'must' => [
{
'terms' => {
'src_ip' => [
'1.2.3.4',
'1.2.3.5',
'1.2.3.6'
]
}
}
]
}
},
],
'04-file-expansion-csv' => [
[sprintf "src_ip:%s[-1]", $csvfile],
{
'bool' => {
'must' => [
{
'terms' => {
'src_ip' => [
'1.2.3.4',
'1.2.3.5',
'1.2.3.6'
]
}
}
]
( run in 0.681 second using v1.01-cache-2.11-cpan-39bf76dae61 )