aboutsummaryrefslogtreecommitdiffstats
path: root/t/open311/endpoint/schema.t
blob: b669ca4a5c2931f448c376015dc5e509a3b08349 (plain)
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
use strict; use warnings;

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

use Data::Rx;
use Open311::Endpoint;

my $endpoint = Open311::Endpoint->new;
my $schema = $endpoint->rx;

subtest 'comma tests' => sub {

    dies_ok {
        my $comma = $schema->make_schema({
            type => '/open311/comma',
        });
    } 'Construction dies on no contents';

    dies_ok {
        my $comma = $schema->make_schema({
            type => '/open311/comma',
            contents => '/open311/status',
            zirble => 'fleem',
        });
    } 'Construction dies on extra arguments';

    my $comma = $schema->make_schema({
        type => '/open311/comma',
        contents => '/open311/status',
        trim => 1,
    });

    ok ! $comma->check( undef ), 'Undef is not a valid string';
    ok ! $comma->check( [] ),    'Reference is not a valid string';

    ok ! $comma->check( 'zibble' ),      'invalid string';
    ok ! $comma->check( 'open,zibble' ),  'an invalid element';

    ok $comma->check( 'open' ),         'single value';
    ok $comma->check( 'open,closed' ), 'multiple values ok';
    ok $comma->check( 'open, closed ' ), 'spaces trimmed ok';
};

subtest 'datetime tests' => sub {

    dies_ok {
        my $comma = $schema->make_schema({
            type => '/open311/datetime',
            zirble => 'fleem',
        });
    } 'Construction dies on extra keys';

    my $dt = $schema->make_schema({
        type => '/open311/datetime',
    });

    ok ! $dt->check( undef ), 'Undef is not a valid string';
    ok ! $dt->check( [] ),    'Reference is not a valid string';

    ok ! $dt->check( '9th Feb 2012' ), 'invalid datetime format';

    ok $dt->check( '1994-11-05T08:15:30-05:00' ), 'datetime format with offset';
    ok $dt->check( '1994-11-05T08:15:30+05:00' ), 'datetime format with positive';
    ok $dt->check( '1994-11-05T13:15:30Z' ),      'datetime format zulu';
};

subtest 'identifier tests' => sub {
    my $id = $schema->make_schema( '/open311/example/identifier' );

    ok ! $id->check( undef ), 'Undef is not a valid string';
    ok ! $id->check( '' ), 'Empty string is not a valid identifier';
    ok ! $id->check( 'foo bar' ), 'String with spaces is not a valid identifier';

    ok $id->check( 'foo' ),     'Ascii word string is a valid identifier';
    ok $id->check( 'foo_bar' ), 'Ascii word string is a valid identifier';
    ok $id->check( 'foo_123' ), 'Ascii word/num string is a valid identifier';
};

done_testing;

1;