| 1 | 1 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | package Metabase::Test::Index; | ||||||||||
| 6 | # ABSTRACT: Test::Routine role for testing Metabase::Index implementations | ||||||||||
| 7 | 1 | our $VERSION = '1.003'; # VERSION | |||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use Metabase::Fact; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Metabase::Test::Fact; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Test::Deep qw/cmp_deeply/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use Test::More 0.92; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 1 | ▶ | use Test::Routine; # a Moose::Role | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use MooseX::Types::Moose qw/Str ClassName/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | ▶ | use MooseX::Types::Structured qw/Map/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | #--------------------------------------------------------------------------# | ||||||||||
| 20 | # requirements from composing class | ||||||||||
| 21 | #--------------------------------------------------------------------------# | ||||||||||
| 22 | |||||||||||
| 23 | 1 | requires '_build_index'; | |||||||||
| 24 | |||||||||||
| 25 | #--------------------------------------------------------------------------# | ||||||||||
| 26 | # fixtures | ||||||||||
| 27 | #--------------------------------------------------------------------------# | ||||||||||
| 28 | |||||||||||
| 29 | 1 | with 'Metabase::Test::Factory'; | |||||||||
| 30 | |||||||||||
| 31 | 1 | has index => ( | |||||||||
| 32 | is => 'ro', | ||||||||||
| 33 | does => 'Metabase::Index', | ||||||||||
| 34 | lazy_build => 1, | ||||||||||
| 35 | ); | ||||||||||
| 36 | |||||||||||
| 37 | #--------------------------------------------------------------------------# | ||||||||||
| 38 | # methods | ||||||||||
| 39 | #--------------------------------------------------------------------------# | ||||||||||
| 40 | |||||||||||
| 41 | 3 | ▶ | sub reset { | ||||||||
|
Subroutine
reset: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 42 | 3 | my $self = shift; | |||||||||
| 43 | 3 | $self->clear_index; | |||||||||
| 44 | 3 | is( $self->index->count, 0, "Index is empty" ); | |||||||||
| 45 | 3 | my @facts = ( | |||||||||
| 46 | $self->get_test_fact("fact1"), | ||||||||||
| 47 | $self->get_test_fact("fact2"), | ||||||||||
| 48 | ); | ||||||||||
| 49 | |||||||||||
| 50 | 3 | $self->index->add($_) for @facts; | |||||||||
| 51 | 3 | is( $self->index->count, scalar @facts, "All test facts added" ); | |||||||||
| 52 | |||||||||||
| 53 | 3 | return @facts; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | #--------------------------------------------------------------------------# | ||||||||||
| 57 | # tests | ||||||||||
| 58 | #--------------------------------------------------------------------------# | ||||||||||
| 59 | |||||||||||
| 60 | 1 | ▶ | test "add and count" => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 61 | 1 | my $self = shift; | |||||||||
| 62 | 1 | $self->clear_index; | |||||||||
| 63 | 1 | my ($fact1, $fact2) = map { $self->get_test_fact("fact$_") } 1..2; | |||||||||
| 64 | |||||||||||
| 65 | 1 | is( $self->index->count, 0, "Index is empty" ); | |||||||||
| 66 | |||||||||||
| 67 | # add() | ||||||||||
| 68 | 1 | ok( $self->index->add( $fact1 ), "Indexed fact 1" ); | |||||||||
| 69 | |||||||||||
| 70 | # count() | ||||||||||
| 71 | 1 | is( $self->index->count, 1, "Index has one entry" ); | |||||||||
| 72 | 1 | is( $self->index->count(-where => [ -eq => 'core.guid' => '0' ]), | |||||||||
| 73 | 0, "Count with (false) query condition is 0" | ||||||||||
| 74 | ); | ||||||||||
| 75 | 1 | is( $self->index->count(-where => [ -eq => 'core.type' => 'Metabase-Test-Fact']), | |||||||||
| 76 | 1, "Count with (true) query condition is 1" | ||||||||||
| 77 | ); | ||||||||||
| 78 | |||||||||||
| 79 | 1 | ok( $self->index->add( $fact2 ), "Indexed fact 2" ); | |||||||||
| 80 | 1 | is( $self->index->count, 2, "Index has two entries" ); | |||||||||
| 81 | |||||||||||
| 82 | 1 | is( $self->index->count(-where => [ -eq => 'core.guid' => $fact1->guid]), | |||||||||
| 83 | 1, "Count with (limited) query condition is 1" | ||||||||||
| 84 | ); | ||||||||||
| 85 | |||||||||||
| 86 | 1 | }; | |||||||||
| 87 | |||||||||||
| 88 | 1 | ▶ | test "search" => sub { | ||||||||
|
Subroutine
search: called CC=2 SCAR=6.9
| |||||||||||
| 89 | 1 | my $self = shift; | |||||||||
| 90 | |||||||||||
| 91 | 1 | my ($fact1, $fact2) = $self->reset; | |||||||||
| 92 | 1 | my $f2_string = $fact2->content; | |||||||||
| 93 | |||||||||||
| 94 | 1 | my $matches; | |||||||||
| 95 | 1 | $matches = $self->index->search( -where => [ -eq => 'core.guid' => $fact1->guid ] ); | |||||||||
| 96 | 1 | is( scalar @$matches, 1, "Found one fact searching for guid" ); | |||||||||
| 97 | |||||||||||
| 98 | 1 | my $count = $self->index->count( -where => [ | |||||||||
| 99 | -eq => 'core.type' => $fact1->type | ||||||||||
| 100 | ] ) ; | ||||||||||
| 101 | 1 | is( $count, 2, "Count found two facts constrained on fact type" ); | |||||||||
| 102 | |||||||||||
| 103 | 1 | $matches = $self->index->search( -where => [ | |||||||||
| 104 | -eq => 'core.type' => $fact1->type | ||||||||||
| 105 | ] ) ; | ||||||||||
| 106 | 1 | is( scalar @$matches, 2, "Found two facts searching for fact type" ); | |||||||||
| 107 | |||||||||||
| 108 | 1 | $matches = $self->index->search( -where => [ | |||||||||
| 109 | -and => | ||||||||||
| 110 | [-eq => 'resource.type' => 'Metabase-Resource-cpan-distfile'], | ||||||||||
| 111 | [-eq => 'resource.cpan_id' => 'UNKNOWN'] | ||||||||||
| 112 | ] | ||||||||||
| 113 | ); | ||||||||||
| 114 | 1 | is( scalar @$matches, 2, "Found two facts searching for resource cpan_id" ); | |||||||||
| 115 | |||||||||||
| 116 | 1 | $matches = $self->index->search( -where => [ | |||||||||
| 117 | -and => | ||||||||||
| 118 | [-eq => 'content.size' => length $f2_string], | ||||||||||
| 119 | [-eq => 'core.type' => 'Metabase-Test-Fact'], | ||||||||||
| 120 | ] ) ; | ||||||||||
| 121 | 1 | is( scalar @$matches, 1, "Found one fact searching on content.size" ); | |||||||||
| 122 | |||||||||||
| 123 | 1 | $matches = $self->index->search( 'core.guid' => $fact2->guid ) ; | |||||||||
| 124 | 1 | is( scalar @$matches, 1, "Found one fact searching on content.size (old API)" ); | |||||||||
| 125 | |||||||||||
| 126 | 1 | $matches = $self->index->search( | |||||||||
| 127 | 'content.size' => length $f2_string, 'core.type' => $fact2->type | ||||||||||
| 128 | ) ; | ||||||||||
| 129 | 1 | is( scalar @$matches, 1, | |||||||||
| 130 | "Found one fact searching on two fields (old API test 2)" | ||||||||||
| 131 | ); | ||||||||||
| 132 | |||||||||||
| 133 | 1 | $matches = $self->index->search( | |||||||||
| 134 | -where => [ -eq => 'core.guid' => $fact2->guid ], | ||||||||||
| 135 | 'content.size' => length $f2_string, 'core.type' => $fact2->type | ||||||||||
| 136 | ) ; | ||||||||||
| 137 | 1 | is( scalar @$matches, 1, | |||||||||
| 138 | "Found one fact searching on three fields (mixed API test)" | ||||||||||
| 139 | ); | ||||||||||
| 140 | |||||||||||
| 141 | 1 | is( $matches->[0], $fact2->guid, "Result GUID matches expected fact GUID" ); | |||||||||
| 142 | |||||||||||
| 143 | 1 | $matches = $self->index->search( -where => [ | |||||||||
| 144 | -and => | ||||||||||
| 145 | [-eq => 'resource.type' => 'Metabase-Resource-cpan-distfile'], | ||||||||||
| 146 | [-eq => 'resource.cpan_id' => "asdljasljfa" ], | ||||||||||
| 147 | ]); | ||||||||||
| 148 | 1 | is( scalar @$matches, 0, "Found no facts searching for bogus dist_author" ); | |||||||||
| 149 | |||||||||||
| 150 | 1 | $matches = $self->index->search( -where => [ -eq => bogus_key => "asdljasljfa"] ); | |||||||||
| 151 | 1 | is( scalar @$matches, 0, "Found no facts searching on bogus key" ); | |||||||||
| 152 | |||||||||||
| 153 | 1 | $matches = $self->index->search( | |||||||||
| 154 | -where => [ -and => | ||||||||||
| 155 | [ -eq => 'core.type' => $fact1->type ], | ||||||||||
| 156 | [ -gt => 'content.size' => 0 ] | ||||||||||
| 157 | ] | ||||||||||
| 158 | ); | ||||||||||
| 159 | 1 | is( scalar @$matches, 2, "Found two facts on compound query" ); | |||||||||
| 160 | |||||||||||
| 161 | 1 | $matches = $self->index->search( | |||||||||
| 162 | -where => [ -and => | ||||||||||
| 163 | [ -eq => 'core.type' => $fact1->type ], | ||||||||||
| 164 | [ -ne => 'core.guid' => 0 ] | ||||||||||
| 165 | ], | ||||||||||
| 166 | -order => [ -asc => 'core.guid' ], | ||||||||||
| 167 | ) ; | ||||||||||
| 168 | 1 | is( scalar @$matches, 2, "Ran ordered search" ); | |||||||||
| 169 | 1 | ok( $matches->[0] lt $matches->[1], "Facts in correct order" ); | |||||||||
| 170 | |||||||||||
| 171 | 1 | $matches = $self->index->search( | |||||||||
| 172 | -where => [ -and => | ||||||||||
| 173 | [ -eq => 'core.type' => $fact1->type ], | ||||||||||
| 174 | [ -ne => 'core.guid' => 0 ] | ||||||||||
| 175 | ], | ||||||||||
| 176 | -order => [ -desc => 'core.guid' ], | ||||||||||
| 177 | ) ; | ||||||||||
| 178 | 1 | is( scalar @$matches, 2, "Ran ordered search (reversed)" ); | |||||||||
| 179 | 1 | ▶ | ok( $matches->[0] gt $matches->[1], "Facts in correct order" ) or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 180 | diag explain $matches; | ||||||||||
| 181 | |||||||||||
| 182 | 1 | $matches = $self->index->search( | |||||||||
| 183 | -where => [ -ne => 'core.guid' => 0 ], | ||||||||||
| 184 | -order => [ -desc => 'core.guid' ], | ||||||||||
| 185 | -limit => 1 | ||||||||||
| 186 | ); | ||||||||||
| 187 | 1 | is( scalar @$matches, 1, "Querying with limit 1 returns 1 result" ); | |||||||||
| 188 | |||||||||||
| 189 | 1 | }; | |||||||||
| 190 | |||||||||||
| 191 | 1 | ▶ | test "exists()" => sub { | ||||||||
|
Subroutine
exists(): called CC=1 SCAR=0.0
| |||||||||||
| 192 | 1 | my $self = shift; | |||||||||
| 193 | |||||||||||
| 194 | 1 | my ($fact1, $fact2) = $self->reset; | |||||||||
| 195 | 1 | my $f2_string = $fact2->content; | |||||||||
| 196 | |||||||||||
| 197 | 1 | ok( $self->index->exists( $fact1->guid ), "Checked exists( guid )" ); | |||||||||
| 198 | 1 | ok( $self->index->exists( uc $fact1->guid ), "Checked exists( GUID )" ); | |||||||||
| 199 | 1 | ok( ! $self->index->exists( '2475e04a-a8e7-11e0-bcb0-5f47df37754e' ), | |||||||||
| 200 | "Checked exists( fakeguid ) - false" | ||||||||||
| 201 | ); | ||||||||||
| 202 | 1 | }; | |||||||||
| 203 | |||||||||||
| 204 | 1 | ▶ | test "delete()" => sub { | ||||||||
|
Subroutine
delete(): called CC=1 SCAR=0.0
| |||||||||||
| 205 | 1 | my $self = shift; | |||||||||
| 206 | 1 | my ($fact1, $fact2) = $self->reset; | |||||||||
| 207 | 1 | ok( $self->index->delete( $fact1->guid ), "Deleted fact 1 from index" ); | |||||||||
| 208 | 1 | is( $self->index->count, 1, "Index has one entry" ); | |||||||||
| 209 | 1 | ok( $self->index->delete( $fact2->guid ), "Deleted fact 2 from index" ); | |||||||||
| 210 | 1 | is( $self->index->count, 0, "Index is empty" ); | |||||||||
| 211 | 1 | }; | |||||||||
| 212 | |||||||||||
| 213 | 1 | 1; | |||||||||
| 214 | |||||||||||
| 215 | __END__ | ||||||||||