blob: 36079f77e77ee45eddb27f90a1e9948f153e7194 [file] [log] [blame]
Akron78c49502017-07-27 16:00:36 +02001use Test::More;
2use strict;
3use warnings;
4
Akron5cf5fca2017-10-09 19:01:47 +02005use_ok('Krawfish::Koral::Compile::Builder');
6use_ok('Krawfish::Koral::Compile');
Akron78c49502017-07-27 16:00:36 +02007
Akron5cf5fca2017-10-09 19:01:47 +02008my $mb = Krawfish::Koral::Compile::Builder->new;
Akron78c49502017-07-27 16:00:36 +02009
Akron01e97e82017-08-03 15:12:25 +020010# Build aggregations
Akron78c49502017-07-27 16:00:36 +020011ok(my $meta = $mb->aggregate( $mb->a_frequencies ), 'Add aggregation');
12is($meta->to_string, 'aggr=[freq]', 'Stringification');
13
14ok($meta = $mb->aggregate(
Akrona3581a92017-08-17 17:45:37 +020015 $mb->a_fields('size', 'age'),
Akron78c49502017-07-27 16:00:36 +020016 $mb->a_frequencies,
17 $mb->a_length
18), 'Add aggregation');
Akrona3581a92017-08-17 17:45:37 +020019is($meta->to_string, "aggr=[fields:['size','age'],freq,length]", 'Stringification');
Akron78c49502017-07-27 16:00:36 +020020
Akroncfa3e012017-08-07 19:46:41 +020021ok($meta = $mb->enrich(
22 $mb->e_fields('author', 'title', 'id')
23), 'Create fields');
24is($meta->to_string, "enrich=[fields:['author','title','id']]", 'Stringification');
Akron78c49502017-07-27 16:00:36 +020025
Akron01e97e82017-08-03 15:12:25 +020026
27# Build sorting
Akron78c49502017-07-27 16:00:36 +020028ok($meta = $mb->sort_by($mb->s_field('author', 1), $mb->s_field('age')), 'Create fields');
29is($meta->to_string, "sort=[field='author'>,field='age'<]", 'Stringification');
30
Akronc1ed58c2017-08-04 17:26:30 +020031
Akron5cf5fca2017-10-09 19:01:47 +020032my $meta_koral = Krawfish::Koral::Compile->new(
Akron78c49502017-07-27 16:00:36 +020033 $mb->sort_by($mb->s_field('author', 1), $mb->s_field('age')),
Akroncfa3e012017-08-07 19:46:41 +020034 $mb->enrich($mb->e_fields('author', 'title', 'id'))
Akron78c49502017-07-27 16:00:36 +020035);
36
Akroncfa3e012017-08-07 19:46:41 +020037
Akron78c49502017-07-27 16:00:36 +020038is(
39 $meta_koral->to_string,
Akroncfa3e012017-08-07 19:46:41 +020040 "sort=[field='author'>,field='age'<],enrich=[fields:['author','title','id']]",
Akron78c49502017-07-27 16:00:36 +020041 'Stringification'
42);
43
44
45# Introduce redundant operations and new sorts
Akron5cf5fca2017-10-09 19:01:47 +020046$meta_koral = Krawfish::Koral::Compile->new(
Akron78c49502017-07-27 16:00:36 +020047 $mb->sort_by($mb->s_field('author', 1), $mb->s_field('age')),
Akroncfa3e012017-08-07 19:46:41 +020048 $mb->enrich($mb->e_fields('author', 'title', 'id')),
Akron78c49502017-07-27 16:00:36 +020049 $mb->sort_by($mb->s_field('length')),
Akroncfa3e012017-08-07 19:46:41 +020050 $mb->enrich($mb->e_fields('subTitle'))
Akron78c49502017-07-27 16:00:36 +020051);
52
53
54is(
55 $meta_koral->to_string,
Akroncfa3e012017-08-07 19:46:41 +020056 "sort=[field='author'>,field='age'<],enrich=[fields:['author','title','id']],sort=[field='length'<],enrich=[fields:['subTitle']]",
Akron78c49502017-07-27 16:00:36 +020057 'Stringification'
58);
59
60
61# This will introduce a sort filter and reorder and simplify the operations
62ok($meta_koral = $meta_koral->normalize, 'Normalize meta object');
63
64is(
65 $meta_koral->to_string,
Akron45d31922017-09-15 17:05:36 +020066 "sort=[field='author'>,field='age'<,field='length'<,field='id'<],enrich=[fields:['author','title','id','subTitle']]",
Akron78c49502017-07-27 16:00:36 +020067 'Stringification'
68);
69
70
71# Check normalization of aggregate functions
Akron5cf5fca2017-10-09 19:01:47 +020072$meta_koral = Krawfish::Koral::Compile->new(
Akron78c49502017-07-27 16:00:36 +020073 $mb->aggregate(
74 $mb->a_length,
Akrona3581a92017-08-17 17:45:37 +020075 $mb->a_fields('author', 'age'),
Akron78c49502017-07-27 16:00:36 +020076 $mb->a_frequencies,
Akrona3581a92017-08-17 17:45:37 +020077 $mb->a_fields('corpus', 'age')
Akron78c49502017-07-27 16:00:36 +020078 )
79);
80
81ok($meta_koral = $meta_koral->normalize, 'Normalization');
82
83is($meta_koral->to_string,
Akrona3581a92017-08-17 17:45:37 +020084 "aggr=[length,freq,fields:['author','age','corpus']]",
Akron78c49502017-07-27 16:00:36 +020085 'stringification');
86
87done_testing;
88
89__END__;