File: 42-callbacks-multi-pk.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 (84 lines) | stat: -rw-r--r-- 2,482 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
# $Id: 41-callbacks.t 1037 2005-11-25 14:51:09Z ykerherve $

use strict;

use lib 't/lib';
use lib 't/lib/partitioned';

use Test::More;
use DodTestUtil;

BEGIN { DodTestUtil->check_driver }

plan tests => 13;

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

use Recipe;
use Ingredient;

## test pre_save
{
    my $title    = "Crême brûlée à la pistache";
    my $name     = "Eggs";
    my $quantity = 10;

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

    my $ingredient = Ingredient->new;
    $ingredient->recipe_id($recipe->recipe_id);
    $ingredient->name($name);
    $ingredient->quantity($quantity);

    ## it makes no sense to test if have the wrong init. cond.
    isa_ok $ingredient->primary_key_tuple, 'ARRAY';
    ok($recipe->partition_id, 'Recipe assigned to a cluster');

    my $ran_callback = 0;
    my $test_pre_save = sub {
        is scalar(@_), 2, 'callback received correct number of parameters';
        
        my ($saving) = @_;
        ## This is not the original object, so we can't test it that way.
        isa_ok $saving, 'Ingredient', 'callback received correct kind of object';
        cmp_ok $saving->name, 'eq',  $name, $name;
        cmp_ok $saving->quantity, '==', $quantity, 'quantity';
        ok !defined($saving->id), 'callback received object with right data';

        ## Change rating to test immutability of original.
        $saving->quantity($quantity * 2);

        $ran_callback++;
        return;
    };

    ## Add callback
    Ingredient->add_trigger('pre_save', $test_pre_save);
    
    ## Call the save that should trigger the callback
    $ingredient->save or die "Object did not save successfully";

    is $ran_callback, 1, 'callback ran exactly once';
    ok defined $ingredient->primary_key, 'object did receive a pk';
    
    my $saved = Ingredient->lookup($ingredient->primary_key)
        or die "Object just saved could not be retrieved successfully";
    is $saved->quantity, $quantity * 2, 'change in callback did change saved data';
    is $ingredient->quantity, $quantity, 'change in callback did not change original object';
    $ingredient->refresh;
    is $ingredient->quantity, $quantity * 2, 'refreshed worked';
    ok($ingredient->recipe_id, 'Ingredient assigned to a recipe');
};

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

1;