| Akron | 349747d | 2016-12-05 11:05:53 +0100 | [diff] [blame] | 1 | use Test::More; |
| 2 | use Test::Krawfish; |
| 3 | use strict; |
| 4 | use warnings; |
| 5 | |
| 6 | use_ok('Krawfish::Koral::Corpus::Builder'); |
| 7 | use_ok('Krawfish::Index'); |
| 8 | |
| 9 | my $index = Krawfish::Index->new; |
| Akron | 373df82 | 2016-12-28 15:25:14 +0100 | [diff] [blame] | 10 | ok_index($index, { |
| 11 | id => 2, |
| 12 | author => 'Peter', |
| 13 | genre => 'novel', |
| 14 | age => 4 |
| 15 | } => [qw/aa bb/], 'Add complex document'); |
| 16 | ok_index($index, { |
| 17 | id => 3, |
| 18 | author => 'Peter', |
| 19 | genre => 'novel', |
| 20 | age => 3 |
| 21 | } => [qw/aa bb/], 'Add complex document'); |
| 22 | ok_index($index, { |
| 23 | id => 5, |
| 24 | author => 'Peter', |
| 25 | genre => 'newsletter', |
| 26 | age => 4 |
| 27 | } => [qw/aa bb/], 'Add complex document'); |
| 28 | ok_index($index, { |
| 29 | id => 6, |
| 30 | author => 'Michael', |
| 31 | genre => 'newsletter', |
| 32 | age => 7 |
| 33 | } => [qw/aa bb/], 'Add complex document'); |
| Akron | 349747d | 2016-12-05 11:05:53 +0100 | [diff] [blame] | 34 | |
| 35 | ok(my $cb = Krawfish::Koral::Corpus::Builder->new, 'Create CorpusBuilder'); |
| 36 | |
| 37 | ok(my $query = $cb->field_and( |
| 38 | $cb->string('author')->eq('Peter'), |
| 39 | $cb->string('age')->eq('4') |
| 40 | ), 'Create corpus query'); |
| 41 | |
| Akron | 0e782bc | 2017-05-14 14:04:41 +0200 | [diff] [blame] | 42 | is($query->to_string, 'age=4&author=Peter', 'Stringification'); |
| Akron | 373df82 | 2016-12-28 15:25:14 +0100 | [diff] [blame] | 43 | ok(!$query->is_negative, 'Check negativity'); |
| Akron | 349747d | 2016-12-05 11:05:53 +0100 | [diff] [blame] | 44 | |
| Akron | 2ea61aa | 2017-06-03 16:30:23 +0200 | [diff] [blame] | 45 | ok(my $plan = $query->root_normalize->optimize($index), 'Planning'); |
| Akron | 349747d | 2016-12-05 11:05:53 +0100 | [diff] [blame] | 46 | |
| Akron | 0e782bc | 2017-05-14 14:04:41 +0200 | [diff] [blame] | 47 | is($plan->to_string, "and('age:4','author:Peter')", 'Stringification'); |
| Akron | 349747d | 2016-12-05 11:05:53 +0100 | [diff] [blame] | 48 | |
| 49 | ok($plan->next, 'Init vc'); |
| 50 | is($plan->current->to_string, '[0]', 'First doc'); |
| 51 | ok($plan->next, 'More next'); |
| 52 | is($plan->current->to_string, '[2]', 'First doc'); |
| 53 | ok(!$plan->next, 'No more next'); |
| 54 | |
| Akron | 349747d | 2016-12-05 11:05:53 +0100 | [diff] [blame] | 55 | # Complex virtual corpus |
| 56 | ok($query = $cb->field_or( |
| 57 | $cb->field_and( |
| 58 | $cb->string('author')->eq('Peter'), |
| 59 | $cb->string('age')->eq(3) |
| 60 | ), |
| 61 | $cb->string('id')->eq(2) |
| 62 | ), 'Create corpus query'); |
| 63 | |
| Akron | 0e782bc | 2017-05-14 14:04:41 +0200 | [diff] [blame] | 64 | is($query->to_string, '(age=3&author=Peter)|id=2', 'Stringification'); |
| Akron | 373df82 | 2016-12-28 15:25:14 +0100 | [diff] [blame] | 65 | ok(!$query->is_negative, 'Check negativity'); |
| Akron | 349747d | 2016-12-05 11:05:53 +0100 | [diff] [blame] | 66 | |
| Akron | 2ea61aa | 2017-06-03 16:30:23 +0200 | [diff] [blame] | 67 | ok($plan = $query->root_normalize->optimize($index), 'Planning'); |
| Akron | 349747d | 2016-12-05 11:05:53 +0100 | [diff] [blame] | 68 | |
| Akron | 0e782bc | 2017-05-14 14:04:41 +0200 | [diff] [blame] | 69 | is($plan->to_string, "or(and('age:3','author:Peter'),'id:2')", 'Stringification'); |
| Akron | 349747d | 2016-12-05 11:05:53 +0100 | [diff] [blame] | 70 | |
| 71 | ok($plan->next, 'Init vc'); |
| 72 | is($plan->current->to_string, '[0]', 'First doc'); |
| 73 | ok($plan->next, 'More next'); |
| 74 | is($plan->current->to_string, '[1]', 'First doc'); |
| 75 | ok(!$plan->next, 'No more next'); |
| 76 | |
| Akron | 373df82 | 2016-12-28 15:25:14 +0100 | [diff] [blame] | 77 | # Complex virtual corpus with negation |
| 78 | ok($query = $cb->field_and( |
| 79 | $cb->string('author')->eq('Peter'), |
| 80 | $cb->string('age')->ne(4) |
| 81 | ), |
| 82 | , 'Create corpus query'); |
| 83 | |
| Akron | 0e782bc | 2017-05-14 14:04:41 +0200 | [diff] [blame] | 84 | is($query->to_string, 'age!=4&author=Peter', 'Stringification'); |
| Akron | 373df82 | 2016-12-28 15:25:14 +0100 | [diff] [blame] | 85 | ok(!$query->is_negative, 'Check negativity'); |
| 86 | |
| Akron | 2ea61aa | 2017-06-03 16:30:23 +0200 | [diff] [blame] | 87 | ok(my $norm = $query->root_normalize, 'Plan logically'); |
| 88 | is($norm->to_string, "author=Peter&!age=4", 'Stringification'); |
| Akron | 373df82 | 2016-12-28 15:25:14 +0100 | [diff] [blame] | 89 | |
| Akron | 2ea61aa | 2017-06-03 16:30:23 +0200 | [diff] [blame] | 90 | ok(my $opt = $norm->optimize($index), 'Planning'); |
| 91 | is($opt->to_string, "andNot('author:Peter','age:4')", 'Stringification'); |
| 92 | |
| 93 | |
| 94 | |
| 95 | ok($opt->next, 'Init vc'); |
| 96 | is($opt->current->to_string, '[1]', 'First doc'); |
| 97 | ok(!$opt->next, 'No more next'); |
| Akron | 373df82 | 2016-12-28 15:25:14 +0100 | [diff] [blame] | 98 | |
| 99 | |
| 100 | # Complex virtual corpus with negation |
| 101 | ok($query = $cb->field_and( |
| 102 | $cb->string('author')->ne('Peter'), |
| 103 | $cb->string('age')->ne(4) |
| 104 | ), |
| 105 | , 'Create corpus query'); |
| 106 | |
| Akron | 0e782bc | 2017-05-14 14:04:41 +0200 | [diff] [blame] | 107 | is($query->to_string, 'age!=4&author!=Peter', 'Stringification'); |
| Akron | 2c6c716 | 2017-05-15 18:15:33 +0200 | [diff] [blame] | 108 | ok(!$query->is_negative, 'Check negativity'); |
| Akron | 2ea61aa | 2017-06-03 16:30:23 +0200 | [diff] [blame] | 109 | #ok($query->planned_tree, 'Plan the tree'); |
| 110 | #is($query->to_string, '!(age=4|author=Peter)', 'Planned tree stringification'); |
| Akron | d3355ba | 2017-05-17 21:16:35 +0200 | [diff] [blame] | 111 | |
| Akron | 2ea61aa | 2017-06-03 16:30:23 +0200 | [diff] [blame] | 112 | |
| 113 | ok($plan = $query->root_normalize, 'Planning'); |
| 114 | is($plan->to_string, "[1]&!(age=4|author=Peter)", 'Stringification'); |
| 115 | |
| 116 | ok($plan = $plan->optimize($index), 'Optimizing'); |
| Akron | 0e782bc | 2017-05-14 14:04:41 +0200 | [diff] [blame] | 117 | is($plan->to_string, "not(or('age:4','author:Peter'))", 'Stringification'); |
| Akron | 373df82 | 2016-12-28 15:25:14 +0100 | [diff] [blame] | 118 | |
| Akron | d3355ba | 2017-05-17 21:16:35 +0200 | [diff] [blame] | 119 | done_testing; |
| 120 | __END__ |
| 121 | |
| 122 | |
| Akron | 2ea61aa | 2017-06-03 16:30:23 +0200 | [diff] [blame] | 123 | |
| 124 | |
| 125 | |
| Akron | 373df82 | 2016-12-28 15:25:14 +0100 | [diff] [blame] | 126 | ok($plan->next, 'More next'); |
| 127 | is($plan->current->to_string, '[3]', 'First doc'); |
| 128 | ok(!$plan->next, 'No more next'); |
| 129 | |
| 130 | # Complex virtual corpus with negation |
| 131 | ok($query = $cb->field_and( |
| 132 | $cb->string('genre')->eq('novel'), |
| 133 | $cb->string('author')->ne('Peter'), |
| 134 | $cb->string('age')->ne(4) |
| 135 | ), |
| 136 | , 'Create corpus query'); |
| 137 | |
| Akron | 2ea61aa | 2017-06-03 16:30:23 +0200 | [diff] [blame] | 138 | done_testing; |
| 139 | __END__ |
| 140 | |
| 141 | |
| Akron | 0e782bc | 2017-05-14 14:04:41 +0200 | [diff] [blame] | 142 | ok(!$query->has_classes, 'Contains classes'); |
| 143 | |
| 144 | is($query->to_string, 'age!=4&author!=Peter&genre=novel', 'Stringification'); |
| Akron | 373df82 | 2016-12-28 15:25:14 +0100 | [diff] [blame] | 145 | ok(!$query->is_negative, 'Check negativity'); |
| 146 | ok($plan = $query->plan_for($index), 'Planning'); |
| Akron | 0e782bc | 2017-05-14 14:04:41 +0200 | [diff] [blame] | 147 | is($plan->to_string, "without('genre:novel',or('age:4','author:Peter'))", |
| Akron | 373df82 | 2016-12-28 15:25:14 +0100 | [diff] [blame] | 148 | 'Stringification'); |
| 149 | |
| Akron | f9260ac | 2017-05-12 22:29:34 +0200 | [diff] [blame] | 150 | diag 'Test further'; |
| 151 | |
| 152 | # Especially: |
| 153 | # - First operand is negative, second is positive |
| 154 | # etc. |
| 155 | # - First operands have freq=0, first valid is negative |
| 156 | |
| Akron | 349747d | 2016-12-05 11:05:53 +0100 | [diff] [blame] | 157 | |
| 158 | done_testing; |
| 159 | __END__ |