File: dbic_unique.t

package info (click to toggle)
libhtml-formfu-model-dbic-perl 0.09002-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,064 kB
  • sloc: perl: 2,270; makefile: 2
file content (192 lines) | stat: -rw-r--r-- 4,268 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use strict;
use warnings;
use Test::More tests => 15;

use HTML::FormFu;
use lib 't/lib';
use DBICTestLib 'new_schema';
use MySchema;

my $schema = new_schema();

my $rs = $schema->resultset('User');

# Pre-existing row to check against.
$rs->create( {
        id         => '1',
        name       => 'a',
        title      => 'b',
    } );
# Second row to check against for method_name with record on stash
$rs->create( {
        name       => 'e',
        title      => 'f',
    } );

# Basic form.
{
    my $form = HTML::FormFu->new;
        
    $form->load_config_file('t/constraints/dbic_unique.yml');
    
    $form->stash->{'schema'} = $schema;

    $form->process( {
            'name'                 => 'a',
            'title'                => 'c',
        } );
    
    ok( !$form->submitted_and_valid );

    is_deeply(
        [
            'name',
        ],
        [ $form->has_errors ],
    );
}

# Form where DB column differs from form field name.
{
    my $form = HTML::FormFu->new;
        
    $form->load_config_file('t/constraints/dbic_unique_column.yml');
    
    $form->stash->{'schema'} = $schema;

    $form->process( {
            'username'             => 'a',
            'title'                => 'c',
        } );
    
    ok( !$form->submitted_and_valid );

    is_deeply(
        [
            'username',
        ],
        [ $form->has_errors ],
    );
}

# Form tracking a multi-column unique key (name+title).
{
    my $form = HTML::FormFu->new;
        
    $form->load_config_file('t/constraints/dbic_unique_others.yml');
    
    $form->stash->{'schema'} = $schema;

    $form->process( {
            'name'                 => 'a',
            'title'                => 'c',
        } );
    
    ok( $form->submitted_and_valid );

    $form->process( {
            'name'                 => 'a',
            'title'                => 'b',
        } );
    
    ok( !$form->submitted_and_valid );

    is_deeply(
        [
            'name',
        ],
        [ $form->has_errors ],
    );
}

# Form using a method_name to determine uniqueness with record on stash (is_name_available).
{
    my $form = HTML::FormFu->new;
        
    $form->load_config_file('t/constraints/dbic_unique_method.yml');
    
    my $user = $schema->resultset('User')->find( {name => 'a'} );
    
    $form->stash->{'schema'} = $schema;
    $form->stash->{'user'}   = $user;

    $form->process( {
            'name'                 => 'a',
            'title'                => 'b',
        } );
    
    ok( $form->submitted_and_valid );

    $form->process( {
            'name'                 => 'c',
            'title'                => 'd',
        } );
    
    ok( $form->submitted_and_valid );

    $form->process( {
            'name'                 => 'e',
            'title'                => 'f',
        } );
}

# Form where id_field defined
{
    my $form = HTML::FormFu->new;

    $form->load_config_file('t/constraints/dbic_unique_id_field.yml');

    $form->stash->{'schema'} = $schema;

    # not uniq id - not uniq name => ok (no changes)
    $form->process( {
            'id'                   => '1',
            'name'                 => 'a',
            'title'                => 'c',
        } );

    ok( $form->submitted_and_valid );


    # no id - uniq name => ok
    $form->process( {
            'name'                 => 'c',
            'title'                => 'b',
        } );

    ok( $form->submitted_and_valid );


    # not uniq id - uniq name => ok
    $form->process( {
            'id'                   => '1',
            'name'                 => 'c',
            'title'                => 'b',
        } );

    ok( $form->submitted_and_valid );

    # no id - not uniq name -> error
    $form->process( {
            'name'                 => 'a',
            'title'                => 'b',
        } );

    ok( !$form->submitted_and_valid );

    # uniq id - not uniq name => error
    $form->process( {
            'id'                   => '2',
            'name'                 => 'a',
            'title'                => 'b',
        } );

    ok( !$form->submitted_and_valid );

    is_deeply(
        [
            'name',
        ],
        [ $form->has_errors ],
    );
}