File: dbic_unique_repeatable_id_field.t

package info (click to toggle)
libhtml-formfu-model-dbic-perl 2.03-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,148 kB
  • sloc: perl: 1,429; makefile: 2
file content (89 lines) | stat: -rw-r--r-- 1,722 bytes parent folder | download | duplicates (5)
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
use strict;
use warnings;
use Test::More tests => 8;

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

my $schema = new_schema();

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

# Pre-existing rows
$rs->create( {
    id    => 1,
    name  => 'a',
    title => 'b',
} );

$rs->create( {
    id    => 2,
    name  => 'e',
    title => 'f',
} );

#
my $form = HTML::FormFu->new;

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

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

# not valid
# try updating row#1 with the same name as row#2
# fails Unique
{
    $form->process( {
        'user_1.id'    => 1,
        'user_1.name'  => 'e',
        'user_1.title' => 'title',
    } );

    ok( !$form->submitted_and_valid );

    ok( $form->has_errors('user_1.name') );

    like( $form->get_field({ nested_name => 'user_1.name' }), qr/Value already exists/i );
}

# valid
# update row#1 with the same name it already has
{
    $form->process( {
        'user_1.id'    => 1,
        'user_1.name'  => 'a',
        'user_1.title' => 'title',
    } );

    ok( $form->submitted_and_valid );
}

# not valid
# try creating a new row with the same name as row#1
{
    $form->process( {
        'user_1.id'    => '',
        'user_1.name'  => 'a',
        'user_1.title' => 'title',
    } );

    ok( !$form->submitted_and_valid );

    ok( $form->has_errors('user_1.name') );

    like( $form->get_field({ nested_name => 'user_1.name' }), qr/Value already exists/i );
}

# valid
# create new row with a unique name
{
    $form->process( {
        'user_1.id'    => '',
        'user_1.name'  => 'snowflake',
        'user_1.title' => 'title',
    } );

    ok( $form->submitted_and_valid );
}