File: 03-primary-keys.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 (112 lines) | stat: -rw-r--r-- 3,034 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# $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 => 21;

use Wine;
use Recipe;
use Ingredient;
use PkLess; 

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

## TODO: test primary_key

# test correct behaviour of has_primary_key
{
    my $w = Wine->new;
    $w->save;
    ok $w->has_primary_key, "wine has pk";

    my $r = Recipe->new;
    $r->save;
    ok $r->has_primary_key, "recipe has pk";;
    
    my $i = Ingredient->new;
    $i->recipe_id($r->recipe_id);
    $i->save;
    ok $i->has_primary_key, "ingredient has (multi) pk";

    ## PK less
    my $p = PkLess->new;
    $p->anything("x");
    $p->save;
    ok ! $p->has_primary_key, "pkless has no pk";;

    my $p2 = PkLess->new;
    $p2->anything("y");
    $p2->save;

    ## save behaves correctly (there's never an UPDATE)
    my @res = PkLess->search();
    is scalar @res, 2, "Pk-less populated correctly";
}

# simple class pk fields
{
    isa_ok(Wine->primary_key_tuple(), 'ARRAY', q(Wine's primary key tuple is an arrayref));
    is_deeply(Wine->primary_key_tuple(), ['id'], q(Wine's primary key tuple contains the string 'id'));
    is_deeply(Wine->primary_key_to_terms([100]), { id => 100 });
}

# complex class pk fields
{
    isa_ok(Ingredient->primary_key_tuple, 'ARRAY', q(Ingredient's primary key tuple is an arrayref));
    is_deeply(Ingredient->primary_key_tuple, ['recipe_id', 'id'], q(Ingredient instance's primary key tuple contains 'recipe_id' and 'id'));
    is_deeply(Ingredient->primary_key_to_terms([100, 1000]), { recipe_id => 100, id => 1000 });
}

# simple instance pk fields
{
    my $w = Wine->new;
    isa_ok $w->primary_key_tuple, 'ARRAY', q(Wine instance's primary key tuple is an arrayref);
    is_deeply $w->primary_key_tuple, ['id'], q(Wine instance's primary key tuple contains the string 'id');
    is_deeply($w->primary_key_to_terms, { id => $w->id });
}

# complex instance pk fields
{
    my $i = Ingredient->new;
    is ref $i->primary_key_tuple, 'ARRAY', q(Ingredient instance's primary key tuple is an arrayref);
    is_deeply $i->primary_key_tuple, ['recipe_id', 'id'], q(Ingredient instance's primary key tuple contains 'recipe_id' and 'id');
    is_deeply($i->primary_key_to_terms, { recipe_id => $i->recipe_id, id => $i->id });
}

# 0 might be a valid pk
SKIP: {
    skip "primary key 0 has a special meaning for MySQL", 4 if DodTestUtil::driver eq 'MySQL';
    my $rv = Wine->remove({});
    # make sure that remove returns the number of records deleted (1)
    is($rv, 1, 'correct number of rows deleted');

    my $wine = Wine->new;
    $wine->id(0);
    $wine->name("zero");
    ok $wine->save;
    $wine = Wine->lookup(0);
    ok $wine;
    is $wine->name, "zero";
}

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