| 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::Archive; | ||||||||||
| 6 | # ABSTRACT: Test::Routine role for testing Metabase::Archive 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::Factory; | ||||||||
|
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_archive'; | |||||||||
| 24 | |||||||||||
| 25 | #--------------------------------------------------------------------------# | ||||||||||
| 26 | # fixtures | ||||||||||
| 27 | #--------------------------------------------------------------------------# | ||||||||||
| 28 | |||||||||||
| 29 | 1 | with 'Metabase::Test::Factory'; | |||||||||
| 30 | |||||||||||
| 31 | 1 | has archive => ( | |||||||||
| 32 | is => 'ro', | ||||||||||
| 33 | does => 'Metabase::Archive', | ||||||||||
| 34 | lazy_build => 1, | ||||||||||
| 35 | ); | ||||||||||
| 36 | |||||||||||
| 37 | 2 | ▶ | sub store_all { | ||||||||
|
Subroutine
store_all: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 38 | 2 | my $self = shift; | |||||||||
| 39 | 2 | my @keys = $self->keys_test_fact; | |||||||||
| 40 | 2 | for my $k ( @keys ) { | |||||||||
| 41 | 4 | ok( $self->archive->store( $self->get_test_fact($k)->as_struct ), | |||||||||
| 42 | "stored $k" | ||||||||||
| 43 | ); | ||||||||||
| 44 | } | ||||||||||
| 45 | 2 | return scalar @keys; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | #--------------------------------------------------------------------------# | ||||||||||
| 49 | # tests | ||||||||||
| 50 | #--------------------------------------------------------------------------# | ||||||||||
| 51 | |||||||||||
| 52 | 1 | ▶ | test "store and retrieve" => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 53 | 1 | my $self = shift; | |||||||||
| 54 | 1 | $self->clear_archive; | |||||||||
| 55 | |||||||||||
| 56 | 1 | my $fact = $self->get_test_fact('fact1'); | |||||||||
| 57 | 1 | my $guid = $self->archive->store( $fact->as_struct ); | |||||||||
| 58 | |||||||||||
| 59 | 1 | is( $fact->guid, $guid, "GUID returned matched GUID in fact" ); | |||||||||
| 60 | |||||||||||
| 61 | 1 | my $copy_struct = $self->archive->extract( $guid ); | |||||||||
| 62 | 1 | my $class = Metabase::Fact->class_from_type($copy_struct->{metadata}{core}{type}); | |||||||||
| 63 | |||||||||||
| 64 | 1 | ok( my $copy = $class->from_struct( $copy_struct ), | |||||||||
| 65 | "got a fact from archive" | ||||||||||
| 66 | ); | ||||||||||
| 67 | |||||||||||
| 68 | 1 | cmp_deeply( $copy, $fact, "Extracted fact matches original" ); | |||||||||
| 69 | 1 | }; | |||||||||
| 70 | |||||||||||
| 71 | 1 | ▶ | test "iteration" => sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 72 | 1 | my $self = shift; | |||||||||
| 73 | 1 | $self->clear_archive; | |||||||||
| 74 | 1 | my $n_facts = $self->store_all; | |||||||||
| 75 | |||||||||||
| 76 | 1 | my $iter = $self->archive->iterator; | |||||||||
| 77 | 1 | my @facts; | |||||||||
| 78 | 1 | ▶ | while( my $block = $iter->next ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 2 | foreach my $item ( @$block ) { | |||||||||
| 80 | 2 | push @facts, $item; | |||||||||
| 81 | } | ||||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 1 | is( scalar @facts, $n_facts, "iterator found all facts" ); | |||||||||
| 85 | 1 | }; | |||||||||
| 86 | |||||||||||
| 87 | |||||||||||
| 88 | 1 | ▶ | test "deletion" => sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 89 | 1 | my $self = shift; | |||||||||
| 90 | 1 | $self->clear_archive; | |||||||||
| 91 | 1 | my $n_facts = $self->store_all; | |||||||||
| 92 | |||||||||||
| 93 | 1 | my $fact = $self->get_test_fact('fact1'); | |||||||||
| 94 | |||||||||||
| 95 | 1 | ok( $self->archive->delete( $fact->guid ), "deleted fact1" ); | |||||||||
| 96 | |||||||||||
| 97 | 1 | my $iter = $self->archive->iterator; | |||||||||
| 98 | 1 | my @facts; | |||||||||
| 99 | 1 | ▶ | while( my $block = $iter->next ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 2 | foreach my $item ( @$block ) { | |||||||||
| 101 | 1 | push @facts, $item; | |||||||||
| 102 | } | ||||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 1 | is( scalar @facts, $n_facts-1, "iterator found one less fact" ); | |||||||||
| 106 | 1 | }; | |||||||||
| 107 | |||||||||||
| 108 | 1 | 1; | |||||||||
| 109 | |||||||||||
| 110 | __END__ | ||||||||||