diff options
Diffstat (limited to 't/app/model')
-rw-r--r-- | t/app/model/comment.t | 27 | ||||
-rw-r--r-- | t/app/model/db.t | 8 | ||||
-rw-r--r-- | t/app/model/problem.t | 155 | ||||
-rw-r--r-- | t/app/model/token.t | 96 |
4 files changed, 286 insertions, 0 deletions
diff --git a/t/app/model/comment.t b/t/app/model/comment.t new file mode 100644 index 000000000..93104c2e5 --- /dev/null +++ b/t/app/model/comment.t @@ -0,0 +1,27 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 2; + +use FixMyStreet; +use FixMyStreet::App; + +my $comment_rs = FixMyStreet::App->model('DB::Comment'); + +my $comment = $comment_rs->new( + { + user_id => 1, + problem_id => 1, + text => '', + state => 'confirmed', + anonymous => 0, + mark_fixed => 0, + cobrand => 'default', + cobrand_data => '', + } +); + +is $comment->confirmed_local, undef, 'inflating null confirmed ok'; +is $comment->created_local, undef, 'inflating null confirmed ok'; diff --git a/t/app/model/db.t b/t/app/model/db.t new file mode 100644 index 000000000..bebd68f0b --- /dev/null +++ b/t/app/model/db.t @@ -0,0 +1,8 @@ +use strict; +use warnings; + +use Test::More; + +use_ok 'FixMyStreet::App::Model::DB'; + +done_testing(); diff --git a/t/app/model/problem.t b/t/app/model/problem.t new file mode 100644 index 000000000..1b8804fce --- /dev/null +++ b/t/app/model/problem.t @@ -0,0 +1,155 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; + +use FixMyStreet; +use FixMyStreet::App; +use mySociety::Locale; + +mySociety::Locale::gettext_domain('FixMyStreet'); + +my $problem_rs = FixMyStreet::App->model('DB::Problem'); + +my $problem = $problem_rs->new( + { + postcode => 'EH99 1SP', + latitude => 1, + longitude => 1, + areas => 1, + title => '', + detail => '', + used_map => 1, + user_id => 1, + name => '', + state => 'confirmed', + service => '', + cobrand => 'default', + cobrand_data => '', + } +); + +is $problem->confirmed_local, undef, 'inflating null confirmed ok'; +is $problem->whensent_local, undef, 'inflating null confirmed ok'; +is $problem->lastupdate_local, undef, 'inflating null confirmed ok'; +is $problem->created_local, undef, 'inflating null confirmed ok'; + +for my $test ( + { + desc => 'more or less empty problem', + changed => {}, + errors => { + title => 'Please enter a subject', + detail => 'Please enter some details', + council => 'No council selected', + name => 'Please enter your name', + } + }, + { + desc => 'name too short', + changed => { + name => 'xx', + }, + errors => { + title => 'Please enter a subject', + detail => 'Please enter some details', + council => 'No council selected', + name => 'Please enter your full name, councils need this information - if you do not wish your name to be shown on the site, untick the box', + } + }, + { + desc => 'name is anonymous', + changed => { + name => 'anonymous', + }, + errors => { + title => 'Please enter a subject', + detail => 'Please enter some details', + council => 'No council selected', + name => 'Please enter your full name, councils need this information - if you do not wish your name to be shown on the site, untick the box', + } + }, + { + desc => 'correct name', + changed => { + name => 'A User', + }, + errors => { + title => 'Please enter a subject', + detail => 'Please enter some details', + council => 'No council selected', + } + }, + { + desc => 'correct title', + changed => { + title => 'A Title', + }, + errors => { + detail => 'Please enter some details', + council => 'No council selected', + } + }, + { + desc => 'correct detail', + changed => { + detail => 'Some information about the problem', + }, + errors => { + council => 'No council selected', + } + }, + { + desc => 'incorrectly formatted council', + changed => { + council => 'my council', + }, + errors => { + council => 'No council selected', + } + }, + { + desc => 'correctly formatted council', + changed => { + council => '1001', + }, + errors => { + } + }, + { + desc => 'bad category', + changed => { + category => '-- Pick a category --', + }, + errors => { + category => 'Please choose a category', + } + }, + { + desc => 'bad category', + changed => { + category => '-- Pick a property type --', + }, + errors => { + category => 'Please choose a property type', + } + }, + { + desc => 'correct category', + changed => { + category => 'Horse!', + }, + errors => { + } + }, +) { + $problem->$_( $test->{changed}->{$_} ) for keys %{$test->{changed}}; + + subtest $test->{desc} => sub { + is_deeply $problem->check_for_errors, $test->{errors}, 'check for errors'; + }; +} + +done_testing(); diff --git a/t/app/model/token.t b/t/app/model/token.t new file mode 100644 index 000000000..12945975e --- /dev/null +++ b/t/app/model/token.t @@ -0,0 +1,96 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 45; + +use FixMyStreet; +use FixMyStreet::App; +use mySociety::AuthToken; +use mySociety::DBHandle 'dbh'; + +# set things up so that code using mySociety::DBHandle is happy +FixMyStreet->configure_mysociety_dbhandle(); + +# NOTE - remember that you need to explicitly dbh()->commit after making +# database changes with the mySociety::* modules. + +# create a token using DBIC and check we can read it using AuthToken, and vice +# versa + +my %tests = ( + nested_hash => { foo => 'bar', and => [ 'baz', 'bundy' ] }, + array => [ 'foo', 'bar' ], + scalar => 123, +); + +my $token_rs = FixMyStreet::App->model('DB::Token'); + +# create using DBIC +foreach my $test_data_name ( sort keys %tests ) { + my $test_data = $tests{$test_data_name}; + + pass "--- testing DBIC create using '$test_data_name'"; + + my $dbic_token = + $token_rs->create( { scope => 'testing', data => $test_data } ); + my $token = $dbic_token->token; + ok $token, "stored token '$token'"; + + is_deeply $dbic_token->data, $test_data, "data stored correctly using DBIC"; + + # read back using DBIC + is_deeply $token_rs->find( { token => $token, scope => 'testing' } )->data, + $test_data, + "data read back correctly with DBIC"; + + # read back using mySociety::AuthToken + is_deeply mySociety::AuthToken::retrieve( 'testing', $token ), + $test_data, "data read back correctly with m::AT"; + + # delete token + ok $dbic_token->delete, "delete token"; + + is $token_rs->find( { token => $token, scope => 'testing' } ), + undef, + "token gone for DBIC"; + + # read back using mySociety::AuthToken + is mySociety::AuthToken::retrieve( 'testing', $token ), + undef, "token gone with m::AT"; + +} + +# create using m::AT +foreach my $test_data_name ( sort keys %tests ) { + my $test_data = $tests{$test_data_name}; + + pass "--- testing m::AT create using '$test_data_name'"; + + my $token = mySociety::AuthToken::store( 'testing', $test_data ); + dbh->commit(); + ok $token, "stored token '$token'"; + + # read back using DBIC + is_deeply $token_rs->find( { token => $token, scope => 'testing' } )->data, + $test_data, + "data read back correctly with DBIC"; + + # read back using mySociety::AuthToken + is_deeply mySociety::AuthToken::retrieve( 'testing', $token ), + $test_data, "data read back correctly with m::AT"; + + # delete token + ok mySociety::AuthToken::destroy( 'testing', $token ), "destroy token"; + dbh->commit(); + + is $token_rs->find( { token => $token, scope => 'testing' } ), + undef, + "token gone for DBIC"; + + # read back using mySociety::AuthToken + is mySociety::AuthToken::retrieve( 'testing', $token ), + undef, "token gone with m::AT"; + +} |