| Akron | a1fbdeb | 2016-12-12 02:06:08 +0100 | [diff] [blame] | 1 | use Test::More; |
| 2 | use Test::Krawfish; |
| 3 | use strict; |
| 4 | use warnings; |
| 5 | |
| 6 | use_ok('Krawfish::Index'); |
| Akron | c1ed58c | 2017-08-04 17:26:30 +0200 | [diff] [blame] | 7 | use_ok('Krawfish::Koral'); |
| Akron | a1fbdeb | 2016-12-12 02:06:08 +0100 | [diff] [blame] | 8 | |
| 9 | my $index = Krawfish::Index->new; |
| 10 | ok_index($index, { |
| Akron | c1ed58c | 2017-08-04 17:26:30 +0200 | [diff] [blame] | 11 | id => 'doc-1', |
| Akron | a1fbdeb | 2016-12-12 02:06:08 +0100 | [diff] [blame] | 12 | license => 'free', |
| 13 | corpus => 'corpus-2' |
| Akron | c001d36 | 2016-12-12 19:07:52 +0100 | [diff] [blame] | 14 | } => [qw/aa bb aa bb/], 'Add new document'); |
| Akron | a1fbdeb | 2016-12-12 02:06:08 +0100 | [diff] [blame] | 15 | |
| Akron | c1ed58c | 2017-08-04 17:26:30 +0200 | [diff] [blame] | 16 | my $koral = Krawfish::Koral->new; |
| 17 | my $qb = $koral->query_builder; |
| 18 | my $mb = $koral->meta_builder; |
| 19 | |
| 20 | $koral->query($qb->bool_or('aa', 'bb')); |
| 21 | |
| 22 | $koral->meta( |
| 23 | $mb->snippet |
| 24 | ); |
| 25 | |
| 26 | is($koral->to_string, |
| 27 | "meta=[snippet],query=[aa|bb]", |
| 28 | 'Stringification'); |
| 29 | |
| 30 | ok(my $koral_query = $koral->to_query, 'Normalization'); |
| 31 | |
| 32 | # This is a query that is fine to be send to nodes |
| 33 | is($koral_query->to_string, |
| 34 | "snippet(?:fields('id':sort(field='id'<;sortFilter:filter(aa|bb,[1]))))", |
| 35 | 'Stringification'); |
| 36 | |
| 37 | # This is a query that is fine to be send to segments: |
| 38 | ok($koral_query = $koral_query->identify($index->dict), 'Identify'); |
| 39 | |
| 40 | # This is a query that is fine to be send to nodes |
| 41 | is($koral_query->to_string, |
| 42 | "snippet(?:fields(#4:sort(field=#4<;sortFilter:filter(#7|#8,[1]))))", |
| 43 | 'Stringification'); |
| 44 | |
| 45 | TODO: { |
| 46 | local $TODO = 'Test further - with matches' |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | |
| 51 | done_testing; |
| 52 | __END__ |
| Akron | a1fbdeb | 2016-12-12 02:06:08 +0100 | [diff] [blame] | 53 | |
| 54 | is($query->to_string, 'aa|bb', 'Stringification'); |
| 55 | |
| Akron | c4bf5fb | 2017-07-18 02:20:40 +0200 | [diff] [blame] | 56 | my $prepare = $query->normalize->finalize->optimize($index); |
| Akron | a1fbdeb | 2016-12-12 02:06:08 +0100 | [diff] [blame] | 57 | |
| 58 | is($prepare->to_string, "or('aa','bb')", 'Stringification'); |
| 59 | |
| 60 | # Get facets object |
| Akron | c8e9ad1 | 2017-07-28 15:17:14 +0200 | [diff] [blame] | 61 | ok(my $snippet = Krawfish::Result::Segment::Enrich::Snippet->new( |
| Akron | c001d36 | 2016-12-12 19:07:52 +0100 | [diff] [blame] | 62 | query => $prepare, |
| 63 | index => $index |
| Akron | a1fbdeb | 2016-12-12 02:06:08 +0100 | [diff] [blame] | 64 | ), 'Create count object'); |
| 65 | |
| 66 | ok($snippet->next, 'Next match'); |
| Akron | 7c62cc4 | 2016-12-28 19:24:35 +0100 | [diff] [blame] | 67 | is($snippet->current_match->to_string, "[0:0-1|snippet='aa bb aa bb']", 'Current match'); |
| Akron | a1fbdeb | 2016-12-12 02:06:08 +0100 | [diff] [blame] | 68 | ok($snippet->next, 'Next match'); |
| Akron | 7c62cc4 | 2016-12-28 19:24:35 +0100 | [diff] [blame] | 69 | is($snippet->current_match->to_string, "[0:1-2|snippet='aa bb aa bb']", 'Current match'); |
| Akron | a1fbdeb | 2016-12-12 02:06:08 +0100 | [diff] [blame] | 70 | ok($snippet->next, 'Next match'); |
| Akron | 7c62cc4 | 2016-12-28 19:24:35 +0100 | [diff] [blame] | 71 | is($snippet->current_match->to_string, "[0:2-3|snippet='aa bb aa bb']", 'Current match'); |
| Akron | a1fbdeb | 2016-12-12 02:06:08 +0100 | [diff] [blame] | 72 | ok($snippet->next, 'Next match'); |
| Akron | 7c62cc4 | 2016-12-28 19:24:35 +0100 | [diff] [blame] | 73 | is($snippet->current_match->to_string, "[0:3-4|snippet='aa bb aa bb']", 'Current match'); |
| Akron | a1fbdeb | 2016-12-12 02:06:08 +0100 | [diff] [blame] | 74 | ok(!$snippet->next, 'No more match'); |
| 75 | |
| Akron | a1fbdeb | 2016-12-12 02:06:08 +0100 | [diff] [blame] | 76 | |
| 77 | done_testing; |
| 78 | |
| 79 | __END__ |
| 80 | |
| 81 | |