File: 07-has-a-cached.t

package info (click to toggle)
libdata-objectdriver-perl 0.09-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 744 kB
  • sloc: perl: 6,367; sql: 60; makefile: 2
file content (70 lines) | stat: -rw-r--r-- 1,534 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# $Id: 05-deflate.t 1170 2006-03-24 05:29:48Z btrott $

use strict;

use lib 't/lib';
use lib 't/lib/cached';

require 't/lib/db-common.pl';

use Test::More;
use Test::Exception;
use Scalar::Util;
BEGIN {
    unless (eval { require DBD::SQLite }) {
        plan skip_all => 'Tests require DBD::SQLite';
    }
    unless (eval { require Cache::Memory }) {
        plan skip_all => 'Tests require Cache::Memory';
    }
    unless (eval 'use Scalar::Util qw(weaken); 1') {
        plan skip_all => 'Tests require weakref';
    }
}

plan tests => 3;

use Recipe;
use Ingredient;

setup_dbs({
    global => [ qw( recipes ingredients) ],
});

Ingredient->has_a( {
        class  => 'Recipe',
        column => 'recipe_id',
        parent_method => 'ingredients',
        method => 'recipe',
        cached => 1,
    },
);

## setup  a few datas
my $recipe = Recipe->new;
$recipe->title('Cake');
$recipe->save;

my $ingredient = Ingredient->new;
$ingredient->recipe_id($recipe->recipe_id);
$ingredient->name('Egg');
$ingredient->quantity(5);
$ingredient->save;

my $i3 = Ingredient->new;
$i3->recipe_id($recipe->recipe_id);
$i3->name('Milk');
$i3->quantity(1);
$i3->save;

{ 
    my $r = $ingredient->recipe;
    is $r->recipe_id, $recipe->recipe_id, "recipe id back using 'parent_method'";
    
    ## show me what you have in your belly.
    ok Scalar::Util::isweak($ingredient->{__cache_recipe}), "weak ref";
}

is $ingredient->{__cache_recipe}, undef, "cache has effectively been destroyed";

sub DESTROY { teardown_dbs(qw( global )); }