Support the compute() method, and emulate it if the backend doesnt have it.
[catagits/Catalyst-Plugin-Cache.git] / t / basic.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More 'no_plan';
7 use Test::Exception;
8
9 use ok "Catalyst::Plugin::Cache";
10
11 use Catalyst::Plugin::Cache::Backend::Memory;
12
13 {
14     package MockApp;
15     use base qw/Catalyst::Plugin::Cache/;
16     
17     sub registered_plugins {}
18
19     ### must register a backend class as of 0.06
20     ### the previous code simply ignored a croak
21     ### this is still in line with the documentation.
22     my %config = (
23         'Plugin::Cache' => { 
24             backend => { 
25                 class   => 'Catalyst::Plugin::Cache::Backend::Memory',
26             }
27         }            
28     );
29     sub config { \%config };
30 }
31
32 MockApp->setup;
33 my $c = bless {}, "MockApp";
34
35 can_ok( $c, "register_cache_backend" );
36 can_ok( $c, "unregister_cache_backend" );
37
38 MockApp->register_cache_backend( default => Catalyst::Plugin::Cache::Backend::Memory->new );
39 MockApp->register_cache_backend( moose => Catalyst::Plugin::Cache::Backend::Memory->new );
40
41 can_ok( $c, "cache" );
42
43 ok( $c->cache, "->cache returns a value" );
44
45 can_ok( $c->cache, "get" ); #, "rv from cache" );
46 can_ok( $c->cache("default"), "get" ); #, "default backend" );
47 can_ok( $c->cache("moose"), "get" ); #, "moose backend" );
48
49 ok( !$c->cache("lalalala"), "no lalala backend");
50
51 MockApp->unregister_cache_backend( "moose" );
52
53 ok( !$c->cache("moose"), "moose backend unregistered");
54
55
56 dies_ok {
57     MockApp->register_cache_backend( ding => undef );
58 } "can't register invalid backend";
59
60 dies_ok {
61     MockApp->register_cache_backend( ding => bless {}, "SomeClass" );
62 } "can't register invalid backend";
63
64
65
66 can_ok( $c, "default_cache_backend" );
67
68 can_ok( $c, "choose_cache_backend_wrapper" );
69 can_ok( $c, "choose_cache_backend" );
70
71 can_ok( $c, "cache_set" );
72 can_ok( $c, "cache_get" );
73 can_ok( $c, "cache_remove" );
74
75 $c->cache_set( foo => "bar" );
76 is( $c->cache_get("foo"), "bar", "set" );
77
78 $c->cache_remove( "foo" );
79 is( $c->cache_get("foo"), undef, "remove" );
80
81 MockApp->register_cache_backend( elk => Catalyst::Plugin::Cache::Backend::Memory->new );
82
83 is( $c->choose_cache_backend_wrapper( key => "foo" ), $c->default_cache_backend, "choose default" );
84 is( $c->choose_cache_backend_wrapper( key => "foo", backend => "elk" ), $c->get_cache_backend("elk"), "override choice" );
85
86
87 $c->cache_set( foo => "gorch", backend => "elk" );
88 is( $c->cache_get("foo"), undef, "set to custom backend (get from non custom)" );
89 is( $c->cache_get("foo", backend => "elk"), "gorch", "set to custom backend (get from custom)" );
90
91 my $cache_elk = $c->cache( backend => "elk" );
92 my $cache_norm = $c->cache();
93
94 is( $cache_norm->get("foo"), undef, "default curried cache has no foo");
95 is( $cache_elk->get("foo"), "gorch", "curried custom backend has foo" );
96
97
98 is( $c->cache->get('compute_test'), undef, 'compute_test key is undef by default' );
99 is( $c->cache->compute('compute_test',sub{'monkey'}), 'monkey', 'compute returned code value' );
100 is( $c->cache->get('compute_test'), 'monkey', 'compute_test key is now set' );
101 is( $c->cache->compute('compute_test',sub{'donkey'}), 'monkey', 'compute returned cached value' );
102 $c->cache->remove('compute_test');
103 is( $c->cache->compute('compute_test',sub{'donkey'}), 'donkey', 'compute returned second code value' );
104