File: 05-deflate.t

package info (click to toggle)
libdata-objectdriver-perl 0.25-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 784 kB
  • sloc: perl: 3,795; sql: 64; makefile: 7
file content (98 lines) | stat: -rw-r--r-- 2,233 bytes parent folder | download | duplicates (3)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# $Id$

use strict;

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

use Test::More;
use Test::Exception;
use DodTestUtil;

BEGIN {
    DodTestUtil->check_driver;

    unless (eval { require Cache::Memory }) {
        plan skip_all => 'Tests require Cache::Memory';
    }
}

plan tests => 19;

use Recipe;
use Ingredient;

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

my $recipe = Recipe->new;
$recipe->title('Cake');
$recipe->save;

my $deflated = $recipe->deflate;
is $deflated->{columns}{recipe_id}, $recipe->recipe_id;
is $deflated->{columns}{title}, $recipe->title;

my $r2 = Recipe->inflate($deflated);
ok ! $r2->is_changed;
is $r2->recipe_id, $recipe->recipe_id;
is $r2->title, $recipe->title;

## Install some deflate/inflate in the Cache driver.
{
    no warnings 'once';
    no warnings 'redefine';
    *Data::ObjectDriver::Driver::Cache::Cache::deflate = sub {
        $_[1]->deflate;
    };
    *Data::ObjectDriver::Driver::Cache::Cache::inflate = sub {
        $_[1]->inflate($_[2]);
    };
}

## Ingredients are cached, so make sure that they survive the
## deflate/inflate process.
my $ingredient = Ingredient->new;
$ingredient->recipe_id($recipe->recipe_id);
$ingredient->name('Egg');
$ingredient->quantity(5);
$ingredient->save;

my $i2 = Ingredient->lookup([ $recipe->recipe_id, $ingredient->id ]);
is $i2->id, $ingredient->id;
is $i2->recipe_id, $ingredient->recipe_id;
is $i2->name, $ingredient->name;
is $i2->quantity, $ingredient->quantity;

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

my $is = Ingredient->lookup_multi([
        [ $recipe->recipe_id, $ingredient->id ],
        [ $recipe->recipe_id, $i3->id ],
    ]);
is scalar(@$is), 2;
is $is->[0]->name, 'Egg';
ok $is->[0]->{__cached};
is $is->[1]->name, 'Milk';
ok !$is->[1]->{__cached};

## Do it again! They should both be cached, now.
$is = Ingredient->lookup_multi([
        [ $recipe->recipe_id, $ingredient->id ],
        [ $recipe->recipe_id, $i3->id ],
    ]);
is scalar(@$is), 2;
is $is->[0]->name, 'Egg';
ok $is->[0]->{__cached};
is $is->[1]->name, 'Milk';
ok $is->[1]->{__cached};

END {
    disconnect_all(qw( Recipe Ingredient ));
    teardown_dbs(qw( global ));
}