diff options
Diffstat (limited to 't/open311/endpoint')
-rw-r--r-- | t/open311/endpoint/Endpoint1.pm | 114 | ||||
-rw-r--r-- | t/open311/endpoint/ServiceType1.pm | 12 | ||||
-rw-r--r-- | t/open311/endpoint/schema.t | 82 | ||||
-rw-r--r-- | t/open311/endpoint/spark.t | 64 |
4 files changed, 272 insertions, 0 deletions
diff --git a/t/open311/endpoint/Endpoint1.pm b/t/open311/endpoint/Endpoint1.pm new file mode 100644 index 000000000..ccd16c238 --- /dev/null +++ b/t/open311/endpoint/Endpoint1.pm @@ -0,0 +1,114 @@ +package t::open311::endpoint::Endpoint1; +use Web::Simple; +extends 'Open311::Endpoint'; +use Types::Standard ':all'; +use MooX::HandlesVia; + +use Open311::Endpoint::Service; +use t::open311::endpoint::ServiceType1; +use Open311::Endpoint::Service::Attribute; + +sub services { + return ( + t::open311::endpoint::ServiceType1->new( + service_code => 'POT', + service_name => 'Pothole Repairs', + description => 'Pothole Repairs Service', + attributes => [ + Open311::Endpoint::Service::Attribute->new( + code => 'depth', + required => 1, + datatype => 'number', + datatype_description => 'an integer', + description => 'depth of pothole, in centimetres', + ), + Open311::Endpoint::Service::Attribute->new( + code => 'shape', + required => 0, + datatype => 'singlevaluelist', + datatype_description => 'square | circle | triangle', + description => 'shape of the pothole', + values => { + square => 'Square', + circle => 'Circle', + triangle => 'Triangle', + }, + ), + ], + type => 'realtime', + keywords => [qw/ deep hole wow/], + group => 'highways', + ), + t::open311::endpoint::ServiceType1->new( + service_code => 'BIN', + service_name => 'Bin Enforcement', + description => 'Bin Enforcement Service', + attributes => [], + type => 'realtime', + keywords => [qw/ bin /], + group => 'sanitation', + ) + ); +} + +# FOR TESTING, we'll just maintain requests in a *global* array... +# obviously a real Service driver will use a DB or API call! +{ + our @SERVICE_REQUESTS; + has _requests => ( + is => 'ro', + isa => ArrayRef[ InstanceOf[ 'Open311::Endpoint::Service::Request' ] ], + default => sub { \@SERVICE_REQUESTS }, + handles_via => 'Array', + handles => { + next_request_id => 'count', + _add_request => 'push', + get_request => 'get', + get_requests => 'elements', + filter_requests => 'grep', + } + ); +} + +sub post_service_request { + my ($self, $service, $args) = @_; + + my $request = Open311::Endpoint::Service::Request->new( + + # NB: possible race condition between next_request_id and _add_request + # (this is fine for synchronous test-cases) + + service => $service, + service_request_id => $self->next_request_id, + status => 'open', + description => $args->{description}, + agency_responsible => '', + requested_datetime => DateTime->now(), + updated_datetime => DateTime->now(), + address => $args->{address_string} // '', + address_id => $args->{address_id} // '', + media_url => $args->{media_url} // '', + zipcode => $args->{zipcode} // '', + # NB: other info is passed in that would be stored by an Open311 + # endpoint, see Open311::Endpoint::Service::Request for full list, + # but we don't need to handle all of those in this test + ); + $self->_add_request( $request ); + + return ( $request ); +} + +sub get_service_requests { + my ($self, $args) = @_; + + my $service_code = $args->{service_code} or return $self->get_requests; + # we use ~~ as the service_code arg will be an arrayref like ['POT'] + return $self->filter_requests( sub { shift->service->service_code ~~ $service_code }); +} + +sub get_service_request { + my ($self, $service_request_id, $args) = @_; + return $self->get_request( $service_request_id ); +} + +1; diff --git a/t/open311/endpoint/ServiceType1.pm b/t/open311/endpoint/ServiceType1.pm new file mode 100644 index 000000000..e73b15b7d --- /dev/null +++ b/t/open311/endpoint/ServiceType1.pm @@ -0,0 +1,12 @@ +package t::open311::endpoint::ServiceType1; +use Moo; +extends 'Open311::Endpoint::Service'; +use DateTime; + +use Open311::Endpoint::Service::Request; + +has '+default_service_notice' => ( + default => 'This is a test service', +); + +1; diff --git a/t/open311/endpoint/schema.t b/t/open311/endpoint/schema.t new file mode 100644 index 000000000..b669ca4a5 --- /dev/null +++ b/t/open311/endpoint/schema.t @@ -0,0 +1,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; diff --git a/t/open311/endpoint/spark.t b/t/open311/endpoint/spark.t new file mode 100644 index 000000000..589f39baf --- /dev/null +++ b/t/open311/endpoint/spark.t @@ -0,0 +1,64 @@ +use strict; use warnings; + +use Test::More; + +use Open311::Endpoint; +use Data::Dumper; +use JSON; + +my $endpoint = Open311::Endpoint->new; +my $json = JSON->new; + +subtest "Spark test" => sub { + my $spark = $endpoint->spark; + my $struct = { + foo => { + service_requests => [ 1,2,3 ], + quxes => [ + { + values => [1,2], + }, + { + values => [3,4], + }, + ], + }, + }; + is_deeply $spark->process_for_json($struct), + { + service_requests => [ 1,2,3 ], + quxes => [ + { + values => [1,2], + }, + { + values => [3,4], + }, + ], + }; + + my $xml_struct = $spark->process_for_xml($struct); + is_deeply $xml_struct, + { + foo => { + service_requests => { request => [ 1,2,3 ] }, + quxes => { + quxe => [ + { + values => { + value => [1,2], + }, + }, + { + values => { + value => [3,4], + }, + }, + ] + }, + } + } + or warn Dumper($xml_struct); +}; + +done_testing; |