6cdaf26f88cf8226bd538935651ede2d2f1f393a
[catagits/Catalyst-Plugin-Cache.git] / t / key_regexes.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More 'no_plan';
7
8 use ok "Catalyst::Plugin::Cache";
9 use ok "Catalyst::Plugin::Cache::Choose::KeyRegexes";
10
11 {
12     package MockApp;
13     use base qw/Catalyst::Plugin::Cache Catalyst::Plugin::Cache::Choose::KeyRegexes/;
14
15     our %config = (
16         cache => {
17             key_regexes => [
18                 qr/^foo/ => "foo_store",
19                 qr/^bar/ => "bar_store",
20             ],
21         },
22     );
23     sub config { \%config }
24
25     package MemoryCache;
26     use Storable qw/freeze thaw/;
27     
28     sub new { bless {}, shift }
29     sub get { ${thaw($_[0]{$_[1]}) || return} };
30     sub set { $_[0]{$_[1]} = freeze(\$_[2]) };
31     sub delete { delete $_[0]{$_[1]} };
32 }
33
34
35 MockApp->setup;
36 my $c = bless {}, "MockApp";
37
38 MockApp->register_cache_backend( default => MemoryCache->new );
39 MockApp->register_cache_backend( foo_store => MemoryCache->new );
40 MockApp->register_cache_backend( bar_store => MemoryCache->new );
41
42 is( $c->choose_cache_backend_wrapper( key => "baz" ), $c->cache, "chose default" );
43 is( $c->choose_cache_backend_wrapper( key => "foo" ), $c->cache("foo_store"), "chose foo" );
44 is( $c->choose_cache_backend_wrapper( key => "bar" ), $c->cache("bar_store"), "chose bar" );
45
46 $c->cache_set( foo_laa => "laa" );
47 $c->cache_set( bar_laa => "laa" );
48 $c->cache_set( baz_laa => "laa" );
49
50 is( $c->cache->get("baz_laa"), "laa", "non match stored in default" );
51 is( $c->cache->get("foo_laa"), undef, "no foo key" );
52 is( $c->cache->get("bar_laa"), undef, "no bar key" );
53
54
55 is( $c->cache("foo_store")->get("baz_laa"), undef, "no non match in foo store" );
56 is( $c->cache("foo_store")->get("foo_laa"), "laa", "has foo key" );
57 is( $c->cache("foo_store")->get("bar_laa"), undef, "no bar key" );
58
59
60 is( $c->cache("bar_store")->get("baz_laa"), undef, "no non match in bar store" );
61 is( $c->cache("bar_store")->get("foo_laa"), undef, "no foo key" );
62 is( $c->cache("bar_store")->get("bar_laa"), "laa", "has bar key" );
63
64